-
Notifications
You must be signed in to change notification settings - Fork 114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🎛️ ControlHelper Rewrite #1052
🎛️ ControlHelper Rewrite #1052
Changes from all commits
4f84643
1130508
2e9173b
8825d06
46832c7
4960803
6fd2e73
3420577
92cd6fb
1ec3a49
d31a7a1
b8404da
c22a605
20f6d04
f0da15e
5e61cb9
905e52c
d107e21
4b7b9d2
4dd295c
24a9d95
c2ab799
fffcd00
05deecf
6723ca3
58a3411
428f191
4afd9ec
8cb3691
12e8029
bc9dfaf
4d19a9d
4a96b9d
9247efe
05b287a
bc90720
2177000
2c02e37
dbe76d1
a232e3f
369b3ad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
import { DateTime } from 'luxon'; | ||
|
||
import { getRawEntries } from './commHelper'; | ||
import { logInfo, displayError, logDebug, logWarn } from '../plugin/logger'; | ||
import { FsWindow } from '../types/fileShareTypes'; | ||
import { ServerResponse } from '../types/serverData'; | ||
import i18next from '../i18nextInit'; | ||
|
||
declare let window: FsWindow; | ||
|
||
export const getMyDataHelpers = function ( | ||
fileName: string, | ||
startTimeString: string, | ||
endTimeString: string, | ||
) { | ||
const localWriteFile = function (result: ServerResponse<any>) { | ||
const resultList = result.phone_data; | ||
return new Promise<void>(function (resolve, reject) { | ||
Comment on lines
+16
to
+18
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can this be combined with the similar function that saves |
||
window['resolveLocalFileSystemURL'](window['cordova'].file.cacheDirectory, 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); | ||
}); | ||
}, | ||
); | ||
}); | ||
}); | ||
}; | ||
|
||
const localShareData = function () { | ||
return new Promise<void>(function (resolve, reject) { | ||
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( | ||
function (file) { | ||
const reader = new FileReader(); | ||
|
||
reader.onloadend = function () { | ||
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( | ||
'email-service.email-data.body-data-consists-of-list-of-entries', | ||
), | ||
subject: i18next.t('email-service.email-data.subject-data-dump-from-to', { | ||
start: startTimeString, | ||
end: endTimeString, | ||
}), | ||
}; | ||
window['plugins'].socialsharing.shareWithOptions( | ||
shareObj, | ||
function (result) { | ||
logDebug(`Share Completed? ${result.completed}`); // On Android, most likely returns false | ||
logDebug(`Shared to app: ${result.app}`); | ||
resolve(); | ||
}, | ||
function (msg) { | ||
logDebug(`Sharing failed with message ${msg}`); | ||
}, | ||
); | ||
}; | ||
reader.readAsText(file); | ||
}, | ||
function (error) { | ||
displayError(error, 'Error while downloading JSON dump'); | ||
reject(error); | ||
}, | ||
); | ||
}); | ||
}); | ||
}); | ||
}; | ||
|
||
// 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<void>(function (resolve, reject) { | ||
Comment on lines
+96
to
+99
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Woo! Good addition to the existing functionality! |
||
window['resolveLocalFileSystemURL'](window['cordova'].file.cacheDirectory, 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, | ||
}; | ||
}; | ||
|
||
/** | ||
* 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 const getMyData = function (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'; | ||
alert(`Going to retrieve data to ${dumpFile}`); | ||
|
||
const getDataMethods = getMyDataHelpers(dumpFile, startTimeString, endTimeString); | ||
|
||
getRawEntries(null, startTime.toUnixInteger(), endTime.toUnixInteger()) | ||
.then(getDataMethods.writeFile) | ||
.then(getDataMethods.shareData) | ||
.then(getDataMethods.clearData) | ||
.then(function () { | ||
logInfo('Share queued successfully'); | ||
}) | ||
.catch(function (error) { | ||
displayError(error, 'Error sharing JSON dump'); | ||
}); | ||
}; | ||
|
||
export const fetchOPCode = () => { | ||
return window['cordova'].plugins.OPCodeAuth.getOPCode(); | ||
}; | ||
|
||
export const getSettings = () => { | ||
return window['cordova'].plugins.BEMConnectionSettings.getSettings(); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,7 +39,7 @@ export type CompositeTrip = { | |
confirmed_trip: ObjectId; | ||
distance: number; | ||
duration: number; | ||
end_confirmed_place: ConfirmedPlace; | ||
end_confirmed_place: ServerData<ConfirmedPlace>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dumb typescript question for my edification: what does this change in aid of? |
||
end_fmt_time: string; | ||
end_loc: { type: string; coordinates: number[] }; | ||
end_local_dt: LocalDt; | ||
|
@@ -56,7 +56,7 @@ export type CompositeTrip = { | |
raw_trip: ObjectId; | ||
sections: any[]; // TODO | ||
source: string; | ||
start_confirmed_place: ConfirmedPlace; | ||
start_confirmed_place: ServerData<ConfirmedPlace>; | ||
start_fmt_time: string; | ||
start_loc: { type: string; coordinates: number[] }; | ||
start_local_dt: LocalDt; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extraneous whitespace