From df67f4a78bc0cdac6b63ec9bca9de0541fda4094 Mon Sep 17 00:00:00 2001 From: Johannes Millan Date: Thu, 10 Apr 2025 19:40:04 +0200 Subject: [PATCH] feat: sync whenever app goes to background on mobile --- android/app/capacitor.build.gradle | 2 +- android/capacitor.settings.gradle | 3 +++ package.json | 1 + src/main.ts | 18 ++++++++++++++++++ 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/android/app/capacitor.build.gradle b/android/app/capacitor.build.gradle index a01eee234a..a14290e49e 100644 --- a/android/app/capacitor.build.gradle +++ b/android/app/capacitor.build.gradle @@ -11,7 +11,7 @@ apply from: "../capacitor-cordova-android-plugins/cordova.variables.gradle" dependencies { implementation project(':capacitor-app') implementation project(':capacitor-filesystem') - + implementation project(':capawesome-capacitor-background-task') } diff --git a/android/capacitor.settings.gradle b/android/capacitor.settings.gradle index 2d74be5021..992e128f2a 100644 --- a/android/capacitor.settings.gradle +++ b/android/capacitor.settings.gradle @@ -7,3 +7,6 @@ project(':capacitor-app').projectDir = new File('../node_modules/@capacitor/app/ include ':capacitor-filesystem' project(':capacitor-filesystem').projectDir = new File('../node_modules/@capacitor/filesystem/android') + +include ':capawesome-capacitor-background-task' +project(':capawesome-capacitor-background-task').projectDir = new File('../node_modules/@capawesome/capacitor-background-task/android') diff --git a/package.json b/package.json index cdfb9d22a1..30995e006e 100644 --- a/package.json +++ b/package.json @@ -130,6 +130,7 @@ "@capacitor/cli": "^7.2.0", "@capacitor/core": "^7.2.0", "@capacitor/filesystem": "^7.0.1", + "@capawesome/capacitor-background-task": "^7.0.1", "@csstools/stylelint-formatter-github": "^1.0.0", "@electron/notarize": "^2.5.0", "@fontsource/roboto": "^4.5.8", diff --git a/src/main.ts b/src/main.ts index 93a3f06792..3d6b0a2bfd 100644 --- a/src/main.ts +++ b/src/main.ts @@ -53,6 +53,8 @@ import { AppComponent } from './app/app.component'; import { TranslateHttpLoader } from '@ngx-translate/http-loader'; import { ShortTime2Pipe } from './app/ui/pipes/short-time2.pipe'; import { provideCharts, withDefaultRegisterables } from 'ng2-charts'; +import { BackgroundTask } from '@capawesome/capacitor-background-task'; +import { promiseTimeout } from './app/util/promise-timeout'; if (environment.production || environment.stage) { enableProdMode(); @@ -207,4 +209,20 @@ if (IS_ANDROID_WEB_VIEW) { window.history.back(); } }); + + CapacitorApp.addListener('appStateChange', async ({ isActive }) => { + if (isActive) { + return; + } + // The app state has been changed to inactive. + // Start the background task by calling `beforeExit`. + const taskId = await BackgroundTask.beforeExit(async () => { + // Run your code... + // Finish the background task as soon as everything is done. + console.log('Time window for completing sync started'); + await promiseTimeout(20000); + console.log('Time window for completing sync ended. Closing app!'); + BackgroundTask.finish({ taskId }); + }); + }); }