mirror of
https://github.com/lollipopkit/flutter_server_box.git
synced 2025-12-17 15:24:35 +01:00
#54 snippet group
This commit is contained in:
@@ -5,25 +5,44 @@ import 'package:toolbox/data/model/server/snippet.dart';
|
||||
import 'package:toolbox/data/store/snippet.dart';
|
||||
import 'package:toolbox/locator.dart';
|
||||
|
||||
import '../../core/extension/order.dart';
|
||||
|
||||
class SnippetProvider extends BusyProvider {
|
||||
List<Snippet> get snippets => _snippets;
|
||||
late Order<Snippet> _snippets;
|
||||
Order<Snippet> get snippets => _snippets;
|
||||
|
||||
final _tags = <String>[];
|
||||
List<String> get tags => _tags;
|
||||
|
||||
final _store = locator<SnippetStore>();
|
||||
late List<Snippet> _snippets;
|
||||
|
||||
void loadData() {
|
||||
_snippets = _store.fetch();
|
||||
}
|
||||
|
||||
void _updateTags() {
|
||||
_tags.clear();
|
||||
for (final s in _snippets) {
|
||||
if (s.tags?.isEmpty ?? true) {
|
||||
continue;
|
||||
}
|
||||
_tags.addAll(s.tags!);
|
||||
}
|
||||
_tags.toSet().toList();
|
||||
}
|
||||
|
||||
void add(Snippet snippet) {
|
||||
_snippets.add(snippet);
|
||||
_store.put(snippet);
|
||||
notifyListeners();
|
||||
_updateTags();
|
||||
}
|
||||
|
||||
void del(Snippet snippet) {
|
||||
_snippets.remove(snippet);
|
||||
_store.delete(snippet);
|
||||
notifyListeners();
|
||||
_updateTags();
|
||||
}
|
||||
|
||||
void update(Snippet old, Snippet newOne) {
|
||||
@@ -32,6 +51,21 @@ class SnippetProvider extends BusyProvider {
|
||||
_snippets.remove(old);
|
||||
_snippets.add(newOne);
|
||||
notifyListeners();
|
||||
_updateTags();
|
||||
}
|
||||
|
||||
void renameTag(String old, String newOne) {
|
||||
for (final s in _snippets) {
|
||||
if (s.tags?.contains(old) ?? false) {
|
||||
s.tags?.remove(old);
|
||||
s.tags?.add(newOne);
|
||||
}
|
||||
}
|
||||
for (final s in _snippets) {
|
||||
_store.put(s);
|
||||
}
|
||||
notifyListeners();
|
||||
_updateTags();
|
||||
}
|
||||
|
||||
String get export => json.encode(snippets);
|
||||
|
||||
Reference in New Issue
Block a user