Skip to content

Commit c53424f

Browse files
authored
Merge pull request #251 from beeminder/remove-health-sync-notification
Remove health sync notification
2 parents 5ebd5f5 + 65b74fe commit c53424f

File tree

4 files changed

+7
-100
lines changed

4 files changed

+7
-100
lines changed

BeeSwift/AppDelegate.swift

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
1818

1919
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
2020
resetStateIfUITesting()
21+
removeAllLocalNotifications()
2122

2223
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.font :
2324
UIFont.beeminder.defaultFontPlain.withSize(20)]
@@ -36,10 +37,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
3637

3738
NetworkActivityIndicatorManager.shared.isEnabled = true
3839

39-
if UserDefaults.standard.object(forKey: Constants.healthSyncRemindersPreferenceKey) == nil {
40-
UserDefaults.standard.set(true, forKey: Constants.healthSyncRemindersPreferenceKey)
41-
}
42-
4340
NotificationCenter.default.addObserver(self, selector: #selector(self.updateBadgeCount), name: NSNotification.Name(rawValue: CurrentUserManager.goalsFetchedNotificationName), object: nil)
4441
NotificationCenter.default.addObserver(self, selector: #selector(self.updateBadgeCount), name: NSNotification.Name(rawValue: CurrentUserManager.signedOutNotificationName), object: nil)
4542

@@ -140,19 +137,16 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
140137
// MARK: - UNUserNotificationCenterDelegate
141138

142139
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
143-
switch notification.request.identifier {
144-
case JSONGoal.unlockNotificationIdentifier:
145-
// about to present a notification, a reminder to unlock the device
146-
// yet the app is active, thus we can abandon the notification
147-
completionHandler([])
148-
default:
149-
completionHandler([.alert, .sound, .badge])
150-
}
140+
completionHandler([.alert, .sound, .badge])
151141
}
152142

153143
private func resetStateIfUITesting() {
154144
if ProcessInfo.processInfo.arguments.contains("UI-Testing") {
155145
UserDefaults.standard.removePersistentDomain(forName: Bundle.main.bundleIdentifier!)
156146
}
157147
}
148+
149+
private func removeAllLocalNotifications() {
150+
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
151+
}
158152
}

BeeSwift/Constants.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ struct Constants {
2323
static let nameGoalSortString = "Name"
2424
static let pledgeGoalSortString = "Pledge"
2525
static let deadlineGoalSortString = "Deadline"
26-
static let healthSyncRemindersPreferenceKey = "wantsHealthSyncReminders"
2726
static let healthKitUpdateDictionaryKey = "healthKitUpdateDictionary"
2827
static let goalSortOptions = [Constants.nameGoalSortString, Constants.deadlineGoalSortString, Constants.pledgeGoalSortString, Constants.recentDataGoalSortString]
2928
}

