mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-09-14 15:33:17 +08:00
Centralize sorting code
This commit is contained in:
@ -54,59 +54,19 @@ class NotesCache {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future buildCache(NotesFolder rootFolder, SortingMode sortOrder) {
|
Future buildCache(NotesFolder rootFolder, SortingMode sortingMode) {
|
||||||
print("Saving the NotesCache");
|
print("Saving the NotesCache");
|
||||||
// FIXME: This could be optimized quite a bit
|
// FIXME: This could be optimized quite a bit
|
||||||
var files = rootFolder.getAllNotes();
|
var files = rootFolder.getAllNotes();
|
||||||
assert(files.every((n) => n.loadState == NoteLoadState.Loaded));
|
assert(files.every((n) => n.loadState == NoteLoadState.Loaded));
|
||||||
|
|
||||||
files.sort(_buildSortingFunc(sortOrder));
|
files.sort(sortingMode.sortingFunction());
|
||||||
files = files.sublist(0, 10);
|
files = files.sublist(0, 10);
|
||||||
var fileList = files.map((f) => f.filePath).toList();
|
var fileList = files.map((f) => f.filePath).toList();
|
||||||
|
|
||||||
return saveToDisk(fileList);
|
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
|
@visibleForTesting
|
||||||
Future<List<String>> loadFromDisk() async {
|
Future<List<String>> loadFromDisk() async {
|
||||||
String contents = "";
|
String contents = "";
|
||||||
|
@ -9,16 +9,21 @@ class SortedNotesFolder
|
|||||||
with NotesFolderNotifier
|
with NotesFolderNotifier
|
||||||
implements NotesFolderReadOnly {
|
implements NotesFolderReadOnly {
|
||||||
final NotesFolder folder;
|
final NotesFolder folder;
|
||||||
SortingMode sortingMode;
|
|
||||||
|
SortingMode _sortingMode;
|
||||||
|
NoteSortingFunction _sortFunc;
|
||||||
|
|
||||||
List<Note> _notes = [];
|
List<Note> _notes = [];
|
||||||
|
|
||||||
SortedNotesFolder({
|
SortedNotesFolder({
|
||||||
@required this.folder,
|
@required this.folder,
|
||||||
@required this.sortingMode,
|
@required SortingMode sortingMode,
|
||||||
}) {
|
}) {
|
||||||
|
_sortingMode = sortingMode;
|
||||||
|
_sortFunc = _sortingMode.sortingFunction();
|
||||||
|
|
||||||
_notes = List<Note>.from(folder.notes);
|
_notes = List<Note>.from(folder.notes);
|
||||||
_notes.sort(_compare);
|
_notes.sort(_sortFunc);
|
||||||
|
|
||||||
folder.addFolderAddedListener(_folderAddedListener);
|
folder.addFolderAddedListener(_folderAddedListener);
|
||||||
folder.addFolderRemovedListener(_folderRemovedListener);
|
folder.addFolderRemovedListener(_folderRemovedListener);
|
||||||
@ -56,7 +61,7 @@ class SortedNotesFolder
|
|||||||
var i = 0;
|
var i = 0;
|
||||||
for (; i < _notes.length; i++) {
|
for (; i < _notes.length; i++) {
|
||||||
var n = _notes[i];
|
var n = _notes[i];
|
||||||
if (_compare(n, note) > 0) {
|
if (_sortFunc(n, note) > 0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -75,48 +80,10 @@ class SortedNotesFolder
|
|||||||
}
|
}
|
||||||
|
|
||||||
void _entityChanged() {
|
void _entityChanged() {
|
||||||
_notes.sort(_compare);
|
_notes.sort(_sortFunc);
|
||||||
notifyListeners();
|
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
|
@override
|
||||||
List<Note> get notes => _notes;
|
List<Note> get notes => _notes;
|
||||||
|
|
||||||
@ -127,8 +94,9 @@ class SortedNotesFolder
|
|||||||
bool get isEmpty => folder.isEmpty;
|
bool get isEmpty => folder.isEmpty;
|
||||||
|
|
||||||
void changeSortingMode(SortingMode sm) {
|
void changeSortingMode(SortingMode sm) {
|
||||||
sortingMode = sm;
|
_notes.sort(_sortFunc);
|
||||||
_notes.sort(_compare);
|
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SortingMode get sortingMode => _sortingMode;
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,7 @@
|
|||||||
|
import 'package:gitjournal/core/note.dart';
|
||||||
|
|
||||||
|
typedef NoteSortingFunction = int Function(Note a, Note b);
|
||||||
|
|
||||||
class SortingMode {
|
class SortingMode {
|
||||||
static const Modified = SortingMode("Last Modified", "Modified");
|
static const Modified = SortingMode("Last Modified", "Modified");
|
||||||
static const Created = SortingMode("Created", "Created");
|
static const Created = SortingMode("Created", "Created");
|
||||||
@ -43,4 +47,46 @@ class SortingMode {
|
|||||||
assert(false, "SortingMode toString should never be called");
|
assert(false, "SortingMode toString should never be called");
|
||||||
return "";
|
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);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user