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. They should be able to see the existing event details which will show 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?
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) }