From 1b46a4e8b8beec2cebfdc92fdbbec6c808262aa8 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 5 Jul 2025 09:35:58 -0700 Subject: [PATCH] Default to moving state --- l10n.yaml | 2 ++ lib/geolocation_service.dart | 18 ++++++++++-------- lib/preferences.dart | 2 ++ 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/l10n.yaml b/l10n.yaml index 1622293..c562a60 100644 --- a/l10n.yaml +++ b/l10n.yaml @@ -2,3 +2,5 @@ arb-dir: lib/l10n template-arb-file: app_en.arb output-localization-file: app_localizations.dart preferred-supported-locales: [en] +synthetic-package: false +output-dir: lib/l10n diff --git a/lib/geolocation_service.dart b/lib/geolocation_service.dart index 5e14259..68fa110 100644 --- a/lib/geolocation_service.dart +++ b/lib/geolocation_service.dart @@ -16,8 +16,10 @@ class GeolocationService { } bg.BackgroundGeolocation.onEnabledChange(onEnabledChange); bg.BackgroundGeolocation.onMotionChange(onMotionChange); - bg.BackgroundGeolocation.onLocation(onLocation); bg.BackgroundGeolocation.onHeartbeat(onHeartbeat); + bg.BackgroundGeolocation.onLocation(onLocation, (bg.LocationError error) { + developer.log('Location error', error: error); + }); } static Future onEnabledChange(bool enabled) async { @@ -38,6 +40,10 @@ class GeolocationService { } } + static Future onHeartbeat(bg.HeartbeatEvent event) async { + await bg.BackgroundGeolocation.getCurrentPosition(samples: 1, persist: true, extras: {'heartbeat': true}); + } + static Future onLocation(bg.Location location) async { if (_shouldDelete(location)) { await bg.BackgroundGeolocation.destroyLocation(location.uuid); @@ -51,10 +57,6 @@ class GeolocationService { } } - static Future onHeartbeat(bg.HeartbeatEvent event) async { - await bg.BackgroundGeolocation.getCurrentPosition(samples: 1, persist: true, extras: {'heartbeat': true}); - } - static bool _shouldDelete(bg.Location location) { if (!location.isMoving) return false; if (location.extras?.isNotEmpty == true) return false; @@ -113,11 +115,11 @@ void headlessTask(bg.HeadlessEvent headlessEvent) async { case bg.Event.MOTIONCHANGE: await GeolocationService.onMotionChange(headlessEvent.event); break; - case bg.Event.LOCATION: - await GeolocationService.onLocation(headlessEvent.event); - break; case bg.Event.HEARTBEAT: await GeolocationService.onHeartbeat(headlessEvent.event); break; + case bg.Event.LOCATION: + await GeolocationService.onLocation(headlessEvent.event); + break; } } diff --git a/lib/preferences.dart b/lib/preferences.dart index 6893723..7cc355b 100644 --- a/lib/preferences.dart +++ b/lib/preferences.dart @@ -78,6 +78,7 @@ class Preferences { final fastestLocationUpdateInterval = (instance.getInt(fastestInterval) ?? 30) * 1000; final heartbeatInterval = instance.getInt(heartbeat) ?? 0; return bg.Config( + isMoving: true, enableHeadless: true, stopOnTerminate: false, startOnBoot: true, @@ -102,6 +103,7 @@ class Preferences { preventSuspend: heartbeatInterval > 0, disableElasticity: true, disableStopDetection: instance.getBool(stopDetection) == false, + pausesLocationUpdatesAutomatically: Platform.isIOS ? !(isHighestAccuracy || instance.getBool(stopDetection) == false) : null, fastestLocationUpdateInterval: isHighestAccuracy ? 0 : fastestLocationUpdateInterval, backgroundPermissionRationale: bg.PermissionRationale( title: 'Allow {applicationName} to access this device\'s location in the background',