|
|
|
@ -1,3 +1,4 @@
|
|
|
|
|
import 'package:meta/meta.dart'; |
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart'; |
|
|
|
|
|
|
|
|
|
abstract interface class SettingsStorage { |
|
|
|
@ -14,35 +15,55 @@ abstract interface class SettingsStorage {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
class AppStorage implements SettingsStorage { |
|
|
|
|
AppStorage( |
|
|
|
|
this._id, |
|
|
|
|
this._sharedPreferences, |
|
|
|
|
); |
|
|
|
|
AppStorage(this._id); |
|
|
|
|
|
|
|
|
|
final String _id; |
|
|
|
|
final SharedPreferences _sharedPreferences; |
|
|
|
|
|
|
|
|
|
/// Shared preferences instance. |
|
|
|
|
/// |
|
|
|
|
/// Use [reqireDatabase] to access it. |
|
|
|
|
/// Make sure it has been initialized wiht [init] before. |
|
|
|
|
static SharedPreferences? _sharedPreferences; |
|
|
|
|
|
|
|
|
|
/// Sets up the [SharedPreferences] instance. |
|
|
|
|
/// |
|
|
|
|
/// Required to be called before accessing [reqireDatabase]. |
|
|
|
|
static Future init() async { |
|
|
|
|
_sharedPreferences = await SharedPreferences.getInstance(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@visibleForTesting |
|
|
|
|
static SharedPreferences get reqireDatabase { |
|
|
|
|
if (_sharedPreferences == null) { |
|
|
|
|
throw StateError( |
|
|
|
|
'AppStorage has not been initialized yet. Please make sure AppStorage.init() has been called before and completed.', |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return _sharedPreferences!; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
String _formatKey(final String key) => '$_id-$key'; |
|
|
|
|
|
|
|
|
|
bool containsKey(final String key) => _sharedPreferences.containsKey(_formatKey(key)); |
|
|
|
|
bool containsKey(final String key) => reqireDatabase.containsKey(_formatKey(key)); |
|
|
|
|
|
|
|
|
|
@override |
|
|
|
|
Future<bool> remove(final String key) => _sharedPreferences.remove(_formatKey(key)); |
|
|
|
|
Future<bool> remove(final String key) => reqireDatabase.remove(_formatKey(key)); |
|
|
|
|
|
|
|
|
|
@override |
|
|
|
|
String? getString(final String key) => _sharedPreferences.getString(_formatKey(key)); |
|
|
|
|
String? getString(final String key) => reqireDatabase.getString(_formatKey(key)); |
|
|
|
|
|
|
|
|
|
@override |
|
|
|
|
Future setString(final String key, final String value) => _sharedPreferences.setString(_formatKey(key), value); |
|
|
|
|
Future setString(final String key, final String value) => reqireDatabase.setString(_formatKey(key), value); |
|
|
|
|
|
|
|
|
|
@override |
|
|
|
|
bool? getBool(final String key) => _sharedPreferences.getBool(_formatKey(key)); |
|
|
|
|
bool? getBool(final String key) => reqireDatabase.getBool(_formatKey(key)); |
|
|
|
|
|
|
|
|
|
@override |
|
|
|
|
Future setBool(final String key, final bool value) => _sharedPreferences.setBool(_formatKey(key), value); |
|
|
|
|
Future setBool(final String key, final bool value) => reqireDatabase.setBool(_formatKey(key), value); |
|
|
|
|
|
|
|
|
|
List<String>? getStringList(final String key) => _sharedPreferences.getStringList(_formatKey(key)); |
|
|
|
|
List<String>? getStringList(final String key) => reqireDatabase.getStringList(_formatKey(key)); |
|
|
|
|
|
|
|
|
|
Future setStringList(final String key, final List<String> value) => |
|
|
|
|
_sharedPreferences.setStringList(_formatKey(key), value); |
|
|
|
|
reqireDatabase.setStringList(_formatKey(key), value); |
|
|
|
|
} |
|
|
|
|