@cometchat/chat-uikit-react-native 5.2.11 → 5.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/build.gradle +3 -0
- package/android/src/main/java/com/reactnativecometchatuikit/CometchatUiKitPackage.java +4 -1
- package/android/src/main/java/com/reactnativecometchatuikit/FileManager.java +909 -10
- package/android/src/main/java/com/reactnativecometchatuikit/RichTextEditorPackage.kt +24 -0
- package/android/src/main/java/com/reactnativecometchatuikit/RichTextEditorView.kt +5382 -0
- package/android/src/main/java/com/reactnativecometchatuikit/RichTextEditorViewManager.kt +452 -0
- package/ios/FileManager.m +430 -3
- package/ios/RichTextEditorView.swift +5504 -0
- package/ios/RichTextEditorViewManager.m +66 -0
- package/ios/RichTextEditorViewManager.swift +291 -0
- package/ios/ToolbarIcons.swift +736 -0
- package/package.json +1 -1
- package/react-native-cometchat-ui-kit.podspec +1 -1
- package/src/CometChatCompactMessageComposer/CometChatCompactMessageComposer.tsx +4602 -0
- package/src/CometChatCompactMessageComposer/SingleLineTextComposerConfiguration.tsx +568 -0
- package/src/CometChatCompactMessageComposer/heightUtils.ts +204 -0
- package/src/CometChatCompactMessageComposer/index.ts +56 -0
- package/src/CometChatCompactMessageComposer/resources/index.ts +5 -0
- package/src/CometChatCompactMessageComposer/styles.ts +291 -0
- package/src/CometChatConversations/CometChatConversations.tsx +141 -14
- package/src/CometChatGroupMembers/CometChatGroupMembers.tsx +6 -0
- package/src/CometChatMessageComposer/CometChatMessageComposer.tsx +1 -20
- package/src/CometChatMessageComposer/styles.tsx +0 -1
- package/src/CometChatMessageList/CometChatMessageList.tsx +76 -30
- package/src/CometChatRichTextEditor/RichTextEditorViewNativeComponent.ts +105 -0
- package/src/CometChatRichTextEditor/index.tsx +385 -0
- package/src/CometChatRichTextEditor/types.ts +162 -0
- package/src/CometChatSearch/CometChatSearch.tsx +35 -29
- package/src/calls/CallUtils.ts +4 -4
- package/src/index.ts +48 -1
- package/src/shared/CometChatUiKit/CometChatUIKit.ts +2 -0
- package/src/shared/constants/UIKitConstants.ts +12 -1
- package/src/shared/events/ListenerInitializer.ts +6 -0
- package/src/shared/events/messages.ts +2 -0
- package/src/shared/formatters/CometChatMentionsFormatter/CometChatMentionsFormatter.tsx +5 -5
- package/src/shared/formatters/CometChatRichTextFormatter/index.tsx +768 -0
- package/src/shared/formatters/index.ts +3 -0
- package/src/shared/framework/MessageDataSource.tsx +93 -2
- package/src/shared/helper/composerHelpers.ts +553 -0
- package/src/shared/helper/useKeyboard.tsx +7 -17
- package/src/shared/icons/components/code-blocks.tsx +13 -0
- package/src/shared/icons/components/code.tsx +11 -0
- package/src/shared/icons/components/format-bold.tsx +11 -0
- package/src/shared/icons/components/format-italic.tsx +11 -0
- package/src/shared/icons/components/format-list-bulleted.tsx +11 -0
- package/src/shared/icons/components/format-list-numbered.tsx +11 -0
- package/src/shared/icons/components/format-quote.tsx +13 -0
- package/src/shared/icons/components/format-strikethrough.tsx +13 -0
- package/src/shared/icons/components/format-underlined.tsx +11 -0
- package/src/shared/icons/components/smileys-&-people.tsx +1 -1
- package/src/shared/icons/icon-mapping.tsx +18 -0
- package/src/shared/index.ts +54 -2
- package/src/shared/resources/CometChatLocalizeNew/resources/de/translation.json +37 -32
- package/src/shared/resources/CometChatLocalizeNew/resources/en/translation.json +5 -0
- package/src/shared/resources/CometChatLocalizeNew/resources/es/translation.json +17 -12
- package/src/shared/resources/CometChatLocalizeNew/resources/fr/translation.json +505 -500
- package/src/shared/resources/CometChatLocalizeNew/resources/hi/translation.json +18 -13
- package/src/shared/resources/CometChatLocalizeNew/resources/hu/translation.json +17 -12
- package/src/shared/resources/CometChatLocalizeNew/resources/it/translation.json +504 -499
- package/src/shared/resources/CometChatLocalizeNew/resources/ja/translation.json +504 -499
- package/src/shared/resources/CometChatLocalizeNew/resources/ko/translation.json +504 -499
- package/src/shared/resources/CometChatLocalizeNew/resources/lt/translation.json +18 -13
- package/src/shared/resources/CometChatLocalizeNew/resources/ms/translation.json +17 -12
- package/src/shared/resources/CometChatLocalizeNew/resources/nl/translation.json +505 -500
- package/src/shared/resources/CometChatLocalizeNew/resources/pt/translation.json +17 -12
- package/src/shared/resources/CometChatLocalizeNew/resources/ru/translation.json +17 -12
- package/src/shared/resources/CometChatLocalizeNew/resources/sv/translation.json +18 -13
- package/src/shared/resources/CometChatLocalizeNew/resources/tr/translation.json +505 -500
- package/src/shared/resources/CometChatLocalizeNew/resources/zh/translation.json +17 -12
- package/src/shared/resources/CometChatLocalizeNew/resources/zh-tw/translation.json +18 -13
- package/src/shared/utils/CometChatMessagePreview/CometChatMessagePreview.tsx +132 -11
- package/src/shared/utils/CometChatMessagePreview/style.ts +1 -3
- package/src/shared/utils/CommonUtils.ts +18 -1
- package/src/shared/utils/MarkdownUtils.ts +143 -0
- package/src/shared/utils/MentionUtils.ts +37 -0
- package/src/shared/utils/MessageReceiptUtils.ts +5 -4
- package/src/shared/utils/MessageUtils.tsx +5 -3
- package/src/shared/utils/index.ts +5 -0
- package/src/shared/views/CometChatAudioBubble/CometChatAudioBubble.tsx +1 -1
- package/src/shared/views/CometChatInlineAudioRecorder/AudioWaveformVisualizer.tsx +417 -0
- package/src/shared/views/CometChatInlineAudioRecorder/CometChatInlineAudioRecorder.tsx +537 -0
- package/src/shared/views/CometChatInlineAudioRecorder/index.ts +34 -0
- package/src/shared/views/CometChatInlineAudioRecorder/style.ts +155 -0
- package/src/shared/views/CometChatInlineAudioRecorder/types.ts +303 -0
- package/src/shared/views/CometChatInlineAudioRecorder/useAudioRecorder.ts +1336 -0
- package/src/shared/views/CometChatLinkConfirmPopup/CometChatLinkConfirmPopup.tsx +183 -0
- package/src/shared/views/CometChatLinkConfirmPopup/index.ts +13 -0
- package/src/shared/views/CometChatLinkConfirmPopup/style.ts +128 -0
- package/src/shared/views/CometChatList/Header.tsx +2 -1
- package/src/shared/views/CometChatListItem/style.ts +1 -0
- package/src/shared/views/CometChatSendButtonView/CometChatSendButtonView.tsx +32 -29
- package/src/shared/views/CometChatTextBubble/CometChatTextBubble.tsx +64 -16
- package/src/shared/views/index.ts +39 -0
|
@@ -0,0 +1,1336 @@
|
|
|
1
|
+
import { useReducer, useEffect, useRef, useCallback, useMemo } from 'react';
|
|
2
|
+
import { NativeModules, Platform, PermissionsAndroid, Alert, Linking, NativeEventEmitter } from 'react-native';
|
|
3
|
+
import {
|
|
4
|
+
RecorderAction,
|
|
5
|
+
RecorderReducerState,
|
|
6
|
+
UseAudioRecorderReturn,
|
|
7
|
+
AudioSegment,
|
|
8
|
+
} from './types';
|
|
9
|
+
|
|
10
|
+
// Create event emitter for FileManager
|
|
11
|
+
const FileManagerEmitter = new NativeEventEmitter(NativeModules.FileManager);
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Initial state for the audio recorder reducer.
|
|
15
|
+
* @validates Requirements 9.1, 11.7
|
|
16
|
+
*/
|
|
17
|
+
const initialState: RecorderReducerState = {
|
|
18
|
+
state: 'idle',
|
|
19
|
+
duration: 0,
|
|
20
|
+
currentPosition: 0,
|
|
21
|
+
filePath: null,
|
|
22
|
+
error: null,
|
|
23
|
+
amplitudes: [],
|
|
24
|
+
segments: [],
|
|
25
|
+
isPreviewMode: false,
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Reducer function for managing audio recorder state.
|
|
30
|
+
* Implements predictable state management with valid state transitions.
|
|
31
|
+
*
|
|
32
|
+
* @validates Requirements 9.1, 9.2, 9.5, 11.7
|
|
33
|
+
*/
|
|
34
|
+
function recorderReducer(
|
|
35
|
+
state: RecorderReducerState,
|
|
36
|
+
action: RecorderAction
|
|
37
|
+
): RecorderReducerState {
|
|
38
|
+
switch (action.type) {
|
|
39
|
+
case 'START_RECORDING':
|
|
40
|
+
// Only allow starting recording from idle or error states
|
|
41
|
+
if (state.state !== 'idle' && state.state !== 'error') return state;
|
|
42
|
+
return {
|
|
43
|
+
...state,
|
|
44
|
+
state: 'recording',
|
|
45
|
+
error: null,
|
|
46
|
+
amplitudes: [],
|
|
47
|
+
duration: 0,
|
|
48
|
+
currentPosition: 0,
|
|
49
|
+
segments: [],
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
case 'PAUSE_RECORDING':
|
|
53
|
+
if (state.state !== 'recording') return state;
|
|
54
|
+
return {
|
|
55
|
+
...state,
|
|
56
|
+
state: 'paused',
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
case 'RESUME_RECORDING':
|
|
60
|
+
if (state.state !== 'paused') return state;
|
|
61
|
+
return {
|
|
62
|
+
...state,
|
|
63
|
+
state: 'recording',
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
case 'STOP_RECORDING':
|
|
67
|
+
if (state.state !== 'recording' && state.state !== 'paused') return state;
|
|
68
|
+
return {
|
|
69
|
+
...state,
|
|
70
|
+
state: 'completed',
|
|
71
|
+
filePath: action.filePath,
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
case 'START_PLAYBACK':
|
|
75
|
+
if (state.state !== 'completed' && state.state !== 'paused') return state;
|
|
76
|
+
return {
|
|
77
|
+
...state,
|
|
78
|
+
state: 'playing',
|
|
79
|
+
currentPosition: 0,
|
|
80
|
+
isPreviewMode: state.state === 'paused', // Track if we started from paused (preview mode)
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
case 'PAUSE_PLAYBACK':
|
|
84
|
+
if (state.state !== 'playing') return state;
|
|
85
|
+
return {
|
|
86
|
+
...state,
|
|
87
|
+
// Return to paused if we were in preview mode, otherwise completed
|
|
88
|
+
state: state.isPreviewMode ? 'paused' : 'completed',
|
|
89
|
+
isPreviewMode: false,
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
case 'RESUME_PLAYBACK':
|
|
93
|
+
if (state.state !== 'completed') return state;
|
|
94
|
+
return {
|
|
95
|
+
...state,
|
|
96
|
+
state: 'playing',
|
|
97
|
+
isPreviewMode: false,
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
case 'PLAYBACK_COMPLETE':
|
|
101
|
+
if (state.state !== 'playing') return state;
|
|
102
|
+
return {
|
|
103
|
+
...state,
|
|
104
|
+
// Return to paused if we were in preview mode, otherwise completed
|
|
105
|
+
state: state.isPreviewMode ? 'paused' : 'completed',
|
|
106
|
+
currentPosition: state.duration,
|
|
107
|
+
isPreviewMode: false,
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
case 'SEEK':
|
|
111
|
+
return {
|
|
112
|
+
...state,
|
|
113
|
+
currentPosition: action.position,
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
case 'UPDATE_DURATION':
|
|
117
|
+
return {
|
|
118
|
+
...state,
|
|
119
|
+
duration: action.duration,
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
case 'UPDATE_POSITION':
|
|
123
|
+
return {
|
|
124
|
+
...state,
|
|
125
|
+
currentPosition: action.position,
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
case 'ADD_AMPLITUDE':
|
|
129
|
+
return {
|
|
130
|
+
...state,
|
|
131
|
+
amplitudes: [...state.amplitudes, action.amplitude],
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
case 'SET_ERROR':
|
|
135
|
+
return {
|
|
136
|
+
...state,
|
|
137
|
+
state: 'error',
|
|
138
|
+
error: action.error,
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
case 'RESET':
|
|
142
|
+
return initialState;
|
|
143
|
+
|
|
144
|
+
// Segment-related actions
|
|
145
|
+
case 'FINALIZE_SEGMENT':
|
|
146
|
+
return {
|
|
147
|
+
...state,
|
|
148
|
+
segments: [...state.segments, action.segment],
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
case 'START_NEW_SEGMENT':
|
|
152
|
+
return {
|
|
153
|
+
...state,
|
|
154
|
+
state: 'recording',
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
case 'CLEAR_SEGMENTS':
|
|
158
|
+
return {
|
|
159
|
+
...state,
|
|
160
|
+
segments: [],
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
default:
|
|
164
|
+
return state;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Custom hook for managing audio recording and playback.
|
|
170
|
+
* Wraps the FileManager native module with React state management.
|
|
171
|
+
*
|
|
172
|
+
* Features:
|
|
173
|
+
* - State management with useReducer for predictable state transitions
|
|
174
|
+
* - Duration timer that increments during recording
|
|
175
|
+
* - Simulated amplitude generation for waveform visualization
|
|
176
|
+
* - Cleanup on unmount to release native resources
|
|
177
|
+
*
|
|
178
|
+
* @validates Requirements 9.1, 9.2, 9.3, 10.1, 10.5
|
|
179
|
+
*/
|
|
180
|
+
export function useAudioRecorder(): UseAudioRecorderReturn {
|
|
181
|
+
const [reducerState, dispatch] = useReducer(recorderReducer, initialState);
|
|
182
|
+
|
|
183
|
+
// Refs for timers and intervals
|
|
184
|
+
const durationTimerRef = useRef<ReturnType<typeof setInterval> | null>(null);
|
|
185
|
+
const amplitudeTimerRef = useRef<ReturnType<typeof setInterval> | null>(null);
|
|
186
|
+
const playbackTimerRef = useRef<ReturnType<typeof setInterval> | null>(null);
|
|
187
|
+
const recordingStartTimeRef = useRef<number>(0);
|
|
188
|
+
const pausedDurationRef = useRef<number>(0);
|
|
189
|
+
|
|
190
|
+
// Seek operation tracking to prevent race conditions
|
|
191
|
+
const seekOperationIdRef = useRef<number>(0);
|
|
192
|
+
const isSeekingRef = useRef<boolean>(false);
|
|
193
|
+
const seekDebounceTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
194
|
+
const pendingSeekRef = useRef<{ progress: number; operationId: number } | null>(null);
|
|
195
|
+
|
|
196
|
+
// Debug: Track last requested seek position to detect unexpected resets
|
|
197
|
+
const lastRequestedPositionRef = useRef<number>(0);
|
|
198
|
+
const lastRequestedProgressRef = useRef<number>(0);
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Cleanup function to release all native audio resources.
|
|
202
|
+
* Called on unmount and when canceling recording.
|
|
203
|
+
*
|
|
204
|
+
* @validates Requirements 10.5
|
|
205
|
+
*/
|
|
206
|
+
const cleanup = useCallback(() => {
|
|
207
|
+
// Clear all timers
|
|
208
|
+
if (durationTimerRef.current) {
|
|
209
|
+
clearInterval(durationTimerRef.current);
|
|
210
|
+
durationTimerRef.current = null;
|
|
211
|
+
}
|
|
212
|
+
if (amplitudeTimerRef.current) {
|
|
213
|
+
clearInterval(amplitudeTimerRef.current);
|
|
214
|
+
amplitudeTimerRef.current = null;
|
|
215
|
+
}
|
|
216
|
+
if (playbackTimerRef.current) {
|
|
217
|
+
clearInterval(playbackTimerRef.current);
|
|
218
|
+
playbackTimerRef.current = null;
|
|
219
|
+
}
|
|
220
|
+
if (seekDebounceTimerRef.current) {
|
|
221
|
+
clearTimeout(seekDebounceTimerRef.current);
|
|
222
|
+
seekDebounceTimerRef.current = null;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// Release native resources
|
|
226
|
+
NativeModules.FileManager.releaseMediaResources(() => {
|
|
227
|
+
// Resources released
|
|
228
|
+
});
|
|
229
|
+
}, []);
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Cleanup on unmount.
|
|
233
|
+
* @validates Requirements 10.5
|
|
234
|
+
*/
|
|
235
|
+
useEffect(() => {
|
|
236
|
+
return () => {
|
|
237
|
+
cleanup();
|
|
238
|
+
};
|
|
239
|
+
}, [cleanup]);
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Duration timer effect - increments duration during recording state.
|
|
243
|
+
* Replicates the EXACT logic from the old Timer component in CometChatMediaRecorder/Timer.tsx:
|
|
244
|
+
* - Uses a local ref to track time in milliseconds
|
|
245
|
+
* - Increments by 1000ms every second when recording (not paused)
|
|
246
|
+
* - Freezes (stops incrementing) when paused - time value is preserved
|
|
247
|
+
* - Resets to 0 when going back to idle state
|
|
248
|
+
*
|
|
249
|
+
* The old Timer component:
|
|
250
|
+
* - Used `time` state in seconds, incremented by 1 every 1000ms
|
|
251
|
+
* - We use milliseconds for consistency with the rest of the codebase
|
|
252
|
+
*
|
|
253
|
+
* @validates Requirements 1.4, 3.1
|
|
254
|
+
*/
|
|
255
|
+
const timerValueRef = useRef<number>(0);
|
|
256
|
+
|
|
257
|
+
useEffect(() => {
|
|
258
|
+
if (reducerState.state === 'recording') {
|
|
259
|
+
// Not paused -> start counting (increment every 1 second like old Timer)
|
|
260
|
+
// This matches: intervalRef.current = setInterval(() => { setTime((prev) => prev + 1); }, 1000);
|
|
261
|
+
durationTimerRef.current = setInterval(() => {
|
|
262
|
+
timerValueRef.current += 1000; // Increment by 1000ms (1 second)
|
|
263
|
+
dispatch({ type: 'UPDATE_DURATION', duration: timerValueRef.current });
|
|
264
|
+
}, 1000);
|
|
265
|
+
} else if (reducerState.state === 'paused') {
|
|
266
|
+
// Paused -> clear interval, so time is frozen (but value preserved in timerValueRef)
|
|
267
|
+
// This matches the old Timer's behavior when paused=true
|
|
268
|
+
if (durationTimerRef.current) {
|
|
269
|
+
clearInterval(durationTimerRef.current);
|
|
270
|
+
durationTimerRef.current = null;
|
|
271
|
+
}
|
|
272
|
+
// Note: timerValueRef.current is NOT reset here - time is frozen but preserved
|
|
273
|
+
} else if (reducerState.state === 'idle') {
|
|
274
|
+
// Reset timer when going back to idle (like resetKey changing in old Timer)
|
|
275
|
+
timerValueRef.current = 0;
|
|
276
|
+
if (durationTimerRef.current) {
|
|
277
|
+
clearInterval(durationTimerRef.current);
|
|
278
|
+
durationTimerRef.current = null;
|
|
279
|
+
}
|
|
280
|
+
} else {
|
|
281
|
+
// For other states (completed, playing, error), just stop the interval
|
|
282
|
+
// but preserve the duration value
|
|
283
|
+
if (durationTimerRef.current) {
|
|
284
|
+
clearInterval(durationTimerRef.current);
|
|
285
|
+
durationTimerRef.current = null;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// Cleanup when unmounting or when effect re-runs
|
|
290
|
+
return () => {
|
|
291
|
+
if (durationTimerRef.current) {
|
|
292
|
+
clearInterval(durationTimerRef.current);
|
|
293
|
+
durationTimerRef.current = null;
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
}, [reducerState.state]);
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Amplitude listening effect - receives real-time amplitude data from native module.
|
|
300
|
+
* The native module streams audio amplitude values (0.0 to 1.0) during recording.
|
|
301
|
+
* Uses WhatsApp-style amplitude processing for natural waveform visualization.
|
|
302
|
+
*
|
|
303
|
+
* @validates Requirements 2.1, 2.5
|
|
304
|
+
*/
|
|
305
|
+
useEffect(() => {
|
|
306
|
+
let subscription: { remove: () => void } | null = null;
|
|
307
|
+
|
|
308
|
+
if (reducerState.state === 'recording') {
|
|
309
|
+
// Listen for native amplitude events
|
|
310
|
+
subscription = FileManagerEmitter.addListener('audioAmplitude', (event: { amplitude: number }) => {
|
|
311
|
+
const amplitude = event.amplitude;
|
|
312
|
+
|
|
313
|
+
// WhatsApp-style amplitude processing:
|
|
314
|
+
// - Use the raw amplitude directly (already normalized 0-1 from native)
|
|
315
|
+
// - Apply minimal amplification to make quiet sounds visible
|
|
316
|
+
// - Keep the natural variation for authentic waveform look
|
|
317
|
+
|
|
318
|
+
// Minimum floor so bars are always visible, max cap at 1.0
|
|
319
|
+
// Use a gentler curve: sqrt for low values, linear for higher
|
|
320
|
+
let visualAmplitude: number;
|
|
321
|
+
if (amplitude < 0.05) {
|
|
322
|
+
// Very quiet - show minimal bar
|
|
323
|
+
visualAmplitude = 0.1 + amplitude * 2;
|
|
324
|
+
} else if (amplitude < 0.3) {
|
|
325
|
+
// Quiet to moderate - gentle boost
|
|
326
|
+
visualAmplitude = 0.2 + amplitude * 1.5;
|
|
327
|
+
} else {
|
|
328
|
+
// Moderate to loud - mostly linear
|
|
329
|
+
visualAmplitude = 0.35 + amplitude * 0.65;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// Clamp to valid range
|
|
333
|
+
const clampedAmplitude = Math.max(0.1, Math.min(1.0, visualAmplitude));
|
|
334
|
+
|
|
335
|
+
dispatch({ type: 'ADD_AMPLITUDE', amplitude: clampedAmplitude });
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
return () => {
|
|
340
|
+
if (subscription) {
|
|
341
|
+
subscription.remove();
|
|
342
|
+
}
|
|
343
|
+
};
|
|
344
|
+
}, [reducerState.state]);
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* Playback position timer effect - updates current position during playback.
|
|
348
|
+
* Uses native getPlaybackPosition for accurate sync with actual audio playback.
|
|
349
|
+
* Also updates duration from native to ensure waveform matches actual audio length.
|
|
350
|
+
* Ignores position updates during active seeking to prevent race conditions.
|
|
351
|
+
*
|
|
352
|
+
* @validates Requirements 4.3
|
|
353
|
+
*/
|
|
354
|
+
useEffect(() => {
|
|
355
|
+
let statusSubscription: { remove: () => void } | null = null;
|
|
356
|
+
|
|
357
|
+
if (reducerState.state === 'playing') {
|
|
358
|
+
// Listen for playback complete event from native
|
|
359
|
+
statusSubscription = FileManagerEmitter.addListener('status', (event: { state: string }) => {
|
|
360
|
+
// Ignore playback complete during seeking - it might be stale
|
|
361
|
+
if (event.state === 'playbackComplete' && !isSeekingRef.current) {
|
|
362
|
+
dispatch({ type: 'PLAYBACK_COMPLETE' });
|
|
363
|
+
if (playbackTimerRef.current) {
|
|
364
|
+
clearInterval(playbackTimerRef.current);
|
|
365
|
+
playbackTimerRef.current = null;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
// Poll native for actual playback position and duration
|
|
371
|
+
playbackTimerRef.current = setInterval(() => {
|
|
372
|
+
// Skip position updates during active seeking to prevent race conditions
|
|
373
|
+
if (isSeekingRef.current) {
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
NativeModules.FileManager.getPlaybackPosition((result: string) => {
|
|
378
|
+
// Double-check we're not seeking when the callback returns
|
|
379
|
+
if (isSeekingRef.current) {
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
try {
|
|
384
|
+
const response = JSON.parse(result);
|
|
385
|
+
if (response.success && typeof response.position === 'number') {
|
|
386
|
+
const position = response.position;
|
|
387
|
+
const nativeDuration = response.duration;
|
|
388
|
+
|
|
389
|
+
// Update duration from native if available and different
|
|
390
|
+
// This ensures waveform matches actual audio length
|
|
391
|
+
if (typeof nativeDuration === 'number' && nativeDuration > 0 && nativeDuration !== reducerState.duration) {
|
|
392
|
+
dispatch({ type: 'UPDATE_DURATION', duration: nativeDuration });
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
// Use native duration for completion check if available
|
|
396
|
+
const effectiveDuration = (typeof nativeDuration === 'number' && nativeDuration > 0)
|
|
397
|
+
? nativeDuration
|
|
398
|
+
: reducerState.duration;
|
|
399
|
+
|
|
400
|
+
// Check if playback is complete (only if not seeking)
|
|
401
|
+
if (position >= effectiveDuration && !isSeekingRef.current) {
|
|
402
|
+
dispatch({ type: 'PLAYBACK_COMPLETE' });
|
|
403
|
+
if (playbackTimerRef.current) {
|
|
404
|
+
clearInterval(playbackTimerRef.current);
|
|
405
|
+
playbackTimerRef.current = null;
|
|
406
|
+
}
|
|
407
|
+
} else {
|
|
408
|
+
dispatch({ type: 'UPDATE_POSITION', position });
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
} catch (e) {
|
|
412
|
+
// Fallback to time-based estimation if native call fails
|
|
413
|
+
}
|
|
414
|
+
});
|
|
415
|
+
}, 100);
|
|
416
|
+
} else {
|
|
417
|
+
if (playbackTimerRef.current) {
|
|
418
|
+
clearInterval(playbackTimerRef.current);
|
|
419
|
+
playbackTimerRef.current = null;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
return () => {
|
|
424
|
+
if (playbackTimerRef.current) {
|
|
425
|
+
clearInterval(playbackTimerRef.current);
|
|
426
|
+
playbackTimerRef.current = null;
|
|
427
|
+
}
|
|
428
|
+
if (statusSubscription) {
|
|
429
|
+
statusSubscription.remove();
|
|
430
|
+
}
|
|
431
|
+
};
|
|
432
|
+
}, [reducerState.state, reducerState.duration]);
|
|
433
|
+
|
|
434
|
+
/**
|
|
435
|
+
* Check and request microphone permission.
|
|
436
|
+
* Reuses existing permission handling patterns from CometChatMediaRecorder.
|
|
437
|
+
*
|
|
438
|
+
* @validates Requirements 10.4
|
|
439
|
+
*/
|
|
440
|
+
const checkMicrophonePermission = useCallback(async (): Promise<boolean> => {
|
|
441
|
+
if (Platform.OS === 'ios') {
|
|
442
|
+
// iOS handles permission in native module
|
|
443
|
+
return true;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
// Android permission check
|
|
447
|
+
const hasPermission = await PermissionsAndroid.check(
|
|
448
|
+
PermissionsAndroid.PERMISSIONS.RECORD_AUDIO
|
|
449
|
+
);
|
|
450
|
+
|
|
451
|
+
if (hasPermission) {
|
|
452
|
+
return true;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
// Request permission
|
|
456
|
+
const granted = await PermissionsAndroid.request(
|
|
457
|
+
PermissionsAndroid.PERMISSIONS.RECORD_AUDIO
|
|
458
|
+
);
|
|
459
|
+
|
|
460
|
+
return granted === PermissionsAndroid.RESULTS.GRANTED;
|
|
461
|
+
}, []);
|
|
462
|
+
|
|
463
|
+
/**
|
|
464
|
+
* Show permission alert with option to open settings.
|
|
465
|
+
*/
|
|
466
|
+
const showPermissionAlert = useCallback(() => {
|
|
467
|
+
Alert.alert(
|
|
468
|
+
'',
|
|
469
|
+
'Microphone permission is required to record audio. Please enable it in settings.',
|
|
470
|
+
[
|
|
471
|
+
{
|
|
472
|
+
style: 'cancel',
|
|
473
|
+
text: 'Cancel',
|
|
474
|
+
},
|
|
475
|
+
{
|
|
476
|
+
style: 'default',
|
|
477
|
+
text: 'Settings',
|
|
478
|
+
onPress: () => {
|
|
479
|
+
Linking.openSettings();
|
|
480
|
+
},
|
|
481
|
+
},
|
|
482
|
+
]
|
|
483
|
+
);
|
|
484
|
+
}, []);
|
|
485
|
+
|
|
486
|
+
/**
|
|
487
|
+
* Start recording audio.
|
|
488
|
+
* Wraps FileManager.startRecording native call.
|
|
489
|
+
*
|
|
490
|
+
* @validates Requirements 10.1
|
|
491
|
+
*/
|
|
492
|
+
const startRecording = useCallback(async (): Promise<void> => {
|
|
493
|
+
try {
|
|
494
|
+
const hasPermission = await checkMicrophonePermission();
|
|
495
|
+
|
|
496
|
+
if (!hasPermission) {
|
|
497
|
+
showPermissionAlert();
|
|
498
|
+
dispatch({ type: 'SET_ERROR', error: 'Microphone permission denied' });
|
|
499
|
+
return;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
// Reset state for new recording
|
|
503
|
+
pausedDurationRef.current = 0;
|
|
504
|
+
recordingStartTimeRef.current = 0;
|
|
505
|
+
// Reset timer value for new recording (like resetKey in old Timer)
|
|
506
|
+
timerValueRef.current = 0;
|
|
507
|
+
|
|
508
|
+
return new Promise((resolve, reject) => {
|
|
509
|
+
NativeModules.FileManager.startRecording((result: string) => {
|
|
510
|
+
try {
|
|
511
|
+
const response = JSON.parse(result);
|
|
512
|
+
|
|
513
|
+
if (response.granted === false) {
|
|
514
|
+
showPermissionAlert();
|
|
515
|
+
dispatch({ type: 'SET_ERROR', error: 'Microphone permission denied' });
|
|
516
|
+
reject(new Error('Microphone permission denied'));
|
|
517
|
+
return;
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
if (response.success === false) {
|
|
521
|
+
dispatch({ type: 'SET_ERROR', error: response.error || 'Failed to start recording' });
|
|
522
|
+
reject(new Error(response.error || 'Failed to start recording'));
|
|
523
|
+
return;
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
dispatch({ type: 'START_RECORDING' });
|
|
527
|
+
resolve();
|
|
528
|
+
} catch (parseError) {
|
|
529
|
+
// If parsing fails, assume success (some platforms return non-JSON)
|
|
530
|
+
dispatch({ type: 'START_RECORDING' });
|
|
531
|
+
resolve();
|
|
532
|
+
}
|
|
533
|
+
});
|
|
534
|
+
});
|
|
535
|
+
} catch (error) {
|
|
536
|
+
const errorMessage = error instanceof Error ? error.message : 'Failed to start recording';
|
|
537
|
+
dispatch({ type: 'SET_ERROR', error: errorMessage });
|
|
538
|
+
throw error;
|
|
539
|
+
}
|
|
540
|
+
}, [checkMicrophonePermission, showPermissionAlert, reducerState.state]);
|
|
541
|
+
|
|
542
|
+
/**
|
|
543
|
+
* Pause recording.
|
|
544
|
+
* Wraps FileManager.finalizeSegment native call for segment-based recording.
|
|
545
|
+
* This finalizes the current segment so it can be previewed.
|
|
546
|
+
* Note: Android API < 24 does not support true pause.
|
|
547
|
+
*
|
|
548
|
+
* @validates Requirements 10.1, 10.3, 11.1
|
|
549
|
+
*/
|
|
550
|
+
const pauseRecording = useCallback(async (): Promise<void> => {
|
|
551
|
+
if (reducerState.state !== 'recording') {
|
|
552
|
+
return;
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
try {
|
|
556
|
+
// Save current amplitudes for this segment
|
|
557
|
+
const currentAmplitudes = [...reducerState.amplitudes];
|
|
558
|
+
const segmentDuration = reducerState.duration - getTotalSegmentsDuration();
|
|
559
|
+
|
|
560
|
+
return new Promise((resolve, reject) => {
|
|
561
|
+
NativeModules.FileManager.finalizeSegment((result: string) => {
|
|
562
|
+
try {
|
|
563
|
+
const response = JSON.parse(result);
|
|
564
|
+
|
|
565
|
+
if (response.success === false) {
|
|
566
|
+
// Fallback to legacy pause
|
|
567
|
+
NativeModules.FileManager.pauseRecording()
|
|
568
|
+
.then(() => {
|
|
569
|
+
pausedDurationRef.current = reducerState.duration;
|
|
570
|
+
dispatch({ type: 'PAUSE_RECORDING' });
|
|
571
|
+
resolve();
|
|
572
|
+
})
|
|
573
|
+
.catch((err: Error) => {
|
|
574
|
+
dispatch({ type: 'SET_ERROR', error: err.message });
|
|
575
|
+
reject(err);
|
|
576
|
+
});
|
|
577
|
+
return;
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
// Create segment from finalized recording
|
|
581
|
+
const segment: AudioSegment = {
|
|
582
|
+
id: `segment-${Date.now()}`,
|
|
583
|
+
filePath: response.segmentPath,
|
|
584
|
+
duration: segmentDuration > 0 ? segmentDuration : (response.duration * 1000) || 0,
|
|
585
|
+
amplitudes: currentAmplitudes.slice(
|
|
586
|
+
reducerState.segments.reduce((acc, s) => acc + s.amplitudes.length, 0)
|
|
587
|
+
),
|
|
588
|
+
createdAt: Date.now(),
|
|
589
|
+
};
|
|
590
|
+
|
|
591
|
+
dispatch({ type: 'FINALIZE_SEGMENT', segment });
|
|
592
|
+
pausedDurationRef.current = reducerState.duration;
|
|
593
|
+
dispatch({ type: 'PAUSE_RECORDING' });
|
|
594
|
+
resolve();
|
|
595
|
+
} catch (parseError) {
|
|
596
|
+
// Fallback to legacy pause
|
|
597
|
+
NativeModules.FileManager.pauseRecording()
|
|
598
|
+
.then(() => {
|
|
599
|
+
pausedDurationRef.current = reducerState.duration;
|
|
600
|
+
dispatch({ type: 'PAUSE_RECORDING' });
|
|
601
|
+
resolve();
|
|
602
|
+
})
|
|
603
|
+
.catch((err: Error) => {
|
|
604
|
+
dispatch({ type: 'SET_ERROR', error: err.message });
|
|
605
|
+
reject(err);
|
|
606
|
+
});
|
|
607
|
+
}
|
|
608
|
+
});
|
|
609
|
+
});
|
|
610
|
+
} catch (error) {
|
|
611
|
+
const errorMessage = error instanceof Error ? error.message : 'Failed to pause recording';
|
|
612
|
+
dispatch({ type: 'SET_ERROR', error: errorMessage });
|
|
613
|
+
throw error;
|
|
614
|
+
}
|
|
615
|
+
}, [reducerState.state, reducerState.duration, reducerState.amplitudes, reducerState.segments]);
|
|
616
|
+
|
|
617
|
+
/**
|
|
618
|
+
* Helper function to get total duration of all finalized segments.
|
|
619
|
+
*/
|
|
620
|
+
const getTotalSegmentsDuration = useCallback((): number => {
|
|
621
|
+
return reducerState.segments.reduce((acc, segment) => acc + segment.duration, 0);
|
|
622
|
+
}, [reducerState.segments]);
|
|
623
|
+
|
|
624
|
+
/**
|
|
625
|
+
* Resume recording.
|
|
626
|
+
* Wraps FileManager.resumeRecording native call.
|
|
627
|
+
*
|
|
628
|
+
* @validates Requirements 10.1, 10.3
|
|
629
|
+
*/
|
|
630
|
+
const resumeRecording = useCallback(async (): Promise<void> => {
|
|
631
|
+
if (reducerState.state !== 'paused') {
|
|
632
|
+
return;
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
try {
|
|
636
|
+
await NativeModules.FileManager.resumeRecording();
|
|
637
|
+
|
|
638
|
+
// Reset the recording start time for the new segment
|
|
639
|
+
recordingStartTimeRef.current = Date.now();
|
|
640
|
+
|
|
641
|
+
dispatch({ type: 'RESUME_RECORDING' });
|
|
642
|
+
} catch (error) {
|
|
643
|
+
const errorMessage = error instanceof Error ? error.message : 'Failed to resume recording';
|
|
644
|
+
dispatch({ type: 'SET_ERROR', error: errorMessage });
|
|
645
|
+
throw error;
|
|
646
|
+
}
|
|
647
|
+
}, [reducerState.state]);
|
|
648
|
+
|
|
649
|
+
/**
|
|
650
|
+
* Continue recording by starting a new segment.
|
|
651
|
+
* This is the WhatsApp-style flow: pause -> preview -> continue recording.
|
|
652
|
+
* Creates a new audio file for the next segment.
|
|
653
|
+
*
|
|
654
|
+
* @validates Requirements 11.2, 3.5
|
|
655
|
+
*/
|
|
656
|
+
const continueRecording = useCallback(async (): Promise<void> => {
|
|
657
|
+
if (reducerState.state !== 'paused') {
|
|
658
|
+
return;
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
try {
|
|
662
|
+
return new Promise((resolve, reject) => {
|
|
663
|
+
NativeModules.FileManager.startNewSegment((result: string) => {
|
|
664
|
+
try {
|
|
665
|
+
const response = JSON.parse(result);
|
|
666
|
+
|
|
667
|
+
if (response.success === false) {
|
|
668
|
+
// Fallback to legacy resume
|
|
669
|
+
NativeModules.FileManager.resumeRecording()
|
|
670
|
+
.then(() => {
|
|
671
|
+
recordingStartTimeRef.current = Date.now();
|
|
672
|
+
dispatch({ type: 'RESUME_RECORDING' });
|
|
673
|
+
resolve();
|
|
674
|
+
})
|
|
675
|
+
.catch((err: Error) => {
|
|
676
|
+
dispatch({ type: 'SET_ERROR', error: err.message });
|
|
677
|
+
reject(err);
|
|
678
|
+
});
|
|
679
|
+
return;
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
// Reset the recording start time for the new segment
|
|
683
|
+
recordingStartTimeRef.current = Date.now();
|
|
684
|
+
|
|
685
|
+
dispatch({ type: 'START_NEW_SEGMENT' });
|
|
686
|
+
resolve();
|
|
687
|
+
} catch (parseError) {
|
|
688
|
+
// Fallback to legacy resume
|
|
689
|
+
NativeModules.FileManager.resumeRecording()
|
|
690
|
+
.then(() => {
|
|
691
|
+
recordingStartTimeRef.current = Date.now();
|
|
692
|
+
dispatch({ type: 'RESUME_RECORDING' });
|
|
693
|
+
resolve();
|
|
694
|
+
})
|
|
695
|
+
.catch((err: Error) => {
|
|
696
|
+
dispatch({ type: 'SET_ERROR', error: err.message });
|
|
697
|
+
reject(err);
|
|
698
|
+
});
|
|
699
|
+
}
|
|
700
|
+
});
|
|
701
|
+
});
|
|
702
|
+
} catch (error) {
|
|
703
|
+
const errorMessage = error instanceof Error ? error.message : 'Failed to continue recording';
|
|
704
|
+
dispatch({ type: 'SET_ERROR', error: errorMessage });
|
|
705
|
+
throw error;
|
|
706
|
+
}
|
|
707
|
+
}, [reducerState.state, reducerState.segments.length]);
|
|
708
|
+
|
|
709
|
+
/**
|
|
710
|
+
* Stop recording and finalize all segments.
|
|
711
|
+
* If multiple segments exist, merges them into a single audio file.
|
|
712
|
+
* Wraps FileManager.releaseMediaResources and mergeSegments native calls.
|
|
713
|
+
*
|
|
714
|
+
* @validates Requirements 10.1, 11.4
|
|
715
|
+
*/
|
|
716
|
+
const stopRecording = useCallback(async (): Promise<string | null> => {
|
|
717
|
+
if (reducerState.state !== 'recording' && reducerState.state !== 'paused') {
|
|
718
|
+
return null;
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
// Calculate final duration before stopping
|
|
722
|
+
let finalDuration = reducerState.duration;
|
|
723
|
+
if (reducerState.state === 'recording' && recordingStartTimeRef.current > 0) {
|
|
724
|
+
finalDuration = pausedDurationRef.current + (Date.now() - recordingStartTimeRef.current);
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
return new Promise((resolve) => {
|
|
728
|
+
NativeModules.FileManager.releaseMediaResources((result: string) => {
|
|
729
|
+
try {
|
|
730
|
+
const response = JSON.parse(result);
|
|
731
|
+
|
|
732
|
+
let filePath = response.file || response.path || null;
|
|
733
|
+
|
|
734
|
+
// Check if we have multiple segments to merge
|
|
735
|
+
const allSegmentPaths = [...reducerState.segments.map(s => s.filePath)];
|
|
736
|
+
if (filePath && reducerState.state === 'recording') {
|
|
737
|
+
// Add the current recording as the last segment
|
|
738
|
+
allSegmentPaths.push(filePath);
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
if (allSegmentPaths.length > 1) {
|
|
742
|
+
// Merge all segments
|
|
743
|
+
NativeModules.FileManager.mergeSegments(allSegmentPaths, (mergeResult: string) => {
|
|
744
|
+
try {
|
|
745
|
+
const mergeResponse = JSON.parse(mergeResult);
|
|
746
|
+
if (mergeResponse.success && mergeResponse.mergedPath) {
|
|
747
|
+
filePath = mergeResponse.mergedPath;
|
|
748
|
+
}
|
|
749
|
+
} catch (mergeParseError) {
|
|
750
|
+
// Keep original filePath
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
finishStopRecording(filePath, finalDuration, response, resolve);
|
|
754
|
+
});
|
|
755
|
+
} else {
|
|
756
|
+
finishStopRecording(filePath, finalDuration, response, resolve);
|
|
757
|
+
}
|
|
758
|
+
} catch (parseError) {
|
|
759
|
+
dispatch({ type: 'SET_ERROR', error: 'Failed to parse recording result' });
|
|
760
|
+
resolve(null);
|
|
761
|
+
}
|
|
762
|
+
});
|
|
763
|
+
});
|
|
764
|
+
}, [reducerState.state, reducerState.duration, reducerState.segments]);
|
|
765
|
+
|
|
766
|
+
/**
|
|
767
|
+
* Helper function to finish stop recording process.
|
|
768
|
+
*/
|
|
769
|
+
const finishStopRecording = useCallback((
|
|
770
|
+
filePath: string | null,
|
|
771
|
+
finalDuration: number,
|
|
772
|
+
response: { duration?: number },
|
|
773
|
+
resolve: (value: string | null) => void
|
|
774
|
+
) => {
|
|
775
|
+
if (filePath) {
|
|
776
|
+
// Update duration from native if available (iOS provides this)
|
|
777
|
+
if (response.duration && response.duration > 0) {
|
|
778
|
+
const nativeDuration = response.duration * 1000;
|
|
779
|
+
dispatch({ type: 'UPDATE_DURATION', duration: nativeDuration });
|
|
780
|
+
} else {
|
|
781
|
+
// Use our calculated duration
|
|
782
|
+
dispatch({ type: 'UPDATE_DURATION', duration: finalDuration });
|
|
783
|
+
}
|
|
784
|
+
dispatch({ type: 'STOP_RECORDING', filePath });
|
|
785
|
+
resolve(filePath);
|
|
786
|
+
} else {
|
|
787
|
+
dispatch({ type: 'SET_ERROR', error: 'No file path returned' });
|
|
788
|
+
resolve(null);
|
|
789
|
+
}
|
|
790
|
+
}, []);
|
|
791
|
+
|
|
792
|
+
/**
|
|
793
|
+
* Start playback of recorded audio.
|
|
794
|
+
* Wraps FileManager.playAudio native call.
|
|
795
|
+
*
|
|
796
|
+
* @validates Requirements 10.1
|
|
797
|
+
*/
|
|
798
|
+
const startPlayback = useCallback(async (): Promise<void> => {
|
|
799
|
+
if (reducerState.state !== 'completed') {
|
|
800
|
+
return;
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
if (!reducerState.filePath) {
|
|
804
|
+
return;
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
return new Promise((resolve, reject) => {
|
|
808
|
+
NativeModules.FileManager.playAudio((result: string) => {
|
|
809
|
+
try {
|
|
810
|
+
const response = JSON.parse(result);
|
|
811
|
+
|
|
812
|
+
if (response.success === false) {
|
|
813
|
+
dispatch({ type: 'SET_ERROR', error: response.error || 'Failed to start playback' });
|
|
814
|
+
reject(new Error(response.error || 'Failed to start playback'));
|
|
815
|
+
return;
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
// Reset tracking refs since we're starting from 0
|
|
819
|
+
lastRequestedPositionRef.current = 0;
|
|
820
|
+
lastRequestedProgressRef.current = 0;
|
|
821
|
+
|
|
822
|
+
dispatch({ type: 'START_PLAYBACK' });
|
|
823
|
+
resolve();
|
|
824
|
+
} catch (parseError) {
|
|
825
|
+
// Assume success if parsing fails
|
|
826
|
+
dispatch({ type: 'START_PLAYBACK' });
|
|
827
|
+
resolve();
|
|
828
|
+
}
|
|
829
|
+
});
|
|
830
|
+
});
|
|
831
|
+
}, [reducerState.state, reducerState.filePath]);
|
|
832
|
+
|
|
833
|
+
/**
|
|
834
|
+
* Start playback preview while paused.
|
|
835
|
+
* Plays the finalized segments without stopping the recording session.
|
|
836
|
+
* For multiple segments, merges them first for gapless playback.
|
|
837
|
+
* This allows the user to preview what they've recorded while still being able to continue.
|
|
838
|
+
*
|
|
839
|
+
* @validates Requirements 3.4, 4.1
|
|
840
|
+
*/
|
|
841
|
+
const startPlaybackPreview = useCallback(async (): Promise<void> => {
|
|
842
|
+
if (reducerState.state !== 'paused') {
|
|
843
|
+
return;
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
// Get all segment paths to play
|
|
847
|
+
const segmentPaths = reducerState.segments.map(s => s.filePath);
|
|
848
|
+
|
|
849
|
+
if (segmentPaths.length === 0) {
|
|
850
|
+
return;
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
// Reset tracking refs since we're starting from 0
|
|
854
|
+
lastRequestedPositionRef.current = 0;
|
|
855
|
+
lastRequestedProgressRef.current = 0;
|
|
856
|
+
|
|
857
|
+
return new Promise((resolve, reject) => {
|
|
858
|
+
if (segmentPaths.length === 1) {
|
|
859
|
+
// Single segment - use playAudio directly with the segment path
|
|
860
|
+
NativeModules.FileManager.playAudio((result: string) => {
|
|
861
|
+
try {
|
|
862
|
+
const response = JSON.parse(result);
|
|
863
|
+
if (response.success === false) {
|
|
864
|
+
dispatch({ type: 'SET_ERROR', error: response.error || 'Failed to start playback' });
|
|
865
|
+
reject(new Error(response.error || 'Failed to start playback'));
|
|
866
|
+
return;
|
|
867
|
+
}
|
|
868
|
+
dispatch({ type: 'START_PLAYBACK' });
|
|
869
|
+
resolve();
|
|
870
|
+
} catch (parseError) {
|
|
871
|
+
dispatch({ type: 'START_PLAYBACK' });
|
|
872
|
+
resolve();
|
|
873
|
+
}
|
|
874
|
+
});
|
|
875
|
+
} else {
|
|
876
|
+
// Multiple segments - merge first for gapless playback, then play
|
|
877
|
+
NativeModules.FileManager.mergeSegments(segmentPaths, (mergeResult: string) => {
|
|
878
|
+
try {
|
|
879
|
+
const mergeResponse = JSON.parse(mergeResult);
|
|
880
|
+
if (mergeResponse.success && mergeResponse.mergedPath) {
|
|
881
|
+
// Play the merged file
|
|
882
|
+
NativeModules.FileManager.playAudio((playResult: string) => {
|
|
883
|
+
try {
|
|
884
|
+
const playResponse = JSON.parse(playResult);
|
|
885
|
+
if (playResponse.success === false) {
|
|
886
|
+
dispatch({ type: 'SET_ERROR', error: playResponse.error || 'Failed to start playback' });
|
|
887
|
+
reject(new Error(playResponse.error || 'Failed to start playback'));
|
|
888
|
+
return;
|
|
889
|
+
}
|
|
890
|
+
dispatch({ type: 'START_PLAYBACK' });
|
|
891
|
+
resolve();
|
|
892
|
+
} catch (parseError) {
|
|
893
|
+
dispatch({ type: 'START_PLAYBACK' });
|
|
894
|
+
resolve();
|
|
895
|
+
}
|
|
896
|
+
});
|
|
897
|
+
} else {
|
|
898
|
+
// Fallback to sequential playback if merge fails
|
|
899
|
+
NativeModules.FileManager.playSegments(segmentPaths, (result: string) => {
|
|
900
|
+
try {
|
|
901
|
+
const response = JSON.parse(result);
|
|
902
|
+
if (response.success === false) {
|
|
903
|
+
dispatch({ type: 'SET_ERROR', error: response.error || 'Failed to start playback' });
|
|
904
|
+
reject(new Error(response.error || 'Failed to start playback'));
|
|
905
|
+
return;
|
|
906
|
+
}
|
|
907
|
+
dispatch({ type: 'START_PLAYBACK' });
|
|
908
|
+
resolve();
|
|
909
|
+
} catch (parseError) {
|
|
910
|
+
dispatch({ type: 'START_PLAYBACK' });
|
|
911
|
+
resolve();
|
|
912
|
+
}
|
|
913
|
+
});
|
|
914
|
+
}
|
|
915
|
+
} catch (parseError) {
|
|
916
|
+
// Fallback to sequential playback
|
|
917
|
+
NativeModules.FileManager.playSegments(segmentPaths, (result: string) => {
|
|
918
|
+
dispatch({ type: 'START_PLAYBACK' });
|
|
919
|
+
resolve();
|
|
920
|
+
});
|
|
921
|
+
}
|
|
922
|
+
});
|
|
923
|
+
}
|
|
924
|
+
});
|
|
925
|
+
}, [reducerState.state, reducerState.segments]);
|
|
926
|
+
|
|
927
|
+
/**
|
|
928
|
+
* Pause playback.
|
|
929
|
+
* Wraps FileManager.pausePlaying native call.
|
|
930
|
+
*
|
|
931
|
+
* @validates Requirements 10.1
|
|
932
|
+
*/
|
|
933
|
+
const pausePlayback = useCallback(async (): Promise<void> => {
|
|
934
|
+
if (reducerState.state !== 'playing') {
|
|
935
|
+
return;
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
return new Promise((resolve) => {
|
|
939
|
+
NativeModules.FileManager.pausePlaying((result: string) => {
|
|
940
|
+
dispatch({ type: 'PAUSE_PLAYBACK' });
|
|
941
|
+
resolve();
|
|
942
|
+
});
|
|
943
|
+
});
|
|
944
|
+
}, [reducerState.state]);
|
|
945
|
+
|
|
946
|
+
/**
|
|
947
|
+
* Resume playback.
|
|
948
|
+
* Wraps FileManager.resumePlaying native call.
|
|
949
|
+
* Now works on both iOS and Android for instant resume from paused position.
|
|
950
|
+
*
|
|
951
|
+
* @validates Requirements 10.1
|
|
952
|
+
*/
|
|
953
|
+
const resumePlayback = useCallback(async (): Promise<void> => {
|
|
954
|
+
if (reducerState.state !== 'completed') {
|
|
955
|
+
return;
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
return new Promise((resolve) => {
|
|
959
|
+
// Both iOS and Android now support resumePlaying for instant resume
|
|
960
|
+
NativeModules.FileManager.resumePlaying((result: string) => {
|
|
961
|
+
dispatch({ type: 'RESUME_PLAYBACK' });
|
|
962
|
+
resolve();
|
|
963
|
+
});
|
|
964
|
+
});
|
|
965
|
+
}, [reducerState.state]);
|
|
966
|
+
|
|
967
|
+
/**
|
|
968
|
+
* Seek to a specific position in the recording.
|
|
969
|
+
* Position is expressed as a progress value from 0.0 to 1.0.
|
|
970
|
+
* Calls native seekTo for actual audio seeking during playback.
|
|
971
|
+
* Uses debouncing and operation tracking to prevent race conditions.
|
|
972
|
+
*
|
|
973
|
+
* @validates Requirements 5.1, 5.2, 5.3
|
|
974
|
+
*/
|
|
975
|
+
const seekTo = useCallback(async (progress: number): Promise<void> => {
|
|
976
|
+
// Clamp progress to valid range
|
|
977
|
+
const clampedProgress = Math.max(0, Math.min(1, progress));
|
|
978
|
+
const position = Math.floor(clampedProgress * reducerState.duration);
|
|
979
|
+
|
|
980
|
+
// Track last requested position for reset detection
|
|
981
|
+
lastRequestedPositionRef.current = position;
|
|
982
|
+
lastRequestedProgressRef.current = clampedProgress;
|
|
983
|
+
|
|
984
|
+
// Increment operation ID to track this seek
|
|
985
|
+
const operationId = ++seekOperationIdRef.current;
|
|
986
|
+
isSeekingRef.current = true;
|
|
987
|
+
|
|
988
|
+
// Update UI position immediately for smooth feedback
|
|
989
|
+
dispatch({ type: 'SEEK', position });
|
|
990
|
+
|
|
991
|
+
// If currently playing, debounce the native seek call
|
|
992
|
+
if (reducerState.state === 'playing') {
|
|
993
|
+
// Clear any pending debounced seek
|
|
994
|
+
if (seekDebounceTimerRef.current) {
|
|
995
|
+
clearTimeout(seekDebounceTimerRef.current);
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
// Store the pending seek
|
|
999
|
+
pendingSeekRef.current = { progress: clampedProgress, operationId };
|
|
1000
|
+
|
|
1001
|
+
// Debounce the actual native call by 100ms
|
|
1002
|
+
return new Promise((resolve) => {
|
|
1003
|
+
seekDebounceTimerRef.current = setTimeout(() => {
|
|
1004
|
+
// Check if this is still the latest seek operation
|
|
1005
|
+
if (operationId !== seekOperationIdRef.current) {
|
|
1006
|
+
resolve();
|
|
1007
|
+
return;
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
const finalPosition = Math.floor(clampedProgress * reducerState.duration);
|
|
1011
|
+
|
|
1012
|
+
NativeModules.FileManager.seekTo(finalPosition, (result: string) => {
|
|
1013
|
+
// Only clear seeking flag if this is still the latest operation
|
|
1014
|
+
if (operationId === seekOperationIdRef.current) {
|
|
1015
|
+
// Clear seeking flag immediately since native has confirmed seek is complete
|
|
1016
|
+
isSeekingRef.current = false;
|
|
1017
|
+
pendingSeekRef.current = null;
|
|
1018
|
+
}
|
|
1019
|
+
resolve();
|
|
1020
|
+
});
|
|
1021
|
+
}, 100);
|
|
1022
|
+
});
|
|
1023
|
+
}
|
|
1024
|
+
|
|
1025
|
+
// Not playing, just update position and clear seeking flag
|
|
1026
|
+
setTimeout(() => {
|
|
1027
|
+
if (operationId === seekOperationIdRef.current) {
|
|
1028
|
+
isSeekingRef.current = false;
|
|
1029
|
+
}
|
|
1030
|
+
}, 50);
|
|
1031
|
+
}, [reducerState.duration, reducerState.state]);
|
|
1032
|
+
|
|
1033
|
+
/**
|
|
1034
|
+
* Seek and start playback from a specific position.
|
|
1035
|
+
* Used when tapping on waveform in preview/paused mode.
|
|
1036
|
+
* Uses debouncing and operation tracking to prevent race conditions.
|
|
1037
|
+
*
|
|
1038
|
+
* @validates Requirements 5.1, 5.2
|
|
1039
|
+
*/
|
|
1040
|
+
const seekAndPlay = useCallback(async (progress: number): Promise<void> => {
|
|
1041
|
+
// Clamp progress to valid range
|
|
1042
|
+
const clampedProgress = Math.max(0, Math.min(1, progress));
|
|
1043
|
+
const position = Math.floor(clampedProgress * reducerState.duration);
|
|
1044
|
+
|
|
1045
|
+
// Track last requested position for reset detection
|
|
1046
|
+
lastRequestedPositionRef.current = position;
|
|
1047
|
+
lastRequestedProgressRef.current = clampedProgress;
|
|
1048
|
+
|
|
1049
|
+
// Increment operation ID to track this seek
|
|
1050
|
+
const operationId = ++seekOperationIdRef.current;
|
|
1051
|
+
isSeekingRef.current = true;
|
|
1052
|
+
|
|
1053
|
+
// Update UI position immediately
|
|
1054
|
+
dispatch({ type: 'SEEK', position });
|
|
1055
|
+
|
|
1056
|
+
// Clear any pending debounced seek
|
|
1057
|
+
if (seekDebounceTimerRef.current) {
|
|
1058
|
+
clearTimeout(seekDebounceTimerRef.current);
|
|
1059
|
+
seekDebounceTimerRef.current = null;
|
|
1060
|
+
}
|
|
1061
|
+
|
|
1062
|
+
// For single segment in completed state, use playFromPosition with debouncing
|
|
1063
|
+
if (reducerState.state === 'completed' && reducerState.filePath) {
|
|
1064
|
+
// Store the pending seek
|
|
1065
|
+
pendingSeekRef.current = { progress: clampedProgress, operationId };
|
|
1066
|
+
|
|
1067
|
+
return new Promise((resolve, reject) => {
|
|
1068
|
+
// Debounce by 150ms to allow rapid taps to settle
|
|
1069
|
+
seekDebounceTimerRef.current = setTimeout(() => {
|
|
1070
|
+
// Check if this is still the latest seek operation
|
|
1071
|
+
if (operationId !== seekOperationIdRef.current) {
|
|
1072
|
+
isSeekingRef.current = false;
|
|
1073
|
+
resolve();
|
|
1074
|
+
return;
|
|
1075
|
+
}
|
|
1076
|
+
|
|
1077
|
+
const finalPosition = Math.floor(clampedProgress * reducerState.duration);
|
|
1078
|
+
|
|
1079
|
+
NativeModules.FileManager.playFromPosition(finalPosition, (result: string) => {
|
|
1080
|
+
// Check if this is still the latest operation before updating state
|
|
1081
|
+
if (operationId !== seekOperationIdRef.current) {
|
|
1082
|
+
resolve();
|
|
1083
|
+
return;
|
|
1084
|
+
}
|
|
1085
|
+
|
|
1086
|
+
try {
|
|
1087
|
+
const response = JSON.parse(result);
|
|
1088
|
+
if (response.success) {
|
|
1089
|
+
dispatch({ type: 'START_PLAYBACK' });
|
|
1090
|
+
// Clear seeking flag immediately since native has confirmed seek is complete
|
|
1091
|
+
isSeekingRef.current = false;
|
|
1092
|
+
pendingSeekRef.current = null;
|
|
1093
|
+
resolve();
|
|
1094
|
+
} else {
|
|
1095
|
+
isSeekingRef.current = false;
|
|
1096
|
+
pendingSeekRef.current = null;
|
|
1097
|
+
reject(new Error(response.error));
|
|
1098
|
+
}
|
|
1099
|
+
} catch (e) {
|
|
1100
|
+
dispatch({ type: 'START_PLAYBACK' });
|
|
1101
|
+
isSeekingRef.current = false;
|
|
1102
|
+
pendingSeekRef.current = null;
|
|
1103
|
+
resolve();
|
|
1104
|
+
}
|
|
1105
|
+
});
|
|
1106
|
+
}, 150);
|
|
1107
|
+
});
|
|
1108
|
+
}
|
|
1109
|
+
|
|
1110
|
+
// For preview mode (paused with segments), merge segments first then play from position
|
|
1111
|
+
if (reducerState.state === 'paused' && reducerState.segments.length > 0) {
|
|
1112
|
+
const segmentPaths = reducerState.segments.map(s => s.filePath);
|
|
1113
|
+
const finalPosition = Math.floor(clampedProgress * reducerState.duration);
|
|
1114
|
+
|
|
1115
|
+
// Store the pending seek
|
|
1116
|
+
pendingSeekRef.current = { progress: clampedProgress, operationId };
|
|
1117
|
+
|
|
1118
|
+
return new Promise((resolve, reject) => {
|
|
1119
|
+
// Debounce by 150ms to allow rapid taps to settle
|
|
1120
|
+
seekDebounceTimerRef.current = setTimeout(() => {
|
|
1121
|
+
// Check if this is still the latest seek operation
|
|
1122
|
+
if (operationId !== seekOperationIdRef.current) {
|
|
1123
|
+
isSeekingRef.current = false;
|
|
1124
|
+
resolve();
|
|
1125
|
+
return;
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
const seekPosition = Math.floor(clampedProgress * reducerState.duration);
|
|
1129
|
+
|
|
1130
|
+
if (segmentPaths.length === 1) {
|
|
1131
|
+
// Single segment - play directly from position
|
|
1132
|
+
NativeModules.FileManager.playFromPosition(seekPosition, (result: string) => {
|
|
1133
|
+
if (operationId !== seekOperationIdRef.current) {
|
|
1134
|
+
resolve();
|
|
1135
|
+
return;
|
|
1136
|
+
}
|
|
1137
|
+
try {
|
|
1138
|
+
const response = JSON.parse(result);
|
|
1139
|
+
if (response.success) {
|
|
1140
|
+
dispatch({ type: 'START_PLAYBACK' });
|
|
1141
|
+
// Clear seeking flag immediately since native has confirmed seek is complete
|
|
1142
|
+
isSeekingRef.current = false;
|
|
1143
|
+
pendingSeekRef.current = null;
|
|
1144
|
+
resolve();
|
|
1145
|
+
} else {
|
|
1146
|
+
isSeekingRef.current = false;
|
|
1147
|
+
pendingSeekRef.current = null;
|
|
1148
|
+
reject(new Error(response.error));
|
|
1149
|
+
}
|
|
1150
|
+
} catch (e) {
|
|
1151
|
+
dispatch({ type: 'START_PLAYBACK' });
|
|
1152
|
+
isSeekingRef.current = false;
|
|
1153
|
+
pendingSeekRef.current = null;
|
|
1154
|
+
resolve();
|
|
1155
|
+
}
|
|
1156
|
+
});
|
|
1157
|
+
} else {
|
|
1158
|
+
// Multiple segments - merge first, then play from position
|
|
1159
|
+
NativeModules.FileManager.mergeSegments(segmentPaths, (mergeResult: string) => {
|
|
1160
|
+
if (operationId !== seekOperationIdRef.current) {
|
|
1161
|
+
resolve();
|
|
1162
|
+
return;
|
|
1163
|
+
}
|
|
1164
|
+
try {
|
|
1165
|
+
const mergeResponse = JSON.parse(mergeResult);
|
|
1166
|
+
if (mergeResponse.success && mergeResponse.mergedPath) {
|
|
1167
|
+
// Now play from the requested position
|
|
1168
|
+
NativeModules.FileManager.playFromPosition(seekPosition, (playResult: string) => {
|
|
1169
|
+
if (operationId !== seekOperationIdRef.current) {
|
|
1170
|
+
resolve();
|
|
1171
|
+
return;
|
|
1172
|
+
}
|
|
1173
|
+
try {
|
|
1174
|
+
const playResponse = JSON.parse(playResult);
|
|
1175
|
+
if (playResponse.success) {
|
|
1176
|
+
dispatch({ type: 'START_PLAYBACK' });
|
|
1177
|
+
// Clear seeking flag immediately since native has confirmed seek is complete
|
|
1178
|
+
isSeekingRef.current = false;
|
|
1179
|
+
pendingSeekRef.current = null;
|
|
1180
|
+
resolve();
|
|
1181
|
+
} else {
|
|
1182
|
+
isSeekingRef.current = false;
|
|
1183
|
+
pendingSeekRef.current = null;
|
|
1184
|
+
reject(new Error(playResponse.error));
|
|
1185
|
+
}
|
|
1186
|
+
} catch (e) {
|
|
1187
|
+
dispatch({ type: 'START_PLAYBACK' });
|
|
1188
|
+
isSeekingRef.current = false;
|
|
1189
|
+
pendingSeekRef.current = null;
|
|
1190
|
+
resolve();
|
|
1191
|
+
}
|
|
1192
|
+
});
|
|
1193
|
+
} else {
|
|
1194
|
+
// Merge failed, fall back to starting from beginning
|
|
1195
|
+
isSeekingRef.current = false;
|
|
1196
|
+
pendingSeekRef.current = null;
|
|
1197
|
+
// Reset the last requested position since we're starting from 0
|
|
1198
|
+
lastRequestedPositionRef.current = 0;
|
|
1199
|
+
lastRequestedProgressRef.current = 0;
|
|
1200
|
+
NativeModules.FileManager.playSegments(segmentPaths, (result: string) => {
|
|
1201
|
+
dispatch({ type: 'START_PLAYBACK' });
|
|
1202
|
+
resolve();
|
|
1203
|
+
});
|
|
1204
|
+
}
|
|
1205
|
+
} catch (e) {
|
|
1206
|
+
// Merge parse error, fall back to starting from beginning
|
|
1207
|
+
isSeekingRef.current = false;
|
|
1208
|
+
pendingSeekRef.current = null;
|
|
1209
|
+
lastRequestedPositionRef.current = 0;
|
|
1210
|
+
lastRequestedProgressRef.current = 0;
|
|
1211
|
+
NativeModules.FileManager.playSegments(segmentPaths, (result: string) => {
|
|
1212
|
+
dispatch({ type: 'START_PLAYBACK' });
|
|
1213
|
+
resolve();
|
|
1214
|
+
});
|
|
1215
|
+
}
|
|
1216
|
+
});
|
|
1217
|
+
}
|
|
1218
|
+
}, 150);
|
|
1219
|
+
});
|
|
1220
|
+
}
|
|
1221
|
+
|
|
1222
|
+
// Clear seeking flag for other cases
|
|
1223
|
+
isSeekingRef.current = false;
|
|
1224
|
+
pendingSeekRef.current = null;
|
|
1225
|
+
}, [reducerState.state, reducerState.duration, reducerState.filePath, reducerState.segments]);
|
|
1226
|
+
|
|
1227
|
+
/**
|
|
1228
|
+
* Cancel recording and cleanup all resources.
|
|
1229
|
+
* Deletes all segment files and releases native resources.
|
|
1230
|
+
*
|
|
1231
|
+
* @validates Requirements 7.2, 7.4, 10.5, 11.5
|
|
1232
|
+
*/
|
|
1233
|
+
const cancel = useCallback(async (): Promise<void> => {
|
|
1234
|
+
// Clear all timers
|
|
1235
|
+
if (durationTimerRef.current) {
|
|
1236
|
+
clearInterval(durationTimerRef.current);
|
|
1237
|
+
durationTimerRef.current = null;
|
|
1238
|
+
}
|
|
1239
|
+
if (amplitudeTimerRef.current) {
|
|
1240
|
+
clearInterval(amplitudeTimerRef.current);
|
|
1241
|
+
amplitudeTimerRef.current = null;
|
|
1242
|
+
}
|
|
1243
|
+
if (playbackTimerRef.current) {
|
|
1244
|
+
clearInterval(playbackTimerRef.current);
|
|
1245
|
+
playbackTimerRef.current = null;
|
|
1246
|
+
}
|
|
1247
|
+
if (seekDebounceTimerRef.current) {
|
|
1248
|
+
clearTimeout(seekDebounceTimerRef.current);
|
|
1249
|
+
seekDebounceTimerRef.current = null;
|
|
1250
|
+
}
|
|
1251
|
+
|
|
1252
|
+
// Reset refs
|
|
1253
|
+
pausedDurationRef.current = 0;
|
|
1254
|
+
recordingStartTimeRef.current = 0;
|
|
1255
|
+
timerValueRef.current = 0;
|
|
1256
|
+
seekOperationIdRef.current = 0;
|
|
1257
|
+
isSeekingRef.current = false;
|
|
1258
|
+
pendingSeekRef.current = null;
|
|
1259
|
+
lastRequestedPositionRef.current = 0;
|
|
1260
|
+
lastRequestedProgressRef.current = 0;
|
|
1261
|
+
|
|
1262
|
+
// Delete all segment files if any
|
|
1263
|
+
const segmentPaths = reducerState.segments.map(s => s.filePath);
|
|
1264
|
+
if (segmentPaths.length > 0) {
|
|
1265
|
+
return new Promise((resolve) => {
|
|
1266
|
+
NativeModules.FileManager.deleteSegments(segmentPaths, (deleteSegmentsResult: string) => {
|
|
1267
|
+
// Also delete the main file and release resources
|
|
1268
|
+
NativeModules.FileManager.deleteFile((deleteResult: string) => {
|
|
1269
|
+
NativeModules.FileManager.releaseMediaResources((releaseResult: string) => {
|
|
1270
|
+
dispatch({ type: 'CLEAR_SEGMENTS' });
|
|
1271
|
+
dispatch({ type: 'RESET' });
|
|
1272
|
+
resolve();
|
|
1273
|
+
});
|
|
1274
|
+
});
|
|
1275
|
+
});
|
|
1276
|
+
});
|
|
1277
|
+
}
|
|
1278
|
+
|
|
1279
|
+
// No segments, just delete the main file
|
|
1280
|
+
return new Promise((resolve) => {
|
|
1281
|
+
NativeModules.FileManager.deleteFile((deleteResult: string) => {
|
|
1282
|
+
// Release media resources
|
|
1283
|
+
NativeModules.FileManager.releaseMediaResources((releaseResult: string) => {
|
|
1284
|
+
dispatch({ type: 'RESET' });
|
|
1285
|
+
resolve();
|
|
1286
|
+
});
|
|
1287
|
+
});
|
|
1288
|
+
});
|
|
1289
|
+
}, [reducerState.state, reducerState.filePath, reducerState.segments]);
|
|
1290
|
+
|
|
1291
|
+
/**
|
|
1292
|
+
* Computed properties for state checks.
|
|
1293
|
+
* @validates Requirements 9.3, 11.7
|
|
1294
|
+
*/
|
|
1295
|
+
const isRecording = useMemo(() => reducerState.state === 'recording', [reducerState.state]);
|
|
1296
|
+
const isPaused = useMemo(() => reducerState.state === 'paused', [reducerState.state]);
|
|
1297
|
+
const isPlaying = useMemo(() => reducerState.state === 'playing', [reducerState.state]);
|
|
1298
|
+
const isCompleted = useMemo(() => reducerState.state === 'completed', [reducerState.state]);
|
|
1299
|
+
const hasRecording = useMemo(() => reducerState.duration > 0, [reducerState.duration]);
|
|
1300
|
+
const hasMultipleSegments = useMemo(() => reducerState.segments.length > 1, [reducerState.segments.length]);
|
|
1301
|
+
const canContinueRecording = useMemo(() => reducerState.state === 'paused', [reducerState.state]);
|
|
1302
|
+
|
|
1303
|
+
return {
|
|
1304
|
+
// State
|
|
1305
|
+
state: reducerState.state,
|
|
1306
|
+
duration: reducerState.duration,
|
|
1307
|
+
currentPosition: reducerState.currentPosition,
|
|
1308
|
+
filePath: reducerState.filePath,
|
|
1309
|
+
error: reducerState.error,
|
|
1310
|
+
amplitudes: reducerState.amplitudes,
|
|
1311
|
+
segments: reducerState.segments,
|
|
1312
|
+
hasMultipleSegments,
|
|
1313
|
+
|
|
1314
|
+
// Actions
|
|
1315
|
+
startRecording,
|
|
1316
|
+
pauseRecording,
|
|
1317
|
+
resumeRecording,
|
|
1318
|
+
continueRecording,
|
|
1319
|
+
stopRecording,
|
|
1320
|
+
startPlayback,
|
|
1321
|
+
startPlaybackPreview,
|
|
1322
|
+
pausePlayback,
|
|
1323
|
+
resumePlayback,
|
|
1324
|
+
seekTo,
|
|
1325
|
+
seekAndPlay,
|
|
1326
|
+
cancel,
|
|
1327
|
+
|
|
1328
|
+
// Computed properties
|
|
1329
|
+
isRecording,
|
|
1330
|
+
isPaused,
|
|
1331
|
+
isPlaying,
|
|
1332
|
+
isCompleted,
|
|
1333
|
+
hasRecording,
|
|
1334
|
+
canContinueRecording,
|
|
1335
|
+
};
|
|
1336
|
+
}
|