Skip to content

Commit 6593c3f

Browse files
authored
LOOP-4093 Infer delivered loop-not-looping notifications on app becoming active (#508)
* Infer delivered loop-not-looping notifications on app becoming active * Update tests
1 parent e42c038 commit 6593c3f

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

Loop/Managers/LoopAlertsManager.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,10 @@ public class LoopAlertsManager {
134134
UserDefaults.appGroup?.loopNotRunningNotifications = scheduledNotifications
135135
}
136136

137-
func clearLoopNotRunningNotifications() {
138-
139-
// Any past alerts have been delivered at this point
137+
func inferDeliveredLoopNotRunningNotifications() {
138+
// Infer that any past alerts have been delivered at this point
140139
let now = getCurrentDate()
140+
var stillPendingNotifications = [StoredLoopNotRunningNotification]()
141141
for notification in UserDefaults.appGroup?.loopNotRunningNotifications ?? [] {
142142
print("Comparing alert \(notification.alertAt) to \(now) (real date = \(Date())")
143143
if notification.alertAt < now {
@@ -146,10 +146,15 @@ public class LoopAlertsManager {
146146
let interruptionLevel: Alert.InterruptionLevel = notification.isCritical ? .critical : .timeSensitive
147147
let alert = Alert(identifier: alertIdentifier, foregroundContent: nil, backgroundContent: content, trigger: .immediate, interruptionLevel: interruptionLevel)
148148
alertManager.recordIssued(alert: alert, at: notification.alertAt)
149+
} else {
150+
stillPendingNotifications.append(notification)
149151
}
150152
}
153+
UserDefaults.appGroup?.loopNotRunningNotifications = stillPendingNotifications
154+
}
151155

152-
UserDefaults.appGroup?.loopNotRunningNotifications = []
156+
func clearLoopNotRunningNotifications() {
157+
inferDeliveredLoopNotRunningNotifications()
153158

154159
// Clear out any existing not-running notifications
155160
UNUserNotificationCenter.current().getDeliveredNotifications { (notifications) in

Loop/Managers/LoopAppManager.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ class LoopAppManager: NSObject {
243243
ProfileExpirationAlerter.alertIfNeeded(viewControllerToPresentFrom: rootViewController)
244244
}
245245
deviceDataManager?.didBecomeActive()
246+
loopAlertsManager.inferDeliveredLoopNotRunningNotifications()
246247
}
247248

248249
// MARK: - Remote Notification

LoopTests/Managers/Alerts/LoopAlertManagerTests.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,29 +68,29 @@ class LoopAlertManagerTests: XCTestCase {
6868
XCTAssertEqual(4, UserDefaults.appGroup?.loopNotRunningNotifications.count)
6969
}
7070

71-
func testLoopDidCompleteAfter10MinutesDoesNotRecordAlert() {
71+
func testLoopFailureFor10MinutesDoesNotRecordAlert() {
7272
loopAlertsManager.loopDidComplete()
7373
XCTAssertNil(mockAlertStore.issuedAlert)
7474
loopAlertsManager.getCurrentDate = { return Date().addingTimeInterval(.minutes(10))}
75-
loopAlertsManager.loopDidComplete()
75+
loopAlertsManager.inferDeliveredLoopNotRunningNotifications()
7676
XCTAssertNil(mockAlertStore.issuedAlert)
7777
}
7878

79-
func testLoopDidCompleteAfter30MinutesRecordsTimeSensitiveAlert() {
79+
func testLoopFailureFor30MinutesRecordsTimeSensitiveAlert() {
8080
loopAlertsManager.loopDidComplete()
8181
XCTAssertNil(mockAlertStore.issuedAlert)
8282
loopAlertsManager.getCurrentDate = { return Date().addingTimeInterval(.minutes(30))}
83-
loopAlertsManager.loopDidComplete()
84-
XCTAssertEqual(4, UserDefaults.appGroup?.loopNotRunningNotifications.count)
83+
loopAlertsManager.inferDeliveredLoopNotRunningNotifications()
84+
XCTAssertEqual(3, UserDefaults.appGroup?.loopNotRunningNotifications.count)
8585
XCTAssertNotNil(mockAlertStore.issuedAlert)
8686
XCTAssertEqual(.timeSensitive, mockAlertStore.issuedAlert!.interruptionLevel)
8787
}
8888

89-
func testLoopDidCompleteAfter30MinutesRecordsCriticalAlert() {
89+
func testLoopFailureFor65MinutesRecordsCriticalAlert() {
9090
loopAlertsManager.loopDidComplete()
9191
loopAlertsManager.getCurrentDate = { return Date().addingTimeInterval(.minutes(65))}
92-
loopAlertsManager.loopDidComplete()
93-
XCTAssertEqual(4, UserDefaults.appGroup?.loopNotRunningNotifications.count)
92+
loopAlertsManager.inferDeliveredLoopNotRunningNotifications()
93+
XCTAssertEqual(1, UserDefaults.appGroup?.loopNotRunningNotifications.count)
9494
XCTAssertNotNil(mockAlertStore.issuedAlert)
9595
XCTAssertEqual(.critical, mockAlertStore.issuedAlert!.interruptionLevel)
9696
}

0 commit comments

Comments
 (0)