diff --git a/lib/core/notes_cache.dart b/lib/core/notes_cache.dart index 1ceeb7c2..d6c54d4b 100644 --- a/lib/core/notes_cache.dart +++ b/lib/core/notes_cache.dart @@ -54,59 +54,19 @@ class NotesCache { } } - Future buildCache(NotesFolder rootFolder, SortingMode sortOrder) { + Future buildCache(NotesFolder rootFolder, SortingMode sortingMode) { print("Saving the NotesCache"); // FIXME: This could be optimized quite a bit var files = rootFolder.getAllNotes(); assert(files.every((n) => n.loadState == NoteLoadState.Loaded)); - files.sort(_buildSortingFunc(sortOrder)); + files.sort(sortingMode.sortingFunction()); files = files.sublist(0, 10); var fileList = files.map((f) => f.filePath).toList(); return saveToDisk(fileList); } - Function _buildSortingFunc(SortingMode order) { - switch (order) { - case SortingMode.Modified: - return (Note a, Note b) { - var aDt = a.modified ?? a.fileLastModified; - var bDt = b.modified ?? b.fileLastModified; - if (aDt == null && bDt != null) { - return -1; - } - if (aDt != null && bDt == null) { - return -1; - } - if (bDt == null || aDt == null) { - return 0; - } - return bDt.compareTo(aDt); - }; - - // FIXE: We should have an actual created date! - case SortingMode.Created: - return (Note a, Note b) { - var aDt = a.created ?? a.fileLastModified; - var bDt = b.created ?? b.fileLastModified; - if (aDt == null && bDt != null) { - return -1; - } - if (aDt != null && bDt == null) { - return -1; - } - if (bDt == null || aDt == null) { - return 0; - } - return bDt.compareTo(aDt); - }; - } - - assert(false, "Why is the sorting Func nill?"); - return () => {}; - } - @visibleForTesting Future> loadFromDisk() async { String contents = ""; diff --git a/lib/core/sorted_notes_folder.dart b/lib/core/sorted_notes_folder.dart index 7b690c07..6c89454c 100644 --- a/lib/core/sorted_notes_folder.dart +++ b/lib/core/sorted_notes_folder.dart @@ -9,16 +9,21 @@ class SortedNotesFolder with NotesFolderNotifier implements NotesFolderReadOnly { final NotesFolder folder; - SortingMode sortingMode; + + SortingMode _sortingMode; + NoteSortingFunction _sortFunc; List _notes = []; SortedNotesFolder({ @required this.folder, - @required this.sortingMode, + @required SortingMode sortingMode, }) { + _sortingMode = sortingMode; + _sortFunc = _sortingMode.sortingFunction(); + _notes = List.from(folder.notes); - _notes.sort(_compare); + _notes.sort(_sortFunc); folder.addFolderAddedListener(_folderAddedListener); folder.addFolderRemovedListener(_folderRemovedListener); @@ -56,7 +61,7 @@ class SortedNotesFolder var i = 0; for (; i < _notes.length; i++) { var n = _notes[i]; - if (_compare(n, note) > 0) { + if (_sortFunc(n, note) > 0) { break; } } @@ -75,48 +80,10 @@ class SortedNotesFolder } void _entityChanged() { - _notes.sort(_compare); + _notes.sort(_sortFunc); notifyListeners(); } - int _compare(Note a, Note b) { - switch (sortingMode) { - case SortingMode.Created: - // vHanda FIXME: We should use when the file was created in the FS, but that doesn't - // seem to be acessible via dart - var aDt = a.created ?? a.fileLastModified; - var bDt = b.created ?? b.fileLastModified; - if (aDt == null && bDt != null) { - return -1; - } - if (aDt != null && bDt == null) { - return -1; - } - if (bDt == null || aDt == null) { - return 0; - } - return bDt.compareTo(aDt); - - case SortingMode.Modified: - var aDt = a.modified ?? a.fileLastModified; - var bDt = b.modified ?? b.fileLastModified; - if (aDt == null && bDt != null) { - return -1; - } - if (aDt != null && bDt == null) { - return -1; - } - if (bDt == null || aDt == null) { - return 0; - } - if (bDt == null || aDt == null) { - return 0; - } - return bDt.compareTo(aDt); - } - return 0; - } - @override List get notes => _notes; @@ -127,8 +94,9 @@ class SortedNotesFolder bool get isEmpty => folder.isEmpty; void changeSortingMode(SortingMode sm) { - sortingMode = sm; - _notes.sort(_compare); + _notes.sort(_sortFunc); notifyListeners(); } + + SortingMode get sortingMode => _sortingMode; } diff --git a/lib/core/sorting_mode.dart b/lib/core/sorting_mode.dart index 24d7185d..46ee0778 100644 --- a/lib/core/sorting_mode.dart +++ b/lib/core/sorting_mode.dart @@ -1,3 +1,7 @@ +import 'package:gitjournal/core/note.dart'; + +typedef NoteSortingFunction = int Function(Note a, Note b); + class SortingMode { static const Modified = SortingMode("Last Modified", "Modified"); static const Created = SortingMode("Created", "Created"); @@ -43,4 +47,46 @@ class SortingMode { assert(false, "SortingMode toString should never be called"); return ""; } + + NoteSortingFunction sortingFunction() { + switch (_str) { + case "Created": + return (Note a, Note b) { + // vHanda FIXME: We should use when the file was created in the FS, but that doesn't + // seem to be acessible via dart + var aDt = a.created ?? a.fileLastModified; + var bDt = b.created ?? b.fileLastModified; + if (aDt == null && bDt != null) { + return -1; + } + if (aDt != null && bDt == null) { + return -1; + } + if (bDt == null || aDt == null) { + return 0; + } + return bDt.compareTo(aDt); + }; + + case "Modified": + default: + return (Note a, Note b) { + var aDt = a.modified ?? a.fileLastModified; + var bDt = b.modified ?? b.fileLastModified; + if (aDt == null && bDt != null) { + return -1; + } + if (aDt != null && bDt == null) { + return -1; + } + if (bDt == null || aDt == null) { + return 0; + } + if (bDt == null || aDt == null) { + return 0; + } + return bDt.compareTo(aDt); + }; + } + } }