Skip to content

Commit

Permalink
WEB-2037 new downloadBase64 method (#147)
Browse files Browse the repository at this point in the history
* WEB-2037 new downloadBase64 method

* min app version

* doc

* doc
  • Loading branch information
atabel authored Sep 24, 2024
1 parent 8268e7c commit adb5983
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 1 deletion.
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,64 @@ shareBase64({
});
```
### downloadBase64
<kbd>App version >=24.10</kbd> <kbd>App version >=24.9 in O2ES</kbd>
Opens the provided file using the download webview mode. The file is provided as
a base64 encoded string.
```ts
downloadBase64: ({contentInBase64: string; fileName: string}) => Promise<void>;
```
- The file type will be inferred from the `fileName` extension. The file
extension is mandatory. Take into account that iOS webview won't be able to
render file types not supported by Safari.
#### Behaviour
##### Android
1. Once file is correctly processed, a "Downloaded" notification is shown in the
system notifications inbox. System will try to open the file when clicking on
it.
2. Simultaneously, app will try to open the given file, this may result in 3
situations:
- No app that can handle this type of file is available
- Nothing will happen, user feedback will be just the previous generated
notification.
- Multiple apps can handle this type of file
- System will show a desambiguator window to select the app which will
be used to open the file.
- Single app can open this type of file (Or an app is set as default for
these kind of files)
- Downloaded content will be opened using the only available App that
supports its extension.
https://github.com/user-attachments/assets/6feaed05-89f2-467b-b017-dc966bae1213
##### iOS
The behavior will be similar to the current webview download mode but
downloading the file before showing it
1.- Once the file is correctly procesed, it will be stored in a tmp directory
2.- The app will open a modal webview presenting the local file in a web browser
similar to the one used in download webview mode but hiding the "Open in Safari"
button (due to no sense for a local file).
https://github.com/user-attachments/assets/66726efd-4867-4c08-997e-a85f9cfb7c31
#### Example
```ts
downloadBase64({
contentInBase64: 'SGVsbG8sIHd(...)vcmxkCg==',
fileName: 'hello.pdf',
});
```
### updateNavigationBar
<kbd>App version >= 10.7: Partial support</kbd><br/> <kbd>App version >= 11.8:
Expand Down
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export {
getDeviceModel,
getDeviceTac,
shareBase64,
downloadBase64,
} from './src/device';

export {
Expand Down
51 changes: 50 additions & 1 deletion src/__tests__/device-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
getAttStatus,
getDeviceModel,
shareBase64,
downloadBase64,
} from '../../index';
import {
createFakeAndroidPostMessage,
Expand Down Expand Up @@ -372,5 +373,53 @@ test('shareBase64 failure', async () => {
fileName: 'example.pdf',
});

expect(res).rejects.toEqual(error);
await expect(res).rejects.toEqual(error);
});

test('downloadBase64', async () => {
createFakeWebKitPostMessage({
checkMessage: (msg) => {
expect(msg.type).toBe('DOWNLOAD_BASE64');
expect(msg.payload).toMatchObject({
content: ANY_STRING,
fileName: 'example.pdf',
});
},
getResponse: (msg) => ({
type: 'DOWNLOAD_BASE64',
id: msg.id,
}),
});

const res = await downloadBase64({
contentInBase64: ANY_STRING,
fileName: 'example.pdf',
});

expect(res).toBe(undefined);
});

test('downloadBase64 failure', async () => {
const error = {
code: 400,
description: 'bar',
};

createFakeWebKitPostMessage({
checkMessage: (msg) => {
expect(msg.type).toBe('DOWNLOAD_BASE64');
},
getResponse: (msg) => ({
type: 'ERROR',
id: msg.id,
payload: error,
}),
});

const res = downloadBase64({
contentInBase64: ANY_STRING,
fileName: 'example.pdf',
});

await expect(res).rejects.toEqual(error);
});
12 changes: 12 additions & 0 deletions src/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,15 @@ export const shareBase64 = (params: {
fileName: params.fileName,
},
});

export const downloadBase64 = (params: {
contentInBase64: string;
fileName: string;
}): Promise<void> =>
postMessageToNativeApp({
type: 'DOWNLOAD_BASE64',
payload: {
content: params.contentInBase64,
fileName: params.fileName,
},
});
5 changes: 5 additions & 0 deletions src/post-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,11 @@ export type ResponsesFromNativeApp = {
id: string;
payload: void;
};
DOWNLOAD_BASE64: {
type: 'DOWNLOAD_BASE64';
id: string;
payload: void;
};
};

export type NativeAppResponsePayload<
Expand Down

0 comments on commit adb5983

Please sign in to comment.