From 9e2d59f3fa8cfbf0942ae2b2aa1c3aebe2ac91a4 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Sun, 8 Dec 2019 00:56:55 +0100 Subject: [PATCH] NotesFolder: Listen to changes of the subFolders and Notes A NotesFolder doesn't only change when a note/folder is added or removed. It is also changed when the contents of the Notes/Folders it contains have changed. --- lib/core/notes_folder.dart | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/core/notes_folder.dart b/lib/core/notes_folder.dart index 5bc2f213..48954919 100644 --- a/lib/core/notes_folder.dart +++ b/lib/core/notes_folder.dart @@ -14,6 +14,22 @@ class NotesFolder with ChangeNotifier { NotesFolder(this.parent, this.folderPath); + @override + void dispose() { + _entities.forEach((e) { + if (e.isFolder) { + e.folder.removeListener(_entityChanged); + } else { + e.note.removeListener(_entityChanged); + } + }); + super.dispose(); + } + + void _entityChanged() { + notifyListeners(); + } + bool get isEmpty { return _entities.isEmpty; } @@ -65,6 +81,13 @@ class NotesFolder with ChangeNotifier { // FIXME: This should not reconstruct the Notes or NotesFolders once constructed. Future loadRecursively() async { final dir = Directory(folderPath); + _entities.forEach((e) { + if (e.isFolder) { + e.folder.dispose(); + } else { + e.note.dispose(); + } + }); _entities = []; var lister = dir.list(recursive: false, followLinks: false); @@ -74,23 +97,28 @@ class NotesFolder with ChangeNotifier { if (subFolder.name.startsWith('.')) { continue; } + subFolder.addListener(_entityChanged); await subFolder.loadRecursively(); var noteFSEntity = NoteFSEntity(folder: subFolder); _entities.add(noteFSEntity); + continue; } var note = Note(this, fsEntity.path); if (!note.filePath.toLowerCase().endsWith('.md')) { continue; } + note.addListener(_entityChanged); await note.load(); var noteFSEntity = NoteFSEntity(note: note); _entities.add(noteFSEntity); } - notifyListeners(); + if (_entities.isNotEmpty) { + notifyListeners(); + } } void add(Note note) {