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
This commit is contained in:
Vishesh Handa
2020-07-13 17:13:02 +02:00
parent 885edbffc1
commit 84fab89f48
4 changed files with 33 additions and 8 deletions

View File

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

View File

@ -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<NoteEditor> {
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<NoteEditor> {
}
}
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<NoteEditor> {
return false;
}
void _saveNote(Note note) async {
if (!_noteModified(note)) return;
// Returns bool indicating if the note was successfully saved
Future<bool> _saveNote(Note note) async {
if (!_noteModified(note)) return true;
Log.d("Note modified - saving");
try {
@ -326,7 +331,16 @@ class NoteEditorState extends State<NoteEditor> {
: 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() {

View File

@ -259,10 +259,11 @@ class StateContainer with ChangeNotifier {
Future<void> 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");

View File

@ -58,3 +58,12 @@ NotesFolderFS getFolderForEditor(
return rootFolder.getFolderWithSpec(spec);
}
}
Future<void> showAlertDialog(
BuildContext context, String title, String message) async {
var dialog = AlertDialog(
title: Text(title),
content: Text(message),
);
return showDialog(context: context, builder: (context) => dialog);
}