70 lines
2.1 KiB
Dart
70 lines
2.1 KiB
Dart
import 'dart:async';
|
|
import 'dart:convert';
|
|
|
|
import 'package:fl_clash/models/models.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
import 'constant.dart';
|
|
|
|
class Preferences {
|
|
static Preferences? _instance;
|
|
Completer<SharedPreferences?> sharedPreferencesCompleter = Completer();
|
|
|
|
Future<bool> get isInit async =>
|
|
await sharedPreferencesCompleter.future != null;
|
|
|
|
Preferences._internal() {
|
|
SharedPreferences.getInstance()
|
|
.then((value) => sharedPreferencesCompleter.complete(value))
|
|
.onError((_, _) => sharedPreferencesCompleter.complete(null));
|
|
}
|
|
|
|
factory Preferences() {
|
|
_instance ??= Preferences._internal();
|
|
return _instance!;
|
|
}
|
|
|
|
Future<int> getVersion() async {
|
|
final preferences = await sharedPreferencesCompleter.future;
|
|
return preferences?.getInt('version') ?? 0;
|
|
}
|
|
|
|
Future<void> setVersion(int version) async {
|
|
final preferences = await sharedPreferencesCompleter.future;
|
|
await preferences?.setInt('version', version);
|
|
}
|
|
|
|
Future<void> saveShareState(SharedState shareState) async {
|
|
final preferences = await sharedPreferencesCompleter.future;
|
|
await preferences?.setString('sharedState', json.encode(shareState));
|
|
}
|
|
|
|
Future<Map<String, Object?>?> getConfigMap() async {
|
|
final preferences = await sharedPreferencesCompleter.future;
|
|
final configString = preferences?.getString(configKey);
|
|
if (configString == null) return null;
|
|
final Map<String, Object?>? configMap = json.decode(configString);
|
|
return configMap;
|
|
}
|
|
|
|
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 sharedPreferencesCompleter.future;
|
|
return preferences?.setString(configKey, json.encode(config)) ?? false;
|
|
}
|
|
|
|
Future<void> clearPreferences() async {
|
|
final sharedPreferencesIns = await sharedPreferencesCompleter.future;
|
|
await sharedPreferencesIns?.clear();
|
|
}
|
|
}
|
|
|
|
final preferences = Preferences();
|