From eac7ff3c638296890e63dcc101b19f3bdb509a7d Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Thu, 22 Oct 2020 15:10:44 +0200 Subject: [PATCH] Auto save the note when the App loses focus --- changelog.yml | 1 + lib/screens/note_editor.dart | 22 +++++++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/changelog.yml b/changelog.yml index 68c0fb04..e3691a4e 100644 --- a/changelog.yml +++ b/changelog.yml @@ -3,6 +3,7 @@ features: - text: "Allow the Git Repo to be accessible by other apps. You can now edit the notes with any other editor in Android, and additionally sync with any other mechanism as the folder is in a public location - #99" improvements: + - text: "Auto save the Note when the App loses focus. The changes are not committed, but this way you never lose your notes" - text: "Allow GitJournal to be visible in the iOS Files app" - text: "Allow syncing via iCloud" - text: "Add Shortcuts support in iOS - #262" diff --git a/lib/screens/note_editor.dart b/lib/screens/note_editor.dart index 7be40a8d..32c75589 100644 --- a/lib/screens/note_editor.dart +++ b/lib/screens/note_editor.dart @@ -76,7 +76,7 @@ class NoteEditor extends StatefulWidget { enum EditorType { Markdown, Raw, Checklist, Journal } -class NoteEditorState extends State { +class NoteEditorState extends State with WidgetsBindingObserver { Note note; EditorType editorType = EditorType.Markdown; MdYamlDoc originalNoteData = MdYamlDoc(); @@ -120,6 +120,8 @@ class NoteEditorState extends State { @override void initState() { super.initState(); + WidgetsBinding.instance.addObserver(this); + if (widget.defaultEditorType != null) { editorType = widget.defaultEditorType; } else { @@ -143,6 +145,24 @@ class NoteEditorState extends State { } } + @override + void dispose() { + WidgetsBinding.instance.removeObserver(this); + super.dispose(); + } + + @override + void didChangeAppLifecycleState(AppLifecycleState state) { + Log.i("Note Edit State: $state"); + if (state != AppLifecycleState.resumed) { + var note = _getNoteFromEditor(); + if (!_noteModified(note)) return; + + Log.d("App Lost Focus - saving note"); + note.save(); + } + } + @override Widget build(BuildContext context) { return WillPopScope(