forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecordingTimer.swift
More file actions
58 lines (48 loc) · 1.39 KB
/
Copy pathRecordingTimer.swift
File metadata and controls
58 lines (48 loc) · 1.39 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import Foundation
/// Dedicated timer for recording duration that doesn't trigger global AppState re-renders.
/// Only views that explicitly observe this class will update when duration changes.
@MainActor
class RecordingTimer: ObservableObject {
static let shared = RecordingTimer()
/// Current recording duration in seconds
@Published private(set) var duration: TimeInterval = 0
private var timer: Timer?
private var startTime: Date?
private init() {}
/// Start the recording timer
func start() {
startTime = Date()
duration = 0
// Update every second
timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { [weak self] _ in
Task { @MainActor in
guard let self = self, let start = self.startTime else { return }
self.duration = Date().timeIntervalSince(start)
}
}
}
/// Stop the recording timer
func stop() {
timer?.invalidate()
timer = nil
startTime = nil
}
/// Reset the timer to zero
func reset() {
stop()
duration = 0
}
/// Restart the timer from zero (keeps running)
func restart() {
stop()
start()
}
/// Formatted duration string (HH:MM:SS)
var formattedDuration: String {
let total = Int(duration)
let hours = total / 3600
let minutes = (total % 3600) / 60
let seconds = total % 60
return String(format: "%02d:%02d:%02d", hours, minutes, seconds)
}
}