From f62dd3151a98f5c3960c53ee29b01bb7acb8deb3 Mon Sep 17 00:00:00 2001 From: Angelo Verlain Date: Thu, 23 Mar 2023 11:47:27 +0200 Subject: [PATCH] restore saved notes exactly as they were if there were no open notes when you closed the app, the most recently edited note will be opened when you open the app again --- src/application.ts | 50 ++++++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/src/application.ts b/src/application.ts index 08e1b3c..a29d7bb 100644 --- a/src/application.ts +++ b/src/application.ts @@ -60,27 +60,26 @@ export class Application extends Adw.Application { this.init_actions(); - load_notes() - .then((notes) => { - notes.forEach((note) => { - note.connect("notify::modified", (_, x) => { - save_note(note); - }); - - // changing open doesn't change modified - note.connect("notify::open", () => { - save_note(note); - }); - - this.notes_list.append(note); - - if (note.open) { - this.show_note(note.uuid); - } + try { + const notes = load_notes(); + + notes.forEach((note) => { + note.connect("notify::modified", (_, x) => { + save_note(note); + }); + + // changing open doesn't change modified + note.connect("notify::open", () => { + save_note(note); }); - this.sort_notes(); - }).catch(console.log); + this.notes_list.append(note); + }); + + this.sort_notes(); + } catch (error) { + console.error(error as any); + } } save() { @@ -108,14 +107,21 @@ export class Application extends Adw.Application { this.foreach_note((note) => { if (note.open) { - has_one_open = true; + if (has_one_open == false) has_one_open = true; this.show_note(note.uuid); } }); if (!has_one_open) { - settings.set_boolean("show-all-notes", true); - this.all_notes(); + const last_open_note = this.notes_array() + .sort((a, b) => b.modified.compare(a.modified))[0]; + + if (has_one_open) { + this.show_note(last_open_note.uuid); + } else { + settings.set_boolean("show-all-notes", true); + this.all_notes(); + } } }