From 20f8d7f7febfad061aaffe387399e07921e75b50 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Sun, 9 Feb 2020 17:33:19 +0100 Subject: [PATCH] NoteEditor: Improve modified check If the only modification is the addition of a 'modified' tag, then we shouldn't count it as modified. I'm not too fond of this logic. --- lib/core/note.dart | 6 +++--- lib/core/note_data.dart | 5 +++++ lib/screens/note_editor.dart | 24 +++++++++++------------- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/lib/core/note.dart b/lib/core/note.dart index 1e157301..7a13bfdd 100644 --- a/lib/core/note.dart +++ b/lib/core/note.dart @@ -24,7 +24,7 @@ class Note with ChangeNotifier implements Comparable { DateTime _created; DateTime _modified; NoteData _data = NoteData(); - NoteSerializer _noteSerializer = NoteSerializer(); + NoteSerializer noteSerializer = NoteSerializer(); DateTime _fileLastModified; @@ -88,13 +88,13 @@ class Note with ChangeNotifier implements Comparable { } NoteData get data { - _noteSerializer.encode(this, _data); + noteSerializer.encode(this, _data); return _data; } set data(NoteData data) { _data = data; - _noteSerializer.decode(_data, this); + noteSerializer.decode(_data, this); notifyListeners(); } diff --git a/lib/core/note_data.dart b/lib/core/note_data.dart index 22dbf99e..f9472ef7 100644 --- a/lib/core/note_data.dart +++ b/lib/core/note_data.dart @@ -10,6 +10,11 @@ class NoteData { props = props ?? LinkedHashMap(); } + NoteData.from(NoteData other) { + body = String.fromCharCodes(other.body.codeUnits); + props = LinkedHashMap.from(other.props); + } + @override int get hashCode => body.hashCode ^ props.hashCode; diff --git a/lib/screens/note_editor.dart b/lib/screens/note_editor.dart index d47262d3..06a37ced 100644 --- a/lib/screens/note_editor.dart +++ b/lib/screens/note_editor.dart @@ -1,12 +1,12 @@ import 'package:flutter/material.dart'; import 'package:gitjournal/core/note.dart'; +import 'package:gitjournal/core/note_data.dart'; import 'package:gitjournal/core/notes_folder.dart'; import 'package:gitjournal/editors/markdown_editor.dart'; import 'package:gitjournal/editors/raw_editor.dart'; import 'package:gitjournal/editors/todo_editor.dart'; import 'package:gitjournal/state_container.dart'; -import 'package:gitjournal/core/note_data_serializers.dart'; import 'package:gitjournal/widgets/folder_selection_dialog.dart'; import 'package:gitjournal/widgets/rename_dialog.dart'; @@ -36,7 +36,7 @@ enum EditorType { Markdown, Raw, Todo } class NoteEditorState extends State { Note note; EditorType editorType = EditorType.Markdown; - String noteSerialized = ""; + NoteData originalNoteData = NoteData(); final _rawEditorKey = GlobalKey(); final _markdownEditorKey = GlobalKey(); @@ -51,8 +51,7 @@ class NoteEditorState extends State { } NoteEditorState.fromNote(this.note) { - var serializer = MarkdownYAMLSerializer(); - noteSerialized = serializer.encode(note.data); + originalNoteData = NoteData.from(note.data); } @override @@ -224,14 +223,14 @@ class NoteEditorState extends State { if (_isNewNote) { return note.title.isNotEmpty || note.body.isNotEmpty; } - var serializer = MarkdownYAMLSerializer(); - var finalNoteSerialized = serializer.encode(note.data); - bool modified = finalNoteSerialized != noteSerialized; - if (modified) { - print("Original Serialization: " + noteSerialized); - print("New Serialization: " + finalNoteSerialized); + + if (note.data != originalNoteData) { + var newWithoutModified = NoteData.from(note.data); + newWithoutModified.props.remove(note.noteSerializer.settings.modifiedKey); + + return newWithoutModified != originalNoteData; } - return modified; + return false; } void _saveNote(Note note) { @@ -291,8 +290,7 @@ class NoteEditorState extends State { FlatButton( onPressed: () { // FIXME: This shouldn't be required. Why is the original note modified? - var serializer = MarkdownYAMLSerializer(); - note.data = serializer.decode(noteSerialized); + note.data = originalNoteData; Navigator.pop(context); // Alert box Navigator.pop(context); // Note Editor