Files
FlClash/lib/common/preferences.dart
T

171 lines
4.6 KiB
Dart
Raw Normal View History

2024-04-30 23:38:49 +08:00
import 'dart:async';
import 'dart:convert';
import 'package:fl_clash/common/boot_record.dart';
import 'package:fl_clash/common/common.dart';
import 'package:fl_clash/common/system_dns.dart';
import 'package:fl_clash/enum/enum.dart';
2025-02-03 23:32:00 +08:00
import 'package:fl_clash/models/models.dart';
import 'package:flutter/foundation.dart';
2024-04-30 23:38:49 +08:00
import 'package:shared_preferences/shared_preferences.dart';
class Preferences {
static Preferences? _instance;
var _store = ResolveOnce(_open);
2025-02-03 23:32:00 +08:00
Future<bool> get isInit async => await _store.value != null;
2024-04-30 23:38:49 +08:00
Preferences._internal();
static Future<SharedPreferences?> _open() async {
try {
return await SharedPreferences.getInstance();
} catch (_) {
return null;
}
}
@visibleForTesting
Future<void> rebind() async {
_store = ResolveOnce(_open);
await _store.value;
2024-04-30 23:38:49 +08:00
}
factory Preferences() {
_instance ??= Preferences._internal();
return _instance!;
}
2025-12-16 11:23:09 +08:00
Future<int> getVersion() async {
final preferences = await _store.value;
2025-12-16 11:23:09 +08:00
return preferences?.getInt('version') ?? 0;
2024-04-30 23:38:49 +08:00
}
2025-12-16 11:23:09 +08:00
Future<void> setVersion(int version) async {
final preferences = await _store.value;
2025-12-16 11:23:09 +08:00
await preferences?.setInt('version', version);
2024-04-30 23:38:49 +08:00
}
2025-12-16 11:23:09 +08:00
Future<void> saveShareState(SharedState shareState) async {
final preferences = await _store.value;
2025-12-16 11:23:09 +08:00
await preferences?.setString('sharedState', json.encode(shareState));
}
Future<Map<String, Object?>?> getConfigMap() async {
try {
final preferences = await _store.value;
2025-12-16 11:23:09 +08:00
final configString = preferences?.getString(configKey);
if (configString == null) return null;
final Map<String, Object?>? configMap = json.decode(configString);
return configMap;
} catch (e) {
commonPrint.log(
'getConfigMap error ${e.toString()}',
logLevel: LogLevel.warning,
);
2025-12-16 11:23:09 +08:00
return null;
}
}
Future<Map<String, Object?>?> getClashConfigMap() async {
try {
final preferences = await _store.value;
2025-12-16 11:23:09 +08:00
final clashConfigString = preferences?.getString(clashConfigKey);
if (clashConfigString == null) return null;
return json.decode(clashConfigString);
} catch (e) {
commonPrint.log(
'getClashConfigMap error ${e.toString()}',
logLevel: LogLevel.warning,
);
2025-12-16 11:23:09 +08:00
return null;
}
2025-02-09 18:39:38 +08:00
}
2025-06-07 01:48:34 +08:00
Future<void> clearClashConfig() async {
2025-12-16 11:23:09 +08:00
try {
final preferences = await _store.value;
2025-12-16 11:23:09 +08:00
await preferences?.remove(clashConfigKey);
return;
} catch (e) {
commonPrint.log(
'clearClashConfig error ${e.toString()}',
logLevel: LogLevel.warning,
);
2025-12-16 11:23:09 +08:00
return;
}
}
Future<Config?> getConfig() async {
final configMap = await getConfigMap();
if (configMap == null) {
return null;
}
return Config.fromJson(configMap);
}
Future<bool> saveConfig(Config config) async {
final preferences = await _store.value;
2025-12-16 11:23:09 +08:00
return preferences?.setString(configKey, json.encode(config)) ?? false;
2024-04-30 23:38:49 +08:00
}
Future<SystemDnsRecord?> getSystemDnsRecord() async {
try {
final sharedPreferencesIns = await _store.value;
final raw = sharedPreferencesIns?.getString(systemDnsRecordKey);
if (raw == null) {
return null;
}
return SystemDnsRecord.fromJson(json.decode(raw));
} catch (e) {
commonPrint.log(
'getSystemDnsRecord error ${e.toString()}',
logLevel: LogLevel.warning,
);
return null;
}
}
Future<void> saveSystemDnsRecord(SystemDnsRecord record) async {
final sharedPreferencesIns = await _store.value;
await sharedPreferencesIns?.setString(
systemDnsRecordKey,
json.encode(record),
);
}
Future<void> clearSystemDnsRecord() async {
final sharedPreferencesIns = await _store.value;
await sharedPreferencesIns?.remove(systemDnsRecordKey);
}
Future<BootRecord?> getBootRecord() async {
try {
final sharedPreferencesIns = await _store.value;
final raw = sharedPreferencesIns?.getString(bootRecordKey);
if (raw == null) {
return null;
}
return BootRecord.fromJson(json.decode(raw));
} catch (e) {
commonPrint.log(
'getBootRecord error ${e.toString()}',
logLevel: LogLevel.warning,
);
return null;
}
}
Future<void> saveBootRecord(BootRecord record) async {
final sharedPreferencesIns = await _store.value;
await sharedPreferencesIns?.setString(bootRecordKey, json.encode(record));
}
2025-06-07 01:48:34 +08:00
Future<void> clearPreferences() async {
final sharedPreferencesIns = await _store.value;
2025-12-16 11:23:09 +08:00
await sharedPreferencesIns?.clear();
2024-04-30 23:38:49 +08:00
}
}
2025-02-03 23:32:00 +08:00
final preferences = Preferences();