From 78ecc7a479b6bd147bf6850990761f7a8627ce9d Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Tue, 18 Dec 2018 16:13:44 +0100 Subject: [PATCH] MOBILE-2590 mainmenu: Make redirect wait for tabs to be loaded --- src/core/mainmenu/pages/menu/menu.ts | 58 ++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 16 deletions(-) diff --git a/src/core/mainmenu/pages/menu/menu.ts b/src/core/mainmenu/pages/menu/menu.ts index 5f0a2a647..d41e91a2e 100644 --- a/src/core/mainmenu/pages/menu/menu.ts +++ b/src/core/mainmenu/pages/menu/menu.ts @@ -37,6 +37,7 @@ export class CoreMainMenuPage implements OnDestroy { protected subscription; protected redirectObs: any; + protected pendingRedirect: any; @ViewChild('mainTabs') mainTabs: CoreIonTabsComponent; @@ -57,24 +58,13 @@ export class CoreMainMenuPage implements OnDestroy { this.showTabs = true; this.redirectObs = this.eventsProvider.on(CoreEventsProvider.LOAD_PAGE_MAIN_MENU, (data) => { - // Check if the redirect page is the root page of any of the tabs. - const i = this.tabs.findIndex((tab, i) => { - return tab.page == data.redirectPage; - }); - - if (i >= 0) { - // Tab found. Set the params. - this.tabs[i].pageParams = Object.assign({}, data.redirectParams); + if (!this.loaded) { + // View isn't ready yet, wait for it to be ready. + this.pendingRedirect = data; } else { - // Tab not found, use a phantom tab. - this.redirectPage = data.redirectPage; - this.redirectParams = data.redirectParams; + delete this.pendingRedirect; + this.handleRedirect(data); } - - setTimeout(() => { - // Let the tab load the params before navigating. - this.mainTabs.selectTabRootByIndex(i + 1); - }); }); this.subscription = this.menuDelegate.getHandlers().subscribe((handlers) => { @@ -106,6 +96,42 @@ export class CoreMainMenuPage implements OnDestroy { }); this.loaded = this.menuDelegate.areHandlersLoaded(); + + if (this.loaded && this.pendingRedirect) { + // Wait for tabs to be initialized and then handle the redirect. + setTimeout(() => { + if (this.pendingRedirect) { + this.handleRedirect(this.pendingRedirect); + delete this.pendingRedirect; + } + }); + } + }); + } + + /** + * Handle a redirect. + * + * @param {any} data Data received. + */ + protected handleRedirect(data: any): void { + // Check if the redirect page is the root page of any of the tabs. + const i = this.tabs.findIndex((tab, i) => { + return tab.page == data.redirectPage; + }); + + if (i >= 0) { + // Tab found. Set the params. + this.tabs[i].pageParams = Object.assign({}, data.redirectParams); + } else { + // Tab not found, use a phantom tab. + this.redirectPage = data.redirectPage; + this.redirectParams = data.redirectParams; + } + + setTimeout(() => { + // Let the tab load the params before navigating. + this.mainTabs.selectTabRootByIndex(i + 1); }); }