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 {
567567 )
568568 }
569569
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
570584 /// Where iOS has no idle time: since when the user was away, for `resolveIdle`, when the
571585 /// app was in the background from `backgrounded` to `now` with `running` going for more
572586 /// than `idleMinutes` of that time (`org-clock-idle-time`, 0 for never).
Sources/OrgstarMobile/OrgstarMobileApp.swift +7 −6
@@ -10,6 +10,8 @@ import UserNotifications
1010struct OrgstarMobileApp: App {
1111 @UIApplicationDelegateAdaptor private var delegate: AppDelegate
1212 @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
1315
1416 init() {
1517 // Debug builds, and runs from Xcode, whose scheme sets ORGSTAR_PERF.
@@ -35,6 +37,10 @@ struct OrgstarMobileApp: App {
3537 ContentUnavailableView("Can't open the index", systemImage: "exclamationmark.triangle", description: Text(model.failure ?? ""))
3638 }
3739 }
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 }
3844 }
3945}
4046
@@ -119,8 +125,6 @@ struct RootView: View {
119125 @State private var hour = 0
120126 @State private var showingClockReport = false
121127 @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?
124128 @Environment(\.scenePhase) private var scenePhase
125129 @AppStorage(OnboardingScreen.doneKey) private var onboarded = false
126130 @State private var onboarding = false
@@ -323,16 +327,13 @@ struct RootView: View {
323327 // Unsaved edits are written before the app may be stopped.
324328 if phase == .background {
325329 for path in session.dirtyPaths { _ = session.save(path) }
326 backgrounded = Date()
327330 }
328331 // Files and settings synced while away; not every sync tool tells file presenters.
329332 guard phase == .active else { return }
330333 // 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()) {
333335 MobileClock.resolveIdle(since: since, model: model) { message = $0 }
334336 }
335 backgrounded = nil
336337 takeShared()
337338 model.reloadConfig()
338339 Task { await workspace.refresh() }
Tests/OrgAppTests/ClockHistoryModelTests.swift +13
@@ -205,4 +205,17 @@ struct ClockHistoryModelTests {
205205 let earlier = start.addingTimeInterval(-30 * 60)
206206 #expect(ClockModel.idleSince(backgrounded: earlier, now: start.addingTimeInterval(20 * 60), running: running, idleMinutes: 15) == start)
207207 }
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 }
208221}