Skip to content
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

✉️📭 Refractored emailService.ts into shareLocalDBFile.ts #1160

Merged
merged 15 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions package.cordovabuild.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
"ANDROID_HOST": " ",
"ANDROID_PATHPREFIX": "/"
},
"cordova-plugin-email-composer": {},
"cordova-plugin-x-socialsharing": {
"PHOTO_LIBRARY_ADD_USAGE_DESCRIPTION": "This app requires photo library access to share photos on social media.",
"PHOTO_LIBRARY_USAGE_DESCRIPTION": "This app requires photo library access to share photos on social media."
Expand Down Expand Up @@ -128,7 +127,6 @@
"cordova-plugin-em-settings": "git+https://github.com/e-mission/cordova-connection-settings.git#v1.2.3",
"cordova-plugin-em-unifiedlogger": "git+https://github.com/e-mission/cordova-unified-logger.git#v1.3.6",
"cordova-plugin-em-usercache": "git+https://github.com/e-mission/cordova-usercache.git#v1.1.9",
"cordova-plugin-email-composer": "git+https://github.com/katzer/cordova-plugin-email-composer.git#0.10.1",
"cordova-plugin-file": "8.0.0",
"cordova-plugin-inappbrowser": "5.0.0",
"cordova-plugin-ionic-keyboard": "2.2.0",
Expand Down
12 changes: 5 additions & 7 deletions www/i18n/en.json
the-bay-kay marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"force-sync": "Force sync",
"share": "Share",
"download-json-dump": "Download json dump",
"email-log": "Email log",
"share-log": "Share log",
"upload-log": "Upload log",
"view-privacy": "View Privacy Policy",
"user-data": "User data",
Expand Down Expand Up @@ -278,16 +278,14 @@
"reading-unprocessed-data": "Reading unprocessed data..."
},

