-
Notifications
You must be signed in to change notification settings - Fork 208
/
MobileApp.ts
89 lines (77 loc) · 4.2 KB
/
MobileApp.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import { AsyncMethodsOf, BeEvent, Logger, PromiseReturnType } from "@itwin/core-bentley";
import { IModelReadRpcInterface, IModelTileRpcInterface, IpcWebSocketFrontend } from "@itwin/core-common";
import { IModelAppOptions, IpcApp, NativeApp, NativeAppOpts, NotificationHandler } from "@itwin/core-frontend";
import { mobileAppStrings } from "../common/MobileAppChannel";
import { MobileAppFunctions, MobileNotifications } from "../common/MobileAppProps";
import { MobileRpcManager } from "../common/MobileRpcManager";
import { MobileAuthorizationFrontend } from "./MobileAuthorizationFrontend";
/** @beta */
export type MobileAppOpts = NativeAppOpts & { iModelApp: { authorizationClient?: never } };
/** receive notifications from backend */
class MobileAppNotifyHandler extends NotificationHandler implements MobileNotifications {
public get channelName() { return mobileAppStrings.mobileAppNotify; }
public notifyMemoryWarning() {
Logger.logWarning("mobileApp", "Low memory warning");
if (MobileApp.onMemoryWarning.numberOfListeners === 0) {
alert("Low memory warning");
}
MobileApp.onMemoryWarning.raiseEvent();
}
public notifyOrientationChanged() { MobileApp.onOrientationChanged.raiseEvent(); }
public notifyWillTerminate() { MobileApp.onWillTerminate.raiseEvent(); }
public notifyAuthAccessTokenChanged(accessToken: string | undefined, expirationDate: string | undefined) {
MobileApp.onAuthAccessTokenChanged.raiseEvent(accessToken, expirationDate);
}
}
/** @beta */
export class MobileApp {
public static onMemoryWarning = new BeEvent<() => void>();
public static onOrientationChanged = new BeEvent<() => void>();
public static onEnterForeground = new BeEvent<() => void>();
public static onEnterBackground = new BeEvent<() => void>();
public static onWillTerminate = new BeEvent<() => void>();
public static onAuthAccessTokenChanged = new BeEvent<(accessToken: string | undefined, expirationDate: string | undefined) => void>();
public static async callBackend<T extends AsyncMethodsOf<MobileAppFunctions>>(methodName: T, ...args: Parameters<MobileAppFunctions[T]>) {
return IpcApp.callIpcChannel(mobileAppStrings.mobileAppChannel, methodName, ...args) as PromiseReturnType<MobileAppFunctions[T]>;
}
private static _isValid = false;
public static get isValid() { return this._isValid; }
/** @beta */
public static async startup(opts?: MobileAppOpts) {
attachDirectEventCallbacks();
const iModelAppOpts: IModelAppOptions = {
...opts?.iModelApp,
};
const authorizationClient = new MobileAuthorizationFrontend();
iModelAppOpts.authorizationClient = authorizationClient;
if (!this._isValid) {
this.onAuthAccessTokenChanged.addListener((accessToken: string | undefined, expirationDate: string | undefined) => {
authorizationClient.setAccessToken(accessToken, expirationDate);
});
const rpcInterfaces = opts?.iModelApp?.rpcInterfaces ?? [IModelReadRpcInterface, IModelTileRpcInterface]; // eslint-disable-line deprecation/deprecation
MobileRpcManager.initializeClient(rpcInterfaces);
this._isValid = true;
}
const socket = new IpcWebSocketFrontend(); // needs work
await NativeApp.startup(socket, { ...opts, iModelApp: iModelAppOpts });
MobileAppNotifyHandler.register(); // receives notifications from backend
}
}
/*
The suspend/resume lifecycle events cannot be reliably sent from the backend due to timing issues that arise when
inter-operating with the actual suspend and resume behavior on the native side.
Instead, they are sent directly to the browser here from platform-specific code.
*/
function attachDirectEventCallbacks() {
(window as any)._imodeljs_rpc_lifecycle = (evt: "suspend" | "resume") => {
if (evt === "suspend") {
MobileApp.onEnterBackground.raiseEvent();
} else if (evt === "resume") {
MobileApp.onEnterForeground.raiseEvent();
}
};
}