Auto save the note when the App loses focus

This commit is contained in:
Vishesh Handa
2020-10-22 15:10:44 +02:00
parent f81b2ed167
commit eac7ff3c63
2 changed files with 22 additions and 1 deletions

View File

@ -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"

View File

@ -76,7 +76,7 @@ class NoteEditor extends StatefulWidget {
enum EditorType { Markdown, Raw, Checklist, Journal }
class NoteEditorState extends State<NoteEditor> {
class NoteEditorState extends State<NoteEditor> with WidgetsBindingObserver {
Note note;
EditorType editorType = EditorType.Markdown;
MdYamlDoc originalNoteData = MdYamlDoc();
@ -120,6 +120,8 @@ class NoteEditorState extends State<NoteEditor> {
@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<NoteEditor> {
}
}
@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(