Skip to content

Commit a6b6e51

Browse files
committed
feat: keep recording on reboot + allow to delete them
1 parent e71c116 commit a6b6e51

5 files changed

Lines changed: 63 additions & 4 deletions

File tree

example/src/App.tsx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import React, {
1818
} from 'react';
1919
import {
2020
ActivityIndicator,
21+
Alert,
2122
Image,
2223
Linking,
2324
Pressable,
@@ -35,11 +36,13 @@ import { Icons } from './assets';
3536
import {
3637
generateAudioList,
3738
playbackSpeedSequence,
39+
getRecordedAudios,
3840
type ListItem,
3941
} from './constants';
4042
import stylesheet from './styles';
4143
import { Colors } from './theme';
4244
import FastImage from 'react-native-fast-image';
45+
import fs from 'react-native-fs';
4346

4447
const RenderListItem = React.memo(
4548
({
@@ -248,6 +251,37 @@ const AppContainer = () => {
248251
);
249252
};
250253

254+
const handleDeleteRecordings = async () => {
255+
const recordings = await getRecordedAudios();
256+
257+
const deleteRecordings = async () => {
258+
await Promise.all(recordings.map(async recording => fs.unlink(recording)))
259+
.then(() => {
260+
Alert.alert(
261+
'All recording deleted',
262+
'All recordings have been deleted successfully! Reboot the app to see the changes.',
263+
[{ text: 'Dismiss' }]
264+
);
265+
})
266+
.catch(error => {
267+
Alert.alert(
268+
'Error deleting recordings',
269+
'Below error happened while deleting recordings:\n' + error,
270+
[{ text: 'Dismiss' }]
271+
);
272+
});
273+
};
274+
275+
Alert.alert(
276+
'Delete all recording',
277+
`Continue to delete all ${recordings.length} recordings.\n App restart is required!`,
278+
[
279+
{ text: 'Cancel', style: 'cancel' },
280+
{ text: 'OK', onPress: deleteRecordings },
281+
]
282+
);
283+
};
284+
251285
return (
252286
<View style={styles.appContainer}>
253287
<StatusBar
@@ -265,6 +299,15 @@ const AppContainer = () => {
265299
style={styles.simformImage}
266300
resizeMode="contain"
267301
/>
302+
<Pressable
303+
style={styles.playBackControlPressable}
304+
onPress={handleDeleteRecordings}>
305+
<Image
306+
source={Icons.delete}
307+
style={styles.buttonImage}
308+
resizeMode="contain"
309+
/>
310+
</Pressable>
268311
</View>
269312
<ScrollView scrollEnabled={shouldScroll}>
270313
{list.map(item => (
7.72 KB
Loading

example/src/assets/icons/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ export const Icons = {
44
simform: require('./simform.png'),
55
mic: require('./mic.png'),
66
logo: require('./logo.png'),
7+
delete: require('./delete.png'),
78
};

example/src/constants/Audios.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import fs from 'react-native-fs';
22
import RNFetchBlob from 'rn-fetch-blob';
33
import { globalMetrics } from '../../src/theme';
4+
import { Platform } from 'react-native';
45

56
export interface ListItem {
67
fromCurrentUser: boolean;
@@ -69,17 +70,30 @@ const audioAssetArray = [
6970
'file_example_mp3_15s.mp3',
7071
];
7172

73+
/**
74+
* Retrieve previously recorded audio files from the cache/document directory.
75+
* @returns
76+
*/
77+
export const getRecordedAudios = async (): Promise<string[]> => {
78+
const recordingSavingPath = Platform.select({ ios: fs.DocumentDirectoryPath, default: fs.CachesDirectoryPath })
79+
80+
const items = await fs.readDir(recordingSavingPath)
81+
return items.filter(item => item.path.endsWith('.m4a')).map(item => item.path)
82+
}
83+
7284
/**
7385
* Generate a list of file objects with information about successfully copied files (Android)
7486
* or all files (iOS).
7587
* @returns {Promise<ListItem[]>} A Promise that resolves to the list of file objects.
7688
*/
7789
export const generateAudioList = async (): Promise<ListItem[]> => {
78-
const audioAssets = await copyFilesToNativeResources();
90+
const audioAssetPaths = (await copyFilesToNativeResources()).map(value => `${filePath}/${value}`);
91+
const recordedAudios = await getRecordedAudios()
7992

8093
// Generate the final list based on the copied or available files
81-
return audioAssets?.map?.((value, index) => ({
94+
return [...audioAssetPaths, ...recordedAudios].map?.((value, index) => ({
8295
fromCurrentUser: index % 2 !== 0,
83-
path: `${filePath}/${value}`,
96+
path: value,
8497
}));
98+
8599
};

example/src/styles.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ const styles = (params: StyleSheetParams = {}) =>
9292
},
9393
simformImageContainer: {
9494
alignItems: 'center',
95-
justifyContent: 'center',
95+
justifyContent: 'space-around',
96+
flexDirection: 'row',
9697
},
9798
loadingText: {
9899
color: Colors.black,

0 commit comments

Comments
 (0)