From a232e3f86eba687659710d398ff6f952091eea56 Mon Sep 17 00:00:00 2001 From: Katie Rischpater <98350084+the-bay-kay@users.noreply.github.com> Date: Mon, 27 Nov 2023 13:25:31 -0800 Subject: [PATCH 1/2] Revert "Switched to old fileIO, fixes issue with android" Found a fix for `['resolveLocalFileSystemURL']` method of fileIO, that works with Android and iOS. Reverting back to this method and updating, to make this consistent with `uploadService.ts` This reverts commit dbe76d11d0011d1e349020bdd918beda6a154df6. --- www/js/services/controlHelper.ts | 69 ++++++++++++++++++++++---------- 1 file changed, 48 insertions(+), 21 deletions(-) diff --git a/www/js/services/controlHelper.ts b/www/js/services/controlHelper.ts index f3f93013c..f2741b0dd 100644 --- a/www/js/services/controlHelper.ts +++ b/www/js/services/controlHelper.ts @@ -16,34 +16,38 @@ export const getMyDataHelpers = function ( const localWriteFile = function (result: ServerResponse) { const resultList = result.phone_data; return new Promise(function (resolve, reject) { - window.requestFileSystem(window.LocalFileSystem.TEMPORARY, 0, function (fs) { - fs.root.getFile(fileName, { create: true, exclusive: false }, function (fileEntry) { - logDebug(`fileEntry ${fileEntry.nativeURL} is file? ${fileEntry.isFile.toString()}`); - fileEntry.createWriter(function (fileWriter) { - fileWriter.onwriteend = function () { - logDebug('Successful file write...'); - resolve(); - }; - fileWriter.onerror = function (e) { - logDebug(`Failed file write: ${e.toString()}`); - reject(); - }; - logDebug(`fileWriter is: ${JSON.stringify(fileWriter.onwriteend, null, 2)}`); - // if data object is not passed in, create a new blob instead. - const dataObj = new Blob([JSON.stringify(resultList, null, 2)], { - type: 'application/json', + window['resolveLocalFileSystemURL'](window['cordova'].file.tempDirectory, function (fs) { + fs.filesystem.root.getFile( + fileName, + { create: true, exclusive: false }, + function (fileEntry) { + logDebug(`fileEntry ${fileEntry.nativeURL} is file? ${fileEntry.isFile.toString()}`); + fileEntry.createWriter(function (fileWriter) { + fileWriter.onwriteend = function () { + logDebug('Successful file write...'); + resolve(); + }; + fileWriter.onerror = function (e) { + logDebug(`Failed file write: ${e.toString()}`); + reject(); + }; + logDebug(`fileWriter is: ${JSON.stringify(fileWriter.onwriteend, null, 2)}`); + // if data object is not passed in, create a new blob instead. + const dataObj = new Blob([JSON.stringify(resultList, null, 2)], { + type: 'application/json', + }); + fileWriter.write(dataObj); }); - fileWriter.write(dataObj); - }); - }); + }, + ); }); }); }; const localShareData = function () { return new Promise(function (resolve, reject) { - window.requestFileSystem(window.LocalFileSystem.TEMPORARY, 0, function (fs) { - fs.root.getFile(fileName, null, function (fileEntry) { + window['resolveLocalFileSystemURL'](window['cordova'].file.tempDirectory, function (fs) { + fs.filesystem.root.getFile(fileName, null, function (fileEntry) { logDebug(`fileEntry ${fileEntry.nativeURL} is file? ${fileEntry.isFile.toString()}`); fileEntry.file( function (file) { @@ -89,9 +93,31 @@ export const getMyDataHelpers = function ( }); }; + // window['cordova'].file.TempDirectory is not guaranteed to free up memory, + // so it's good practice to remove the file right after it's used! + const localClearData = function () { + return new Promise(function (resolve, reject) { + window['resolveLocalFileSystemURL'](window['cordova'].file.tempDirectory, function (fs) { + fs.filesystem.root.getFile(fileName, null, function (fileEntry) { + fileEntry.remove( + () => { + logDebug(`Successfully cleaned up file ${fileName}`); + resolve(); + }, + (err) => { + logWarn(`Error deleting ${fileName} : ${err}`); + reject(err); + }, + ); + }); + }); + }); + }; + return { writeFile: localWriteFile, shareData: localShareData, + clearData: localClearData, }; }; @@ -115,6 +141,7 @@ export const getMyData = function (timeStamp: Date) { getRawEntries(null, startTime.toUnixInteger(), endTime.toUnixInteger()) .then(getDataMethods.writeFile) .then(getDataMethods.shareData) + .then(getDataMethods.clearData) .then(function () { logInfo('Share queued successfully'); }) From 369b3adc5a9e63d16356ea2d5ad8fb567f9e7ea3 Mon Sep 17 00:00:00 2001 From: Katie Rischpater <98350084+the-bay-kay@users.noreply.github.com> Date: Mon, 27 Nov 2023 13:30:33 -0800 Subject: [PATCH 2/2] Changed fileIO directory to fix android issue --- www/js/services/controlHelper.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/www/js/services/controlHelper.ts b/www/js/services/controlHelper.ts index f2741b0dd..9969327be 100644 --- a/www/js/services/controlHelper.ts +++ b/www/js/services/controlHelper.ts @@ -16,7 +16,7 @@ export const getMyDataHelpers = function ( const localWriteFile = function (result: ServerResponse) { const resultList = result.phone_data; return new Promise(function (resolve, reject) { - window['resolveLocalFileSystemURL'](window['cordova'].file.tempDirectory, function (fs) { + window['resolveLocalFileSystemURL'](window['cordova'].file.cacheDirectory, function (fs) { fs.filesystem.root.getFile( fileName, { create: true, exclusive: false }, @@ -46,7 +46,7 @@ export const getMyDataHelpers = function ( const localShareData = function () { return new Promise(function (resolve, reject) { - window['resolveLocalFileSystemURL'](window['cordova'].file.tempDirectory, function (fs) { + window['resolveLocalFileSystemURL'](window['cordova'].file.cacheDirectory, function (fs) { fs.filesystem.root.getFile(fileName, null, function (fileEntry) { logDebug(`fileEntry ${fileEntry.nativeURL} is file? ${fileEntry.isFile.toString()}`); fileEntry.file( @@ -93,11 +93,11 @@ export const getMyDataHelpers = function ( }); }; - // window['cordova'].file.TempDirectory is not guaranteed to free up memory, + // window['cordova'].file.cacheDirectory is not guaranteed to free up memory, // so it's good practice to remove the file right after it's used! const localClearData = function () { return new Promise(function (resolve, reject) { - window['resolveLocalFileSystemURL'](window['cordova'].file.tempDirectory, function (fs) { + window['resolveLocalFileSystemURL'](window['cordova'].file.cacheDirectory, function (fs) { fs.filesystem.root.getFile(fileName, null, function (fileEntry) { fileEntry.remove( () => {