diff --git a/lib/core/note.dart b/lib/core/note.dart index dc1face8..5908cb7d 100644 --- a/lib/core/note.dart +++ b/lib/core/note.dart @@ -213,6 +213,7 @@ class Note with NotesNotifier { newName += '.md'; } + var oldFilePath = filePath; var parentDirName = p.dirname(filePath); var newFilePath = p.join(parentDirName, newName); if (_loadState != NoteLoadState.None) { @@ -221,6 +222,7 @@ class Note with NotesNotifier { } _filePath = newFilePath; + notifyRenameListeners(this, oldFilePath); _notifyModified(); } diff --git a/lib/core/note_notifier.dart b/lib/core/note_notifier.dart index 499d482d..81039f56 100644 --- a/lib/core/note_notifier.dart +++ b/lib/core/note_notifier.dart @@ -4,9 +4,11 @@ import 'package:flutter/foundation.dart'; import 'note.dart'; typedef NoteModificationCallback = void Function(Note note); +typedef NoteRenameCallback = void Function(Note note, String oldPath); class NotesNotifier implements ChangeNotifier { var _modListeners = ObserverList(); + var _renameListeners = ObserverList(); void addModifiedListener(NoteModificationCallback listener) { _modListeners.add(listener); @@ -16,11 +18,20 @@ class NotesNotifier implements ChangeNotifier { _modListeners.remove(listener); } + void addRenameListener(NoteRenameCallback listener) { + _renameListeners.add(listener); + } + + void removeRenameListener(NoteRenameCallback listener) { + _renameListeners.remove(listener); + } + @mustCallSuper @override void dispose() { assert(_debugAssertNotDisposed()); _modListeners = null; + _renameListeners = null; _listeners = null; } @@ -151,7 +162,7 @@ class NotesNotifier implements ChangeNotifier { assert(_debugAssertNotDisposed()); if (_modListeners != null) { final localListeners = List.from(_modListeners); - for (NoteModificationCallback listener in localListeners) { + for (var listener in localListeners) { try { if (_modListeners.contains(listener)) { listener(note); @@ -175,4 +186,33 @@ class NotesNotifier implements ChangeNotifier { } } } + + void notifyRenameListeners(Note note, String oldPath) { + assert(_debugAssertNotDisposed()); + if (_renameListeners != null) { + final localListeners = List.from(_renameListeners); + for (var listener in localListeners) { + try { + if (_renameListeners.contains(listener)) { + listener(note, oldPath); + } + } catch (exception, stack) { + FlutterError.reportError(FlutterErrorDetails( + exception: exception, + stack: stack, + library: 'foundation library', + context: ErrorDescription( + 'while dispatching notifications for $runtimeType'), + informationCollector: () sync* { + yield DiagnosticsProperty( + 'The $runtimeType sending notification was', + this, + style: DiagnosticsTreeStyle.errorProperty, + ); + }, + )); + } + } + } + } } diff --git a/lib/core/notes_folder_fs.dart b/lib/core/notes_folder_fs.dart index 202db79c..6bab0c3c 100644 --- a/lib/core/notes_folder_fs.dart +++ b/lib/core/notes_folder_fs.dart @@ -45,6 +45,12 @@ class NotesFolderFS with NotesFolderNotifier implements NotesFolder { notifyNoteModified(-1, note); } + void _noteRenamed(Note note, String oldPath) { + assert(_entityMap.containsKey(oldPath)); + _entityMap.remove(oldPath); + _entityMap[note.filePath] = note; + } + void reset(String folderPath) { _folderPath = folderPath; @@ -194,6 +200,7 @@ class NotesFolderFS with NotesFolderNotifier implements NotesFolder { } //Log.d("Found file ${fsEntity.path}"); note.addModifiedListener(_noteModified); + note.addRenameListener(_noteRenamed); _notes.add(note); _entityMap[fsEntity.path] = note; @@ -254,6 +261,7 @@ class NotesFolderFS with NotesFolderNotifier implements NotesFolder { void remove(Note note) { assert(note.parent == this); note.removeModifiedListener(_noteModified); + note.removeRenameListener(_noteRenamed); assert(_notes.indexWhere((n) => n.filePath == note.filePath) != -1); assert(_entityMap.containsKey(note.filePath)); diff --git a/lib/core/notes_folder_notifier.dart b/lib/core/notes_folder_notifier.dart index 5ca6e5aa..3ff6888a 100644 --- a/lib/core/notes_folder_notifier.dart +++ b/lib/core/notes_folder_notifier.dart @@ -7,6 +7,8 @@ import 'notes_folder.dart'; typedef FolderNotificationCallback = void Function( int index, NotesFolder folder); typedef NoteNotificationCallback = void Function(int index, Note note); +typedef NoteRenamedCallback = void Function( + int index, Note note, String oldPath); class NotesFolderNotifier implements ChangeNotifier { var _folderAddedListeners = ObserverList(); @@ -15,6 +17,7 @@ class NotesFolderNotifier implements ChangeNotifier { var _noteAddedListeners = ObserverList(); var _noteRemovedListeners = ObserverList(); var _noteModifiedListeners = ObserverList(); + var _noteRenameListeners = ObserverList(); void addFolderRemovedListener(FolderNotificationCallback listener) { _folderRemovedListeners.add(listener); @@ -56,6 +59,14 @@ class NotesFolderNotifier implements ChangeNotifier { _noteModifiedListeners.remove(listener); } + void addNoteRenameListener(NoteRenamedCallback listener) { + _noteRenameListeners.add(listener); + } + + void removeNoteRenameListener(NoteRenamedCallback listener) { + _noteRenameListeners.remove(listener); + } + @mustCallSuper @override void dispose() { @@ -64,6 +75,7 @@ class NotesFolderNotifier implements ChangeNotifier { _noteAddedListeners = null; _noteRemovedListeners = null; _noteModifiedListeners = null; + _noteRenameListeners = null; assert(_debugAssertNotDisposed()); _listeners = null; @@ -157,6 +169,36 @@ class NotesFolderNotifier implements ChangeNotifier { _notifyNoteCallback(_noteModifiedListeners, index, note); } + void notifyNoteRenamed(int index, Note note, String oldPath) { + if (_listeners == null) { + return; + } + final localListeners = List.from(_noteRenameListeners); + for (var listener in localListeners) { + try { + if (_listeners.contains(listener)) { + listener(index, note, oldPath); + } + } catch (exception, stack) { + FlutterError.reportError(FlutterErrorDetails( + exception: exception, + stack: stack, + library: 'GitJournal', + context: ErrorDescription( + 'while dispatching notifications for $runtimeType'), + informationCollector: () sync* { + yield DiagnosticsProperty( + 'The $runtimeType sending notification was', + this, + style: DiagnosticsTreeStyle.errorProperty, + ); + }, + )); + } + } + notifyListeners(); + } + // // ChangeNotifier implementation - How to not duplicate this? //