mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-30 11:33:34 +08:00
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:
@ -62,6 +62,7 @@ editors:
|
|||||||
addImage: Add Image from Gallery
|
addImage: Add Image from Gallery
|
||||||
editFileName: Edit File Name
|
editFileName: Edit File Name
|
||||||
tags: Edit Tags
|
tags: Edit Tags
|
||||||
|
saveNoteFailed: Failed to Save Note
|
||||||
pro: Pro
|
pro: Pro
|
||||||
actions:
|
actions:
|
||||||
newNote: New Note
|
newNote: New Note
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
|
||||||
|
import 'package:easy_localization/easy_localization.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:collection/collection.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/editors/checklist_editor.dart';
|
||||||
import 'package:gitjournal/error_reporting.dart';
|
import 'package:gitjournal/error_reporting.dart';
|
||||||
import 'package:gitjournal/state_container.dart';
|
import 'package:gitjournal/state_container.dart';
|
||||||
|
import 'package:gitjournal/utils.dart';
|
||||||
import 'package:gitjournal/utils/logger.dart';
|
import 'package:gitjournal/utils/logger.dart';
|
||||||
import 'package:gitjournal/widgets/folder_selection_dialog.dart';
|
import 'package:gitjournal/widgets/folder_selection_dialog.dart';
|
||||||
import 'package:gitjournal/widgets/note_editor_selector.dart';
|
import 'package:gitjournal/widgets/note_editor_selector.dart';
|
||||||
@ -134,8 +136,8 @@ class NoteEditorState extends State<NoteEditor> {
|
|||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return WillPopScope(
|
return WillPopScope(
|
||||||
onWillPop: () async {
|
onWillPop: () async {
|
||||||
_saveNote(_getNoteFromEditor());
|
var savedNote = await _saveNote(_getNoteFromEditor());
|
||||||
return true;
|
return savedNote;
|
||||||
},
|
},
|
||||||
child: _getEditor(),
|
child: _getEditor(),
|
||||||
);
|
);
|
||||||
@ -219,9 +221,11 @@ class NoteEditorState extends State<NoteEditor> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _exitEditorSelected(Note note) {
|
void _exitEditorSelected(Note note) async {
|
||||||
_saveNote(note);
|
var saved = await _saveNote(note);
|
||||||
Navigator.pop(context);
|
if (saved) {
|
||||||
|
Navigator.pop(context);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _renameNoteSelected(Note _note) async {
|
void _renameNoteSelected(Note _note) async {
|
||||||
@ -315,8 +319,9 @@ class NoteEditorState extends State<NoteEditor> {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void _saveNote(Note note) async {
|
// Returns bool indicating if the note was successfully saved
|
||||||
if (!_noteModified(note)) return;
|
Future<bool> _saveNote(Note note) async {
|
||||||
|
if (!_noteModified(note)) return true;
|
||||||
|
|
||||||
Log.d("Note modified - saving");
|
Log.d("Note modified - saving");
|
||||||
try {
|
try {
|
||||||
@ -326,7 +331,16 @@ class NoteEditorState extends State<NoteEditor> {
|
|||||||
: await stateContainer.updateNote(note);
|
: await stateContainer.updateNote(note);
|
||||||
} catch (e, stackTrace) {
|
} catch (e, stackTrace) {
|
||||||
logException(e, stackTrace);
|
logException(e, stackTrace);
|
||||||
|
|
||||||
|
await showAlertDialog(
|
||||||
|
context,
|
||||||
|
tr("editors.common.saveNoteFailed"),
|
||||||
|
e.toString(),
|
||||||
|
);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
Note _getNoteFromEditor() {
|
Note _getNoteFromEditor() {
|
||||||
|
@ -259,10 +259,11 @@ class StateContainer with ChangeNotifier {
|
|||||||
Future<void> addNote(Note note) async {
|
Future<void> addNote(Note note) async {
|
||||||
logEvent(Event.NoteAdded);
|
logEvent(Event.NoteAdded);
|
||||||
|
|
||||||
note.parent.add(note);
|
|
||||||
note.updateModified();
|
note.updateModified();
|
||||||
await note.save();
|
await note.save();
|
||||||
|
|
||||||
|
note.parent.add(note);
|
||||||
|
|
||||||
return _opLock.synchronized(() async {
|
return _opLock.synchronized(() async {
|
||||||
Log.d("Got addNote lock");
|
Log.d("Got addNote lock");
|
||||||
|
|
||||||
|
@ -58,3 +58,12 @@ NotesFolderFS getFolderForEditor(
|
|||||||
return rootFolder.getFolderWithSpec(spec);
|
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);
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user