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

WEB-2068 showLoadingOverlay / hideLoadingOverlay #152

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
64 changes: 64 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1257,6 +1257,70 @@ Writes the given text to the clipboard
writeTextToClipboard: (text: string) => Promise<void>;
```

### showLoadingOverlay / hideLoadingOverlay
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dpastor, if you have a video with a demo I can add it to the docs

Copy link
Contributor

@dpastor dpastor Oct 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have, but probably is better to wait until we merge it, adding a video with the test page @dzayas has which simulates a more real vivo flow.


<kbd>App version >=24.10</kbd>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this min app version ok, @dpastor ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thanks :)


Shows a loading overlay screen while a task is being performed. You can control
when to hide it with the `hideLoadingOverlay` method.

```ts
showLoadingOverlay: ({
/**
* Whether the in animation is enabled (false by default)
*/
inAnimation?: boolean;
/**
* Whether the out animation is enabled (false by default)
*/
outAnimation?: boolean;
/**
* Minimum duration of the loop animation in milliseconds (0 by default)
*/
minimumLoopDurationMs?: number;
/**
* whether the loop animation should be stopped immediately or not (true by default)
*/
stopAnimationCycle?: boolean;
/**
* Whether the background animation is enabled (false by default)
*/
backgroundAnimation?: boolean;
/**
* List of description texts to be shown one after the other
*/
descriptions?: Array<string>;
/**
* Duration of each description in milliseconds (3000 by default)
*/
descriptionDurationMs?: number;
/**
* After this timeout loading screen would be hidden automatically (20000 by default)
*/
timeoutMs?: number;
/**
* (Only Android) If true, after loading screen has been hidden, if user presses android back button, webview window will close (true by default)
*/
closeOnBackButtonPressAfterFinish?: boolean;
}) => Promise<void>;

hideLoadingOverlay: () => Promise<void>;
```

#### Example

```ts
await showLoadingOverlay({
inAnimation: true,
outAnimation: true,
stopAnimationCycle: false,
descriptions: ['Loading...', 'Please wait...'],
descriptionDurationMs: 3000,
});
await doExpensiveTask();
await hideLoadingOverlay();
```

## Error handling

If an uncontrolled error occurs, promise will be rejected with an error object:
Expand Down
2 changes: 2 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ export {
onNavigationBarIconClicked,
triggerPinOrBiometricAuthentication,
focusNavbar,
showLoadingOverlay,
hideLoadingOverlay,
} from './src/utils';
export type {ShareOptions, NavigationBarIcon} from './src/utils';

Expand Down
52 changes: 52 additions & 0 deletions src/__tests__/utils-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
getAppMetadata,
getNetworkConnectionInfo,
getTopazValues,
hideLoadingOverlay,
showLoadingOverlay,
} from '../utils';

const ANY_STRING = 'any-string';
Expand Down Expand Up @@ -651,3 +653,53 @@ test('get Topaz values', async () => {
expect(res).toEqual({syncId});
});
});

test('showLoadingOverlay', async () => {
createFakeAndroidPostMessage({
checkMessage: (message) => {
expect(message.type).toBe('SHOW_LOADING_OVERLAY');
expect(message.payload).toEqual({
inAnimation: true,
outAnimation: true,
stopAnimationCycle: false,
timeoutMs: 30000,
descriptions: [
'Loading your data',
'Please wait',
'We are almost there',
],
});
},
getResponse: (message) => ({
type: message.type,
id: message.id,
}),
});

await showLoadingOverlay({
inAnimation: true,
outAnimation: true,
stopAnimationCycle: false,
timeoutMs: 30000,
descriptions: [
'Loading your data',
'Please wait',
'We are almost there',
],
});
});

test('hideLoadingOverlay', async () => {
createFakeAndroidPostMessage({
checkMessage: (message) => {
expect(message.type).toBe('HIDE_LOADING_OVERLAY');
expect(message.payload).toBeUndefined();
},
getResponse: (message) => ({
type: message.type,
id: message.id,
}),
});

await hideLoadingOverlay();
});
10 changes: 10 additions & 0 deletions src/post-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,16 @@ export type ResponsesFromNativeApp = {
id: string;
payload: void;
};
SHOW_LOADING_OVERLAY: {
type: 'SHOW_LOADING_OVERLAY';
id: string;
payload: void;
};
HIDE_LOADING_OVERLAY: {
type: 'HIDE_LOADING_OVERLAY';
id: string;
payload: void;
};
};

export type NativeAppResponsePayload<
Expand Down
43 changes: 43 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,46 @@ export const triggerPinOrBiometricAuthentication = (

export const focusNavbar = (): Promise<{focused: boolean}> =>
postMessageToNativeApp({type: 'FOCUS_NAVBAR'});

export const showLoadingOverlay = (payload: {
/**
* Whether the in animation is enabled (false by default)
*/
inAnimation?: boolean;
/**
* Whether the out animation is enabled (false by default)
*/
outAnimation?: boolean;
/**
* Minimum duration of the loop animation in milliseconds (0 by default)
*/
minimumLoopDurationMs?: number;
/**
* whether the loop animation should be stopped immediately or not (true by default)
*/
stopAnimationCycle?: boolean;
/**
* Whether the background animation is enabled (false by default)
*/
backgroundAnimation?: boolean;
/**
* List of description texts to be shown one after the other
*/
descriptions?: Array<string>;
/**
* Duration of each description in milliseconds (3000 by default)
*/
descriptionDurationMs?: number;
/**
* After this timeout loading screen would be hidden automatically (20000 by default)
*/
timeoutMs?: number;
/**
* (Only Android) If true, after loading screen has been hidden, if user presses android back button, webview window will close (true by default)
*/
closeOnBackButtonPressAfterFinish?: boolean;
}): Promise<void> =>
postMessageToNativeApp({type: 'SHOW_LOADING_OVERLAY', payload});

export const hideLoadingOverlay = (): Promise<void> =>
postMessageToNativeApp({type: 'HIDE_LOADING_OVERLAY'});
Loading