trcr/lib/preferences.dart

95 lines
3.5 KiB
Dart
Raw Normal View History

2025-05-05 22:43:43 -07:00
import 'dart:io';
import 'dart:math';
2025-05-07 17:42:07 -07:00
import 'package:flutter_background_geolocation/flutter_background_geolocation.dart' as bg;
2025-05-05 22:43:43 -07:00
import 'package:shared_preferences/shared_preferences.dart';
class Preferences {
2025-05-10 09:51:51 -07:00
static late SharedPreferences instance;
2025-05-05 22:43:43 -07:00
static const String id = 'id';
static const String url = 'url';
static const String accuracy = 'accuracy';
static const String interval = 'interval';
static const String distance = 'distance';
static const String buffer = 'buffer';
static Future<void> init() async {
2025-05-10 09:51:51 -07:00
instance = await SharedPreferences.getInstance();
2025-05-05 22:43:43 -07:00
if (Platform.isIOS) {
2025-05-10 09:51:51 -07:00
await _migrate();
2025-05-05 22:43:43 -07:00
}
2025-05-10 09:51:51 -07:00
await instance.setString(id, instance.getString(id) ?? (Random().nextInt(90000000) + 10000000).toString());
await instance.setString(url, instance.getString(url) ?? 'http://demo.traccar.org:5055');
await instance.setString(accuracy, instance.getString(accuracy) ?? 'medium');
await instance.setInt(interval, instance.getInt(interval) ?? 300);
final distanceValue = instance.getInt(distance);
2025-05-06 07:33:27 -07:00
if (distanceValue == null || distanceValue <= 0) {
2025-05-10 09:51:51 -07:00
await instance.setInt(distance, 75);
2025-05-06 07:33:27 -07:00
}
2025-05-10 09:51:51 -07:00
await instance.setBool(buffer, instance.getBool(buffer) ?? true);
2025-05-05 22:43:43 -07:00
}
2025-05-10 09:51:51 -07:00
static bg.Config geolocationConfig() {
2025-05-07 17:42:07 -07:00
return bg.Config(
stopOnTerminate: false,
startOnBoot: true,
2025-05-10 09:51:51 -07:00
desiredAccuracy: switch (instance.getString(accuracy)) {
2025-05-07 17:42:07 -07:00
'high' => bg.Config.DESIRED_ACCURACY_HIGH,
'low' => bg.Config.DESIRED_ACCURACY_LOW,
_ => bg.Config.DESIRED_ACCURACY_MEDIUM,
},
2025-05-10 09:51:51 -07:00
url: instance.getString(url),
2025-05-07 17:42:07 -07:00
params: {
2025-05-10 09:51:51 -07:00
"device_id": instance.getString(id),
2025-05-07 17:42:07 -07:00
},
2025-05-10 09:51:51 -07:00
distanceFilter: instance.getInt(distance)?.toDouble(),
locationUpdateInterval: (instance.getInt(interval) ?? 0) * 1000,
maxRecordsToPersist: instance.getBool(buffer) != false ? -1 : 0,
2025-05-09 07:56:34 -07:00
logLevel: bg.Config.LOG_LEVEL_INFO,
logMaxDays: 1,
2025-05-07 17:42:07 -07:00
);
}
2025-05-10 09:51:51 -07:00
static Future<void> _migrate() async {
final oldId = instance.getString('device_id_preference');
2025-05-05 22:43:43 -07:00
if (oldId != null) {
2025-05-10 09:51:51 -07:00
instance.setString(id, oldId);
instance.remove('device_id_preference');
2025-05-05 22:43:43 -07:00
}
2025-05-10 09:51:51 -07:00
final oldUrl = instance.getString('server_url_preference');
2025-05-05 22:43:43 -07:00
if (oldUrl != null) {
2025-05-10 09:51:51 -07:00
instance.setString(url, oldUrl);
instance.remove('server_url_preference');
2025-05-05 22:43:43 -07:00
}
2025-05-10 09:51:51 -07:00
final oldAccuracy = instance.getString('accuracy_preference');
2025-05-05 22:43:43 -07:00
if (oldAccuracy != null) {
2025-05-10 09:51:51 -07:00
instance.setString(accuracy, oldAccuracy);
instance.remove('accuracy_preference');
2025-05-05 22:43:43 -07:00
}
2025-05-10 09:51:51 -07:00
final oldIntervalString = instance.getString('frequency_preference');
2025-05-05 22:43:43 -07:00
final oldInterval = oldIntervalString != null ? int.tryParse(oldIntervalString) : null;
if (oldInterval != null) {
2025-05-10 09:51:51 -07:00
instance.setInt(interval, oldInterval);
instance.remove('frequency_preference');
2025-05-05 22:43:43 -07:00
}
2025-05-10 09:51:51 -07:00
final oldDistanceString = instance.getString('distance_preference');
2025-05-05 22:43:43 -07:00
final oldDistance = oldDistanceString != null ? int.tryParse(oldDistanceString) : null;
if (oldDistance != null) {
2025-05-10 09:51:51 -07:00
instance.setInt(distance, oldDistance);
instance.remove('distance_preference');
2025-05-05 22:43:43 -07:00
}
2025-05-10 09:51:51 -07:00
final oldAngleString = instance.getString('angle_preference');
2025-05-05 22:43:43 -07:00
final oldAngle = oldAngleString != null ? int.tryParse(oldAngleString) : null;
if (oldAngle != null) {
2025-05-10 09:51:51 -07:00
instance.setInt('angle', oldAngle);
instance.remove('angle_preference');
2025-05-05 22:43:43 -07:00
}
2025-05-10 09:51:51 -07:00
final oldBuffer = instance.getBool('buffer_preference');
2025-05-05 22:43:43 -07:00
if (oldBuffer != null) {
2025-05-10 09:51:51 -07:00
instance.setBool(buffer, oldBuffer);
instance.remove('buffer_preference');
2025-05-05 22:43:43 -07:00
}
}
}