171 lines
4.6 KiB
Dart
171 lines
4.6 KiB
Dart
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';
|
|
import 'package:fl_clash/models/models.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
class Preferences {
|
|
static Preferences? _instance;
|
|
var _store = ResolveOnce(_open);
|
|
|
|
Future<bool> get isInit async => await _store.value != null;
|
|
|
|
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;
|
|
}
|
|
|
|
factory Preferences() {
|
|
_instance ??= Preferences._internal();
|
|
return _instance!;
|
|
}
|
|
|
|
Future<int> getVersion() async {
|
|
final preferences = await _store.value;
|
|
return preferences?.getInt('version') ?? 0;
|
|
}
|
|
|
|
Future<void> setVersion(int version) async {
|
|
final preferences = await _store.value;
|
|
await preferences?.setInt('version', version);
|
|
}
|
|
|
|
Future<void> saveShareState(SharedState shareState) async {
|
|
final preferences = await _store.value;
|
|
await preferences?.setString('sharedState', json.encode(shareState));
|
|
}
|
|
|
|
Future<Map<String, Object?>?> getConfigMap() async {
|
|
try {
|
|
final preferences = await _store.value;
|
|
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,
|
|
);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
Future<Map<String, Object?>?> getClashConfigMap() async {
|
|
try {
|
|
final preferences = await _store.value;
|
|
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,
|
|
);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
Future<void> clearClashConfig() async {
|
|
try {
|
|
final preferences = await _store.value;
|
|
await preferences?.remove(clashConfigKey);
|
|
return;
|
|
} catch (e) {
|
|
commonPrint.log(
|
|
'clearClashConfig error ${e.toString()}',
|
|
logLevel: LogLevel.warning,
|
|
);
|
|
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;
|
|
return preferences?.setString(configKey, json.encode(config)) ?? false;
|
|
}
|
|
|
|
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));
|
|
}
|
|
|
|
Future<void> clearPreferences() async {
|
|
final sharedPreferencesIns = await _store.value;
|
|
await sharedPreferencesIns?.clear();
|
|
}
|
|
}
|
|
|
|
final preferences = Preferences();
|