BeeSwift/HealthKitConfigViewController.swift

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ class HealthKitConfigViewController: UIViewController {
1616
var tableView = UITableView()
1717
var goals : [JSONGoal] = []
1818
let cellReuseIdentifier = "healthKitConfigTableViewCell"
19-
var syncRemindersSwitch = UISwitch()
2019
let margin = 12
2120

2221
override func viewDidLoad() {
@@ -30,52 +29,11 @@ class HealthKitConfigViewController: UIViewController {
3029
let backItem = UIBarButtonItem(title: "Back", style: .plain, target: nil, action: nil)
3130
self.navigationItem.backBarButtonItem = backItem
3231

33-
let syncRemindersContainer = UIView()
34-
self.view.addSubview(syncRemindersContainer)
35-
syncRemindersContainer.backgroundColor = {
36-
if #available(iOS 13.0, *) {
37-
return .secondarySystemBackground
38-
} else {
39-
return .clear
40-
}
41-
}()
42-
syncRemindersContainer.snp.makeConstraints { make in
43-
make.left.equalTo(self.margin)
44-
make.right.equalTo(-self.margin)
45-
46-
make.top.equalTo(self.topLayoutGuide.snp.bottom)
47-
make.height.equalTo(Constants.defaultTextFieldHeight)
48-
}
49-
50-
let syncRemindersLabel = BSLabel()
51-
syncRemindersContainer.addSubview(syncRemindersLabel)
52-
syncRemindersLabel.text = "Sync Health data reminders"
53-
if #available(iOS 13.0, *) {
54-
syncRemindersLabel.backgroundColor = .secondarySystemBackground
55-
} else {
56-
syncRemindersLabel.backgroundColor = .clear
57-
}
58-
syncRemindersLabel.snp.makeConstraints { make in
59-
make.left.equalTo(self.margin)
60-
make.right.equalTo(-self.margin)
61-
62-
make.height.equalTo(Constants.defaultTextFieldHeight)
63-
}
64-
65-
syncRemindersContainer.addSubview(self.syncRemindersSwitch)
66-
self.syncRemindersSwitch.isOn = UserDefaults.standard.bool(forKey: Constants.healthSyncRemindersPreferenceKey)
67-
self.syncRemindersSwitch.addTarget(self, action: #selector(self.syncRemindersSwitchValueChanged), for: .valueChanged)
68-
self.syncRemindersSwitch.snp.makeConstraints { (make) in
69-
make.centerY.equalTo(syncRemindersLabel)
70-
make.right.equalTo(-self.margin)
71-
}
72-
7332
self.view.addSubview(self.tableView)
7433
self.tableView.snp.makeConstraints { (make) -> Void in
7534
make.left.equalTo(self.margin)
7635
make.right.equalTo(-self.margin)
77-
78-
make.top.equalTo(syncRemindersContainer.snp.bottom).offset(self.margin)
36+
make.top.equalTo(self.topLayoutGuide.snp.bottom).offset(self.margin)
7937
make.bottom.equalTo(self.bottomLayoutGuide.snp.top)
8038
}
8139

@@ -108,13 +66,6 @@ class HealthKitConfigViewController: UIViewController {
10866
// Dispose of any resources that can be recreated.
10967
}
11068

111-
@objc func syncRemindersSwitchValueChanged() {
112-
UserDefaults.standard.set(self.syncRemindersSwitch.isOn, forKey: Constants.healthSyncRemindersPreferenceKey)
113-
if self.syncRemindersSwitch.isOn == false {
114-
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
115-
}
116-
}
117-
11869
@objc func fetchGoals() {
11970
self.tableView.refreshControl?.endRefreshing()
12071

BeeSwift/JSONGoal.swift

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,6 @@ class JSONGoal {
318318
guard let sampleType = self.hkSampleType() else { return nil }
319319
return HKObserverQuery(sampleType: sampleType, predicate: nil, updateHandler: { (query, completionHandler, error) in
320320
self.hkQueryForLast(days: 1, success: nil, errorCompletion: nil)
321-
self.setUnlockNotification()
322321
completionHandler()
323322
})
324323
}
@@ -512,7 +511,6 @@ class JSONGoal {
512511
}
513512
DispatchQueue.global(qos: .background).asyncAfter(deadline: .now() + .seconds(5), execute: { [weak self] in
514513
self?.updateBeeminderWithStatsCollection(collection: statsCollection, success: nil, errorCompletion: nil)
515-
self?.setUnlockNotification()
516514
})
517515
}
518516

@@ -526,46 +524,11 @@ class JSONGoal {
526524
}
527525

528526
self?.updateBeeminderWithStatsCollection(collection: statsCollection, success: nil, errorCompletion: nil)
529-
self?.setUnlockNotification()
530527
}
531528
}
532529
healthStore.execute(query)
533530
}
534531

535-
public static let unlockNotificationIdentifier: String = "health sync unlock reminder"
536-
537-
func setUnlockNotification() {
538-
guard UserDefaults.standard.bool(forKey: Constants.healthSyncRemindersPreferenceKey) else { return }
539-
540-
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
541-
let content = UNMutableNotificationContent()
542-
content.title = "Health Sync"
543-
content.body = "Unlock your phone to sync your Health data with Beeminder."
544-
let date = Date()
545-
let calendar = Calendar.current
546-
let hour = calendar.component(.hour, from: date)
547-
548-
var trigger : UNNotificationTrigger
549-
if hour < 9 {
550-
// data synced before 9 am. Schedule for nine hours from now.
551-
trigger = UNTimeIntervalNotificationTrigger(timeInterval: 32400.0, repeats: false)
552-
}
553-
else if hour >= 9 && hour < 17 {
554-
// data synced during the day, before 5 pm. schedule for 8 pm.
555-
var components = DateComponents()
556-
components.hour = 20
557-
trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: false)
558-
} else {
559-
// data synced after 5 pm. Schedule for 9 am next morning.
560-
var components = DateComponents()
561-
components.hour = 9
562-
trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: false)
563-
}
564-
565-
let notification = UNNotificationRequest.init(identifier: JSONGoal.unlockNotificationIdentifier, content: content, trigger: trigger)
566-
UNUserNotificationCenter.current().add(notification, withCompletionHandler: nil)
567-
}
568-
569532
func updateBeeminderWithStatsCollection(collection : HKStatisticsCollection, success: (() -> ())?, errorCompletion: (() -> ())?) {
570533
guard let healthStore = HealthStoreManager.sharedManager.healthStore else { return }
571534

0 commit comments

Comments
 (0)