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 const String keyPermissionsGranted = 'permissions_granted'; 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 bool get permissionsGranted => instance.getBool(keyPermissionsGranted) ?? false; static Future setPermissionsGranted(bool value) async { await instance.setBool(keyPermissionsGranted, value); } 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; } }