forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterjectGraceWindow.swift
More file actions
42 lines (34 loc) · 1.1 KB
/
Copy pathInterjectGraceWindow.swift
File metadata and controls
42 lines (34 loc) · 1.1 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
import Foundation
/// ~10s post-dismiss window where hover on the pill or PTT-start re-shows
/// the last card. The card payload lives with the manager; this type only
/// owns the arm/consume/expiry machine.
struct InterjectGraceWindow: Equatable, Sendable {
static let duration: TimeInterval = 10
private(set) var expiresAt: Date?
var isArmed: Bool { expiresAt != nil }
mutating func arm(now: Date, duration: TimeInterval = Self.duration) {
expiresAt = now.addingTimeInterval(duration)
}
mutating func clear() {
expiresAt = nil
}
func isActive(at now: Date) -> Bool {
guard let expiresAt else { return false }
return now < expiresAt
}
/// Hover or PTT-start. Returns true once; later calls see a cleared window.
mutating func consume(at now: Date) -> Bool {
guard isActive(at: now) else {
if let expiresAt, now >= expiresAt {
self.expiresAt = nil
}
return false
}
expiresAt = nil
return true
}
mutating func expireIfNeeded(at now: Date) {
guard let expiresAt, now >= expiresAt else { return }
self.expiresAt = nil
}
}