diff --git a/app.webpack.config.js b/app.webpack.config.js index 1ae381ad5..5a2825294 100644 --- a/app.webpack.config.js +++ b/app.webpack.config.js @@ -102,6 +102,7 @@ module.exports = (env, params = {}) => { // env.appComponents.push('~/android/cameraactivity'); env.appComponents.push('~/android/activity.android'); env.appComponents.push('~/android/camera_scan_activity'); + env.appComponents.push('~/android/sync_alarm_receiver'); const config = webpackConfig(env, params); config.entry.application = '~/android/application.android'; const { diff --git a/app/android/sync_alarm_receiver.ts b/app/android/sync_alarm_receiver.ts new file mode 100644 index 000000000..6696b4431 --- /dev/null +++ b/app/android/sync_alarm_receiver.ts @@ -0,0 +1,37 @@ +import { Application, Utils } from '@nativescript/core'; +import { SYNC_ALARM_ACTION, SYNC_THROTTLE_ALARM, syncService } from '../services/sync.android'; +import { start } from '~/startHandler'; + +/** + * BroadcastReceiver for handling sync alarms on Android + */ +@NativeClass() +@JavaProxy('__PACKAGE__.SyncAlarmReceiver') +export class SyncAlarmReceiver extends android.content.BroadcastReceiver { + async onReceive(context: android.content.Context, intent: android.content.Intent) { + const action = intent.getAction(); + + // Check if this is our sync alarm action + if (action !== SYNC_ALARM_ACTION) { + return; + } + + const serviceId = intent.getLongExtra('serviceId', -1); + if (serviceId === -1) { + return; + } + + DEV_LOG && console.log('SyncAlarmReceiver', 'onReceive', action, serviceId, Application.servicesStarted); + function handleIntent() { + syncService.triggerThrottledSync(serviceId).catch((error) => { + console.error('SyncAlarmReceiver', 'failed to trigger sync', error); + }); + } + try { + await start(); + handleIntent(); + } catch (error) { + console.error(error, error.stack); + } + } +} diff --git a/app/app.ts b/app/app.ts index 87d934f4d..b8c1c4650 100644 --- a/app/app.ts +++ b/app/app.ts @@ -31,12 +31,14 @@ import { SETTINGS_APP_VERSION, SETTINGS_SYNC_ON_START } from '~/utils/constants' import { startOnCam } from './variables'; import { CollectionViewTraceCategory } from '@nativescript-community/ui-collectionview'; import { init as sharedInit } from '@shared/index'; +import { start, stopAppServices } from '~/startHandler'; declare module '@nativescript/core/application/application-common' { interface ApplicationCommon { servicesStarted: boolean; } } + try { // we cant really use firstAppOpen anymore as all apps // already installed with older version without this code would @@ -126,38 +128,25 @@ try { Trace.enable(); } if (__DEV__ && __IOS__) { - RocketSim.loadRocketSimConnect(); + // TODO: it breaks NSUSerDefaults + // RocketSim.loadRocketSimConnect(); DEV_LOG && console.log('loadRocketSimConnect'); } let launched = false; - async function start() { - try { - Application.servicesStarted = false; - // DEV_LOG && console.log('start'); - setDocumentsService(documentsService); - await Promise.all([networkService.start(), securityService.start(), syncService.start(), ocrService.start(getCurrentISO3Language()), documentsService.start()]); - Application.servicesStarted = true; - // DEV_LOG && console.log('servicesStarted'); - Application.notify({ eventName: 'servicesStarted' }); - if (ApplicationSettings.getBoolean(SETTINGS_SYNC_ON_START, false)) { - syncService.syncDocuments({ withFolders: true }); - } - } catch (error) { - showError(error, PLAY_STORE_BUILD ? { forcedMessage: lc('startup_error') } : {}); - } - } + Application.on(Application.launchEvent, async () => { // DEV_LOG && console.log('launch'); startThemeHelper(); launched = true; - start(); + + start().catch(showError); }); Application.on(Application.resumeEvent, () => { if (!launched) { // DEV_LOG && console.log('resume'); launched = true; - start(); + start().catch(showError); } }); let pageInstance; @@ -165,14 +154,7 @@ try { DEV_LOG && console.log('exit'); launched = false; // ocrService.stop(); - try { - securityService.stop(); - // wait for sync to stop to stop documentService as their could be writes to the db - await syncService.stop(); - documentsService.stop(); - } catch (error) { - console.error(error, error.stack); - } + stopAppServices(); pageInstance?.$destroy(); pageInstance = null; }); diff --git a/app/components/common/ListItemAutoSize.svelte b/app/components/common/ListItemAutoSize.svelte index 9322c0220..22751a204 100644 --- a/app/components/common/ListItemAutoSize.svelte +++ b/app/components/common/ListItemAutoSize.svelte @@ -117,7 +117,7 @@ fontSize={subtitleFontSize * $fontScale} marginLeft={16} opacity={enabled === false ? 0.38 : 1} - text={typeof rightValue === 'function' ? rightValue() : rightValue} + text={(typeof rightValue === 'function' ? rightValue() : rightValue) + ''} textAlignment="right" verticalAlignment="middle" visibility={!!rightValue ? 'visible' : 'collapse'} diff --git a/app/components/list/MainList.svelte b/app/components/list/MainList.svelte index dc32f71a6..2af12c123 100644 --- a/app/components/list/MainList.svelte +++ b/app/components/list/MainList.svelte @@ -226,6 +226,7 @@ } export async function refresh(force = true, filter?: string) { + // DEV_LOG && console.log('refresh', force, filter); if (loading || (!force && lastRefreshFilter === filter) || !documentsService.started) { return; @@ -255,6 +256,7 @@ documents.map((d) => d.doc?.id) ); updateNoDocument(); + } catch (error) { showError(error); } finally { diff --git a/app/components/settings/FolderImageSyncSettings.svelte b/app/components/settings/FolderImageSyncSettings.svelte index 3e7058f01..ce2f3d607 100644 --- a/app/components/settings/FolderImageSyncSettings.svelte +++ b/app/components/settings/FolderImageSyncSettings.svelte @@ -1,24 +1,25 @@