mirror of
https://github.com/lollipopkit/flutter_server_box.git
synced 2025-12-17 07:14:28 +01:00
Init snippet page and store
This commit is contained in:
33
lib/data/store/snippet.dart
Normal file
33
lib/data/store/snippet.dart
Normal file
@@ -0,0 +1,33 @@
|
||||
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));
|
||||
}
|
||||
|
||||
List<Snippet> fetch() {
|
||||
return getSnippetList(
|
||||
json.decode(box.get('snippet', defaultValue: '[]')!));
|
||||
}
|
||||
|
||||
void delete(Snippet s) {
|
||||
final ss = fetch();
|
||||
ss.removeAt(index(s));
|
||||
box.put('snippet', json.encode(ss));
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user