diff --git a/lib/api/preset/image.dart b/lib/api/preset/image.dart index 3af30df..d910bb7 100644 --- a/lib/api/preset/image.dart +++ b/lib/api/preset/image.dart @@ -3,7 +3,7 @@ part of preset; // presets are essentially a format that represents a BooruImage before it gets added class PresetImage extends VirtualPreset{ - PresetImage({this.image, this.tags, this.sources, this.replaceID, this.rating, this.relatedImages, super.key}); + PresetImage({this.image, this.tags, this.sources, this.replaceID, this.rating, this.relatedImages, this.note, super.key}); File? image; Map>? tags; @@ -11,6 +11,7 @@ class PresetImage extends VirtualPreset{ Rating? rating; ImageID? replaceID; List? relatedImages; + String? note; static Future fromExistingImage(BooruImage image) async { final Booru booru = await getCurrentBooru(); @@ -21,7 +22,8 @@ class PresetImage extends VirtualPreset{ tags: await booru.separateTagsByType(image.tags.split(" ")), rating: image.rating, replaceID: image.id, - relatedImages: image.relatedImages + relatedImages: image.relatedImages, + note: image.note ); } diff --git a/lib/api/writable.dart b/lib/api/writable.dart index 68f56dd..9fd6c84 100644 --- a/lib/api/writable.dart +++ b/lib/api/writable.dart @@ -100,7 +100,8 @@ Future insertImage(PresetImage preset) async { "tags": tagSet.join(" "), if(ratingString != null) "rating": ratingString, "sources": preset.sources ?? [], - "related": preset.relatedImages ?? [] + "related": preset.relatedImages ?? [], + if(preset.note != null && preset.note!.isNotEmpty) "note": preset.note }; // find if inputed id already exists, and if no add to its latest index, otherwise replace element on that id @@ -115,23 +116,6 @@ Future insertImage(PresetImage preset) async { return (await booru.getImage(id))!; } -Future editNote(String id, String? note) async { - final Booru booru = await getCurrentBooru(); - - // add to json - Map raw = await booru.getRawInfo(); - List files = raw["files"]; - - int index = files.indexWhere((e) => e["id"] == id); - if (index < 0) throw "Does not exist"; - else { - if(note == null || note.isEmpty) raw["files"][index].remove("note"); - else raw["files"][index]["note"] = note; - } - - await writeSettings(booru.path, raw); -} - Future writeSpecificTags(Map> specificTags) async { final Booru booru = await getCurrentBooru(); diff --git a/lib/views/image_manager/form.dart b/lib/views/image_manager/form.dart index 009ef23..5e1abcb 100644 --- a/lib/views/image_manager/form.dart +++ b/lib/views/image_manager/form.dart @@ -99,7 +99,8 @@ class _ImageManagerFormState extends State { rating: rating, replaceID: widget.preset?.replaceID, relatedImages: relatedImages, - key: widget.preset?.key + key: widget.preset?.key, + note: widget.preset?.note )); if(widget.onErrorUpdate != null) widget.onErrorUpdate!(!validation); } diff --git a/lib/views/navigation/image_view.dart b/lib/views/navigation/image_view.dart index a75506f..a9cf7c7 100644 --- a/lib/views/navigation/image_view.dart +++ b/lib/views/navigation/image_view.dart @@ -449,6 +449,7 @@ class NotesView extends StatefulWidget { class _NotesViewState extends State { final controller = TextEditingController(); Timer? _debounce; + late BooruImage image; @override void initState() { @@ -458,8 +459,8 @@ class _NotesViewState extends State { void setText() async { final booru = await getCurrentBooru(); - final image = await booru.getImage(widget.id.toString()); - controller.text = image!.note ?? ""; + image = (await booru.getImage(widget.id.toString()))!; + controller.text = image.note ?? ""; } @override @@ -481,9 +482,10 @@ class _NotesViewState extends State { ), onChanged: (value) { if (_debounce?.isActive ?? false) _debounce?.cancel(); - _debounce = Timer(const Duration(seconds: 1), () { - debugPrint("debounced"); - editNote(widget.id.toString(), value); + _debounce = Timer(const Duration(seconds: 1), () async { + final preset = await PresetImage.fromExistingImage(image); + preset.note = value; + await insertImage(preset); }); }, ),