From b37e7f4a708823db73d99cca57db662fcecb8d32 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Tue, 26 Jul 2022 14:36:29 +0200 Subject: [PATCH] MOBILE-4096 core: Fix open routes when main tab not loaded yet --- src/core/services/navigator.ts | 35 +++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/core/services/navigator.ts b/src/core/services/navigator.ts index 746eb23b7..a9ea1c921 100644 --- a/src/core/services/navigator.ts +++ b/src/core/services/navigator.ts @@ -13,7 +13,7 @@ // limitations under the License. import { Injectable } from '@angular/core'; -import { ActivatedRoute, ActivatedRouteSnapshot, Params } from '@angular/router'; +import { ActivatedRoute, ActivatedRouteSnapshot, NavigationEnd, Params } from '@angular/router'; import { NavigationOptions } from '@ionic/angular/providers/nav-controller'; @@ -30,6 +30,8 @@ import { CoreScreen } from './screen'; import { CoreError } from '@classes/errors/error'; import { CoreMainMenuDelegate } from '@features/mainmenu/services/mainmenu-delegate'; import { CorePlatform } from '@services/platform'; +import { filter } from 'rxjs/operators'; +import { CorePromisedValue } from '@classes/promised-value'; /** * Redirect payload. @@ -562,6 +564,13 @@ export class CoreNavigatorService { return this.navigate(`/main/${path}`, options); } + if (this.isCurrent('/main')) { + // Main menu is loaded, but no tab selected yet. Wait for a tab to be loaded. + await this.waitForMainMenuTab(); + + return this.navigate(`/main/${this.getCurrentMainMenuTab()}/${path}`, options); + } + // Open the path within in main menu. return this.navigate('/main', { ...options, @@ -667,6 +676,30 @@ export class CoreNavigatorService { return !!this.getCurrentRoute().snapshot.routeConfig?.canDeactivate?.length; } + /** + * Wait for a main menu tab route to be loaded. + * + * @return Promise resolved when the route is loaded. + */ + protected waitForMainMenuTab(): Promise { + if (this.getCurrentMainMenuTab()) { + return Promise.resolve(); + } + + const promise = new CorePromisedValue(); + + const navSubscription = Router.events + .pipe(filter(event => event instanceof NavigationEnd)) + .subscribe(() => { + if (this.getCurrentMainMenuTab()) { + navSubscription?.unsubscribe(); + promise.resolve(); + } + }); + + return promise; + } + } export const CoreNavigator = makeSingleton(CoreNavigatorService);