fix: store type

This commit is contained in:
lollipopkit
2024-01-27 23:14:46 +08:00
parent b08265221f
commit 2e11d8827e
7 changed files with 80 additions and 43 deletions

View File

@@ -95,12 +95,16 @@ extension StoreX on PersistentStore {
String key,
List<T> defaultValue, {
bool updateLastModified = true,
T Function(dynamic val)? decoder,
dynamic Function(T val)? encoder,
}) {
return _StoreListProperty<T>(
box,
key,
defaultValue,
updateLastModified: updateLastModified,
encoder: encoder,
decoder: decoder,
);
}
}
@@ -118,12 +122,16 @@ class _StoreProperty<T> implements StorePropertyBase<T> {
this._key,
this.defaultValue, {
this.updateLastModified = true,
this.decoder,
this.encoder,
});
final Box _box;
final String _key;
T defaultValue;
bool updateLastModified;
final T Function(dynamic val)? decoder;
final dynamic Function(T val)? encoder;
@override
ValueListenable<T> listenable() {
@@ -134,6 +142,9 @@ class _StoreProperty<T> implements StorePropertyBase<T> {
T fetch() {
final stored = _box.get(_key);
if (stored == null || stored is! T) {
if (decoder != null) {
return decoder!(stored);
}
return defaultValue;
}
return stored;
@@ -142,6 +153,9 @@ class _StoreProperty<T> implements StorePropertyBase<T> {
@override
Future<void> put(T value) {
if (updateLastModified) _box.updateLastModified();
if (encoder != null) {
return _box.put(_key, encoder!(value));
}
return _box.put(_key, value);
}
@@ -157,12 +171,16 @@ class _StoreListProperty<T> implements StorePropertyBase<List<T>> {
this._key,
this.defaultValue, {
this.updateLastModified = true,
this.decoder,
this.encoder,
});
final Box _box;
final String _key;
List<T> defaultValue;
bool updateLastModified;
final T Function(dynamic val)? decoder;
final dynamic Function(T val)? encoder;
@override
ValueListenable<List<T>> listenable() {
@@ -172,17 +190,20 @@ class _StoreListProperty<T> implements StorePropertyBase<List<T>> {
@override
List<T> fetch() {
final val = _box.get(_key, defaultValue: defaultValue)!;
if (val is! List) {
throw Exception('StoreListProperty("$_key") is: ${val.runtimeType}');
}
return List<T>.from(val);
return decoder == null
? List<T>.from(val)
: val.map((e) => decoder!.call(e)).toList();
}
@override
Future<void> put(List<T> value) {
if (updateLastModified) _box.updateLastModified();
if (encoder != null) {
return _box.put(_key, value.map(encoder!).toList());
}
return _box.put(_key, value);
}

View File

@@ -39,3 +39,12 @@ String pathJoin(String path1, String path2) {
bool isFileUrl(String url) => url.split('/').last.contains('.');
int get timeStamp => DateTime.now().millisecondsSinceEpoch;
bool isBaseType(Object? obj) {
return obj is String ||
obj is int ||
obj is double ||
obj is bool ||
obj is List ||
obj is Map;
}

View File

@@ -190,10 +190,9 @@ abstract final class ICloud {
return;
}
final dlFile = await File(await Paths.bak).readAsString();
final dlBak = await Computer.shared.start(Backup.fromJsonString, dlFile);
await dlBak.restore();
final dlFile = await File(await Paths.bak).readAsString();
final dlBak = await Computer.shared.start(Backup.fromJsonString, dlFile);
await dlBak.restore();
await backup();
}