|
|
|
@ -15,22 +15,17 @@ abstract interface class SettingsStorage {
|
|
|
|
|
Future<bool> remove(final String key); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@immutable |
|
|
|
|
@internal |
|
|
|
|
class AppStorage implements SettingsStorage { |
|
|
|
|
const AppStorage(this._id); |
|
|
|
|
|
|
|
|
|
final String _id; |
|
|
|
|
|
|
|
|
|
final class NeonStorage { |
|
|
|
|
/// Shared preferences instance. |
|
|
|
|
/// |
|
|
|
|
/// Use [reqireDatabase] to access it. |
|
|
|
|
/// Use [database] 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]. |
|
|
|
|
/// Required to be called before accessing [database]. |
|
|
|
|
static Future init() async { |
|
|
|
|
if (_sharedPreferences != null) { |
|
|
|
|
return; |
|
|
|
@ -40,37 +35,45 @@ class AppStorage implements SettingsStorage {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@visibleForTesting |
|
|
|
|
static SharedPreferences get reqireDatabase { |
|
|
|
|
static SharedPreferences get database { |
|
|
|
|
if (_sharedPreferences == null) { |
|
|
|
|
throw StateError( |
|
|
|
|
'AppStorage has not been initialized yet. Please make sure AppStorage.init() has been called before and completed.', |
|
|
|
|
'NeonStorage has not been initialized yet. Please make sure NeonStorage.init() has been called before and completed.', |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return _sharedPreferences!; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@immutable |
|
|
|
|
@internal |
|
|
|
|
class AppStorage implements SettingsStorage { |
|
|
|
|
const AppStorage(this._id); |
|
|
|
|
|
|
|
|
|
final String _id; |
|
|
|
|
|
|
|
|
|
String _formatKey(final String key) => '$_id-$key'; |
|
|
|
|
|
|
|
|
|
bool containsKey(final String key) => reqireDatabase.containsKey(_formatKey(key)); |
|
|
|
|
bool containsKey(final String key) => NeonStorage.database.containsKey(_formatKey(key)); |
|
|
|
|
|
|
|
|
|
@override |
|
|
|
|
Future<bool> remove(final String key) => reqireDatabase.remove(_formatKey(key)); |
|
|
|
|
Future<bool> remove(final String key) => NeonStorage.database.remove(_formatKey(key)); |
|
|
|
|
|
|
|
|
|
@override |
|
|
|
|
String? getString(final String key) => reqireDatabase.getString(_formatKey(key)); |
|
|
|
|
String? getString(final String key) => NeonStorage.database.getString(_formatKey(key)); |
|
|
|
|
|
|
|
|
|
@override |
|
|
|
|
Future setString(final String key, final String value) => reqireDatabase.setString(_formatKey(key), value); |
|
|
|
|
Future setString(final String key, final String value) => NeonStorage.database.setString(_formatKey(key), value); |
|
|
|
|
|
|
|
|
|
@override |
|
|
|
|
bool? getBool(final String key) => reqireDatabase.getBool(_formatKey(key)); |
|
|
|
|
bool? getBool(final String key) => NeonStorage.database.getBool(_formatKey(key)); |
|
|
|
|
|
|
|
|
|
@override |
|
|
|
|
Future setBool(final String key, final bool value) => reqireDatabase.setBool(_formatKey(key), value); |
|
|
|
|
Future setBool(final String key, final bool value) => NeonStorage.database.setBool(_formatKey(key), value); |
|
|
|
|
|
|
|
|
|
List<String>? getStringList(final String key) => reqireDatabase.getStringList(_formatKey(key)); |
|
|
|
|
List<String>? getStringList(final String key) => NeonStorage.database.getStringList(_formatKey(key)); |
|
|
|
|
|
|
|
|
|
Future setStringList(final String key, final List<String> value) => |
|
|
|
|
reqireDatabase.setStringList(_formatKey(key), value); |
|
|
|
|
NeonStorage.database.setStringList(_formatKey(key), value); |
|
|
|
|
} |
|
|
|
|