@@ -19,32 +19,47 @@ const filePath: string = globalMetrics.isAndroid
1919 * Copy a file to the specified destination path if it doesn't exist.
2020 * @param {string } value - The name of the file to copy.
2121 * @param {string } destinationPath - The destination path to copy the file to.
22- * @returns {Promise<void > } A Promise that resolves when the file is copied.
22+ * @returns {Promise<boolean > } A Promise that resolves to true if the file is copied successfully, otherwise false .
2323 */
2424const copyFile = async (
2525 value : string ,
2626 destinationPath : string
27- ) : Promise < void > => {
27+ ) : Promise < boolean > => {
2828 const fileExists = await fs . exists ( `${ destinationPath } /${ value } ` ) ;
2929
3030 if ( ! fileExists ) {
3131 try {
3232 const file = await fs . readFileRes ( `raw/${ value } ` , 'base64' ) ;
3333 await fs . writeFile ( `${ destinationPath } /${ value } ` , file , 'base64' ) ;
34+ return true ;
3435 } catch ( error ) {
35- console . error ( error ) ;
36+ console . error ( `Error copying file ${ value } : ` , error ) ;
37+ return false ;
3638 }
3739 }
40+
41+ return true ; // File already exists
3842} ;
3943
4044/**
41- * Copy all files in the 'audioAssetArray' to the destination path (Android only).
42- * @returns {Promise<void > } A Promise that resolves when all files are copied.
45+ * Copy all files in the 'audioAssetArray' to the destination path (Android only), or return all files (iOS) .
46+ * @returns {Promise<string[] > } A Promise that resolves to a list of successfully copied file paths .
4347 */
44- const copyFilesToAndroidResources = async ( ) : Promise < void > => {
48+ const copyFilesToNativeResources = async ( ) : Promise < string [ ] > => {
4549 if ( globalMetrics . isAndroid ) {
46- await Promise . all ( audioAssetArray . map ( value => copyFile ( value , filePath ) ) ) ;
50+ const successfulCopies = await Promise . all (
51+ audioAssetArray . map ( async value => {
52+ const isSuccess = await copyFile ( value , filePath ) ;
53+ return isSuccess ? value : null ;
54+ } )
55+ ) ;
56+
57+ // Filter out unsuccessful file copies
58+ return successfulCopies ?. filter ?.( value => value !== null ) ;
4759 }
60+
61+ // On iOS, return all files without copying
62+ return audioAssetArray ;
4863} ;
4964
5065const audioAssetArray = [
@@ -54,15 +69,17 @@ const audioAssetArray = [
5469 'file_example_mp3_15s.mp3' ,
5570] ;
5671
57- copyFilesToAndroidResources ( ) ;
58-
5972/**
60- * List of file objects with information about the files.
61- * @type {ListItem[] }
73+ * Generate a list of file objects with information about successfully copied files (Android)
74+ * or all files (iOS).
75+ * @returns {Promise<ListItem[]> } A Promise that resolves to the list of file objects.
6276 */
63- export const audioListArray : ListItem [ ] = audioAssetArray . map (
64- ( value , index ) => ( {
77+ export const generateAudioList = async ( ) : Promise < ListItem [ ] > => {
78+ const audioAssets = await copyFilesToNativeResources ( ) ;
79+
80+ // Generate the final list based on the copied or available files
81+ return audioAssets ?. map ?.( ( value , index ) => ( {
6582 fromCurrentUser : index % 2 !== 0 ,
6683 path : `${ filePath } /${ value } ` ,
67- } )
68- ) ;
84+ } ) ) ;
85+ } ;
0 commit comments