From 215dfc2cec04610b1c08578077abcd447d668505 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Fri, 28 Feb 2020 14:29:07 +0100 Subject: [PATCH] NotesCache: Add a simple way to build the cache --- lib/core/notes_cache.dart | 38 +++++++++++++++++++++++++++++++++----- lib/core/notes_folder.dart | 9 +++++++++ 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/lib/core/notes_cache.dart b/lib/core/notes_cache.dart index 349c8a71..0913c30e 100644 --- a/lib/core/notes_cache.dart +++ b/lib/core/notes_cache.dart @@ -54,12 +54,45 @@ class NotesCache { } var note = Note(parent, fullFilePath); + note.load(); parent.add(note); } return rootFolder; } + Future buildCache(NotesFolder rootFolder, NotesCacheSortOrder sortOrder) { + // FIXME: This could be optimized quite a bit + var files = rootFolder.getAllNotes(); + files.sort(_buildSortingFunc(sortOrder)); + files = files.sublist(0, 10); + var fileList = files.map((f) => f.filePath); + + return saveToDisk(fileList); + } + + Function _buildSortingFunc(NotesCacheSortOrder order) { + switch (order) { + case NotesCacheSortOrder.Modified: + return (Note a, Note b) { + var a1 = a.modified ?? a.fileLastModified; + var b1 = b.modified ?? b.fileLastModified; + return a1.isBefore(b1); + }; + + // FIXE: We should have an actual created date! + case NotesCacheSortOrder.Created: + return (Note a, Note b) { + var a1 = a.created ?? a.fileLastModified; + var b1 = b.created ?? b.fileLastModified; + return a1.isBefore(b1); + }; + } + + assert(false, "Why is the sorting Func nill?"); + return () => {}; + } + @visibleForTesting Future> loadFromDisk() async { String contents = ""; @@ -81,8 +114,3 @@ class NotesCache { return File(filePath).writeAsString(contents); } } - -// -// To Add: buildCache(NotesFolder rootFolder) -// To Add: either noteAdded / noteRemoved -// or monitor the root NotesFolder directly diff --git a/lib/core/notes_folder.dart b/lib/core/notes_folder.dart index 45b84bbf..c6ab95bf 100644 --- a/lib/core/notes_folder.dart +++ b/lib/core/notes_folder.dart @@ -304,4 +304,13 @@ class NotesFolder int compareTo(NotesFolder other) { return folderPath.compareTo(other.folderPath); } + + List getAllNotes() { + var notes = List.from(_notes); + + for (var folder in _folders) { + notes.addAll(folder.getAllNotes()); + } + return notes; + } }