@@ -89,19 +89,14 @@ class ScheduleTableViewController: UITableViewController {
8989 override func tableView( _ tableView: UITableView , cellForRowAt indexPath: IndexPath ) -> UITableViewCell {
9090
9191 let cell = tableView. dequeueReusableCell ( withIdentifier: " TimelineTableViewCell " , for: indexPath) as! TimelineTableViewCell
92-
93- // Grab from our custom config
94- // Description unused for now
95- // let (timelinePoint, allColor, title, description, isFavorite, date)
96-
9792 let currentTimelineEvent = timelineEvents [ convertIndex ( fromIndexPath: indexPath) ]
9893
9994 /*
10095 Overview of how cells are drawn
10196
10297 ----------------
10398 backColor |
104- tPoint.color o 12:30 (bold)
99+ tPoint.color o Title (bold)
105100 |
106101 frontColor | Description
107102 |
@@ -214,21 +209,59 @@ class ScheduleTableViewController: UITableViewController {
214209 }
215210
216211 // Update view
217- // (FavoriteButton subclass handles this condition)
212+ // (FavoriteButton subclass handles updating this condition)
218213 favButton. isSelected = !favButton. isSelected
219214
220- // Update model
221- // (We handle this condition!)
215+ // Now, prep the model:
216+
217+ // Get the event at this position
222218 let indexPath = IndexPath ( row: favButton. row!, section: favButton. section!)
223219 let selectedEvent = timelineEvents [ convertIndex ( fromIndexPath: indexPath) ]
220+
221+ // Update model
224222 selectedEvent. isFavorite = !selectedEvent. isFavorite
225- print ( " user toggled \( selectedEvent. event. title) " )
223+
224+ // Manage the list of events
225+ if selectedEvent. isFavorite {
226+ scheduleFavoriteNotification ( forEvent: selectedEvent)
227+ print ( " Scheduled notification for \( selectedEvent. event. title) " )
228+ } else {
229+ unscheduleFavoriteNotification ( forEvent: selectedEvent)
230+ print ( " Unscheduled notification for \( selectedEvent. event. title) " )
231+ }
226232
227233 // @TODO: Handle updating favorite with server
228- // @TODO: Handle notifying users on their favorited events
229234 }
230235
236+ private func scheduleFavoriteNotification( forEvent timelineEvent: TimelineEvent ) {
237+
238+ // Extract components and set trigger
239+ let components = Calendar . current. dateComponents ( in: . current, from: timelineEvent. event. time)
240+ let trigger = UNCalendarNotificationTrigger ( dateMatching: components, repeats: false )
231241
242+ print ( timelineEvent. event. uuid)
243+
244+
245+ let content = UNMutableNotificationContent ( )
246+ content. title = timelineEvent. event. title + " is starting! "
247+ content. body = timelineEvent. event. description
248+ let request = UNNotificationRequest ( identifier: timelineEvent. event. uuid, content: content, trigger: trigger)
249+
250+ // Add request to local notification center
251+ UNUserNotificationCenter . current ( ) . add ( request) { error in
252+ if let error = error {
253+ DispatchQueue . main. async {
254+ MessageHandler . showNotificationRegisterError ( withEventTitle: timelineEvent. event. title)
255+ }
256+ return
257+ }
258+ }
259+ }
260+
261+ private func unscheduleFavoriteNotification( forEvent timelineEvent: TimelineEvent ) {
262+ let identifier = timelineEvent. event. uuid
263+ UNUserNotificationCenter . current ( ) . removePendingNotificationRequests ( withIdentifiers: [ identifier] )
264+ }
232265
233266 // MARK: Section headers and view configuration
234267
0 commit comments