From 3a2a57f9856d15f55f8bdcf701ef975b3336046c Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Sun, 1 Dec 2019 10:41:35 +0100 Subject: [PATCH] Move Folder loading logic inside NoteFolder --- lib/note_folder.dart | 28 ++++++++++++++++++++++++++++ lib/storage/git_storage.dart | 32 ++------------------------------ 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/lib/note_folder.dart b/lib/note_folder.dart index 19ba13bc..344e36d9 100644 --- a/lib/note_folder.dart +++ b/lib/note_folder.dart @@ -1,3 +1,5 @@ +import 'dart:io'; + import 'package:gitjournal/note.dart'; class NoteFSEntity { @@ -37,4 +39,30 @@ class NoteFolder { } return notes; } + + // FIXME: This asynchronously loads everything. Maybe it should just list them, and the individual entities + // should be loaded as required? + Future load() async { + final dir = Directory(folderPath); + + var lister = dir.list(recursive: false, followLinks: false); + await for (var fsEntity in lister) { + if (fsEntity is Directory) { + var subFolder = NoteFolder(fsEntity.path); + await subFolder.load(); + + var noteFSEntity = NoteFSEntity(this, folder: subFolder); + entities.add(noteFSEntity); + } + + var note = Note(fsEntity.path); + if (!note.filePath.toLowerCase().endsWith('.md')) { + continue; + } + await note.load(); + + var noteFSEntity = NoteFSEntity(this, note: note); + entities.add(noteFSEntity); + } + } } diff --git a/lib/storage/git_storage.dart b/lib/storage/git_storage.dart index efd54f68..c9a4295a 100644 --- a/lib/storage/git_storage.dart +++ b/lib/storage/git_storage.dart @@ -1,5 +1,4 @@ import 'dart:async'; -import 'dart:io'; import 'package:fimber/fimber.dart'; import 'package:flutter/foundation.dart'; @@ -77,41 +76,14 @@ class GitNoteRepository { } Future> listNotes() async { - var noteFolder = await loadFolder(NoteFolder(notesBasePath)); + var noteFolder = NoteFolder(notesBasePath); + await noteFolder.load(); var notes = noteFolder.getAllNotes(); notes.sort((a, b) => b.compareTo(a)); return notes; } - // FIXME: This asynchronously loads everything. Maybe it should just list them, and the individual entities - // should be loaded as required? - Future loadFolder(NoteFolder rootFolder) async { - final dir = Directory(rootFolder.folderPath); - - var lister = dir.list(recursive: false, followLinks: false); - await for (var fsEntity in lister) { - if (fsEntity is Directory) { - var subFolder = NoteFolder(fsEntity.path); - subFolder = await loadFolder(subFolder); - - var noteFSEntity = NoteFSEntity(rootFolder, folder: subFolder); - rootFolder.entities.add(noteFSEntity); - } - - var note = Note(fsEntity.path); - if (!note.filePath.toLowerCase().endsWith('.md')) { - continue; - } - await note.load(); - - var noteFSEntity = NoteFSEntity(rootFolder, note: note); - rootFolder.entities.add(noteFSEntity); - } - - return rootFolder; - } - Future sync() async { try { await _gitRepo.pull();