Skip to content

Commit

Permalink
Merge pull request #106 from peterpeterparker/fix/native-filesystem-e…
Browse files Browse the repository at this point in the history
…xport-pdf

fix: native file system PDF file type
  • Loading branch information
peterpeterparker authored Feb 7, 2021
2 parents ed487cc + b00b38c commit d53e7a7
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 11 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
<a name="1.6.2"></a>

# 1.6.2 (2021-12-07)

### Fix

- native file system export PDF type

<a name="1.6.1"></a>

# 1.6.1 (2021-12-07)
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tietracker",
"version": "1.6.1",
"version": "1.6.2",
"private": true,
"dependencies": {
"@capacitor/android": "^2.4.6",
Expand Down
2 changes: 1 addition & 1 deletion src/services/backup/backup.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class BackupService {
exportNativeFileSystem(currency: Currency, vat: number | undefined, signature: string | undefined): Promise<void> {
return new Promise<void>(async (resolve, reject) => {
try {
const fileHandle: FileSystemFileHandle = await getNewFileHandle();
const fileHandle: FileSystemFileHandle = await getNewFileHandle('xlsx');

if (!fileHandle) {
reject('Cannot access filesystem.');
Expand Down
20 changes: 15 additions & 5 deletions src/services/export/export.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export class ExportService {
currency: Currency,
vat: number | undefined,
bill: boolean,
type: 'xlsx' | 'pdf', signature: string | undefined
type: 'xlsx' | 'pdf',
signature: string | undefined
): Promise<void> {
return new Promise<void>(async (resolve, reject) => {
if (invoice === undefined || invoice.project_id === undefined) {
Expand All @@ -51,7 +52,7 @@ export class ExportService {
}

try {
const fileHandle: FileSystemFileHandle = await getNewFileHandle();
const fileHandle: FileSystemFileHandle = await getNewFileHandle(type);

if (!fileHandle) {
reject('Cannot access filesystem.');
Expand Down Expand Up @@ -81,7 +82,8 @@ export class ExportService {
currency: Currency,
vat: number | undefined,
bill: boolean,
type: 'xlsx' | 'pdf', signature: string | undefined
type: 'xlsx' | 'pdf',
signature: string | undefined
): Promise<void> {
return new Promise<void>(async (resolve, reject) => {
if (invoice === undefined || invoice.project_id === undefined) {
Expand Down Expand Up @@ -175,7 +177,15 @@ export class ExportService {
return `${name}${from ? '-' + format(from, 'yyyy-MM-dd') : ''}${to ? '-' + format(to, 'yyyy-MM-dd') : ''}.${type}`;
}

private async postMessage(invoice: Invoice, invoices: string[], currency: Currency, vat: number | undefined, bill: boolean, type: 'xlsx' | 'pdf', signature: string | undefined) {
private async postMessage(
invoice: Invoice,
invoices: string[],
currency: Currency,
vat: number | undefined,
bill: boolean,
type: 'xlsx' | 'pdf',
signature: string | undefined
) {
await i18next.loadNamespaces('export');

this.exportWorker.postMessage({
Expand All @@ -188,7 +198,7 @@ export class ExportService {
bill: bill,
i18n: exportLabels(),
type,
signature
signature,
});
}
}
17 changes: 14 additions & 3 deletions src/utils/utils.filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {DirectoryEntry, File} from '@ionic-native/file';

import {SocialSharing} from '@ionic-native/social-sharing';

export async function getNewFileHandle(): Promise<FileSystemFileHandle> {
const opts: SaveFilePickerOptions = {
export async function getNewFileHandle(type: 'pdf' | 'xlsx'): Promise<FileSystemFileHandle> {
const xlsxOpts: SaveFilePickerOptions = {
types: [
{
description: 'Excel Files',
Expand All @@ -16,7 +16,18 @@ export async function getNewFileHandle(): Promise<FileSystemFileHandle> {
],
};

return showSaveFilePicker(opts);
const pdfOpts: SaveFilePickerOptions = {
types: [
{
description: 'PDF Files',
accept: {
'application/pdf': ['.pdf'],
},
},
],
};

return showSaveFilePicker(type === 'pdf' ? pdfOpts : xlsxOpts);
}

export async function writeFile(fileHandle: FileSystemFileHandle, contents: string | BufferSource | Blob) {
Expand Down

0 comments on commit d53e7a7

Please sign in to comment.