From b14e094e59225892504a6b646deefe07868a6227 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Mon, 13 Jul 2020 17:27:39 +0200 Subject: [PATCH] Copy Note to clipboard when it cannot be saved Also avoid showing the actual exception. It's not very useful to the user, and we're logging the exception to Sentry anyway. Related to #124 --- assets/langs/en.yaml | 4 +++- lib/core/note.dart | 4 ++++ lib/screens/note_editor.dart | 6 ++++-- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/assets/langs/en.yaml b/assets/langs/en.yaml index a39f24fa..80556359 100644 --- a/assets/langs/en.yaml +++ b/assets/langs/en.yaml @@ -62,7 +62,9 @@ editors: addImage: Add Image from Gallery editFileName: Edit File Name tags: Edit Tags - saveNoteFailed: Failed to Save Note + saveNoteFailed: + title: Failed to Save Note + message: We're sorry, but we cannot seem to save the Note. It has been copied to the clipboard to avoid data loss. pro: Pro actions: newNote: New Note diff --git a/lib/core/note.dart b/lib/core/note.dart index 296250a6..fd977ce0 100644 --- a/lib/core/note.dart +++ b/lib/core/note.dart @@ -303,6 +303,10 @@ class Note with NotesNotifier { await file.writeAsString(contents); } + String serialize() { + return _serializer.encode(data); + } + // FIXME: What about error handling? Future remove() async { assert(_filePath != null); diff --git a/lib/screens/note_editor.dart b/lib/screens/note_editor.dart index 196b0c02..a1debd5d 100644 --- a/lib/screens/note_editor.dart +++ b/lib/screens/note_editor.dart @@ -3,6 +3,7 @@ import 'dart:io'; import 'package:easy_localization/easy_localization.dart'; import 'package:flutter/material.dart'; import 'package:collection/collection.dart'; +import 'package:flutter/services.dart'; import 'package:gitjournal/core/note.dart'; import 'package:gitjournal/core/md_yaml_doc.dart'; @@ -331,11 +332,12 @@ class NoteEditorState extends State { : await stateContainer.updateNote(note); } catch (e, stackTrace) { logException(e, stackTrace); + Clipboard.setData(ClipboardData(text: note.serialize())); await showAlertDialog( context, - tr("editors.common.saveNoteFailed"), - e.toString(), + tr("editors.common.saveNoteFailed.title"), + tr("editors.common.saveNoteFailed.message"), ); return false; }