@@ -55,15 +55,6 @@ let voiceMemosReducer = Reducer<VoiceMemosState, VoiceMemosAction, VoiceMemosEnv
55
55
}
56
56
) ,
57
57
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
-
67
58
switch action {
68
59
case . alertDismissed:
69
60
state. alert = nil
@@ -86,7 +77,12 @@ let voiceMemosReducer = Reducer<VoiceMemosState, VoiceMemosAction, VoiceMemosEnv
86
77
return . none
87
78
88
79
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
+ )
90
86
return . none
91
87
}
92
88
@@ -113,7 +109,12 @@ let voiceMemosReducer = Reducer<VoiceMemosState, VoiceMemosAction, VoiceMemosEnv
113
109
case let . recordPermissionResponse( permission) :
114
110
state. audioRecorderPermission = permission ? . allowed : . denied
115
111
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
+ )
117
118
return . none
118
119
} else {
119
120
state. alert = AlertState ( title: TextState ( " Permission is required to record voice memos. " ) )
@@ -227,14 +228,14 @@ struct VoiceMemos_Previews: PreviewProvider {
227
228
voiceMemos: [
228
229
VoiceMemoState (
229
230
date: Date ( ) ,
230
- duration: 30 ,
231
- mode: . playing ( progress : 0.3 ) ,
231
+ duration: 5 ,
232
+ mode: . notPlaying ,
232
233
title: " Functions " ,
233
234
url: URL ( string: " https://www.pointfree.co/functions " ) !
234
235
) ,
235
236
VoiceMemoState (
236
237
date: Date ( ) ,
237
- duration: 2 ,
238
+ duration: 5 ,
238
239
mode: . notPlaying,
239
240
title: " " ,
240
241
url: URL ( string: " https://www.pointfree.co/untitled " ) !
@@ -243,14 +244,9 @@ struct VoiceMemos_Previews: PreviewProvider {
243
244
) ,
244
245
reducer: voiceMemosReducer,
245
246
environment: VoiceMemosEnvironment (
246
- audioPlayer: . live ,
247
+ audioPlayer: . mock ,
247
248
// 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,
254
250
mainRunLoop: QueueScheduler . main,
255
251
openSettings: { } ,
256
252
temporaryDirectory: { URL ( fileURLWithPath: NSTemporaryDirectory ( ) ) } ,
@@ -260,3 +256,36 @@ struct VoiceMemos_Previews: PreviewProvider {
260
256
)
261
257
}
262
258
}
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