Skip to content

Commit

Permalink
Revert "fix(#42): use Cordova to write file on native mobile"
Browse files Browse the repository at this point in the history
This reverts commit da8f87c.
  • Loading branch information
peterpeterparker committed Jan 22, 2020
1 parent da8f87c commit 6542237
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 60 deletions.
5 changes: 0 additions & 5 deletions android/app/src/main/res/xml/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@
<widget version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<access origin="*" />

<feature name="File">
<param name="android-package" value="org.apache.cordova.file.FileUtils"/>
<param name="onload" value="true"/>
</feature>

<feature name="SocialSharing">
<param name="android-package" value="nl.xservices.plugins.SocialSharing"/>
</feature>
Expand Down
5 changes: 0 additions & 5 deletions ios/App/App/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@
<widget version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<access origin="*" />

<feature name="File">
<param name="ios-package" value="CDVFile"/>
<param name="onload" value="true"/>
</feature>

<feature name="SocialSharing">
<param name="ios-package" value="SocialSharing"/>
<param name="onload" value="true"/>
Expand Down
20 changes: 0 additions & 20 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@
"@date-io/date-fns": "^1.3.13",
"@deckdeckgo/color": "^1.0.0-rc.3-3",
"@ionic-native/core": "^5.19.1",
"@ionic-native/file": "^5.19.1",
"@ionic-native/social-sharing": "^5.19.1",
"@ionic/react": "^4.11.8",
"@ionic/react-router": "^4.11.8",
"@material-ui/core": "^4.8.3",
"@material-ui/pickers": "^3.2.9",
"cordova-plugin-file": "^6.0.2",
"cordova-plugin-x-socialsharing": "^5.6.3",
"date-fns": "^2.9.0",
"es6-promise-plugin": "^4.2.2",
Expand Down
12 changes: 6 additions & 6 deletions public/workers/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ async function exportToExcel(invoices, client, currency, vat, i18n) {

const buf = await workbook.xlsx.writeBuffer();

return new Blob([buf], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'});
return new Blob([buf]);
}

function generateTotal(worksheet, invoices, currencyFormat, vat, i18n) {
Expand All @@ -97,9 +97,9 @@ function generateTotal(worksheet, invoices, currencyFormat, vat, i18n) {

worksheet.mergeCells(`E${index}:F${index}`);
worksheet.getCell(`E${index}`).value = i18n.total;
worksheet.getCell(`G${index}`).value = {formula: totalRef};
worksheet.getCell(`G${index}`).value = { formula: totalRef };
worksheet.getCell(`G${index}`).numFmt = currencyFormat;
worksheet.getCell(`G${index}`).font = {font: {bold: true}};
worksheet.getCell(`G${index}`).font = {font:{bold: true}};

if (vat > 0) {
index++;
Expand All @@ -117,14 +117,14 @@ function generateTotal(worksheet, invoices, currencyFormat, vat, i18n) {

worksheet.mergeCells(`E${index}:F${index}`);
worksheet.getCell(`E${index}`).value = i18n.total_vat_excluded;
worksheet.getCell(`G${index}`).value = {formula: `${totalRef}*100/(100+(${vatRef}*100))`};
worksheet.getCell(`G${index}`).value = { formula: `${totalRef}*100/(100+(${vatRef}*100))` };
worksheet.getCell(`G${index}`).numFmt = currencyFormat;

index++;

worksheet.mergeCells(`E${index}:F${index}`);
worksheet.getCell(`E${index}`).value = i18n.vat;
worksheet.getCell(`G${index}`).value = {formula: `${totalRef}*(${vatRef}*100)/(100+(${vatRef}*100))`};
worksheet.getCell(`G${index}`).value = { formula: `${totalRef}*(${vatRef}*100)/(100+(${vatRef}*100))` };
worksheet.getCell(`G${index}`).numFmt = currencyFormat;
}

Expand All @@ -133,7 +133,7 @@ function generateTotal(worksheet, invoices, currencyFormat, vat, i18n) {

worksheet.mergeCells(`E${index}:F${index}`);
worksheet.getCell(`E${index}`).value = i18n.total_billable_hours;
worksheet.getCell(`G${index}`).value = {formula: `F${invoices.length + 2}`};
worksheet.getCell(`G${index}`).value = { formula: `F${invoices.length + 2}` };
worksheet.getCell(`G${index}`).numFmt = '0.00';
}

Expand Down
100 changes: 78 additions & 22 deletions src/services/export/export.service.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import {isPlatform} from '@ionic/react';
import {interval} from '../../utils/utils.date';

import {Invoice} from '../../store/interfaces/invoice';
import {format} from 'date-fns';

import i18next from 'i18next';

import {interval} from '../../utils/utils.date';

import {Invoice} from '../../store/interfaces/invoice';

import {SocialSharing } from '@ionic-native/social-sharing';
import {File, IWriteOptions, DirectoryEntry} from '@ionic-native/file';

import {Plugins, FilesystemDirectory, FilesystemEncoding} from '@capacitor/core';
import {StatResult} from '@capacitor/core/dist/esm/core-plugin-definitions';

import {Currency} from '../../definitions/currency';

const {Filesystem} = Plugins;

export class ExportService {

private static instance: ExportService;
Expand Down Expand Up @@ -121,16 +122,18 @@ export class ExportService {

this.exportWorker.onmessage = async ($event: MessageEvent) => {
if ($event && $event.data) {
const dir: DirectoryEntry = await this.getMobileDir();
await this.makeMobileDir();

const writeOptions: IWriteOptions = {
replace: true,
append: false
};
const content: string = await this.blobToString($event.data);

await File.writeFile(dir.nativeURL, filename, $event.data, writeOptions);
await Filesystem.writeFile({
path: `tietracker/${filename}`,
data: content,
directory: FilesystemDirectory.Documents,
encoding: FilesystemEncoding.UTF8
});

await this.shareMobile(invoice, dir.nativeURL, filename);
await this.shareMobile(invoice, filename);
}
};

Expand All @@ -144,6 +147,27 @@ export class ExportService {
});
}

private blobToString(data: Blob): Promise<string> {
return new Promise<string>((resolve, reject) => {
const fileReader: FileReader = new FileReader();

fileReader.onload = () => {
if (!fileReader.result) {
reject('Could not convert blob content.');
return;
}

resolve(fileReader.result as string);
};

fileReader.onerror = (error) => {
reject(error);
};

fileReader.readAsText(data);
});
}

private async getNewFileHandle(filename: string): Promise<FileSystemFileHandle> {
const opts: ChooseFileSystemEntriesOptions = {
type: 'saveFile',
Expand Down Expand Up @@ -192,28 +216,60 @@ export class ExportService {
return `${name}${from ? '-' + format(from, 'yyyy-MM-dd') : ''}${to ? '-' + format(to, 'yyyy-MM-dd') : ''}.xlsx`
}

private getMobileDir(): Promise<DirectoryEntry> {
return new Promise<DirectoryEntry>(async (resolve, reject) => {
private makeMobileDir(): Promise<void> {
return new Promise<void>(async (resolve, reject) => {
try {
const rootDir: DirectoryEntry = await File.resolveDirectoryUrl(isPlatform('ios') ? File.syncedDataDirectory : File.dataDirectory);
const exist: boolean = await this.existMobileDir();

rootDir.getDirectory('tietracker', {create: true}, (newDir: DirectoryEntry) => {
resolve(newDir);
}, (err) => {
reject(new Error('Directory not found or not created.'));
if (exist) {
resolve();
return;
}

await Filesystem.mkdir({
path: 'tietracker',
directory: FilesystemDirectory.Documents,
recursive: false
});

resolve();
} catch (e) {
reject(e);
}
});
}

private shareMobile(invoice: Invoice, path: string, filename: string): Promise<void> {
private existMobileDir(): Promise<boolean> {
return new Promise<boolean>(async (resolve) => {
try {
await Filesystem.stat({
path: 'tietracker',
directory: FilesystemDirectory.Documents
});

resolve(true);
} catch (e) {
resolve(false);
}
});
}

private shareMobile(invoice: Invoice, filename: string): Promise<void> {
return new Promise<void>(async (resolve, reject) => {
try {
const stat: StatResult = await Filesystem.stat({
path: `tietracker/${filename}`,
directory: FilesystemDirectory.Documents
});

if (!stat || !stat.uri) {
reject('File not found.');
return;
}

await SocialSharing.shareWithOptions({
subject: this.shareSubject(invoice),
files: [`${path}/${filename}`],
files: [stat.uri],
chooserTitle: 'Pick an app'
});

Expand Down

0 comments on commit 6542237

Please sign in to comment.