From 7cc8fa0b9eb160dd8a416108216c0441c6c480e2 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Fri, 4 Jun 2021 12:56:13 +0200 Subject: [PATCH] MOBILE-3320 android: Fix back button with leave guards --- src/app/app.component.ts | 13 +++++++++---- src/core/features/login/services/login-helper.ts | 8 ++------ src/core/features/mainmenu/pages/menu/menu.ts | 14 ++++---------- .../features/mainmenu/pages/more/more.module.ts | 3 +++ src/core/services/app.ts | 8 ++++++++ 5 files changed, 26 insertions(+), 20 deletions(-) diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 7457bda23..713c57567 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -224,11 +224,17 @@ export class AppComponent implements OnInit, AfterViewInit { document.addEventListener('ionBackButton', (event: BackButtonEvent) => { // This callback should have the lowest priority in the app. event.detail.register(-100, async () => { + const initialPath = CoreNavigator.getCurrentPath(); + if (initialPath.startsWith('/main/')) { + // Main menu has its own callback to handle back. If this callback is called it means we should exit app. + CoreApp.closeApp(); + + return; + } + // This callback can be called at the same time as Ionic's back navigation callback. // Check if the path changes due to the back navigation handler, to know if we're at root level. // Ionic doc recommends IonRouterOutlet.canGoBack, but there's no easy way to get the current outlet from here. - const initialPath = CoreNavigator.getCurrentPath(); - // The path seems to change immediately (0 ms timeout), but use 50ms just in case. await CoreUtils.wait(50); @@ -238,8 +244,7 @@ export class AppComponent implements OnInit, AfterViewInit { } // Quit the app. - const nav = window.navigator; // eslint-disable-line @typescript-eslint/no-explicit-any - nav.app?.exitApp(); + CoreApp.closeApp(); }); }); } diff --git a/src/core/features/login/services/login-helper.ts b/src/core/features/login/services/login-helper.ts index 5ebfdfe8e..955ac5457 100644 --- a/src/core/features/login/services/login-helper.ts +++ b/src/core/features/login/services/login-helper.ts @@ -661,9 +661,7 @@ export class CoreLoginHelperProvider { // Always open it in browser because the user might have the session stored in there. CoreUtils.openInBrowser(loginUrl); - - const nav = window.navigator; // eslint-disable-line @typescript-eslint/no-explicit-any - nav.app?.exitApp(); + CoreApp.closeApp(); return true; } @@ -695,9 +693,7 @@ export class CoreLoginHelperProvider { }); } else { CoreUtils.openInBrowser(loginUrl); - - const nav = window.navigator; // eslint-disable-line @typescript-eslint/no-explicit-any - nav.app?.exitApp(); + CoreApp.closeApp(); } } diff --git a/src/core/features/mainmenu/pages/menu/menu.ts b/src/core/features/mainmenu/pages/menu/menu.ts index 0bba478b1..b2c968bbd 100644 --- a/src/core/features/mainmenu/pages/menu/menu.ts +++ b/src/core/features/mainmenu/pages/menu/menu.ts @@ -226,16 +226,10 @@ export class CoreMainMenuPage implements OnInit, OnDestroy { // Use a priority lower than 0 (navigation). event.detail.register(-10, async (processNextHandler: () => void) => { // This callback can be called at the same time as Ionic's back navigation callback. - // Check if the path changes due to the back navigation handler, to know if we're at root level of the tab. - // Ionic doc recommends IonRouterOutlet.canGoBack, but there's no easy way to get the current outlet from here. - const initialPath = CoreNavigator.getCurrentPath(); - - // The path seems to change immediately (0 ms timeout), but use 50ms just in case. - await CoreUtils.wait(50); - - if (CoreNavigator.getCurrentPath() != initialPath) { - // Ionic has navigated back, nothing else to do. - return; + // Check if user is already at the root of a tab. + const mainMenuRootRoute = CoreNavigator.getCurrentRoute({ routeData: { isMainMenuRoot: true } }); + if (!mainMenuRootRoute) { + return; // Not at root level, let Ionic handle the navigation. } // No back navigation, already at root level. Check if we should change tab. diff --git a/src/core/features/mainmenu/pages/more/more.module.ts b/src/core/features/mainmenu/pages/more/more.module.ts index 48cd31b3b..e1a10ac61 100644 --- a/src/core/features/mainmenu/pages/more/more.module.ts +++ b/src/core/features/mainmenu/pages/more/more.module.ts @@ -30,6 +30,9 @@ import { buildTabMainRoutes } from '@features/mainmenu/mainmenu-tab-routing.modu deps: [Injector], useFactory: (injector: Injector) => buildTabMainRoutes(injector, { component: CoreMainMenuMorePage, + data: { + isMainMenuRoot: true, + }, }), }, ], diff --git a/src/core/services/app.ts b/src/core/services/app.ts index 8430448e2..27dc93587 100644 --- a/src/core/services/app.ts +++ b/src/core/services/app.ts @@ -562,6 +562,14 @@ export class CoreAppProvider { return redirect; } + /** + * Close the app. + */ + closeApp(): void { + const nav = window.navigator; // eslint-disable-line @typescript-eslint/no-explicit-any + nav.app?.exitApp(); + } + /** * Forget redirect data. */