diff --git a/android/app/capacitor.build.gradle b/android/app/capacitor.build.gradle index e839d61bb..382282005 100644 --- a/android/app/capacitor.build.gradle +++ b/android/app/capacitor.build.gradle @@ -9,6 +9,7 @@ android { apply from: "../capacitor-cordova-android-plugins/cordova.variables.gradle" dependencies { + implementation project(':capacitor-app') implementation project(':capacitor-camera') implementation project(':capacitor-clipboard') implementation project(':capacitor-device') diff --git a/android/capacitor.settings.gradle b/android/capacitor.settings.gradle index 16ae80873..d70bebb2f 100644 --- a/android/capacitor.settings.gradle +++ b/android/capacitor.settings.gradle @@ -2,6 +2,9 @@ include ':capacitor-android' project(':capacitor-android').projectDir = new File('../node_modules/@capacitor/android/capacitor') +include ':capacitor-app' +project(':capacitor-app').projectDir = new File('../node_modules/@capacitor/app/android') + include ':capacitor-camera' project(':capacitor-camera').projectDir = new File('../node_modules/@capacitor/camera/android') diff --git a/ios/App/Podfile b/ios/App/Podfile index 1d119b985..0272fa8f3 100644 --- a/ios/App/Podfile +++ b/ios/App/Podfile @@ -11,6 +11,7 @@ install! 'cocoapods', :disable_input_output_paths => true def capacitor_pods pod 'Capacitor', :path => '../../node_modules/@capacitor/ios' pod 'CapacitorCordova', :path => '../../node_modules/@capacitor/ios' + pod 'CapacitorApp', :path => '../../node_modules/@capacitor/app' pod 'CapacitorCamera', :path => '../../node_modules/@capacitor/camera' pod 'CapacitorClipboard', :path => '../../node_modules/@capacitor/clipboard' pod 'CapacitorDevice', :path => '../../node_modules/@capacitor/device' diff --git a/ios/App/Podfile.lock b/ios/App/Podfile.lock index 25e270a26..d4dfb94dc 100644 --- a/ios/App/Podfile.lock +++ b/ios/App/Podfile.lock @@ -1,7 +1,9 @@ PODS: - Capacitor (6.2.0): - CapacitorCordova - - CapacitorCamera (6.1.1): + - CapacitorApp (6.0.2): + - Capacitor + - CapacitorCamera (6.1.2): - Capacitor - CapacitorClipboard (6.0.2): - Capacitor @@ -23,6 +25,7 @@ PODS: DEPENDENCIES: - "Capacitor (from `../../node_modules/@capacitor/ios`)" + - "CapacitorApp (from `../../node_modules/@capacitor/app`)" - "CapacitorCamera (from `../../node_modules/@capacitor/camera`)" - "CapacitorClipboard (from `../../node_modules/@capacitor/clipboard`)" - "CapacitorCordova (from `../../node_modules/@capacitor/ios`)" @@ -37,6 +40,8 @@ DEPENDENCIES: EXTERNAL SOURCES: Capacitor: :path: "../../node_modules/@capacitor/ios" + CapacitorApp: + :path: "../../node_modules/@capacitor/app" CapacitorCamera: :path: "../../node_modules/@capacitor/camera" CapacitorClipboard: @@ -60,7 +65,8 @@ EXTERNAL SOURCES: SPEC CHECKSUMS: Capacitor: 1f3c7b9802d958cd8c4eb63895fff85dff2e1eea - CapacitorCamera: b9904b48258a91a48930e72a400638a5fc223b27 + CapacitorApp: 2a8c3a0b0814322e5e6e15fe595f02c3808f0f8b + CapacitorCamera: 9052bd3f464f135ba983feccd8ac1266272f0549 CapacitorClipboard: 55e0a514f1e97b1409d533266c119dcbff3e78c3 CapacitorCordova: b33e7f4aa4ed105dd43283acdd940964374a87d9 CapacitorDevice: 1a215717f0b5061503b21a03508b0ec458a57d78 @@ -71,6 +77,6 @@ SPEC CHECKSUMS: CapacitorSplashScreen: 68893659d77b5f82d753b3a70475082845e3039c CapacitorStatusBar: 3b9ac7d0684770522c532d1158a1434512ab1477 -PODFILE CHECKSUM: cecd7e9afdf00b54ffac291bfc0edab8a2ebdc11 +PODFILE CHECKSUM: 61b2d909d0bdbc1bf01bc1dc64bda379371a49ac COCOAPODS: 1.16.2 diff --git a/package.json b/package.json index e8e84fc50..c9be64805 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "type": "module", "dependencies": { "@capacitor/android": "^6.1.2", + "@capacitor/app": "^6.0.2", "@capacitor/camera": "^6.1.0", "@capacitor/cli": "^6.1.2", "@capacitor/clipboard": "^6.0.2", diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index bd5a3ccd7..462f0e891 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -28,6 +28,7 @@ import Market from "$lib/components/market/Market.svelte" import { swipe } from "$lib/components/ui/Swipe" import { ScreenOrientation } from "@capacitor/screen-orientation" + import { App } from "@capacitor/app" import BottomNavBarMobile from "$lib/layouts/BottomNavBarMobile.svelte" import { goto } from "$app/navigation" import { routes } from "$lib/defaults/routes" @@ -35,6 +36,7 @@ import { Keyboard, KeyboardResize } from "@capacitor/keyboard" import { changeSafeAreaColorsOnAndroid } from "$lib/plugins/safeAreaColorAndroid" import { changeSafeAreaColorsOniOS } from "$lib/plugins/safeAreaColoriOS" + import { ToastMessage } from "$lib/state/ui/toast" log.debug("Initializing app, layout routes page.") @@ -295,6 +297,35 @@ } } + onMount(() => { + let exitAppInNextPress = false + if (isAndroid()) { + App.addListener("backButton", async _ => { + const sidebarOpenValue = get(UIStore.state.sidebarOpen) + if (!sidebarOpenValue) { + UIStore.openSidebar() + return + } + + if (history.length <= 2 && !exitAppInNextPress) { + exitAppInNextPress = true + alert("Press back again to exit.") + } else if (history.length <= 2 && exitAppInNextPress) { + await App.exitApp() + } + + if (history.length > 2) { + exitAppInNextPress = false + history.back() + } + }) + + return () => { + App.removeAllListeners() + } + } + }) + onMount(async () => { await fetchDeviceInfo() if (isAndroidOriOS()) {