Skip to content

Commit

Permalink
🐛 Re-enable "Download JSON dump"
Browse files Browse the repository at this point in the history
After the giant refactor, both "email log" and "download JSON dump" were
broken. We fixed "email log" in
e-mission#1160 but didn't fix "Download
JSON dump then, because of lack of time.

This fixes the "Download JSON dump" as well by making it similar to
implementation in "email log".

It was already fairly similar, but for some reason, was reading the data before
sharing the file

```
             const reader = new FileReader();

             reader.onloadend = () => {
             const readResult = this.result as string;
             logDebug(`Successfull file read with ${readResult.length} characters`);
```

and "this" doesn't exist, resulting in an undefined error.
Since the shareObj takes in a file name anyway, I just made this similar to the
emailLog changes by passing in the filename directly.

Also, similar ot the "Email Log", added a `.txt` extension so that the file can
be sent on iOS.

Testing done:
- Before this change: clicking on "Download JSON dump" did not do anything
- After this change: clicking on "Download JSON dump" launched the share menu

I haven't actually tried sharing yet because gmail is not configured on the emulator.
  • Loading branch information
shankari committed Sep 6, 2024
1 parent fefe100 commit b527f37
Showing 1 changed file with 19 additions and 28 deletions.
47 changes: 19 additions & 28 deletions www/js/services/controlHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,41 +39,32 @@ export function getMyDataHelpers(fileName: string, startTimeString: string, endT
function localShareData() {
return new Promise<void>((resolve, reject) => {
window['resolveLocalFileSystemURL'](window['cordova'].file.cacheDirectory, (fs) => {
fs.filesystem.root.getFile(fileName, null, (fileEntry) => {
logDebug(`fileEntry ${fileEntry.nativeURL} is file? ${fileEntry.isFile.toString()}`);
fileEntry.file(
(file) => {
const reader = new FileReader();

reader.onloadend = () => {
const readResult = this.result as string;
logDebug(`Successfull file read with ${readResult.length} characters`);
const dataArray = JSON.parse(readResult);
logDebug(`Successfully read resultList of size ${dataArray.length}`);
let attachFile = fileEntry.nativeURL;
const shareObj = {
files: [attachFile],
message: i18next.t(
'shareFile-service.send-data.body-data-consists-of-list-of-entries',
),
subject: i18next.t('shareFile-service.send-data.subject-data-dump-from-to', {
fs.filesystem.root.getFile(fileName,
null,
(fileEntry) => {
logDebug(`fileEntry ${fileEntry.nativeURL} is file? ${fileEntry.isFile.toString()}`);
const shareObj = {
files: [fileEntry.nativeURL],
message: i18next.t(
'shareFile-service.send-data.body-data-consists-of-list-of-entries',
),
subject: i18next.t('shareFile-service.send-data.subject-data-dump-from-to',
{
start: startTimeString,
end: endTimeString,
}),
};
window['plugins'].socialsharing.shareWithOptions(
}),
};
window['plugins'].socialsharing.shareWithOptions(
shareObj,
(result) => {
logDebug(`Share Completed? ${result.completed}`); // On Android, most likely returns false
logDebug(`Shared to app: ${result.app}`);
resolve();
},
(msg) => {
logDebug(`Sharing failed with message ${msg}`);
(error) => {
displayError(error, `Sharing failed with message`);
},
);
};
reader.readAsText(file);
},
(error) => {
displayError(error, 'Error while downloading JSON dump');
Expand All @@ -82,7 +73,6 @@ export function getMyDataHelpers(fileName: string, startTimeString: string, endT
);
});
});
});
}

// window['cordova'].file.cacheDirectory is not guaranteed to free up memory,
Expand Down Expand Up @@ -117,15 +107,16 @@ export function getMyDataHelpers(fileName: string, startTimeString: string, endT
* getMyData fetches timeline data for a given day, and then gives the user a prompt to share the data
* @param timeStamp initial timestamp of the timeline to be fetched.
*/
export function getMyData(timeStamp: Date) {
export async function getMyData(timeStamp: Date) {
// We are only retrieving data for a single day to avoid
// running out of memory on the phone
const endTime = DateTime.fromJSDate(timeStamp);
const startTime = endTime.startOf('day');
const startTimeString = startTime.toFormat("yyyy'-'MM'-'dd");
const endTimeString = endTime.toFormat("yyyy'-'MM'-'dd");

const dumpFile = startTimeString + '.' + endTimeString + '.timeline';
// let's rename this to .txt so that we can email it on iPhones
const dumpFile = startTimeString + '.' + endTimeString + '.timeline.txt';
alert(`Going to retrieve data to ${dumpFile}`);

const getDataMethods = getMyDataHelpers(dumpFile, startTimeString, endTimeString);
Expand Down

0 comments on commit b527f37

Please sign in to comment.