Skip to content

Commit 4ac97fc

Browse files
committed
Make webview go back one page instead of closing the app
1 parent a5dda01 commit 4ac97fc

File tree

5 files changed

+34
-9
lines changed

5 files changed

+34
-9
lines changed

android/app/capacitor.build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ android {
99

1010
apply from: "../capacitor-cordova-android-plugins/cordova.variables.gradle"
1111
dependencies {
12+
implementation project(':capacitor-app')
1213
implementation project(':capacitor-push-notifications')
1314
implementation project(':capacitor-status-bar')
1415
implementation project(':capgo-capacitor-social-login')

android/capacitor.settings.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
include ':capacitor-android'
33
project(':capacitor-android').projectDir = new File('../node_modules/@capacitor/android/capacitor')
44

5+
include ':capacitor-app'
6+
project(':capacitor-app').projectDir = new File('../node_modules/@capacitor/app/android')
7+
58
include ':capacitor-push-notifications'
69
project(':capacitor-push-notifications').projectDir = new File('../node_modules/@capacitor/push-notifications/android')
710

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"postinstall": "./scripts/post_install.sh"
2828
},
2929
"dependencies": {
30+
"@capacitor/app": "7.1.0",
3031
"@capacitor/core": "7.4.4",
3132
"@capacitor/push-notifications": "7.0.3",
3233
"@capacitor/status-bar": "7.0.3",

web/pages/_app.tsx

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,22 @@ import '../styles/globals.css'
1010
import {Major_Mono_Display} from 'next/font/google'
1111
import clsx from 'clsx'
1212
import {initTracking} from 'web/lib/service/analytics'
13-
import WebPush from "web/lib/service/web-push";
14-
import AndroidPush from "web/lib/service/android-push";
15-
import {isAndroidWebView} from "web/lib/util/webview";
16-
import {Capacitor} from '@capacitor/core';
17-
import {StatusBar} from '@capacitor/status-bar';
13+
import WebPush from "web/lib/service/web-push"
14+
import AndroidPush from "web/lib/service/android-push"
15+
import {isAndroidWebView} from "web/lib/util/webview"
16+
import {Capacitor} from '@capacitor/core'
17+
import {StatusBar} from '@capacitor/status-bar'
18+
import {App} from '@capacitor/app'
19+
import {useRouter} from "next/navigation";
1820

1921
if (Capacitor.isNativePlatform()) {
2022
// Only runs on iOS/Android native
2123
// Note sure it's doing anything, though, need to check
22-
StatusBar.setOverlaysWebView({overlay: false}).catch(console.warn);
23-
// StatusBar.setStyle({style: Style.Light}).catch(console.warn);
24+
StatusBar.setOverlaysWebView({overlay: false}).catch(console.warn)
25+
// StatusBar.setStyle({style: Style.Light}).catch(console.warn)
26+
App.addListener('backButton', ({canGoBack}) => {
27+
window.dispatchEvent(new CustomEvent('appBackButton'))
28+
})
2429
}
2530

2631

@@ -63,6 +68,7 @@ function MyApp({Component, pageProps}: AppProps<PageProps>) {
6368
console.log('isAndroidWebView app:', isAndroidWebView())
6469
useEffect(printBuildInfo, [])
6570
useHasLoaded()
71+
const router = useRouter()
6672

6773
useEffect(() => {
6874
initTracking()
@@ -75,10 +81,19 @@ function MyApp({Component, pageProps}: AppProps<PageProps>) {
7581
}
7682
}, [])
7783

84+
useEffect(() => {
85+
const handleBack = () => {
86+
router.back()
87+
}
88+
89+
window.addEventListener('appBackButton', handleBack)
90+
return () => window.removeEventListener('appBackButton', handleBack)
91+
}, [router])
92+
7893
// useEffect(() => {
7994
// // Expose globally for native bridge
80-
// (window as any).oauthRedirect = oauthRedirect;
81-
// }, []);
95+
// (window as any).oauthRedirect = oauthRedirect
96+
// }, [])
8297

8398
const title = 'Compass'
8499
const description = 'The platform for intentional connections'

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,11 @@
11161116
resolved "https://registry.yarnpkg.com/@capacitor/android/-/android-7.4.4.tgz#a14a1e844bd5079982427e247fdd17555b5fbedd"
11171117
integrity sha512-y8knfV1JXNrd6XZZLZireGT+EBCN0lvOo+HZ/s7L8LkrPBu4nY5UZn0Wxz4yOezItEII9rqYJSHsS5fMJG9gdw==
11181118

1119+
"@capacitor/[email protected]":
1120+
version "7.1.0"
1121+
resolved "https://registry.yarnpkg.com/@capacitor/app/-/app-7.1.0.tgz#d275eebbb0fec6104b3f9c41f09f7598d7808b47"
1122+
integrity sha512-W7m09IWrUjZbo7AKeq+rc/KyucxrJekTBg0l4QCm/yDtCejE3hebxp/W2esU26KKCzMc7H3ClkUw32E9lZkwRA==
1123+
11191124
"@capacitor/[email protected]":
11201125
version "3.0.5"
11211126
resolved "https://registry.yarnpkg.com/@capacitor/assets/-/assets-3.0.5.tgz#55c66458e703ce95e2e3188caad8b987b94b66f0"

0 commit comments

Comments
 (0)