Handle back button navigation on android

This commit is contained in:
Tony Giorgio
2023-08-22 14:43:23 -05:00
committed by Tony Giorgio
parent 01b8645a54
commit 70df3ce672
6 changed files with 40 additions and 2 deletions

View File

@@ -9,6 +9,7 @@ android {
apply from: "../capacitor-cordova-android-plugins/cordova.variables.gradle"
dependencies {
implementation project(':capacitor-app')
implementation project(':capacitor-clipboard')
implementation project(':capacitor-filesystem')
implementation project(':capacitor-toast')

View File

@@ -2,6 +2,9 @@
include ':capacitor-android'
project(':capacitor-android').projectDir = new File('../node_modules/.pnpm/@capacitor+android@5.2.2_@capacitor+core@5.2.2/node_modules/@capacitor/android/capacitor')
include ':capacitor-app'
project(':capacitor-app').projectDir = new File('../node_modules/.pnpm/@capacitor+app@5.0.6_@capacitor+core@5.2.2/node_modules/@capacitor/app/android')
include ':capacitor-clipboard'
project(':capacitor-clipboard').projectDir = new File('../node_modules/.pnpm/@capacitor+clipboard@5.0.6_@capacitor+core@5.2.2/node_modules/@capacitor/clipboard/android')

View File

@@ -44,6 +44,7 @@
},
"dependencies": {
"@capacitor/android": "^5.2.2",
"@capacitor/app": "^5.0.6",
"@capacitor/clipboard": "^5.0.6",
"@capacitor/core": "^5.2.2",
"@capacitor/filesystem": "^5.1.1",

11
pnpm-lock.yaml generated
View File

@@ -7,6 +7,9 @@ importers:
'@capacitor/android':
specifier: ^5.2.2
version: 5.2.2(@capacitor/core@5.2.2)
'@capacitor/app':
specifier: ^5.0.6
version: 5.0.6(@capacitor/core@5.2.2)
'@capacitor/clipboard':
specifier: ^5.0.6
version: 5.0.6(@capacitor/core@5.2.2)
@@ -1479,6 +1482,14 @@ packages:
'@capacitor/core': 5.2.2
dev: false
/@capacitor/app@5.0.6(@capacitor/core@5.2.2):
resolution: {integrity: sha512-6ZXVdnNmaYILasC/RjQw+yfTmq2ZO7Q3v5lFcDVfq3PFGnybyYQh+RstBrYri+376OmXOXxBD7E6UxBhrMzXGA==}
peerDependencies:
'@capacitor/core': ^5.0.0
dependencies:
'@capacitor/core': 5.2.2
dev: false
/@capacitor/assets@2.0.4(@types/node@18.17.1)(typescript@5.1.6):
resolution: {integrity: sha512-1zYuwJujihsCLavojpZQQEjQjxG8BgD2/QWaX1Je2TQgN8UVbmnQG7gO+6gaHqP+hgZu7SOcJ+BlMG+tarBXLA==}
engines: {node: '>=10.3.0'}

View File

@@ -1,4 +1,6 @@
import { Match, Show, Suspense, Switch } from "solid-js";
import { App as CapacitorApp } from "@capacitor/app";
import { Capacitor } from "@capacitor/core";
import { Match, onCleanup, Show, Suspense, Switch } from "solid-js";
import { A } from "solid-start";
import settings from "~/assets/icons/settings.svg";
@@ -28,6 +30,25 @@ export function App() {
const i18n = useI18n();
const [state, _actions] = useMegaStore();
// Check if the platform is Android to handle back
if (Capacitor.getPlatform() === "android") {
const { remove } = CapacitorApp.addListener(
"backButton",
({ canGoBack }) => {
if (!canGoBack) {
CapacitorApp.exitApp();
} else {
window.history.back();
}
}
);
// Ensure the listener is cleaned up when the component is destroyed
onCleanup(() => {
remove();
});
}
return (
<SafeArea>
<DefaultMain>

View File

@@ -65,7 +65,8 @@ export default defineConfig({
"@capacitor/core",
"@capacitor/filesystem",
"@capacitor/toast",
"@mutinywallet/barcode-scanner"
"@mutinywallet/barcode-scanner",
"@capacitor/app"
],
// This is necessary because otherwise `vite dev` can't find the wasm
exclude: ["@mutinywallet/mutiny-wasm", "@mutinywallet/waila-wasm"]