-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserNotificationManager.swift
95 lines (75 loc) · 2.67 KB
/
UserNotificationManager.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//
// UserNotificationManager.swift
// c3-pro
//
// Created by Pascal Pfiffner on 7/17/15.
// Copyright (c) 2015 Boston Children's Hospital. All rights reserved.
//
import Foundation
import C3PRO
/**
An instance of this class communicates between NotificationManager and ProfileManager.
*/
class UserNotificationManager {
static let shared = UserNotificationManager()
private init() {}
/**
Makes sure all notifications are in-line with our current user defaults settings.
This method cancels all notifications (if changing time of day does not cancel those that were rescheduled), then re-creates all
reminders for the user.
*/
func synchronizeNotifications(with profileManager: ProfileManager?) {
let defaults = UserDefaults.standard
// cancel all notifications
let keep = defaults.surveyRemindersEnable
NotificationManager.shared.cancelExistingNotifications(ofTypes: [], evenRescheduled: !keep)
if keep, let manager = profileManager, let tasks = manager.user?.tasks {
NotificationManager.shared.ensureProperNotificationSettings()
let timeOfDay = defaults.surveyRemindersTimeOfDay
// re-add all tasks that want reminders (for the first occurrence only)
var notified = [String]()
for task in tasks {
if !notified.contains(task.taskId) {
if let (notification, type) = manager.notification(for: task, suggestedDate: timeOfDay) {
NotificationManager.shared.schedule(notification, type: type)
}
notified.append(task.taskId)
}
}
}
defaults.synchronize()
}
}
/**
Extend NSUserDefaults to manage survey reminders.
*/
extension UserDefaults {
private var surveyRemindersEnableKey: String {
return "reminders.surveys.enable"
}
public var surveyRemindersEnable: Bool {
if nil != string(forKey: surveyRemindersEnableKey) {
return bool(forKey: surveyRemindersEnableKey)
}
return true
}
public func surveyRemindersEnable(_ flag: Bool, profileManager: ProfileManager?) {
set(flag, forKey: surveyRemindersEnableKey)
UserNotificationManager.shared.synchronizeNotifications(with: profileManager)
}
private var surveyRemindersTimeOfDayKey: String {
return "reminders.surveys.time-of-day"
}
public var surveyRemindersTimeOfDay: DateComponents {
let parts = array(forKey: surveyRemindersTimeOfDayKey) as? [Int]
var comps = DateComponents()
comps.hour = parts?.first ?? 10
comps.minute = parts?.last
return comps
}
public func surveyRemindersSet(timeOfDay: DateComponents, profileManager: ProfileManager?) {
let parts = [timeOfDay.hour ?? 10, timeOfDay.minute ?? 00]
set(parts, forKey: surveyRemindersTimeOfDayKey)
UserNotificationManager.shared.synchronizeNotifications(with: profileManager)
}
}