opt.: store

This commit is contained in:
lollipopkit
2023-08-28 18:07:22 +08:00
parent e20f2d32e8
commit 11c3bf795b
20 changed files with 193 additions and 122 deletions

View File

@@ -7,7 +7,7 @@ extension OrderX<T> on Order<T> {
void move(
int oldIndex,
int newIndex, {
StoreProperty<List<T>>? property,
StorePropertyBase<List<T>>? property,
_OnMove<T>? onMove,
}) {
if (oldIndex == newIndex) return;
@@ -35,7 +35,7 @@ extension OrderX<T> on Order<T> {
List<T> items,
int o,
int n, {
StoreProperty<List<T>>? property,
StorePropertyBase<List<T>>? property,
_OnMove<T>? onMove,
}) {
if (o == n) return;

View File

@@ -10,35 +10,72 @@ class PersistentStore<E> {
box = await Hive.openBox(boxName);
return this;
}
StoreProperty<T> property<T>(String key, {T? defaultValue}) {
return StoreProperty<T>(box, key, defaultValue);
}
}
class StoreProperty<T> {
abstract class StorePropertyBase<T> {
ValueListenable<T> listenable();
T fetch();
Future<void> put(T value);
Future<void> delete();
}
class StoreProperty<T> implements StorePropertyBase<T> {
StoreProperty(this._box, this._key, this.defaultValue);
final Box _box;
final String _key;
T? defaultValue;
T defaultValue;
@override
ValueListenable<T> listenable() {
return PropertyListenable<T>(_box, _key, defaultValue);
}
T? fetch() {
return _box.get(_key, defaultValue: defaultValue);
}
dynamic fetchRaw() {
return _box.get(_key, defaultValue: defaultValue);
@override
T fetch() {
return _box.get(_key, defaultValue: defaultValue)!;
}
@override
Future<void> put(T value) {
return _box.put(_key, value);
}
@override
Future<void> delete() {
return _box.delete(_key);
}
}
class StoreListProperty<T> implements StorePropertyBase<List<T>> {
StoreListProperty(this._box, this._key, this.defaultValue);
final Box _box;
final String _key;
List<T> defaultValue;
@override
ValueListenable<List<T>> listenable() {
return PropertyListenable<List<T>>(_box, _key, defaultValue);
}
@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);
}
@override
Future<void> put(List<T> value) {
return _box.put(_key, value);
}
@override
Future<void> delete() {
return _box.delete(_key);
}

View File

@@ -76,7 +76,7 @@ void showLoadingDialog(BuildContext context, {bool barrierDismiss = false}) {
Widget buildSwitch(
BuildContext context,
StoreProperty<bool> prop, {
StorePropertyBase<bool> prop, {
void Function(bool)? func,
}) {
return ValueListenableBuilder(
@@ -115,8 +115,8 @@ String tabTitleName(BuildContext context, AppTab tab) {
}
}
Future<void> loadFontFile(String? localPath) async {
if (localPath == null) return;
Future<void> loadFontFile(String localPath) async {
if (localPath.isEmpty) return;
final name = getFileName(localPath);
if (name == null) return;
var fontLoader = FontLoader(name);