Skip to content

Commit

Permalink
notes: rework how notes are added to use the preset api (fix #23)
Browse files Browse the repository at this point in the history
  • Loading branch information
resucutie committed Sep 1, 2024
1 parent 544c852 commit 1f1a46f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 26 deletions.
6 changes: 4 additions & 2 deletions lib/api/preset/image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ 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<String, List<String>>? tags;
List<String>? sources;
Rating? rating;
ImageID? replaceID;
List<ImageID>? relatedImages;
String? note;

static Future<PresetImage> fromExistingImage(BooruImage image) async {
final Booru booru = await getCurrentBooru();
Expand All @@ -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
);
}

Expand Down
20 changes: 2 additions & 18 deletions lib/api/writable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ Future<BooruImage> 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
Expand All @@ -115,23 +116,6 @@ Future<BooruImage> insertImage(PresetImage preset) async {
return (await booru.getImage(id))!;
}

Future<void> 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<void> writeSpecificTags(Map<String, List<String>> specificTags) async {
final Booru booru = await getCurrentBooru();

Expand Down
3 changes: 2 additions & 1 deletion lib/views/image_manager/form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ class _ImageManagerFormState extends State<ImageManagerForm> {
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);
}
Expand Down
12 changes: 7 additions & 5 deletions lib/views/navigation/image_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ class NotesView extends StatefulWidget {
class _NotesViewState extends State<NotesView> {
final controller = TextEditingController();
Timer? _debounce;
late BooruImage image;

@override
void initState() {
Expand All @@ -458,8 +459,8 @@ class _NotesViewState extends State<NotesView> {

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
Expand All @@ -481,9 +482,10 @@ class _NotesViewState extends State<NotesView> {
),
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);
});
},
),
Expand Down

0 comments on commit 1f1a46f

Please sign in to comment.