diff --git a/android/app/capacitor.build.gradle b/android/app/capacitor.build.gradle index 868dc7d..9e034ff 100644 --- a/android/app/capacitor.build.gradle +++ b/android/app/capacitor.build.gradle @@ -10,6 +10,7 @@ android { apply from: "../capacitor-cordova-android-plugins/cordova.variables.gradle" dependencies { implementation project(':capacitor-action-sheet') + implementation project(':capacitor-app') implementation project(':capacitor-clipboard') implementation project(':capacitor-keyboard') implementation project(':capacitor-preferences') diff --git a/android/app/src/main/assets/capacitor.plugins.json b/android/app/src/main/assets/capacitor.plugins.json index 1f135a1..fbd92fd 100644 --- a/android/app/src/main/assets/capacitor.plugins.json +++ b/android/app/src/main/assets/capacitor.plugins.json @@ -3,6 +3,10 @@ "pkg": "@capacitor/action-sheet", "classpath": "com.capacitorjs.plugins.actionsheet.ActionSheetPlugin" }, + { + "pkg": "@capacitor/app", + "classpath": "com.capacitorjs.plugins.app.AppPlugin" + }, { "pkg": "@capacitor/clipboard", "classpath": "com.capacitorjs.plugins.clipboard.ClipboardPlugin" diff --git a/android/capacitor.settings.gradle b/android/capacitor.settings.gradle index db1b35c..32fc96b 100644 --- a/android/capacitor.settings.gradle +++ b/android/capacitor.settings.gradle @@ -5,6 +5,9 @@ project(':capacitor-android').projectDir = new File('../node_modules/@capacitor/ include ':capacitor-action-sheet' project(':capacitor-action-sheet').projectDir = new File('../node_modules/@capacitor/action-sheet/android') +include ':capacitor-app' +project(':capacitor-app').projectDir = new File('../node_modules/@capacitor/app/android') + include ':capacitor-clipboard' project(':capacitor-clipboard').projectDir = new File('../node_modules/@capacitor/clipboard/android') diff --git a/ios/App/Podfile b/ios/App/Podfile index b62e1f3..bee61ff 100644 --- a/ios/App/Podfile +++ b/ios/App/Podfile @@ -12,6 +12,7 @@ def capacitor_pods pod 'Capacitor', :path => '../../node_modules/@capacitor/ios' pod 'CapacitorCordova', :path => '../../node_modules/@capacitor/ios' pod 'CapacitorActionSheet', :path => '../../node_modules/@capacitor/action-sheet' + pod 'CapacitorApp', :path => '../../node_modules/@capacitor/app' pod 'CapacitorClipboard', :path => '../../node_modules/@capacitor/clipboard' pod 'CapacitorKeyboard', :path => '../../node_modules/@capacitor/keyboard' pod 'CapacitorPreferences', :path => '../../node_modules/@capacitor/preferences' diff --git a/package-lock.json b/package-lock.json index 4843e46..1525eb9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -20,6 +20,7 @@ "@awesome-cordova-plugins/http": "5.39.0", "@capacitor/action-sheet": "4.0.1", "@capacitor/android": "4.3.0", + "@capacitor/app": "4.1.0", "@capacitor/clipboard": "4.0.1", "@capacitor/core": "4.3.0", "@capacitor/ios": "4.3.0", @@ -2598,6 +2599,14 @@ "@capacitor/core": "^4.2.0" } }, + "node_modules/@capacitor/app": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/@capacitor/app/-/app-4.1.0.tgz", + "integrity": "sha512-QPZh+fnndlL1fT8jUmx8R7KPaWZDNJf/Aj1BiU867AxHnQNnM0a6wwDdQKGhA7OOniudekHEdrJAzHnPnKtLNg==", + "peerDependencies": { + "@capacitor/core": "^4.0.0" + } + }, "node_modules/@capacitor/cli": { "version": "4.3.0", "resolved": "https://registry.npmjs.org/@capacitor/cli/-/cli-4.3.0.tgz", @@ -23436,6 +23445,12 @@ "integrity": "sha512-JnyQsxq44wLFPQ1sN1sMISNbVuNVHXdUdseil1mNsag3JOKu1BkLCyC3aqI4ioce63fK6VtoUIvzR4YzqBn1yg==", "requires": {} }, + "@capacitor/app": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/@capacitor/app/-/app-4.1.0.tgz", + "integrity": "sha512-QPZh+fnndlL1fT8jUmx8R7KPaWZDNJf/Aj1BiU867AxHnQNnM0a6wwDdQKGhA7OOniudekHEdrJAzHnPnKtLNg==", + "requires": {} + }, "@capacitor/cli": { "version": "4.3.0", "resolved": "https://registry.npmjs.org/@capacitor/cli/-/cli-4.3.0.tgz", diff --git a/package.json b/package.json index db89d21..a76cfa2 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "@awesome-cordova-plugins/http": "5.39.0", "@capacitor/action-sheet": "4.0.1", "@capacitor/android": "4.3.0", + "@capacitor/app": "4.1.0", "@capacitor/clipboard": "4.0.1", "@capacitor/core": "4.3.0", "@capacitor/ios": "4.3.0", diff --git a/src/app/app.component.ts b/src/app/app.component.ts index a6bb07d..eb1f5eb 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -3,6 +3,7 @@ import { Router } from '@angular/router'; import { SplashScreen } from '@capacitor/splash-screen'; import { StatusBar, Style } from '@capacitor/status-bar'; import { MenuController, Platform } from '@ionic/angular'; +import { CapacitorAppService } from './core'; @Component({ selector: 'app-root', @@ -13,6 +14,8 @@ export class AppComponent { private readonly platform: Platform, private readonly menuController: MenuController, private readonly router: Router, + // Do NOT remove the following services: + private readonly capacitorAppService: CapacitorAppService, ) { void this.initializeApp(); } diff --git a/src/app/core/services/capacitor/capacitor-app/capacitor-app.service.ts b/src/app/core/services/capacitor/capacitor-app/capacitor-app.service.ts new file mode 100644 index 0000000..e9eb9a3 --- /dev/null +++ b/src/app/core/services/capacitor/capacitor-app/capacitor-app.service.ts @@ -0,0 +1,23 @@ +import { Injectable } from '@angular/core'; +import { App } from '@capacitor/app'; + +@Injectable({ + providedIn: 'root', +}) +export class CapacitorAppService { + constructor() { + void App.removeAllListeners().then(() => { + void App.addListener('backButton', ({ canGoBack }) => { + if (canGoBack) { + window.history.back(); + } else { + void this.exitApp(); + } + }); + }); + } + + private async exitApp(): Promise { + return App.exitApp(); + } +} diff --git a/src/app/core/services/capacitor/index.ts b/src/app/core/services/capacitor/index.ts new file mode 100644 index 0000000..fe5fda4 --- /dev/null +++ b/src/app/core/services/capacitor/index.ts @@ -0,0 +1 @@ +export * from './capacitor-app/capacitor-app.service'; diff --git a/src/app/core/services/index.ts b/src/app/core/services/index.ts index 3d7b6f7..d3d353c 100644 --- a/src/app/core/services/index.ts +++ b/src/app/core/services/index.ts @@ -1,4 +1,5 @@ export * from './api'; +export * from './capacitor'; export * from './dialog/dialog.service'; export * from './global-error-handler/global-error-handler.service'; export * from './native-http/native-http.service';