Skip to content

Commit 382799e

Browse files
mbrandonwmluisbrown
authored andcommitted
Voice memos dependency previews (#1259)
* Improve the mock dependencies for voice memos preview. * wip (cherry picked from commit 29d7b9ceb7a87f72f2c3add1ace9b82b449d23d3) # Conflicts: # Examples/VoiceMemos/VoiceMemos/VoiceMemos.swift
1 parent 399a691 commit 382799e

File tree

1 file changed

+50
-21
lines changed

1 file changed

+50
-21
lines changed

Examples/VoiceMemos/VoiceMemos/VoiceMemos.swift

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,6 @@ let voiceMemosReducer = Reducer<VoiceMemosState, VoiceMemosAction, VoiceMemosEnv
5555
}
5656
),
5757
Reducer { state, action, environment in
58-
var newRecordingMemo: RecordingMemoState {
59-
RecordingMemoState(
60-
date: environment.mainRunLoop.now.date,
61-
url: environment.temporaryDirectory()
62-
.appendingPathComponent(environment.uuid().uuidString)
63-
.appendingPathExtension("m4a")
64-
)
65-
}
66-
6758
switch action {
6859
case .alertDismissed:
6960
state.alert = nil
@@ -86,7 +77,12 @@ let voiceMemosReducer = Reducer<VoiceMemosState, VoiceMemosAction, VoiceMemosEnv
8677
return .none
8778

8879
case .allowed:
89-
state.recordingMemo = newRecordingMemo
80+
state.recordingMemo = RecordingMemoState(
81+
date: environment.mainRunLoop.now.date,
82+
url: environment.temporaryDirectory()
83+
.appendingPathComponent(environment.uuid().uuidString)
84+
.appendingPathExtension("m4a")
85+
)
9086
return .none
9187
}
9288

@@ -113,7 +109,12 @@ let voiceMemosReducer = Reducer<VoiceMemosState, VoiceMemosAction, VoiceMemosEnv
113109
case let .recordPermissionResponse(permission):
114110
state.audioRecorderPermission = permission ? .allowed : .denied
115111
if permission {
116-
state.recordingMemo = newRecordingMemo
112+
state.recordingMemo = RecordingMemoState(
113+
date: environment.mainRunLoop.now.date,
114+
url: environment.temporaryDirectory()
115+
.appendingPathComponent(environment.uuid().uuidString)
116+
.appendingPathExtension("m4a")
117+
)
117118
return .none
118119
} else {
119120
state.alert = AlertState(title: TextState("Permission is required to record voice memos."))
@@ -227,14 +228,14 @@ struct VoiceMemos_Previews: PreviewProvider {
227228
voiceMemos: [
228229
VoiceMemoState(
229230
date: Date(),
230-
duration: 30,
231-
mode: .playing(progress: 0.3),
231+
duration: 5,
232+
mode: .notPlaying,
232233
title: "Functions",
233234
url: URL(string: "https://www.pointfree.co/functions")!
234235
),
235236
VoiceMemoState(
236237
date: Date(),
237-
duration: 2,
238+
duration: 5,
238239
mode: .notPlaying,
239240
title: "",
240241
url: URL(string: "https://www.pointfree.co/untitled")!
@@ -243,14 +244,9 @@ struct VoiceMemos_Previews: PreviewProvider {
243244
),
244245
reducer: voiceMemosReducer,
245246
environment: VoiceMemosEnvironment(
246-
audioPlayer: .live,
247+
audioPlayer: .mock,
247248
// NB: AVAudioRecorder doesn't work in previews, so we stub out the dependency here.
248-
audioRecorder: AudioRecorderClient(
249-
currentTime: { 10 },
250-
requestRecordPermission: { true },
251-
startRecording: { _ in try await Task.never() },
252-
stopRecording: {}
253-
),
249+
audioRecorder: .mock,
254250
mainRunLoop: QueueScheduler.main,
255251
openSettings: {},
256252
temporaryDirectory: { URL(fileURLWithPath: NSTemporaryDirectory()) },
@@ -260,3 +256,36 @@ struct VoiceMemos_Previews: PreviewProvider {
260256
)
261257
}
262258
}
259+
260+
extension AudioRecorderClient {
261+
static var mock: Self {
262+
let isRecording = ActorIsolated(false)
263+
let currentTime = ActorIsolated(0.0)
264+
265+
return Self(
266+
currentTime: { await currentTime.value },
267+
requestRecordPermission: { true },
268+
startRecording: { _ in
269+
await isRecording.setValue(true)
270+
while await isRecording.value {
271+
try await Task.sleep(nanoseconds: NSEC_PER_SEC)
272+
await currentTime.withValue { $0 += 1 }
273+
}
274+
return true
275+
},
276+
stopRecording: {
277+
await isRecording.setValue(false)
278+
await currentTime.setValue(0)
279+
}
280+
)
281+
}
282+
}
283+
284+
extension AudioPlayerClient {
285+
static let mock = Self(
286+
play: { _ in
287+
try await Task.sleep(nanoseconds: NSEC_PER_SEC * 5)
288+
return true
289+
}
290+
)
291+
}

0 commit comments

Comments
 (0)