Add heartbeat support

This commit is contained in:
Anton Tananaev 2025-06-11 17:47:43 -07:00
parent d6278202c1
commit 48df696256
3 changed files with 8 additions and 1 deletions

View file

@ -16,6 +16,7 @@
"lowAccuracyLabel": "Low", "lowAccuracyLabel": "Low",
"intervalLabel": "Interval (seconds)", "intervalLabel": "Interval (seconds)",
"distanceLabel": "Distance (meters)", "distanceLabel": "Distance (meters)",
"heartbeatLabel": "Heartbeat (seconds)",
"bufferLabel": "Offline buffering", "bufferLabel": "Offline buffering",
"trackingLabel": "Continuous tracking", "trackingLabel": "Continuous tracking",
"startAction": "Start service", "startAction": "Start service",

View file

@ -13,6 +13,7 @@ class Preferences {
static const String accuracy = 'accuracy'; static const String accuracy = 'accuracy';
static const String interval = 'interval'; static const String interval = 'interval';
static const String distance = 'distance'; static const String distance = 'distance';
static const String heartbeat = 'heartbeat';
static const String buffer = 'buffer'; static const String buffer = 'buffer';
static Future<void> init() async { static Future<void> init() async {
@ -22,7 +23,7 @@ class Preferences {
: SharedPreferencesOptions(), : SharedPreferencesOptions(),
cacheOptions: SharedPreferencesWithCacheOptions( cacheOptions: SharedPreferencesWithCacheOptions(
allowList: { allowList: {
'id', 'url', 'accuracy', 'interval', 'distance', 'buffer', id, url, accuracy, interval, distance, buffer, heartbeat,
'device_id_preference', 'server_url_preference', 'accuracy_preference', 'device_id_preference', 'server_url_preference', 'accuracy_preference',
'frequency_preference', 'distance_preference', 'buffer_preference', 'frequency_preference', 'distance_preference', 'buffer_preference',
}, },
@ -46,10 +47,12 @@ class Preferences {
await instance.setString(accuracy, instance.getString(accuracy) ?? 'medium'); await instance.setString(accuracy, instance.getString(accuracy) ?? 'medium');
await instance.setInt(interval, instance.getInt(interval) ?? 300); await instance.setInt(interval, instance.getInt(interval) ?? 300);
await instance.setInt(distance, instance.getInt(distance) ?? 75); await instance.setInt(distance, instance.getInt(distance) ?? 75);
await instance.setInt(heartbeat, instance.getInt(heartbeat) ?? 0);
await instance.setBool(buffer, instance.getBool(buffer) ?? true); await instance.setBool(buffer, instance.getBool(buffer) ?? true);
} }
static bg.Config geolocationConfig() { static bg.Config geolocationConfig() {
final heartbeatInterval = instance.getInt(interval) ?? 0;
return bg.Config( return bg.Config(
stopOnTerminate: false, stopOnTerminate: false,
startOnBoot: true, startOnBoot: true,
@ -64,10 +67,12 @@ class Preferences {
}, },
distanceFilter: instance.getInt(distance)?.toDouble(), distanceFilter: instance.getInt(distance)?.toDouble(),
locationUpdateInterval: (instance.getInt(interval) ?? 0) * 1000, locationUpdateInterval: (instance.getInt(interval) ?? 0) * 1000,
heartbeatInterval: heartbeatInterval > 0 ? heartbeatInterval : null,
maxRecordsToPersist: instance.getBool(buffer) != false ? -1 : 1, maxRecordsToPersist: instance.getBool(buffer) != false ? -1 : 1,
logLevel: bg.Config.LOG_LEVEL_INFO, logLevel: bg.Config.LOG_LEVEL_INFO,
logMaxDays: 1, logMaxDays: 1,
locationTemplate: _locationTemplate(), locationTemplate: _locationTemplate(),
showsBackgroundLocationIndicator: false,
); );
} }

View file

@ -135,6 +135,7 @@ class _SettingsScreenState extends State<SettingsScreen> {
_buildListTile(AppLocalizations.of(context)!.distanceLabel, Preferences.distance, true), _buildListTile(AppLocalizations.of(context)!.distanceLabel, Preferences.distance, true),
if (Platform.isAndroid && Preferences.instance.getInt(Preferences.distance) == 0) if (Platform.isAndroid && Preferences.instance.getInt(Preferences.distance) == 0)
_buildListTile(AppLocalizations.of(context)!.intervalLabel, Preferences.interval, true), _buildListTile(AppLocalizations.of(context)!.intervalLabel, Preferences.interval, true),
_buildListTile(AppLocalizations.of(context)!.heartbeatLabel, Preferences.heartbeat, true),
SwitchListTile( SwitchListTile(
title: Text(AppLocalizations.of(context)!.bufferLabel), title: Text(AppLocalizations.of(context)!.bufferLabel),
value: buffering, value: buffering,