MOBILE-3320 android: Fix back button with leave guards

main
Dani Palou 2021-06-04 12:56:13 +02:00
parent 32810c54f0
commit 7cc8fa0b9e
5 changed files with 26 additions and 20 deletions

View File

@ -224,11 +224,17 @@ export class AppComponent implements OnInit, AfterViewInit {
document.addEventListener('ionBackButton', (event: BackButtonEvent) => { document.addEventListener('ionBackButton', (event: BackButtonEvent) => {
// This callback should have the lowest priority in the app. // This callback should have the lowest priority in the app.
event.detail.register(-100, async () => { 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. // 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. // 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. // 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. // The path seems to change immediately (0 ms timeout), but use 50ms just in case.
await CoreUtils.wait(50); await CoreUtils.wait(50);
@ -238,8 +244,7 @@ export class AppComponent implements OnInit, AfterViewInit {
} }
// Quit the app. // Quit the app.
const nav = <any> window.navigator; // eslint-disable-line @typescript-eslint/no-explicit-any CoreApp.closeApp();
nav.app?.exitApp();
}); });
}); });
} }

View File

@ -661,9 +661,7 @@ export class CoreLoginHelperProvider {
// Always open it in browser because the user might have the session stored in there. // Always open it in browser because the user might have the session stored in there.
CoreUtils.openInBrowser(loginUrl); CoreUtils.openInBrowser(loginUrl);
CoreApp.closeApp();
const nav = <any> window.navigator; // eslint-disable-line @typescript-eslint/no-explicit-any
nav.app?.exitApp();
return true; return true;
} }
@ -695,9 +693,7 @@ export class CoreLoginHelperProvider {
}); });
} else { } else {
CoreUtils.openInBrowser(loginUrl); CoreUtils.openInBrowser(loginUrl);
CoreApp.closeApp();
const nav = <any> window.navigator; // eslint-disable-line @typescript-eslint/no-explicit-any
nav.app?.exitApp();
} }
} }

View File

@ -226,16 +226,10 @@ export class CoreMainMenuPage implements OnInit, OnDestroy {
// Use a priority lower than 0 (navigation). // Use a priority lower than 0 (navigation).
event.detail.register(-10, async (processNextHandler: () => void) => { event.detail.register(-10, async (processNextHandler: () => void) => {
// This callback can be called at the same time as Ionic's back navigation callback. // 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. // Check if user is already at the root of a tab.
// Ionic doc recommends IonRouterOutlet.canGoBack, but there's no easy way to get the current outlet from here. const mainMenuRootRoute = CoreNavigator.getCurrentRoute({ routeData: { isMainMenuRoot: true } });
const initialPath = CoreNavigator.getCurrentPath(); if (!mainMenuRootRoute) {
return; // Not at root level, let Ionic handle the navigation.
// 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;
} }
// No back navigation, already at root level. Check if we should change tab. // No back navigation, already at root level. Check if we should change tab.

View File

@ -30,6 +30,9 @@ import { buildTabMainRoutes } from '@features/mainmenu/mainmenu-tab-routing.modu
deps: [Injector], deps: [Injector],
useFactory: (injector: Injector) => buildTabMainRoutes(injector, { useFactory: (injector: Injector) => buildTabMainRoutes(injector, {
component: CoreMainMenuMorePage, component: CoreMainMenuMorePage,
data: {
isMainMenuRoot: true,
},
}), }),
}, },
], ],

View File

@ -562,6 +562,14 @@ export class CoreAppProvider {
return redirect; return redirect;
} }
/**
* Close the app.
*/
closeApp(): void {
const nav = <any> window.navigator; // eslint-disable-line @typescript-eslint/no-explicit-any
nav.app?.exitApp();
}
/** /**
* Forget redirect data. * Forget redirect data.
*/ */