-1

I have fscalendar which shows a list of events booked from a API response. The Json data is as follows:

{
    "status": 200,
    "schedule_data": {
        "2020_08_16_1597485600": {
            "schedule_date": "2020-08-16",
            "nanny_id": 2,
            "shift_start": "06:00",
            "shift_end": "13:00",
            "schedule_id": 112
        },
        "2020_08_16": {
            "schedule_date": "2020-08-16",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        },
        "2020_08_17_1597485600": {
            "schedule_date": "2020-08-17",
            "nanny_id": 2,
            "shift_start": "06:00",
            "shift_end": "22:00",
            "schedule_id": 113
        },
        "2020_08_17": {
            "schedule_date": "2020-08-17",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        },
        "2020_08_18": {
            "schedule_date": "2020-08-18",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        },
        "2020_08_19": {
            "schedule_date": "2020-08-19",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        },
        "2020_08_20": {
            "schedule_date": "2020-08-20",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        },
        "2020_08_21": {
            "schedule_date": "2020-08-21",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        },
        "2020_08_22": {
            "schedule_date": "2020-08-22",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        },
        "2020_08_23": {
            "schedule_date": "2020-08-23",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        },
        "2020_08_24": {
            "schedule_date": "2020-08-24",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        },
        "2020_08_25": {
            "schedule_date": "2020-08-25",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        },
        "2020_08_26": {
            "schedule_date": "2020-08-26",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        },
        "2020_08_27": {
            "schedule_date": "2020-08-27",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        },
        "2020_08_28": {
            "schedule_date": "2020-08-28",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        },
        "2020_08_29": {
            "schedule_date": "2020-08-29",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        },
        "2020_08_30": {
            "schedule_date": "2020-08-30",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        },
        "2020_08_31": {
            "schedule_date": "2020-08-31",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        }
    }
}

I have fetched the events from this api response based on the schedule_id where it is not equal to zero.In this way i have two events(16-8-2020,17-8-2020) and i have stored them in an array.The events are showing on the fscalendar using the fscalendar delegate method as given below:

func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {

    self.scheduleview.isHidden = false
    self.mainview.addSubview(self.scheduleview)
    self.todaydateLbl.text = "Availablity on " + self.formatter.string(from: date)
    self.selectdate = self.formatter.string(from: date)
    
      print("calendar did select date \(self.formatter.string(from: date))")
         if monthPosition == .previous || monthPosition == .next {
             calendar.setCurrentPage(date, animated: true)
         }

} I have a view which is shown when the date is clicked which is the scheduleview. It shows the starttime and endtime value if the event is already there. How to get the start and endtime of the event which is already there.Like for example,when user selects 16-8-2020.He should be able to see the existing event details which will show him the start and end time from the API response.How to do it? The json parsing code is given below:

let decoder = JSONDecoder()
                      
                      do {
                          
                          let user = try decoder.decode(ScheduleListModel.self, from: response.data!)
                          self.listdata = user
                        
                        for schedule in self.listdata!.scheduleData {
                            if schedule.value.scheduleID != 0 {
                                self.eventsArray.append(schedule.value.scheduleDate!)
                            }
                        }
                        

                        print("eventsarray is",self.eventsArray)
                        
                        if self.eventsArray.count > 0
                        {
                            self.calendar.delegate = self
                            self.calendar.dataSource = self
                            self.calendar.reloadData()
                        }
                        

                         } catch {
                             debugPrint(error)
                      }

How to get the details from these data available?

Saranya
  • 595
  • 2
  • 7
  • 19

1 Answers1

1

You have "schedule_date": "2020-08-31" in your API response, which I assume you are saving in your data model as well. So when you get the selected date from calendar(_ calendar: FSCalendar, didSelect date: Date method, convert it to the format you have saved in your model and filter your data model.

let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
let selectedDateString = formatter.string(from: date)

let selectedEvents = scheduleData.filter { ($0.value.scheduleId != 0) && ($0.value.scheduleDate == selectedDateString) }
Jithin
  • 913
  • 5
  • 6
  • This will give only first element right.I want to iterate all the elements in the collection. – Saranya Aug 16 '20 at 07:27
  • ohh I thought 1 date will have only one associated event. You can use `filter` instead. Just replace `first` with `filter`. I've update the answer – Jithin Aug 16 '20 at 07:30
  • so it will give me array of events.so again iterate with this events?Because if the scheduledate matches,there are two such values,one with schedule id is zero and another with some value.I need the one with the schedule id not equal to zero. – Saranya Aug 16 '20 at 07:55
  • It depends on how you are going to show the events for the selected date. If its to be displayed in a tableview, you can directly use the filtered array. Otherwise, you will have to iterate and convert it to whichever form you want. – Jithin Aug 16 '20 at 07:59
  • need to show on the events on the calendar itself.not in tableview – Saranya Aug 16 '20 at 08:01
  • which data from the filtered result is going to be shown on calendar? – Jithin Aug 16 '20 at 08:07
  • You can see in the above code.the data with schedule id is not equal to zero.will be shown as events on the calendar.and for this filtered data,i need to get the other values too which are start and end time and its schedule id. – Saranya Aug 16 '20 at 08:52
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/219896/discussion-between-jithin-and-saranya). – Jithin Aug 16 '20 at 09:03