103 lines
3.3 KiB
Dart
103 lines
3.3 KiB
Dart
import 'package:hub/shared/utils/storage_util.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'package:hub/shared/utils/cache_util.dart';
|
|
import 'package:synchronized/synchronized.dart';
|
|
|
|
class SharedPreferencesStorageHelper implements Storage {
|
|
static final SharedPreferencesStorageHelper _instance = SharedPreferencesStorageHelper._internal();
|
|
static final _lock = Lock();
|
|
SharedPreferences? _prefs;
|
|
|
|
factory SharedPreferencesStorageHelper() => _instance;
|
|
|
|
SharedPreferencesStorageHelper._internal();
|
|
|
|
static SharedPreferencesStorageHelper get instance => _instance;
|
|
|
|
bool _isFirstRun = true;
|
|
bool get isFirstRun => _isFirstRun;
|
|
set isFirstRun(bool value) => _setAndCacheBool('first_run', value, (v) => _isFirstRun = v);
|
|
|
|
Future<void> ensureInitialization() async {
|
|
await _lock.synchronized(() async {
|
|
if (_prefs == null) {
|
|
_prefs = await SharedPreferences.getInstance();
|
|
_isFirstRun = _prefs?.getBool('first_run') ?? true;
|
|
}
|
|
});
|
|
}
|
|
|
|
Future<void> _setAndCache<T>(String key, T value, Function(T) cacheSetter, Future<void> Function(String, T) setFunc) async {
|
|
await _lock.synchronized(() async {
|
|
await setFunc(key, value);
|
|
CacheUtil.instance.set(key, value);
|
|
cacheSetter(value);
|
|
});
|
|
}
|
|
|
|
Future<void> _setAndCacheString(String key, String value, Function(String) cacheSetter) async {
|
|
await _setAndCache(key, value, cacheSetter, _prefs!.setString);
|
|
}
|
|
|
|
Future<void> _setAndCacheBool(String key, bool value, Function(bool) cacheSetter) async {
|
|
await _setAndCache(key, value, cacheSetter, _prefs!.setBool);
|
|
}
|
|
|
|
Future<void> _setAndCacheInt(String key, int value, Function(int) cacheSetter) async {
|
|
await _setAndCache(key, value, cacheSetter, _prefs!.setInt);
|
|
}
|
|
|
|
Future<void> _setAndCacheDouble(String key, double value, Function(double) cacheSetter) async {
|
|
await _setAndCache(key, value, cacheSetter, _prefs!.setDouble);
|
|
}
|
|
|
|
Future<void> _setAndCacheStringList(String key, List<String> value, Function(List<String>) cacheSetter) async {
|
|
await _setAndCache(key, value, cacheSetter, _prefs!.setStringList);
|
|
}
|
|
|
|
@override
|
|
Future<void> set(String key, dynamic value) async {
|
|
await _lock.synchronized(() async {
|
|
if (value is bool) {
|
|
await _prefs?.setBool(key, value);
|
|
} else if (value is String) {
|
|
await _prefs?.setString(key, value);
|
|
} else if (value is int) {
|
|
await _prefs?.setInt(key, value);
|
|
} else if (value is double) {
|
|
await _prefs?.setDouble(key, value);
|
|
} else if (value is List<String>) {
|
|
await _prefs?.setStringList(key, value);
|
|
}
|
|
CacheUtil.instance.set(key, value);
|
|
});
|
|
}
|
|
|
|
@override
|
|
Future<dynamic> get(String key) async {
|
|
return await _lock.synchronized(() async {
|
|
var value = CacheUtil.instance.get(key);
|
|
if (value == null) {
|
|
value = _prefs?.get(key);
|
|
CacheUtil.instance.set(key, value);
|
|
}
|
|
return value;
|
|
});
|
|
}
|
|
|
|
@override
|
|
Future<void> delete(String key) async {
|
|
await _lock.synchronized(() async {
|
|
await _prefs?.remove(key);
|
|
CacheUtil.instance.delete(key);
|
|
});
|
|
}
|
|
|
|
Future<void> purge() async {
|
|
await _lock.synchronized(() async {
|
|
await _prefs?.clear();
|
|
CacheUtil.instance.clear();
|
|
await ensureInitialization();
|
|
});
|
|
}
|
|
} |