From d50f7724684169e3b806338181718f5cc5d881f3 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Sun, 1 Mar 2020 13:40:16 +0100 Subject: [PATCH] Only have one SortingMode class It doesn't make sense to duplicate it. --- lib/core/note.dart | 4 ++++ lib/core/notes_cache.dart | 47 ++++++++++++++++++++++++++------------- lib/state_container.dart | 10 ++++----- 3 files changed, 40 insertions(+), 21 deletions(-) diff --git a/lib/core/note.dart b/lib/core/note.dart index 718e51f1..623a3496 100644 --- a/lib/core/note.dart +++ b/lib/core/note.dart @@ -124,6 +124,10 @@ class Note with ChangeNotifier implements Comparable { return _summary; } + NoteLoadState get loadState { + return _loadState; + } + Future load() async { assert(_filePath != null); assert(_filePath.isNotEmpty); diff --git a/lib/core/notes_cache.dart b/lib/core/notes_cache.dart index b5bf4c54..4d97c0c8 100644 --- a/lib/core/notes_cache.dart +++ b/lib/core/notes_cache.dart @@ -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); }; } diff --git a/lib/state_container.dart b/lib/state_container.dart index bac4651b..349bb853 100644 --- a/lib/state_container.dart +++ b/lib/state_container.dart @@ -73,12 +73,10 @@ class StateContainer with ChangeNotifier { Future _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 syncNotes({bool doNotThrow = false}) async {