Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions MeetingBar/App/AppModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ struct AppEnvironment {
var applyLanguage: @MainActor (AppLanguage) -> Bool
var joinNotificationsEnabled: @MainActor () -> Bool
var clearDeliveredNotifications: @MainActor () -> Void
var resetPendingNotificationActions: @MainActor () -> Void = {}

@MainActor
static func live(
Expand Down Expand Up @@ -285,7 +286,8 @@ struct AppEnvironment {
notificationSettingsPublisher: AppSettings.notificationChanges,
applyLanguage: { I18N.instance.changeLanguage(to: $0) },
joinNotificationsEnabled: { Defaults[.joinEventNotification] },
clearDeliveredNotifications: { notificationScheduler.clearDeliveredNotifications() }
clearDeliveredNotifications: { notificationScheduler.clearDeliveredNotifications() },
resetPendingNotificationActions: { notificationScheduler.resetPendingActions() }
)
}
}
Expand Down Expand Up @@ -481,7 +483,11 @@ final class AppModel: ObservableObject {
state.timeContextRevision += 1
reconcileNotificationsFromState()
scheduleRefresh()
case .didWake, .dayChanged, .networkBecameReachable:
case .didWake:
environment.resetPendingNotificationActions()
reconcileNotificationsFromState()
scheduleRefresh()
case .dayChanged, .networkBecameReachable:
scheduleRefresh()
default:
break
Expand Down
7 changes: 7 additions & 0 deletions MeetingBar/Notifications/NotificationScheduler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ final class NotificationScheduler {
runner.setActionSink(nil)
}

/// Task.sleep does not advance during system sleep, so pending delays are stale after wake.
func resetPendingActions() {
actionTasks.values.forEach { $0.cancel() }
actionTasks.removeAll()
latestActionEvents.removeAll()
}

func reconcile(
events: [MBEvent],
settings: NotificationPlanningSettings,
Expand Down
30 changes: 30 additions & 0 deletions MeetingBarTests/NotificationSchedulerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,34 @@ final class NotificationSchedulerTests: BaseTestCase {
scheduler.stop()
}

func testResetPendingActionsRecreatesSleepingTasksOnNextReconcile() async {
let sleeper = ManualNotificationActionSleep()
let scheduler = NotificationScheduler(
sink: FakeNotificationRequestSink(),
actionSink: FakeNotificationActionSink(),
sleepAction: { _ in await sleeper.sleep() },
actionDate: { self.now }
)
let events = [event(id: "slept-through", startsIn: 600)]
let settings = fullscreenOnlySettings(offset: 0)

await scheduler.reconcile(events: events, settings: settings, now: now)
await sleeper.waitUntilSleeping()
await scheduler.reconcile(events: events, settings: settings, now: now)
var count = await sleeper.sleepCount
XCTAssertEqual(count, 1, "A repeated reconcile keeps the existing task")

scheduler.resetPendingActions()
await scheduler.reconcile(events: events, settings: settings, now: now)
for _ in 0..<1000 where count < 2 {
await Task.yield()
count = await sleeper.sleepCount
}
XCTAssertEqual(count, 2, "After wake the delay must be recomputed")
scheduler.stop()
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
await sleeper.wakeAll()
}

func testCancelledManualSleeperCannotFireDismissedAction() async {
let sleeper = ManualNotificationActionSleep()
let sink = FakeNotificationRequestSink()
Expand Down Expand Up @@ -977,8 +1005,10 @@ final class NotificationSchedulerTests: BaseTestCase {
private actor ManualNotificationActionSleep {
private var pending: [CheckedContinuation<Void, Never>] = []
private var observers: [CheckedContinuation<Void, Never>] = []
private(set) var sleepCount = 0

func sleep() async {
sleepCount += 1
await withCheckedContinuation { continuation in
pending.append(continuation)
observers.forEach { $0.resume() }
Expand Down
Loading