nc-photos/app/lib/entity/pref/provider/memory.dart

55 lines
1.5 KiB
Dart
Raw Normal View History

2023-07-17 09:35:45 +02:00
import 'package:nc_photos/entity/pref.dart';
2023-07-03 19:23:42 +02:00
import 'package:np_common/type.dart';
2023-07-17 09:35:45 +02:00
/// [Pref] stored in memory, useful in unit tests
class PrefMemoryProvider extends PrefProvider {
PrefMemoryProvider([
Map<String, dynamic> initialData = const <String, dynamic>{},
]) : _data = Map.of(initialData);
2023-07-03 19:23:42 +02:00
factory PrefMemoryProvider.fromJson(JsonObj json) => PrefMemoryProvider(json);
2023-07-17 09:35:45 +02:00
@override
2023-07-17 10:23:57 +02:00
bool? getBool(PrefKeyInterface key) => _get<bool>(key);
2023-07-17 09:35:45 +02:00
@override
2023-07-17 10:23:57 +02:00
Future<bool> setBool(PrefKeyInterface key, bool value) => _set(key, value);
2023-07-17 09:35:45 +02:00
@override
2023-07-17 10:23:57 +02:00
int? getInt(PrefKeyInterface key) => _get<int>(key);
2023-07-17 09:35:45 +02:00
@override
2023-07-17 10:23:57 +02:00
Future<bool> setInt(PrefKeyInterface key, int value) => _set(key, value);
2023-07-17 09:35:45 +02:00
@override
2023-07-17 10:23:57 +02:00
String? getString(PrefKeyInterface key) => _get<String>(key);
2023-07-17 09:35:45 +02:00
@override
2023-07-17 10:23:57 +02:00
Future<bool> setString(PrefKeyInterface key, String value) =>
_set(key, value);
2023-07-17 09:35:45 +02:00
@override
2023-07-17 10:23:57 +02:00
List<String>? getStringList(PrefKeyInterface key) => _get<List<String>>(key);
2023-07-17 09:35:45 +02:00
@override
2023-07-17 10:23:57 +02:00
Future<bool> setStringList(PrefKeyInterface key, List<String> value) =>
2023-07-17 09:35:45 +02:00
_set(key, value);
@override
2023-07-17 10:23:57 +02:00
Future<bool> remove(PrefKeyInterface key) async {
_data.remove(key.toStringKey());
2023-07-17 09:35:45 +02:00
return true;
}
@override
Future<bool> clear() async {
_data.clear();
return true;
}
2023-07-17 10:23:57 +02:00
T? _get<T>(PrefKeyInterface key) => _data[key.toStringKey()];
2023-07-17 09:35:45 +02:00
2023-07-17 10:23:57 +02:00
Future<bool> _set<T>(PrefKeyInterface key, T value) async {
2023-07-17 09:35:45 +02:00
_data[key.toStringKey()] = value;
return true;
}
final Map<String, dynamic> _data;
}