Only have one SortingMode class

It doesn't make sense to duplicate it.
This commit is contained in:
Vishesh Handa
2020-03-01 13:40:16 +01:00
parent 6d69217cae
commit d50f772468
3 changed files with 40 additions and 21 deletions

View File

@ -124,6 +124,10 @@ class Note with ChangeNotifier implements Comparable<Note> {
return _summary; return _summary;
} }
NoteLoadState get loadState {
return _loadState;
}
Future<NoteLoadState> load() async { Future<NoteLoadState> load() async {
assert(_filePath != null); assert(_filePath != null);
assert(_filePath.isNotEmpty); assert(_filePath.isNotEmpty);

View File

@ -2,16 +2,12 @@ import 'dart:convert';
import 'dart:io'; import 'dart:io';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:gitjournal/settings.dart';
import 'package:path/path.dart' as p; import 'package:path/path.dart' as p;
import 'package:gitjournal/core/note.dart'; import 'package:gitjournal/core/note.dart';
import 'package:gitjournal/core/notes_folder.dart'; import 'package:gitjournal/core/notes_folder.dart';
enum NotesCacheSortOrder {
Modified,
Created,
}
class NotesCache { class NotesCache {
final String filePath; final String filePath;
final String notesBasePath; 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 // FIXME: This could be optimized quite a bit
var files = rootFolder.getAllNotes(); var files = rootFolder.getAllNotes();
assert(files.every((n) => n.loadState == NoteLoadState.Loaded));
files.sort(_buildSortingFunc(sortOrder)); files.sort(_buildSortingFunc(sortOrder));
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();
@ -68,21 +67,39 @@ class NotesCache {
return saveToDisk(fileList); return saveToDisk(fileList);
} }
Function _buildSortingFunc(NotesCacheSortOrder order) { Function _buildSortingFunc(SortingMode order) {
switch (order) { switch (order) {
case NotesCacheSortOrder.Modified: case SortingMode.Modified:
return (Note a, Note b) { return (Note a, Note b) {
var a1 = a.modified ?? a.fileLastModified; var aDt = a.modified ?? a.fileLastModified;
var b1 = b.modified ?? b.fileLastModified; var bDt = b.modified ?? b.fileLastModified;
return b1.compareTo(a1); 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! // FIXE: We should have an actual created date!
case NotesCacheSortOrder.Created: case SortingMode.Created:
return (Note a, Note b) { return (Note a, Note b) {
var a1 = a.created ?? a.fileLastModified; var aDt = a.created ?? a.fileLastModified;
var b1 = b.created ?? b.fileLastModified; var bDt = b.created ?? b.fileLastModified;
return b1.compareTo(a1); 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);
}; };
} }

View File

@ -73,12 +73,10 @@ class StateContainer with ChangeNotifier {
Future<void> _loadNotes() async { Future<void> _loadNotes() async {
// FIXME: We should report the notes that failed to load // FIXME: We should report the notes that failed to load
await appState.notesFolder.loadRecursively(); await appState.notesFolder.loadRecursively();
await _notesCache.buildCache(
var sortOrder = NotesCacheSortOrder.Modified; appState.notesFolder,
if (Settings.instance.sortingMode == SortingMode.Created) { Settings.instance.sortingMode,
sortOrder = NotesCacheSortOrder.Created; );
}
await _notesCache.buildCache(appState.notesFolder, sortOrder);
} }
Future<void> syncNotes({bool doNotThrow = false}) async { Future<void> syncNotes({bool doNotThrow = false}) async {