mirror of
https://github.com/lollipopkit/flutter_server_box.git
synced 2025-12-17 07:14:28 +01:00
76 lines
1.8 KiB
Dart
76 lines
1.8 KiB
Dart
import 'package:fl_lib/fl_lib.dart';
|
|
import 'package:server_box/data/model/server/snippet.dart';
|
|
import 'package:server_box/data/res/store.dart';
|
|
|
|
class SnippetProvider extends Provider {
|
|
const SnippetProvider._();
|
|
static const instance = SnippetProvider._();
|
|
|
|
static final snippets = <Snippet>[].vn;
|
|
static final tags = <String>{}.vn;
|
|
|
|
@override
|
|
void load() {
|
|
super.load();
|
|
final snippets_ = Stores.snippet.fetch();
|
|
final order = Stores.setting.snippetOrder.fetch();
|
|
if (order.isNotEmpty) {
|
|
final surplus = snippets_.reorder(
|
|
order: order,
|
|
finder: (n, name) => n.name == name,
|
|
);
|
|
order.removeWhere((e) => surplus.any((ele) => ele == e));
|
|
if (order != Stores.setting.snippetOrder.fetch()) {
|
|
Stores.setting.snippetOrder.put(order);
|
|
}
|
|
}
|
|
snippets.value = snippets_;
|
|
_updateTags();
|
|
}
|
|
|
|
static void _updateTags() {
|
|
final tags_ = <String>{};
|
|
for (final s in snippets.value) {
|
|
final t = s.tags;
|
|
if (t != null) {
|
|
tags_.addAll(t);
|
|
}
|
|
}
|
|
tags.value = tags_;
|
|
}
|
|
|
|
static void add(Snippet snippet) {
|
|
snippets.value.add(snippet);
|
|
snippets.notify();
|
|
Stores.snippet.put(snippet);
|
|
_updateTags();
|
|
}
|
|
|
|
static void del(Snippet snippet) {
|
|
snippets.value.remove(snippet);
|
|
snippets.notify();
|
|
Stores.snippet.delete(snippet);
|
|
_updateTags();
|
|
}
|
|
|
|
static void update(Snippet old, Snippet newOne) {
|
|
snippets.value.remove(old);
|
|
snippets.value.add(newOne);
|
|
snippets.notify();
|
|
Stores.snippet.delete(old);
|
|
Stores.snippet.put(newOne);
|
|
_updateTags();
|
|
}
|
|
|
|
static void renameTag(String old, String newOne) {
|
|
for (final s in snippets.value) {
|
|
if (s.tags?.contains(old) ?? false) {
|
|
s.tags?.remove(old);
|
|
s.tags?.add(newOne);
|
|
Stores.snippet.put(s);
|
|
}
|
|
}
|
|
_updateTags();
|
|
}
|
|
}
|