mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-07-01 12:23:44 +08:00
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.
This commit is contained in:
@ -20,9 +20,8 @@ class NotesCache {
|
||||
|
||||
void updateCache(NotesFolder rootFolder) {}
|
||||
|
||||
Future<NotesFolder> 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);
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -306,7 +306,7 @@ class NotesFolder
|
||||
}
|
||||
|
||||
List<Note> getAllNotes() {
|
||||
var notes = List.from(_notes);
|
||||
var notes = List<Note>.from(_notes);
|
||||
|
||||
for (var folder in _folders) {
|
||||
notes.addAll(folder.getAllNotes());
|
||||
|
@ -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<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);
|
||||
}
|
||||
|
||||
Future<void> syncNotes({bool doNotThrow = false}) async {
|
||||
|
@ -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);
|
||||
|
Reference in New Issue
Block a user