2026-07-06 03:43:25 +02:00
|
|
|
import Commanding
|
|
|
|
|
import SwiftUI
|
|
|
|
|
import WidgetKit
|
|
|
|
|
|
|
|
|
|
/// The timer of the elapsed recording time, formatted as `mm:ss`: counted up by the system from the content state's anchor when
|
|
|
|
|
/// one is given, and frozen at the reported elapsed time otherwise.
|
|
|
|
|
struct RecordingTimerText: View {
|
2026-07-07 00:06:52 +02:00
|
|
|
|
2026-07-06 03:43:25 +02:00
|
|
|
// MARK: Properties
|
2026-07-07 00:06:52 +02:00
|
|
|
|
2026-07-06 03:43:25 +02:00
|
|
|
/// The dynamic content of the Live Activity to render.
|
|
|
|
|
let state: RecordingActivityAttributes.ContentState
|
2026-07-07 00:06:52 +02:00
|
|
|
|
2026-07-06 03:43:25 +02:00
|
|
|
// MARK: Body
|
2026-07-07 00:06:52 +02:00
|
|
|
|
2026-07-06 03:43:25 +02:00
|
|
|
var body: some View {
|
|
|
|
|
if let anchor = state.anchor {
|
|
|
|
|
Text(
|
|
|
|
|
.durationOffset(to: anchor),
|
|
|
|
|
format: timerFormat
|
|
|
|
|
)
|
|
|
|
|
.multilineTextAlignment(.center)
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
Text(
|
|
|
|
|
Duration
|
|
|
|
|
.seconds(state.elapsed)
|
|
|
|
|
.formatted(timerFormat)
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-07-07 00:06:52 +02:00
|
|
|
|
2026-07-06 03:43:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: - Helpers
|
|
|
|
|
|
|
|
|
|
private extension RecordingTimerText {
|
2026-07-07 00:06:52 +02:00
|
|
|
|
2026-07-06 03:43:25 +02:00
|
|
|
// MARK: Properties
|
2026-07-07 00:06:52 +02:00
|
|
|
|
2026-07-06 03:43:25 +02:00
|
|
|
/// The format of the timer: minutes and seconds, as `mm:ss`.
|
|
|
|
|
var timerFormat: Duration.TimeFormatStyle {
|
|
|
|
|
.time(pattern: .minuteSecond(padMinuteToLength: 2))
|
|
|
|
|
}
|
2026-07-07 00:06:52 +02:00
|
|
|
|
2026-07-06 03:43:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: - Previews
|
|
|
|
|
|
|
|
|
|
#Preview(
|
|
|
|
|
"Recording Timer text",
|
|
|
|
|
as: .dynamicIsland(.compact),
|
|
|
|
|
using: RecordingActivityAttributes()
|
|
|
|
|
) {
|
|
|
|
|
RecordingLiveActivity()
|
|
|
|
|
} contentStates: {
|
|
|
|
|
RecordingActivityAttributes.ContentState(
|
|
|
|
|
state: .recording,
|
|
|
|
|
anchor: .now,
|
|
|
|
|
elapsed: 0
|
|
|
|
|
)
|
|
|
|
|
RecordingActivityAttributes.ContentState(
|
|
|
|
|
state: .paused,
|
|
|
|
|
anchor: nil,
|
|
|
|
|
elapsed: 325
|
|
|
|
|
)
|
|
|
|
|
RecordingActivityAttributes.ContentState(
|
|
|
|
|
state: .processing,
|
|
|
|
|
anchor: nil,
|
|
|
|
|
elapsed: 325
|
|
|
|
|
)
|
|
|
|
|
}
|