From 84fab89f48c7c90cd3e189f4f7244bae8b970409 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Mon, 13 Jul 2020 17:13:02 +0200 Subject: [PATCH] Do not allow us to edit the Note Editor if the Note cannot be saved In some rare cases the note just doesn't get saved on disk - in those cases instead of silently failing we will refuse to exit the note editor, forcing the user to copy the note and paste it somewhere else in order to not lose the date. This isn't the best solution, but it prevents data loss, in a few edge cases which I cannot reproduce. Fixes #124 --- assets/langs/en.yaml | 1 + lib/screens/note_editor.dart | 28 +++++++++++++++++++++------- lib/state_container.dart | 3 ++- lib/utils.dart | 9 +++++++++ 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/assets/langs/en.yaml b/assets/langs/en.yaml index 30abd88a..a39f24fa 100644 --- a/assets/langs/en.yaml +++ b/assets/langs/en.yaml @@ -62,6 +62,7 @@ editors: addImage: Add Image from Gallery editFileName: Edit File Name tags: Edit Tags + saveNoteFailed: Failed to Save Note pro: Pro actions: newNote: New Note diff --git a/lib/screens/note_editor.dart b/lib/screens/note_editor.dart index cb86f07f..196b0c02 100644 --- a/lib/screens/note_editor.dart +++ b/lib/screens/note_editor.dart @@ -1,5 +1,6 @@ import 'dart:io'; +import 'package:easy_localization/easy_localization.dart'; import 'package:flutter/material.dart'; import 'package:collection/collection.dart'; @@ -12,6 +13,7 @@ import 'package:gitjournal/editors/raw_editor.dart'; import 'package:gitjournal/editors/checklist_editor.dart'; import 'package:gitjournal/error_reporting.dart'; import 'package:gitjournal/state_container.dart'; +import 'package:gitjournal/utils.dart'; import 'package:gitjournal/utils/logger.dart'; import 'package:gitjournal/widgets/folder_selection_dialog.dart'; import 'package:gitjournal/widgets/note_editor_selector.dart'; @@ -134,8 +136,8 @@ class NoteEditorState extends State { Widget build(BuildContext context) { return WillPopScope( onWillPop: () async { - _saveNote(_getNoteFromEditor()); - return true; + var savedNote = await _saveNote(_getNoteFromEditor()); + return savedNote; }, child: _getEditor(), ); @@ -219,9 +221,11 @@ class NoteEditorState extends State { } } - void _exitEditorSelected(Note note) { - _saveNote(note); - Navigator.pop(context); + void _exitEditorSelected(Note note) async { + var saved = await _saveNote(note); + if (saved) { + Navigator.pop(context); + } } void _renameNoteSelected(Note _note) async { @@ -315,8 +319,9 @@ class NoteEditorState extends State { return false; } - void _saveNote(Note note) async { - if (!_noteModified(note)) return; + // Returns bool indicating if the note was successfully saved + Future _saveNote(Note note) async { + if (!_noteModified(note)) return true; Log.d("Note modified - saving"); try { @@ -326,7 +331,16 @@ class NoteEditorState extends State { : await stateContainer.updateNote(note); } catch (e, stackTrace) { logException(e, stackTrace); + + await showAlertDialog( + context, + tr("editors.common.saveNoteFailed"), + e.toString(), + ); + return false; } + + return true; } Note _getNoteFromEditor() { diff --git a/lib/state_container.dart b/lib/state_container.dart index de638cbf..c2fab299 100644 --- a/lib/state_container.dart +++ b/lib/state_container.dart @@ -259,10 +259,11 @@ class StateContainer with ChangeNotifier { Future addNote(Note note) async { logEvent(Event.NoteAdded); - note.parent.add(note); note.updateModified(); await note.save(); + note.parent.add(note); + return _opLock.synchronized(() async { Log.d("Got addNote lock"); diff --git a/lib/utils.dart b/lib/utils.dart index 5938c53a..fbe49873 100644 --- a/lib/utils.dart +++ b/lib/utils.dart @@ -58,3 +58,12 @@ NotesFolderFS getFolderForEditor( return rootFolder.getFolderWithSpec(spec); } } + +Future showAlertDialog( + BuildContext context, String title, String message) async { + var dialog = AlertDialog( + title: Text(title), + content: Text(message), + ); + return showDialog(context: context, builder: (context) => dialog); +}