Move the recording side effects into the RecordingViewModel view model in the Recording package target.
This commit is contained in:
@@ -15,7 +15,7 @@ extension RecordingView {
|
||||
// MARK: Properties
|
||||
|
||||
/// The current state of the recording flow.
|
||||
var state: State = .notRecording
|
||||
private(set) var state: State = .notRecording
|
||||
|
||||
/// The number of seconds spent recording, excluding any time spent paused.
|
||||
private(set) var elapsedSeconds: Int = 0
|
||||
@@ -30,6 +30,10 @@ extension RecordingView {
|
||||
/// The instant the current recording stretch started, or `nil` while not recording.
|
||||
@ObservationIgnored
|
||||
private var anchor: ContinuousClock.Instant?
|
||||
|
||||
/// The service that captures the audio from a microphone.
|
||||
@ObservationIgnored
|
||||
private let capturer: any Capturing
|
||||
|
||||
/// The clock that measures the recording time.
|
||||
@ObservationIgnored
|
||||
@@ -39,10 +43,6 @@ extension RecordingView {
|
||||
@ObservationIgnored
|
||||
private let locale: Binding<Locale>
|
||||
|
||||
/// The service that captures the audio from a microphone.
|
||||
@ObservationIgnored
|
||||
private let capturer: any Capturing
|
||||
|
||||
/// The task that updates ``elapsedSeconds`` from the measured recording time once per second while recording.
|
||||
@ObservationIgnored
|
||||
private var taskTimer: Task<Void, Never>?
|
||||
@@ -119,20 +119,28 @@ extension RecordingView {
|
||||
|
||||
/// Handles a press of the main button.
|
||||
///
|
||||
/// Starts a recording when idle, pauses an ongoing recording, or resumes
|
||||
/// a paused one. Does nothing while the input is being processed.
|
||||
/// Starts a recording when idle, pauses an ongoing recording, or resumes a paused one — starting and stopping the timer
|
||||
/// and the audio capture accordingly. Does nothing while the input is being processed.
|
||||
func pressedMain() {
|
||||
guard state != .processing else {
|
||||
return
|
||||
}
|
||||
|
||||
switch state {
|
||||
case .notRecording:
|
||||
state = .recording
|
||||
|
||||
startTimer(true)
|
||||
startCapturer(true)
|
||||
case .recording:
|
||||
state = .paused
|
||||
|
||||
stopTimer()
|
||||
|
||||
Task {
|
||||
try? await capturer.pause()
|
||||
}
|
||||
case .paused:
|
||||
state = .recording
|
||||
|
||||
startTimer(false)
|
||||
startCapturer(false)
|
||||
case .processing:
|
||||
break
|
||||
}
|
||||
@@ -140,48 +148,19 @@ extension RecordingView {
|
||||
|
||||
/// Handles a press of the send button.
|
||||
///
|
||||
/// Moves a paused recording into processing. Does nothing in any other state.
|
||||
/// Moves a paused recording into processing, stopping the timer and kicking off ``processInput()``. Does nothing in any other state.
|
||||
func pressedSend() {
|
||||
guard state == .paused else {
|
||||
return
|
||||
}
|
||||
|
||||
switch state {
|
||||
case .paused:
|
||||
state = .processing
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
/// Reacts to a change of ``state``, expected to be called from the view whenever it observes one.
|
||||
///
|
||||
/// Starts the timer and the audio capture when a recording begins or resumes, stops the timer in every other state — pausing the capture
|
||||
/// while paused — and kicks off ``processInput()`` when the input is sent.
|
||||
///
|
||||
/// - 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
|
||||
) {
|
||||
switch state {
|
||||
case .recording:
|
||||
startTimer(shouldRestartTimer)
|
||||
startCapturer(shouldRestartTimer)
|
||||
case .paused:
|
||||
stopTimer()
|
||||
|
||||
Task {
|
||||
try? await capturer.pause()
|
||||
}
|
||||
case .processing:
|
||||
stopTimer()
|
||||
|
||||
Task {
|
||||
await processInput()
|
||||
}
|
||||
case .notRecording:
|
||||
stopTimer()
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user