From 78d7e6cef0e0d811434d59a30cd82c8978aa2b86 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Fri, 28 Feb 2020 14:55:52 +0100 Subject: [PATCH] Integrate the NotesCache It doesn't make any difference as - 1. The sort order shown our SortedNotesFolder is different 2. Rebuilds of the tree affect everything. Even the elements which should be in the correct place. --- lib/core/notes_cache.dart | 11 ++++------- lib/core/notes_folder.dart | 2 +- lib/state_container.dart | 21 ++++++++++++++++++++- test/notes_cache_test.dart | 4 +++- 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/lib/core/notes_cache.dart b/lib/core/notes_cache.dart index 0913c30e..b5bf4c54 100644 --- a/lib/core/notes_cache.dart +++ b/lib/core/notes_cache.dart @@ -20,9 +20,8 @@ class NotesCache { void updateCache(NotesFolder rootFolder) {} - Future load() async { + Future load(NotesFolder rootFolder) async { var fileList = await loadFromDisk(); - var rootFolder = NotesFolder(null, this.notesBasePath); var sep = Platform.pathSeparator; var notesBasePath = this.notesBasePath; @@ -57,8 +56,6 @@ class NotesCache { note.load(); parent.add(note); } - - return rootFolder; } Future buildCache(NotesFolder rootFolder, NotesCacheSortOrder sortOrder) { @@ -66,7 +63,7 @@ class NotesCache { var files = rootFolder.getAllNotes(); files.sort(_buildSortingFunc(sortOrder)); files = files.sublist(0, 10); - var fileList = files.map((f) => f.filePath); + var fileList = files.map((f) => f.filePath).toList(); return saveToDisk(fileList); } @@ -77,7 +74,7 @@ class NotesCache { return (Note a, Note b) { var a1 = a.modified ?? a.fileLastModified; var b1 = b.modified ?? b.fileLastModified; - return a1.isBefore(b1); + return b1.compareTo(a1); }; // FIXE: We should have an actual created date! @@ -85,7 +82,7 @@ class NotesCache { return (Note a, Note b) { var a1 = a.created ?? a.fileLastModified; var b1 = b.created ?? b.fileLastModified; - return a1.isBefore(b1); + return b1.compareTo(a1); }; } diff --git a/lib/core/notes_folder.dart b/lib/core/notes_folder.dart index c6ab95bf..eb18faa2 100644 --- a/lib/core/notes_folder.dart +++ b/lib/core/notes_folder.dart @@ -306,7 +306,7 @@ class NotesFolder } List getAllNotes() { - var notes = List.from(_notes); + var notes = List.from(_notes); for (var folder in _folders) { notes.addAll(folder.getAllNotes()); diff --git a/lib/state_container.dart b/lib/state_container.dart index fdf8e634..bac4651b 100644 --- a/lib/state_container.dart +++ b/lib/state_container.dart @@ -7,6 +7,7 @@ import 'package:flutter/material.dart'; import 'package:gitjournal/apis/git_migration.dart'; import 'package:gitjournal/appstate.dart'; import 'package:gitjournal/core/note.dart'; +import 'package:gitjournal/core/notes_cache.dart'; import 'package:gitjournal/core/notes_folder.dart'; import 'package:gitjournal/core/git_repo.dart'; import 'package:gitjournal/settings.dart'; @@ -21,6 +22,7 @@ class StateContainer with ChangeNotifier { // We should always just be modifying the 'git remotes' // With that, the StateContainer can be a StatelessWidget GitNoteRepository _gitRepo; + NotesCache _notesCache; StateContainer(this.appState) { assert(appState.localGitRepoConfigured); @@ -42,10 +44,21 @@ class StateContainer with ChangeNotifier { removeExistingRemoteClone(); } - _loadNotes(); + var cachePath = p.join(appState.gitBaseDirectory, "cache.json"); + _notesCache = NotesCache( + filePath: cachePath, + notesBasePath: _gitRepo.gitDirPath, + ); + + _loadFromCache(); _syncNotes(); } + void _loadFromCache() async { + await _notesCache.load(appState.notesFolder); + await _loadNotes(); + } + void removeExistingRemoteClone() async { var remoteGitDir = Directory( p.join(appState.gitBaseDirectory, appState.remoteGitRepoFolderName)); @@ -60,6 +73,12 @@ 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); } Future syncNotes({bool doNotThrow = false}) async { diff --git a/test/notes_cache_test.dart b/test/notes_cache_test.dart index 189ce922..ae510526 100644 --- a/test/notes_cache_test.dart +++ b/test/notes_cache_test.dart @@ -1,6 +1,7 @@ import 'dart:io'; import 'package:gitjournal/core/notes_cache.dart'; +import 'package:gitjournal/core/notes_folder.dart'; import 'package:path/path.dart' as p; import 'package:test/test.dart'; @@ -38,7 +39,8 @@ void main() { test('Should create directory structure accurately', () async { await cache.saveToDisk(fileList); - var rootFolder = await cache.load(); + var rootFolder = NotesFolder(null, '/base'); + await cache.load(rootFolder); expect(rootFolder.subFolders.length, 2); expect(rootFolder.notes.length, 1);