Reset the elapsed seconds after processing for the RecordingViewModel view model in the Recording package target.

This commit is contained in:
2026-07-03 20:30:37 +02:00
parent 851cedc54f
commit 4fd695c2cb
2 changed files with 36 additions and 24 deletions
@@ -17,11 +17,11 @@ extension RecordingView {
var state: State = .notRecording
/// The number of seconds spent recording, excluding any time spent paused.
private(set) var secondsElapsed: Int = 0
private(set) var elapsedSeconds: Int = 0
/// The task that increments ``secondsElapsed`` every second while recording.
/// The task that increments ``elapsedSeconds`` every second while recording.
@ObservationIgnored
private var timerTask: Task<Void, Never>?
private var taskTimer: Task<Void, Never>?
// MARK: Computed
@@ -65,7 +65,7 @@ extension RecordingView {
/// The elapsed recording time, formatted as `mm:ss` for the timer label.
var textTimer: String {
Duration
.seconds(secondsElapsed)
.seconds(elapsedSeconds)
.formatted(.time(pattern: .minuteSecond(padMinuteToLength: 2)))
}
@@ -112,7 +112,7 @@ extension RecordingView {
///
/// Starts the timer when a recording begins or resumes, stops it in every other state, and kicks off ``processInput()`` when the input is sent.
///
/// - Parameter shouldRestartTimer: Whether ``secondsElapsed`` should be reset to zero before the timer starts, which is the case for
/// - Parameter shouldRestartTimer: Whether ``elapsedSeconds`` should be reset to zero before the timer starts, which is the case for
/// a new recording as opposed to one resuming from a pause.
func updatedState(
shouldRestartTimer: Bool
@@ -140,9 +140,9 @@ private extension RecordingView.Model {
// MARK: Methods
/// Processes the recorded input, returning the model to the not-recording state when finished.
/// Processes the recorded input, resetting ``elapsedSeconds`` and returning the model to the not-recording state when finished.
///
/// Currently a placeholder that simulates the work with a five-second delay.
/// Currently a placeholder that simulates the work with a two-second delay.
func processInput() async {
guard state == .processing else {
return
@@ -150,21 +150,22 @@ private extension RecordingView.Model {
try? await Task.sleep(for: .seconds(2))
elapsedSeconds = 0
state = .notRecording
}
/// Starts the timer task, which increments ``secondsElapsed`` once per second until it is cancelled. Any previously running timer task is cancelled first.
/// Starts the timer task, which increments ``elapsedSeconds`` once per second until it is cancelled. Any previously running timer task is cancelled first.
///
/// - Parameter shouldRestartTimer: Whether ``secondsElapsed`` should be reset to zero before the timer starts.
/// - Parameter shouldRestartTimer: Whether ``elapsedSeconds`` should be reset to zero before the timer starts.
func startTimer(
_ shouldRestartTimer: Bool
) {
if shouldRestartTimer {
secondsElapsed = 0
elapsedSeconds = 0
}
timerTask?.cancel()
timerTask = Task { [weak self] in
taskTimer?.cancel()
taskTimer = Task { [weak self] in
while !Task.isCancelled {
try? await Task.sleep(for: .seconds(1))
@@ -172,15 +173,15 @@ private extension RecordingView.Model {
return
}
self.secondsElapsed += 1
self.elapsedSeconds += 1
}
}
}
/// Stops the timer task, if any, keeping ``secondsElapsed`` at its current value.
/// Stops the timer task, if any, keeping ``elapsedSeconds`` at its current value.
func stopTimer() {
timerTask?.cancel()
timerTask = nil
taskTimer?.cancel()
taskTimer = nil
}
}