forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterjectDisplayTimer.swift
More file actions
44 lines (37 loc) · 1.2 KB
/
Copy pathInterjectDisplayTimer.swift
File metadata and controls
44 lines (37 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import Foundation
/// Pause/resume remaining-time tracker for a presented card.
///
/// Hover and PTT pause; releasing them resumes from the leftover duration.
/// The manager sleeps ``remaining(at:)`` and dismisses only when this reports
/// expiry while unpaused.
struct InterjectDisplayTimer: Equatable, Sendable {
var duration: TimeInterval
private var remainingWhenPaused: TimeInterval
private var runningSince: Date?
var isPaused: Bool { runningSince == nil }
static func start(duration: TimeInterval, now: Date) -> InterjectDisplayTimer {
InterjectDisplayTimer(
duration: duration,
remainingWhenPaused: duration,
runningSince: now
)
}
func remaining(at now: Date) -> TimeInterval {
if let runningSince {
return max(0, remainingWhenPaused - now.timeIntervalSince(runningSince))
}
return max(0, remainingWhenPaused)
}
func isExpired(at now: Date) -> Bool {
!isPaused && remaining(at: now) <= 0
}
mutating func pause(now: Date) {
guard runningSince != nil else { return }
remainingWhenPaused = remaining(at: now)
runningSince = nil
}
mutating func resume(now: Date) {
guard isPaused else { return }
runningSince = now
}
}