From 76f70f0d185fc69c56133f08e71e4f388572da55 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Mon, 9 Dec 2019 00:02:40 +0100 Subject: [PATCH] Note: Store when last modified Also sort the notes based on when last modified. Now that we're moving towards being a more note-taking app, it makes sense that a recently modified note goes on top. Of course, the sorting should be controllable by the user, but the default probably should be by when modified. --- lib/core/note.dart | 26 +++++++++++++++++++++++--- lib/state_container.dart | 2 ++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/lib/core/note.dart b/lib/core/note.dart index 2ee4e26e..b2620f4b 100644 --- a/lib/core/note.dart +++ b/lib/core/note.dart @@ -18,6 +18,7 @@ class Note with ChangeNotifier implements Comparable { String filePath = ""; DateTime _created; + DateTime _modified; NoteData _data = NoteData(); DateTime _fileLastModified; @@ -44,6 +45,25 @@ class Note with ChangeNotifier implements Comparable { notifyListeners(); } + DateTime get modified { + return _modified; + } + + set modified(DateTime dt) { + _modified = dt; + + if (hasValidDate()) { + _data.props['modified'] = toIso8601WithTimezone(_modified); + } else { + _data.props.remove('modified'); + } + notifyListeners(); + } + + void updateModified() { + modified = DateTime.now(); + } + String get body { return data.body; } @@ -153,7 +173,7 @@ class Note with ChangeNotifier implements Comparable { @override String toString() { - return 'Note{filePath: $filePath, created: $created, data: $data}'; + return 'Note{filePath: $filePath, created: $created, modified: $modified, data: $data}'; } @override @@ -161,7 +181,7 @@ class Note with ChangeNotifier implements Comparable { if (other == null) { return -1; } - if (created == other.created) return filePath.compareTo(other.filePath); - return created.compareTo(other.created); + if (modified == other.modified) return filePath.compareTo(other.filePath); + return modified.compareTo(other.modified); } } diff --git a/lib/state_container.dart b/lib/state_container.dart index 0a0769c1..6273dcf9 100644 --- a/lib/state_container.dart +++ b/lib/state_container.dart @@ -154,6 +154,7 @@ class StateContainerState extends State { note.filePath = p.join(parentPath, getFileName(note)); } note.parent.insert(index, note); + note.updateModified(); _gitRepo.addNote(note).then((NoteRepoResult _) { syncNotes(); }); @@ -161,6 +162,7 @@ class StateContainerState extends State { void updateNote(Note note) { Fimber.d("State Container updateNote"); + note.updateModified(); _gitRepo.updateNote(note).then((NoteRepoResult _) { syncNotes(); });