Skip to content

Commit d8a1ef2

Browse files
committed
Functionality for scheduling a fav notification
1 parent 167bcee commit d8a1ef2

3 files changed

Lines changed: 52 additions & 12 deletions

File tree

BrickHack-Mobile/AlertMessage.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,11 @@ class MessageHandler {
124124
body: "Please try again later, by restarting the app.",
125125
type: .error)
126126
}
127+
128+
static func showNotificationRegisterError(withEventTitle title: String) {
129+
print("ERROR: Unable to register notification for event \(title)")
130+
showAlertMessage(withTitle: "Unable to register a notification!",
131+
body: "Please try again later.",
132+
type: .error)
133+
}
127134
}

BrickHack-Mobile/Controllers/ScheduleTableViewController.swift

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

BrickHack-Mobile/Models/ScheduleParser.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ class ScheduleParser {
202202

203203
// The parse loop
204204
// @FIXME: Using 3rd sheet as test, convert to 1st for production
205-
for (rowIndex, rowData) in data.sheets[0].data[0].rowData.enumerated() {
205+
for (rowIndex, rowData) in data.sheets[2].data[0].rowData.enumerated() {
206206

207207
// See comment below for how this skip variable functions
208208
var skip = false

0 commit comments

Comments
 (0)