使用 Hive Object

This commit is contained in:
Junyuan Feng
2022-11-05 23:08:56 +08:00
parent c036b78708
commit 398c49bb99
20 changed files with 504 additions and 102 deletions

View File

@@ -1,32 +1,24 @@
import 'dart:convert';
import 'package:toolbox/core/persistant_store.dart';
import 'package:toolbox/data/model/server/snippet.dart';
class SnippetStore extends PersistentStore {
void put(Snippet snippet) {
final ss = fetch();
if (!have(snippet)) ss.add(snippet);
box.put('snippet', json.encode(ss));
box.put(snippet.name, snippet);
}
List<Snippet> fetch() {
return getSnippetList(json.decode(box.get('snippet', defaultValue: '[]')!));
final keys = box.keys;
final ss = <Snippet>[];
for (final key in keys) {
final s = box.get(key);
if (s != null) {
ss.add(s);
}
}
return ss;
}
void delete(Snippet s) {
final ss = fetch();
ss.removeAt(index(s));
box.put('snippet', json.encode(ss));
box.delete(s.name);
}
void update(Snippet old, Snippet newInfo) {
final ss = fetch();
ss[index(old)] = newInfo;
box.put('snippet', json.encode(ss));
}
int index(Snippet s) => fetch().indexWhere((e) => e.name == s.name);
bool have(Snippet s) => index(s) != -1;
}