mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-30 19:36:25 +08:00
Only have one SortingMode class
It doesn't make sense to duplicate it.
This commit is contained in:
@ -124,6 +124,10 @@ class Note with ChangeNotifier implements Comparable<Note> {
|
||||
return _summary;
|
||||
}
|
||||
|
||||
NoteLoadState get loadState {
|
||||
return _loadState;
|
||||
}
|
||||
|
||||
Future<NoteLoadState> load() async {
|
||||
assert(_filePath != null);
|
||||
assert(_filePath.isNotEmpty);
|
||||
|
@ -2,16 +2,12 @@ import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:gitjournal/settings.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
|
||||
import 'package:gitjournal/core/note.dart';
|
||||
import 'package:gitjournal/core/notes_folder.dart';
|
||||
|
||||
enum NotesCacheSortOrder {
|
||||
Modified,
|
||||
Created,
|
||||
}
|
||||
|
||||
class NotesCache {
|
||||
final String filePath;
|
||||
final String notesBasePath;
|
||||
@ -58,9 +54,12 @@ class NotesCache {
|
||||
}
|
||||
}
|
||||
|
||||
Future buildCache(NotesFolder rootFolder, NotesCacheSortOrder sortOrder) {
|
||||
Future buildCache(NotesFolder rootFolder, SortingMode sortOrder) {
|
||||
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 = files.sublist(0, 10);
|
||||
var fileList = files.map((f) => f.filePath).toList();
|
||||
@ -68,21 +67,39 @@ class NotesCache {
|
||||
return saveToDisk(fileList);
|
||||
}
|
||||
|
||||
Function _buildSortingFunc(NotesCacheSortOrder order) {
|
||||
Function _buildSortingFunc(SortingMode order) {
|
||||
switch (order) {
|
||||
case NotesCacheSortOrder.Modified:
|
||||
case SortingMode.Modified:
|
||||
return (Note a, Note b) {
|
||||
var a1 = a.modified ?? a.fileLastModified;
|
||||
var b1 = b.modified ?? b.fileLastModified;
|
||||
return b1.compareTo(a1);
|
||||
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 NotesCacheSortOrder.Created:
|
||||
case SortingMode.Created:
|
||||
return (Note a, Note b) {
|
||||
var a1 = a.created ?? a.fileLastModified;
|
||||
var b1 = b.created ?? b.fileLastModified;
|
||||
return b1.compareTo(a1);
|
||||
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);
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -73,12 +73,10 @@ class StateContainer with ChangeNotifier {
|
||||
Future<void> _loadNotes() async {
|
||||
// FIXME: We should report the notes that failed to load
|
||||
await appState.notesFolder.loadRecursively();
|
||||
|
||||
var sortOrder = NotesCacheSortOrder.Modified;
|
||||
if (Settings.instance.sortingMode == SortingMode.Created) {
|
||||
sortOrder = NotesCacheSortOrder.Created;
|
||||
}
|
||||
await _notesCache.buildCache(appState.notesFolder, sortOrder);
|
||||
await _notesCache.buildCache(
|
||||
appState.notesFolder,
|
||||
Settings.instance.sortingMode,
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> syncNotes({bool doNotThrow = false}) async {
|
||||
|
Reference in New Issue
Block a user