feat: add Preferences for settings persistence
This commit is contained in:
parent
724fbe1bdb
commit
d1b7948723
1 changed files with 103 additions and 0 deletions
103
lib/preferences.dart
Normal file
103
lib/preferences.dart
Normal file
|
|
@ -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<void> 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<void> setServerUrl(String value) async {
|
||||
await instance.setString(keyServerUrl, value);
|
||||
}
|
||||
|
||||
static Future<void> setDeviceId(String value) async {
|
||||
await instance.setString(keyDeviceId, value);
|
||||
}
|
||||
|
||||
static Future<void> setPassword(String value) async {
|
||||
await instance.setString(keyPassword, value);
|
||||
}
|
||||
|
||||
static Future<void> setAccuracy(int value) async {
|
||||
await instance.setInt(keyAccuracy, value);
|
||||
}
|
||||
|
||||
static Future<void> setDistanceFilter(int value) async {
|
||||
await instance.setInt(keyDistanceFilter, value);
|
||||
}
|
||||
|
||||
static Future<void> setInterval(int value) async {
|
||||
await instance.setInt(keyInterval, value);
|
||||
}
|
||||
|
||||
static Future<void> setFastestInterval(int value) async {
|
||||
await instance.setInt(keyFastestInterval, value);
|
||||
}
|
||||
|
||||
static Future<void> setAngleFilter(int value) async {
|
||||
await instance.setInt(keyAngleFilter, value);
|
||||
}
|
||||
|
||||
static Future<void> setHeartbeat(int value) async {
|
||||
await instance.setInt(keyHeartbeat, value);
|
||||
}
|
||||
|
||||
static Future<void> setOfflineBuffer(bool value) async {
|
||||
await instance.setBool(keyOfflineBuffer, value);
|
||||
}
|
||||
|
||||
static Future<void> 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;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue