Centralize sorting code

This commit is contained in:
Vishesh Handa
2020-03-01 13:50:39 +01:00
parent 7bdcd5dd82
commit 3ed246310e
3 changed files with 61 additions and 87 deletions

View File

@ -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<List<String>> loadFromDisk() async {
String contents = "";

View File

@ -9,16 +9,21 @@ class SortedNotesFolder
with NotesFolderNotifier
implements NotesFolderReadOnly {
final NotesFolder folder;
SortingMode sortingMode;
SortingMode _sortingMode;
NoteSortingFunction _sortFunc;
List<Note> _notes = [];
SortedNotesFolder({
@required this.folder,
@required this.sortingMode,
@required SortingMode sortingMode,
}) {
_sortingMode = sortingMode;
_sortFunc = _sortingMode.sortingFunction();
_notes = List<Note>.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<Note> 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;
}

View File

@ -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);
};
}
}
}