Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Android): Fix back button on android #965

Merged
merged 4 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions android/app/capacitor.build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
3 changes: 3 additions & 0 deletions android/capacitor.settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
1 change: 1 addition & 0 deletions ios/App/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
12 changes: 9 additions & 3 deletions ios/App/Podfile.lock
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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`)"
Expand All @@ -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:
Expand All @@ -60,7 +65,8 @@ EXTERNAL SOURCES:

SPEC CHECKSUMS:
Capacitor: 1f3c7b9802d958cd8c4eb63895fff85dff2e1eea
CapacitorCamera: b9904b48258a91a48930e72a400638a5fc223b27
CapacitorApp: 2a8c3a0b0814322e5e6e15fe595f02c3808f0f8b
CapacitorCamera: 9052bd3f464f135ba983feccd8ac1266272f0549
CapacitorClipboard: 55e0a514f1e97b1409d533266c119dcbff3e78c3
CapacitorCordova: b33e7f4aa4ed105dd43283acdd940964374a87d9
CapacitorDevice: 1a215717f0b5061503b21a03508b0ec458a57d78
Expand All @@ -71,6 +77,6 @@ SPEC CHECKSUMS:
CapacitorSplashScreen: 68893659d77b5f82d753b3a70475082845e3039c
CapacitorStatusBar: 3b9ac7d0684770522c532d1158a1434512ab1477

PODFILE CHECKSUM: cecd7e9afdf00b54ffac291bfc0edab8a2ebdc11
PODFILE CHECKSUM: 61b2d909d0bdbc1bf01bc1dc64bda379371a49ac

COCOAPODS: 1.16.2
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
31 changes: 31 additions & 0 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@
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"
import { fetchDeviceInfo, isAndroid, isAndroidOriOS, isiOSMobile } from "$lib/utils/Mobile"
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.")

Expand Down Expand Up @@ -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()) {
Expand Down
Loading