|
|
|
@ -20,9 +20,14 @@ import { isDefined } from '@core/utils'; |
|
|
|
import { MobileActionResult, WidgetMobileActionResult, WidgetMobileActionType } from '@shared/models/widget.models'; |
|
|
|
import { from, of } from 'rxjs'; |
|
|
|
import { Observable } from 'rxjs/internal/Observable'; |
|
|
|
import { catchError } from 'rxjs/operators'; |
|
|
|
import { catchError, tap } from 'rxjs/operators'; |
|
|
|
import { OpenDashboardMessage, ReloadUserMessage, WindowMessage } from '@shared/models/window-message.model'; |
|
|
|
import { Params, Router } from '@angular/router'; |
|
|
|
import { AuthService } from '@core/auth/auth.service'; |
|
|
|
|
|
|
|
const dashboardStateNameHandler = 'tbMobileDashboardStateNameHandler'; |
|
|
|
const dashboardLoadedHandler = 'tbMobileDashboardLoadedHandler'; |
|
|
|
const navigationHandler = 'tbMobileNavigationHandler'; |
|
|
|
const mobileHandler = 'tbMobileHandler'; |
|
|
|
|
|
|
|
// @dynamic
|
|
|
|
@ -34,10 +39,20 @@ export class MobileService { |
|
|
|
private readonly mobileApp; |
|
|
|
private readonly mobileChannel; |
|
|
|
|
|
|
|
constructor(@Inject(WINDOW) private window: Window) { |
|
|
|
private readonly onWindowMessageListener = this.onWindowMessage.bind(this); |
|
|
|
|
|
|
|
private reloadUserObservable: Observable<boolean>; |
|
|
|
private lastDashboardId: string; |
|
|
|
|
|
|
|
constructor(@Inject(WINDOW) private window: Window, |
|
|
|
private router: Router, |
|
|
|
private authService: AuthService) { |
|
|
|
const w = (this.window as any); |
|
|
|
this.mobileChannel = w.flutter_inappwebview; |
|
|
|
this.mobileApp = isDefined(this.mobileChannel); |
|
|
|
if (this.mobileApp) { |
|
|
|
window.addEventListener('message', this.onWindowMessageListener); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public isMobileApp(): boolean { |
|
|
|
@ -50,6 +65,12 @@ export class MobileService { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public onDashboardLoaded() { |
|
|
|
if (this.mobileApp) { |
|
|
|
this.mobileChannel.callHandler(dashboardLoadedHandler); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public handleWidgetMobileAction<T extends MobileActionResult>(type: WidgetMobileActionType, ...args: any[]): |
|
|
|
Observable<WidgetMobileActionResult<T>> { |
|
|
|
if (this.mobileApp) { |
|
|
|
@ -67,4 +88,82 @@ export class MobileService { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public handleMobileNavigation(path?: string, params?: Params) { |
|
|
|
if (this.mobileApp) { |
|
|
|
this.mobileChannel.callHandler(navigationHandler, path, params); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private onWindowMessage(event: MessageEvent) { |
|
|
|
if (event.data) { |
|
|
|
let message: WindowMessage; |
|
|
|
try { |
|
|
|
message = JSON.parse(event.data); |
|
|
|
} catch (e) {} |
|
|
|
if (message && message.type) { |
|
|
|
switch (message.type) { |
|
|
|
case 'openDashboardMessage': |
|
|
|
const openDashboardMessage: OpenDashboardMessage = message.data; |
|
|
|
this.openDashboard(openDashboardMessage); |
|
|
|
break; |
|
|
|
case 'reloadUserMessage': |
|
|
|
const reloadUserMessage: ReloadUserMessage = message.data; |
|
|
|
this.reloadUser(reloadUserMessage); |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private openDashboard(openDashboardMessage: OpenDashboardMessage) { |
|
|
|
if (openDashboardMessage && openDashboardMessage.dashboardId) { |
|
|
|
if (this.reloadUserObservable) { |
|
|
|
this.reloadUserObservable.subscribe( |
|
|
|
(authenticated) => { |
|
|
|
if (authenticated) { |
|
|
|
this.doDashboardNavigation(openDashboardMessage); |
|
|
|
} |
|
|
|
} |
|
|
|
); |
|
|
|
} else { |
|
|
|
this.doDashboardNavigation(openDashboardMessage); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private doDashboardNavigation(openDashboardMessage: OpenDashboardMessage) { |
|
|
|
let url = `/dashboard/${openDashboardMessage.dashboardId}`; |
|
|
|
const params = []; |
|
|
|
if (openDashboardMessage.state) { |
|
|
|
params.push(`state=${openDashboardMessage.state}`); |
|
|
|
} |
|
|
|
if (openDashboardMessage.embedded) { |
|
|
|
params.push(`embedded=true`); |
|
|
|
} |
|
|
|
if (openDashboardMessage.hideToolbar) { |
|
|
|
params.push(`hideToolbar=true`); |
|
|
|
} |
|
|
|
if (this.lastDashboardId === openDashboardMessage.dashboardId) { |
|
|
|
params.push(`reload=${new Date().getTime()}`); |
|
|
|
} |
|
|
|
if (params.length) { |
|
|
|
url += `?${params.join('&')}`; |
|
|
|
} |
|
|
|
this.lastDashboardId = openDashboardMessage.dashboardId; |
|
|
|
this.router.navigateByUrl(url, {replaceUrl: true}); |
|
|
|
} |
|
|
|
|
|
|
|
private reloadUser(reloadUserMessage: ReloadUserMessage) { |
|
|
|
if (reloadUserMessage && reloadUserMessage.accessToken && reloadUserMessage.refreshToken) { |
|
|
|
this.reloadUserObservable = this.authService.setUserFromJwtToken(reloadUserMessage.accessToken, |
|
|
|
reloadUserMessage.refreshToken, true).pipe( |
|
|
|
tap( |
|
|
|
() => { |
|
|
|
this.reloadUserObservable = null; |
|
|
|
} |
|
|
|
) |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|