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();
}

View File

@@ -2,9 +2,9 @@
class BuildData {
static const String name = "ServerBox";
static const int build = 724;
static const int build = 725;
static const String engine = "3.16.8";
static const String buildAt = "2024-01-26 21:05:29";
static const int modifications = 2;
static const String buildAt = "2024-01-27 21:27:49";
static const int modifications = 4;
static const int script = 36;
}

View File

@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:toolbox/core/persistant_store.dart';
import 'package:toolbox/core/utils/platform/base.dart';
import 'package:toolbox/data/model/app/menu/server_func.dart';
import 'package:toolbox/data/model/ssh/virtual_key.dart';
import '../model/app/net_view.dart';
import '../res/default.dart';
@@ -141,6 +142,8 @@ class SettingStore extends PersistentStore {
late final sshVirtKeys = listProperty(
'sshVirtKeys',
Defaults.sshVirtKeys,
encoder: (val) => val.index,
decoder: (val) => VirtKey.values[val],
);
late final netViewType = property(
@@ -209,14 +212,19 @@ class SettingStore extends PersistentStore {
/// Whether collapse UI items by default
late final collapseUIDefault = property('collapseUIDefault', true);
late final serverFuncBtns = listProperty<ServerFuncBtn>('serverBtns', [
ServerFuncBtn.terminal,
ServerFuncBtn.sftp,
ServerFuncBtn.container,
ServerFuncBtn.process,
ServerFuncBtn.pkg,
ServerFuncBtn.snippet,
]);
late final serverFuncBtns = listProperty<ServerFuncBtn>(
'serverBtns',
[
ServerFuncBtn.terminal,
ServerFuncBtn.sftp,
ServerFuncBtn.container,
ServerFuncBtn.process,
ServerFuncBtn.pkg,
ServerFuncBtn.snippet,
],
encoder: (val) => val.index,
decoder: (val) => ServerFuncBtn.values[val],
);
// Never show these settings for users
//