"email-service": {
"email-account-not-configured": "Email account is not configured, cannot send email",
"email-account-mail-app": "You must have the mail app on your phone configured with an email address. Otherwise, this won't work",
"going-to-email": "Going to email database from {{parentDir}}",
"email-log": {
"shareFile-service": {
"send-to": "Please send this file to openpath@nrel.gov, or another OpenPATH team member.",
"send-log": {
"subject-logs": "emission logs",
"body-please-fill-in-what-is-wrong": "please fill in what is wrong"
},
"no-email-address-configured": "No email address configured.",
"email-data": {
"send-data": {
"subject-data-dump-from-to": "Data dump from {{start}} to {{end}}",
"body-data-consists-of-list-of-entries": "Data consists of a list of entries.\nEntry formats are at https://github.com/e-mission/e-mission-server/tree/master/emission/core/wrapper \nData can be loaded locally using instructions at https://github.com/e-mission/e-mission-server#loading-test-data \n and can be manipulated using the example at https://github.com/e-mission/e-mission-server/blob/master/Timeseries_Sample.ipynb"
}
Expand Down
4 changes: 2 additions & 2 deletions www/js/control/LogPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import { FlashList } from '@shopify/flash-list';
import { DateTime } from 'luxon';
import { AlertManager } from '../components/AlertBar';
import { sendEmail } from './emailService';
import { sendLocalDBFile } from '../services/shareLocalDBFile';
import { displayError, logDebug } from '../plugin/logger';
import NavBar from '../components/NavBar';

Expand Down Expand Up @@ -93,7 +93,7 @@
}

function emailLog() {
sendEmail('loggerDB');
sendLocalDBFile('loggerDB');

Check warning on line 96 in www/js/control/LogPage.tsx

View check run for this annotation

Codecov / codecov/patch

www/js/control/LogPage.tsx#L96

Added line #L96 was not covered by tests
}

const separator = () => <View style={{ height: 8 }} />;
Expand Down
6 changes: 3 additions & 3 deletions www/js/control/ProfileSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import { AlertManager } from '../components/AlertBar';
import DataDatePicker from './DataDatePicker';
import PrivacyPolicyModal from './PrivacyPolicyModal';
import { sendEmail } from './emailService';
import { sendLocalDBFile } from '../services/shareLocalDBFile';
import { uploadFile } from './uploadService';
import ActionMenu from '../components/ActionMenu';
import SensedPage from './SensedPage';
Expand Down Expand Up @@ -448,9 +448,9 @@
action={() => setDateDumpVis(true)}></SettingRow>
{logUploadSection}
<SettingRow
textKey="control.email-log"
textKey="control.share-log"
iconName="email"
action={() => sendEmail('loggerDB')}></SettingRow>
action={() => sendLocalDBFile('loggerDB')}></SettingRow>

Check warning on line 453 in www/js/control/ProfileSettings.tsx

View check run for this annotation

Codecov / codecov/patch

www/js/control/ProfileSettings.tsx#L453

Added line #L453 was not covered by tests
<SettingRow
textKey="control.refresh-app-config"
desc={t('control.current-version', { version: appConfig?.version })}
Expand Down
4 changes: 2 additions & 2 deletions www/js/control/SensedPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { useTranslation } from 'react-i18next';
import { FlashList } from '@shopify/flash-list';
import { DateTime } from 'luxon';
import { sendEmail } from './emailService';
import { sendLocalDBFile } from '../services/shareLocalDBFile';
import NavBar from '../components/NavBar';

const SensedPage = ({ pageVis, setPageVis }) => {
Expand Down Expand Up @@ -60,7 +60,7 @@
<View
style={{ paddingHorizontal: 15, flexDirection: 'row', justifyContent: 'space-between' }}>
<IconButton icon="refresh" onPress={() => updateEntries()} />
<IconButton icon="email" onPress={() => sendEmail('userCacheDB')} />
<IconButton icon="email" onPress={() => sendLocalDBFile('userCacheDB')} />

Check warning on line 63 in www/js/control/SensedPage.tsx

View check run for this annotation

Codecov / codecov/patch

www/js/control/SensedPage.tsx#L63

Added line #L63 was not covered by tests
</View>

<FlashList
Expand Down
52 changes: 0 additions & 52 deletions www/js/control/emailService.ts

This file was deleted.

4 changes: 2 additions & 2 deletions www/js/services/controlHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ export function getMyDataHelpers(fileName: string, startTimeString: string, endT
const shareObj = {
files: [attachFile],
message: i18next.t(
'email-service.email-data.body-data-consists-of-list-of-entries',
'shareFile-service.send-data.body-data-consists-of-list-of-entries',
),
subject: i18next.t('email-service.email-data.subject-data-dump-from-to', {
subject: i18next.t('shareFile-service.send-data.subject-data-dump-from-to', {
start: startTimeString,
end: endTimeString,
}),
Expand Down
121 changes: 121 additions & 0 deletions www/js/services/shareLocalDBFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import i18next from 'i18next';
import { displayError, displayErrorMsg, logDebug, logWarn } from '../plugin/logger';

function localDBHelpers(fileName: string, fileExtension: string = '.txt') {
async function localCopyFile() {
return new Promise<void>((resolve, reject) => {

Check warning on line 6 in www/js/services/shareLocalDBFile.ts

View check run for this annotation

Codecov / codecov/patch

www/js/services/shareLocalDBFile.ts#L5-L6

Added lines #L5 - L6 were not covered by tests
let pathToFile, parentDirectory;
if (window['cordova'].platformId == 'android') {
// parentDirectory: file:///data/user/0/edu.berkeley.eecs.emission/files/
parentDirectory = window['cordova'].file.dataDirectory.replace('files', 'databases');

Check warning on line 10 in www/js/services/shareLocalDBFile.ts

View check run for this annotation

Codecov / codecov/patch

www/js/services/shareLocalDBFile.ts#L10

Added line #L10 was not covered by tests
the-bay-kay marked this conversation as resolved.
Show resolved Hide resolved
// pathToFile: /data/user/0/edu.berkeley.eecs.emission/files/
pathToFile = parentDirectory.replace('file://', '') + fileName;

Check warning on line 12 in www/js/services/shareLocalDBFile.ts

View check run for this annotation

Codecov / codecov/patch

www/js/services/shareLocalDBFile.ts#L12

Added line #L12 was not covered by tests
} else if (window['cordova'].platformId == 'ios') {
// parentDirectory: file:///var/mobile/Containers/Data/Application/<32-hex-digit-id>/Library/NoCloud/../
parentDirectory = window['cordova'].file.dataDirectory + '../';
the-bay-kay marked this conversation as resolved.
Show resolved Hide resolved
pathToFile = 'LocalDatabase/' + fileName;
} else {
displayErrorMsg('Error: Unknown OS!');
throw new Error('Error: Unknown OS!');

Check warning on line 19 in www/js/services/shareLocalDBFile.ts

View check run for this annotation

Codecov / codecov/patch

www/js/services/shareLocalDBFile.ts#L15-L19

Added lines #L15 - L19 were not covered by tests
}

window['resolveLocalFileSystemURL'](parentDirectory, (fs) => {

Check warning on line 22 in www/js/services/shareLocalDBFile.ts

View check run for this annotation

Codecov / codecov/patch

www/js/services/shareLocalDBFile.ts#L22

Added line #L22 was not covered by tests
// On iOS, pass in relative path to getFile https://github.com/e-mission/e-mission-phone/pull/1160#issuecomment-2192112472
// On Android, pass in absolute path to getFile https://github.com/e-mission/e-mission-phone/pull/1160#issuecomment-2204297874
fs.filesystem.root.getFile(pathToFile, { create: false, exclusive: false }, (fileEntry) => {

Check warning on line 25 in www/js/services/shareLocalDBFile.ts

View check run for this annotation

Codecov / codecov/patch

www/js/services/shareLocalDBFile.ts#L25

Added line #L25 was not covered by tests
// logDebug(`fileEntry ${fileEntry.nativeURL} is file? ${fileEntry.isFile.toString()}`);
logDebug(`fileEntry is: ${JSON.stringify(fileEntry, null, 2)}`);
the-bay-kay marked this conversation as resolved.
Show resolved Hide resolved
window['resolveLocalFileSystemURL'](window['cordova'].file.cacheDirectory, (copyDir) => {
logDebug(`DirectoryEntry is: ${JSON.stringify(copyDir.filesystem.root, null, 2)}`);

Check warning on line 29 in www/js/services/shareLocalDBFile.ts

View check run for this annotation

Codecov / codecov/patch

www/js/services/shareLocalDBFile.ts#L27-L29

Added lines #L27 - L29 were not covered by tests

fileEntry.copyTo(

Check warning on line 31 in www/js/services/shareLocalDBFile.ts

View check run for this annotation

Codecov / codecov/patch

www/js/services/shareLocalDBFile.ts#L31

Added line #L31 was not covered by tests
copyDir.filesystem.root,
fileName + fileExtension,
(res) => {
logDebug(`Res: ${res}`);
resolve();

Check warning on line 36 in www/js/services/shareLocalDBFile.ts

View check run for this annotation

Codecov / codecov/patch

www/js/services/shareLocalDBFile.ts#L34-L36

Added lines #L34 - L36 were not covered by tests
},
(rej) => {
displayErrorMsg(`Rej: ${JSON.stringify(rej, null, 2)}`);
reject();

Check warning on line 40 in www/js/services/shareLocalDBFile.ts

View check run for this annotation

Codecov / codecov/patch

www/js/services/shareLocalDBFile.ts#L38-L40

Added lines #L38 - L40 were not covered by tests
},
);
});
});
});
});
}

function localShareFile() {
return new Promise<void>((resolve, reject) => {
window['resolveLocalFileSystemURL'](window['cordova'].file.cacheDirectory, (fs) => {
fs.filesystem.root.getFile(

Check warning on line 52 in www/js/services/shareLocalDBFile.ts

View check run for this annotation

Codecov / codecov/patch

www/js/services/shareLocalDBFile.ts#L49-L52

Added lines #L49 - L52 were not covered by tests
fileName + fileExtension,
null,
(fileEntry) => {
const shareObj = {

Check warning on line 56 in www/js/services/shareLocalDBFile.ts

View check run for this annotation

Codecov / codecov/patch

www/js/services/shareLocalDBFile.ts#L55-L56

Added lines #L55 - L56 were not covered by tests
files: [fileEntry.nativeURL],
message: i18next.t('shareFile-service.send-log.body-please-fill-in-what-is-wrong'),
subject: i18next.t('shareFile-service.send-log.subject-logs'),
};
window['plugins'].socialsharing.shareWithOptions(

Check warning on line 61 in www/js/services/shareLocalDBFile.ts

View check run for this annotation

Codecov / codecov/patch

www/js/services/shareLocalDBFile.ts#L61

Added line #L61 was not covered by tests
shareObj,
(result) => {
logDebug(`Share Completed? ${result.completed}`); // On Android, most likely returns false
logDebug(`Shared to app: ${result.app}`);
resolve();

Check warning on line 66 in www/js/services/shareLocalDBFile.ts

View check run for this annotation

Codecov / codecov/patch

www/js/services/shareLocalDBFile.ts#L63-L66

Added lines #L63 - L66 were not covered by tests
},
(msg) => {
displayErrorMsg(`Sharing failed with message ${msg}`);

Check warning on line 69 in www/js/services/shareLocalDBFile.ts

View check run for this annotation

Codecov / codecov/patch

www/js/services/shareLocalDBFile.ts#L68-L69

Added lines #L68 - L69 were not covered by tests
},
louisg1337 marked this conversation as resolved.
Show resolved Hide resolved
);
},
(error) => {
displayError(error, 'Error while sharing logs');
reject(error);

Check warning on line 75 in www/js/services/shareLocalDBFile.ts

View check run for this annotation

Codecov / codecov/patch

www/js/services/shareLocalDBFile.ts#L73-L75

Added lines #L73 - L75 were not covered by tests
},
);
});
});
}

function localClearTmpFile() {
return new Promise<void>((resolve, reject) => {
window['resolveLocalFileSystemURL'](window['cordova'].file.cacheDirectory, (fs) => {
fs.filesystem.root.getFile(fileName + fileExtension, null, (fileEntry) => {
fileEntry.remove(
() => {
logDebug(`Successfully cleaned up file ${fileName}`);
resolve();

Check warning on line 89 in www/js/services/shareLocalDBFile.ts

View check run for this annotation

Codecov / codecov/patch

www/js/services/shareLocalDBFile.ts#L82-L89

Added lines #L82 - L89 were not covered by tests
},
(err) => {
displayError(err, `Error deleting ${fileName}`);
reject(err);

Check warning on line 93 in www/js/services/shareLocalDBFile.ts

View check run for this annotation

Codecov / codecov/patch

www/js/services/shareLocalDBFile.ts#L91-L93

Added lines #L91 - L93 were not covered by tests
},
);
});
});
});
}

return {

Check warning on line 101 in www/js/services/shareLocalDBFile.ts

View check run for this annotation

Codecov / codecov/patch

www/js/services/shareLocalDBFile.ts#L101

Added line #L101 was not covered by tests
copyFile: localCopyFile,
shareData: localShareFile,
clearTmpFile: localClearTmpFile,
};
}
export async function sendLocalDBFile(database: string) {
alert(i18next.t('shareFile-service.send-to'));

Check warning on line 108 in www/js/services/shareLocalDBFile.ts

View check run for this annotation

Codecov / codecov/patch

www/js/services/shareLocalDBFile.ts#L107-L108

Added lines #L107 - L108 were not covered by tests

const dataMethods = localDBHelpers(database);
dataMethods

Check warning on line 111 in www/js/services/shareLocalDBFile.ts

View check run for this annotation

Codecov / codecov/patch

www/js/services/shareLocalDBFile.ts#L110-L111

Added lines #L110 - L111 were not covered by tests
.copyFile()
.then(dataMethods.shareData)
.then(dataMethods.clearTmpFile)
.then(() => {
logDebug(`File Shared!`);

Check warning on line 116 in www/js/services/shareLocalDBFile.ts

View check run for this annotation

Codecov / codecov/patch

www/js/services/shareLocalDBFile.ts#L115-L116

Added lines #L115 - L116 were not covered by tests
})
.catch((err) => {
displayError(err);

Check warning on line 119 in www/js/services/shareLocalDBFile.ts

View check run for this annotation

Codecov / codecov/patch

www/js/services/shareLocalDBFile.ts#L118-L119

Added lines #L118 - L119 were not covered by tests
});
}
Loading