From d1b79487237e8cb4d37a4eabea5912cf58c7f48b Mon Sep 17 00:00:00 2001 From: fiatcode Date: Thu, 30 Apr 2026 11:12:30 +0700 Subject: [PATCH] feat: add Preferences for settings persistence --- lib/preferences.dart | 103 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 lib/preferences.dart diff --git a/lib/preferences.dart b/lib/preferences.dart new file mode 100644 index 0000000..bd7ed63 --- /dev/null +++ b/lib/preferences.dart @@ -0,0 +1,103 @@ +import 'package:shared_preferences/shared_preferences.dart'; + +class Preferences { + static const String keyServerUrl = 'server_url'; + static const String keyDeviceId = 'device_id'; + static const String keyPassword = 'password'; + static const String keyAccuracy = 'accuracy'; + static const String keyDistanceFilter = 'distance_filter'; + static const String keyInterval = 'interval'; + static const String keyFastestInterval = 'fastest_interval'; + static const String keyAngleFilter = 'angle_filter'; + static const String keyHeartbeat = 'heartbeat'; + static const String keyOfflineBuffer = 'offline_buffer'; + static const String keyStopDetection = 'stop_detection'; + + static SharedPreferences? _instance; + + static Future init() async { + _instance = await SharedPreferences.getInstance(); + } + + static SharedPreferences get instance { + if (_instance == null) { + throw Exception('Preferences not initialized. Call Preferences.init() first.'); + } + return _instance!; + } + + static String get serverUrl => + instance.getString(keyServerUrl) ?? 'https://demo.traccar.org'; + + static String get deviceId => + instance.getString(keyDeviceId) ?? _generateDeviceId(); + + static String get password => instance.getString(keyPassword) ?? ''; + + static int get accuracy => instance.getInt(keyAccuracy) ?? 0; + + static int get distanceFilter => instance.getInt(keyDistanceFilter) ?? 75; + + static int get interval => instance.getInt(keyInterval) ?? 300; + + static int get fastestInterval => instance.getInt(keyFastestInterval) ?? 30; + + static int get angleFilter => instance.getInt(keyAngleFilter) ?? 0; + + static int get heartbeat => instance.getInt(keyHeartbeat) ?? 60; + + static bool get offlineBuffer => instance.getBool(keyOfflineBuffer) ?? true; + + static bool get stopDetection => instance.getBool(keyStopDetection) ?? true; + + static Future setServerUrl(String value) async { + await instance.setString(keyServerUrl, value); + } + + static Future setDeviceId(String value) async { + await instance.setString(keyDeviceId, value); + } + + static Future setPassword(String value) async { + await instance.setString(keyPassword, value); + } + + static Future setAccuracy(int value) async { + await instance.setInt(keyAccuracy, value); + } + + static Future setDistanceFilter(int value) async { + await instance.setInt(keyDistanceFilter, value); + } + + static Future setInterval(int value) async { + await instance.setInt(keyInterval, value); + } + + static Future setFastestInterval(int value) async { + await instance.setInt(keyFastestInterval, value); + } + + static Future setAngleFilter(int value) async { + await instance.setInt(keyAngleFilter, value); + } + + static Future setHeartbeat(int value) async { + await instance.setInt(keyHeartbeat, value); + } + + static Future setOfflineBuffer(bool value) async { + await instance.setBool(keyOfflineBuffer, value); + } + + static Future setStopDetection(bool value) async { + await instance.setBool(keyStopDetection, value); + } + + static String _generateDeviceId() { + final random = DateTime.now().millisecondsSinceEpoch % 100000000; + final deviceId = random.toString().padLeft(8, '0'); + instance.setString(keyDeviceId, deviceId); + return deviceId; + } +} \ No newline at end of file