Skip to content

Commit

Permalink
WEB-2062 GET_BATTERY_INFO message (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
atabel authored Oct 7, 2024
1 parent 685c8cc commit 28acd63
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 8 deletions.
33 changes: 25 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1212,6 +1212,31 @@ focusNavbar: () => Promise<{
}>;
```
### openOnboarding
Opens the app Onboarding (as if it where the first time the user logs in)
```ts
openOnboarding = () => Promise<void>
```
### getBatteryInfo
<kbd>App version >=24.10</kbd>
Obtains information about the device battery status
```ts
getBatteryInfo: () => Promise<{
batteryLevel: number | null;
isPowerSafeMode: boolean;
}>;
```
- `batteryLevel`: battery level in percentage (0 - 100). `null` if the battery
information is unavailable.
- `isPowerSafeMode`: true if the device is in power saving mode.
## Error handling
If an uncontrolled error occurs, promise will be rejected with an error object:
Expand All @@ -1228,14 +1253,6 @@ To inspect the bridge traffic, you can use the `setLogger` method:
setLogger((...args) => console.log(...args));
```
### openOnboarding
Opens the app Onboarding (as if it where the first time the user logs in)
```ts
openOnboarding = () => Promise<void>
```
## License
This project is licensed under the terms of the MIT license. See the
Expand Down
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export {
getDeviceTac,
shareBase64,
downloadBase64,
getBatteryInfo,
} from './src/device';

export {
Expand Down
24 changes: 24 additions & 0 deletions src/__tests__/device-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
getDeviceModel,
shareBase64,
downloadBase64,
getBatteryInfo,
} from '../../index';
import {
createFakeAndroidPostMessage,
Expand Down Expand Up @@ -423,3 +424,26 @@ test('downloadBase64 failure', async () => {

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

test('getBatteryInfo', async () => {
createFakeWebKitPostMessage({
checkMessage: (msg) => {
expect(msg.type).toBe('GET_BATTERY_INFO');
},
getResponse: (msg) => ({
type: 'GET_BATTERY_INFO',
payload: {
batteryLevel: 76,
isPowerSafeMode: false,
},
id: msg.id,
}),
});

const res = await getBatteryInfo();

expect(res).toEqual({
batteryLevel: 76,
isPowerSafeMode: false,
});
});
5 changes: 5 additions & 0 deletions src/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,8 @@ export const downloadBase64 = (params: {
fileName: params.fileName,
},
});

export const getBatteryInfo = (): Promise<{
batteryLevel: number | null;
isPowerSafeMode: boolean;
}> => postMessageToNativeApp({type: 'GET_BATTERY_INFO'});
8 changes: 8 additions & 0 deletions src/post-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,14 @@ export type ResponsesFromNativeApp = {
id: string;
payload: void;
};
GET_BATTERY_INFO: {
type: 'GET_BATTERY_INFO';
id: string;
payload: {
batteryLevel: number | null;
isPowerSafeMode: boolean;
};
};
};

export type NativeAppResponsePayload<
Expand Down

0 comments on commit 28acd63

Please sign in to comment.