mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-28 18:03:14 +08:00
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.
This commit is contained in:
@ -24,7 +24,7 @@ class Note with ChangeNotifier implements Comparable<Note> {
|
||||
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<Note> {
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
@ -10,6 +10,11 @@ class NoteData {
|
||||
props = props ?? LinkedHashMap<String, dynamic>();
|
||||
}
|
||||
|
||||
NoteData.from(NoteData other) {
|
||||
body = String.fromCharCodes(other.body.codeUnits);
|
||||
props = LinkedHashMap<String, dynamic>.from(other.props);
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode => body.hashCode ^ props.hashCode;
|
||||
|
||||
|
@ -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<NoteEditor> {
|
||||
Note note;
|
||||
EditorType editorType = EditorType.Markdown;
|
||||
String noteSerialized = "";
|
||||
NoteData originalNoteData = NoteData();
|
||||
|
||||
final _rawEditorKey = GlobalKey<RawEditorState>();
|
||||
final _markdownEditorKey = GlobalKey<MarkdownEditorState>();
|
||||
@ -51,8 +51,7 @@ class NoteEditorState extends State<NoteEditor> {
|
||||
}
|
||||
|
||||
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<NoteEditor> {
|
||||
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<NoteEditor> {
|
||||
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
|
||||
|
Reference in New Issue
Block a user