Commit 9e1739554b
9e1739554bbd0817179865637176fa110731b9c1
parent: 6be12de78e
Verified · cmc
cmc <hello@cleberg.net> · 2026-11-21 09:40 UTC
Count idle time on iOS only while every window is in the background
Layout: unified · split
Sources/OrgApp/ClockModel.swift
+14
| @@ -567,6 +567,20 @@ public final class ClockModel { |
| 567 | 567 | ) |
| 568 | 568 | } |
| 569 | 569 | |
| 570 | /// When every window of the app went to the background, for `cameBack`. |
| 571 | @ObservationIgnored private var awayAt: Date? |
| 572 | |
| 573 | /// iOS: the app, all its windows, went to the background at `now`. |
| 574 | public func wentAway(at now: Date = Date()) { awayAt = now } |
| 575 | |
| 576 | /// iOS: a window became active. Since when the user was idle, the first time after the |
| 577 | /// whole app was away; nil when a window came back while another was in use. |
| 578 | public func cameBack(now: Date = Date(), idleMinutes: Int) -> Date? { |
| 579 | defer { awayAt = nil } |
| 580 | guard let awayAt else { return nil } |
| 581 | return Self.idleSince(backgrounded: awayAt, now: now, running: running, idleMinutes: idleMinutes) |
| 582 | } |
| 583 | |
| 570 | 584 | /// Where iOS has no idle time: since when the user was away, for `resolveIdle`, when the |
| 571 | 585 | /// app was in the background from `backgrounded` to `now` with `running` going for more |
| 572 | 586 | /// than `idleMinutes` of that time (`org-clock-idle-time`, 0 for never). |
Sources/OrgstarMobile/OrgstarMobileApp.swift
+7 −6
| @@ -10,6 +10,8 @@ import UserNotifications |
| 10 | 10 | struct OrgstarMobileApp: App { |
| 11 | 11 | @UIApplicationDelegateAdaptor private var delegate: AppDelegate |
| 12 | 12 | @State private var model = MobileModel.shared |
| 13 | /// The app's phase: in the background only when every window is. |
| 14 | @Environment(\.scenePhase) private var scenePhase |
| 13 | 15 | |
| 14 | 16 | init() { |
| 15 | 17 | // Debug builds, and runs from Xcode, whose scheme sets ORGSTAR_PERF. |
| @@ -35,6 +37,10 @@ struct OrgstarMobileApp: App { |
| 35 | 37 | ContentUnavailableView("Can't open the index", systemImage: "exclamationmark.triangle", description: Text(model.failure ?? "")) |
| 36 | 38 | } |
| 37 | 39 | } |
| 40 | // For the clock's idle time: time working in another window isn't time away. |
| 41 | .onChange(of: scenePhase) { _, phase in |
| 42 | if phase == .background { model.clock.wentAway() } |
| 43 | } |
| 38 | 44 | } |
| 39 | 45 | } |
| 40 | 46 | |
| @@ -119,8 +125,6 @@ struct RootView: View { |
| 119 | 125 | @State private var hour = 0 |
| 120 | 126 | @State private var showingClockReport = false |
| 121 | 127 | @State private var message: String? |
| 122 | | /// When the app went to the background, for the clock's idle time. |
| 123 | | @State private var backgrounded: Date? |
| 124 | 128 | @Environment(\.scenePhase) private var scenePhase |
| 125 | 129 | @AppStorage(OnboardingScreen.doneKey) private var onboarded = false |
| 126 | 130 | @State private var onboarding = false |
| @@ -323,16 +327,13 @@ struct RootView: View { |
| 323 | 327 | // Unsaved edits are written before the app may be stopped. |
| 324 | 328 | if phase == .background { |
| 325 | 329 | for path in session.dirtyPaths { _ = session.save(path) } |
| 326 | | backgrounded = Date() |
| 327 | 330 | } |
| 328 | 331 | // Files and settings synced while away; not every sync tool tells file presenters. |
| 329 | 332 | guard phase == .active else { return } |
| 330 | 333 | // iOS has no idle time: time away from the app while a clock ran stands in for it. |
| 331 | | if let left = backgrounded, let since = ClockModel.idleSince( |
| 332 | | backgrounded: left, now: Date(), running: model.clock.running, idleMinutes: OrgPreferences.clockIdleTime()) { |
| 334 | if let since = model.clock.cameBack(idleMinutes: OrgPreferences.clockIdleTime()) { |
| 333 | 335 | MobileClock.resolveIdle(since: since, model: model) { message = $0 } |
| 334 | 336 | } |
| 335 | | backgrounded = nil |
| 336 | 337 | takeShared() |
| 337 | 338 | model.reloadConfig() |
| 338 | 339 | Task { await workspace.refresh() } |
Tests/OrgAppTests/ClockHistoryModelTests.swift
+13
| @@ -205,4 +205,17 @@ struct ClockHistoryModelTests { |
| 205 | 205 | let earlier = start.addingTimeInterval(-30 * 60) |
| 206 | 206 | #expect(ClockModel.idleSince(backgrounded: earlier, now: start.addingTimeInterval(20 * 60), running: running, idleMinutes: 15) == start) |
| 207 | 207 | } |
| 208 | |
| 209 | /// iOS: only time with every window in the background is idle, and it's asked about once. |
| 210 | @Test func awayTimeIsTheWholeApps() { |
| 211 | let clock = ClockModel(file: stateFile, defaults: defaults) |
| 212 | _ = clock.clockIn(session, path: work.path, at: offset("* A"), now: start) |
| 213 | // One window went to the background while another stayed in use. |
| 214 | #expect(clock.cameBack(now: start.addingTimeInterval(60 * 60), idleMinutes: 15) == nil) |
| 215 | let left = start.addingTimeInterval(10 * 60) |
| 216 | clock.wentAway(at: left) |
| 217 | #expect(clock.cameBack(now: left.addingTimeInterval(20 * 60), idleMinutes: 15) == left) |
| 218 | // A second window becoming active isn't asked about again. |
| 219 | #expect(clock.cameBack(now: left.addingTimeInterval(21 * 60), idleMinutes: 15) == nil) |
| 220 | } |
| 208 | 221 | } |