diff --git a/lib/appstate.dart b/lib/appstate.dart index bc7f7b21..31827c7d 100644 --- a/lib/appstate.dart +++ b/lib/appstate.dart @@ -1,6 +1,7 @@ import 'package:shared_preferences/shared_preferences.dart'; import 'package:fimber/fimber.dart'; import 'package:gitjournal/note.dart'; +import 'package:gitjournal/note_folder.dart'; class AppState { // @@ -27,6 +28,7 @@ class AppState { } List notes = []; + NoteFolder noteFolder; AppState(SharedPreferences pref) { localGitRepoConfigured = pref.getBool("localGitRepoConfigured") ?? false; diff --git a/lib/note_folder.dart b/lib/note_folder.dart index c0ce83dd..7ff4966f 100644 --- a/lib/note_folder.dart +++ b/lib/note_folder.dart @@ -57,14 +57,19 @@ class NoteFolder { // FIXME: This asynchronously loads everything. Maybe it should just list them, and the individual entities // should be loaded as required? - Future load() async { + // FIXME: This should not reconstruct the Notes or NotesFolders once constructed. + Future loadRecursively() async { final dir = Directory(folderPath); + entities = []; 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(); + if (subFolder.name.startsWith('.')) { + continue; + } + await subFolder.loadRecursively(); var noteFSEntity = NoteFSEntity(this, folder: subFolder); entities.add(noteFSEntity); diff --git a/lib/screens/folder_listing.dart b/lib/screens/folder_listing.dart index 1e59485b..8a6d01df 100644 --- a/lib/screens/folder_listing.dart +++ b/lib/screens/folder_listing.dart @@ -2,29 +2,21 @@ import 'package:flutter/material.dart'; import 'package:gitjournal/widgets/app_bar_menu_button.dart'; import 'package:gitjournal/widgets/app_drawer.dart'; +import 'package:gitjournal/state_container.dart'; + +import 'package:gitjournal/note_folder.dart'; typedef void ParentSelectChanged(bool isSelected); -/// # Tree View -/// -/// Creates a tree view widget. The widget is a List View with a [List] of -/// [Parent] widgets. The [TreeView] is nested inside a [Scrollbar] if the -/// [TreeView.hasScrollBar] property is true. class TreeView extends StatelessWidget { final List parentList; - final bool hasScrollBar; TreeView({ this.parentList = const [], - this.hasScrollBar = false, }); @override Widget build(BuildContext context) { - return hasScrollBar ? Scrollbar(child: _getTreeList()) : _getTreeList(); - } - - Widget _getTreeList() { return ListView.builder( itemBuilder: (context, index) { return parentList[index]; @@ -130,28 +122,11 @@ class ChildList extends StatelessWidget { class FolderListingScreen extends StatelessWidget { @override Widget build(BuildContext context) { + final container = StateContainer.of(context); + final appState = container.appState; + var treeView = TreeView( - parentList: [ - Parent( - parent: _buildFolderTile("Desktop"), - childList: ChildList( - children: [ - Parent( - parent: _buildFolderTile('documents'), - childList: ChildList( - children: [ - _buildFolderTile('Resume.docx'), - _buildFolderTile('Billing-Info.docx'), - ], - ), - ), - _buildFolderTile('MeetingReport.xls'), - _buildFolderTile('MeetingReport.pdf'), - _buildFolderTile('Demo.zip'), - ], - ), - ), - ], + parentList: _constructParentList(appState.noteFolder.entities), ); return Scaffold( @@ -164,6 +139,43 @@ class FolderListingScreen extends StatelessWidget { ); } + List _constructParentList(List entities) { + var parents = []; + entities.forEach((entity) { + if (entity.isNote) { + return; + } + + var folder = entity.folder; + var p = Parent( + parent: _buildFolderTile(folder.name), + childList: _constructChildList(folder.entities), + ); + + parents.add(p); + }); + return parents; + } + + ChildList _constructChildList(List entities) { + var children = []; + entities.forEach((entity) { + if (entity.isNote) { + return; + } + + var folder = entity.folder; + var p = Parent( + parent: _buildFolderTile(folder.name), + childList: _constructChildList(folder.entities), + ); + + children.add(p); + }); + + return ChildList(children: children); + } + Widget _buildFolderTile(String name) { // FIXME: The trailing icon should change based on if it is expanded or not diff --git a/lib/state_container.dart b/lib/state_container.dart index b4200e14..afb02a5c 100644 --- a/lib/state_container.dart +++ b/lib/state_container.dart @@ -8,6 +8,7 @@ import 'package:gitjournal/analytics.dart'; import 'package:gitjournal/apis/git_migration.dart'; import 'package:gitjournal/appstate.dart'; import 'package:gitjournal/note.dart'; +import 'package:gitjournal/note_folder.dart'; import 'package:gitjournal/note_fileName.dart'; import 'package:gitjournal/storage/git_storage.dart'; import 'package:path/path.dart' as p; @@ -56,6 +57,7 @@ class StateContainerState extends State { dirName: appState.localGitRepoPath, ); } + appState.noteFolder = NoteFolder(noteRepo.notesBasePath); // Just a fail safe if (!appState.remoteGitRepoConfigured) { @@ -77,9 +79,17 @@ class StateContainerState extends State { } } + Future> _loadNotes() async { + await appState.noteFolder.loadRecursively(); + var notes = appState.noteFolder.getAllNotes(); + notes.sort((a, b) => b.compareTo(a)); + + return notes; + } + void _loadNotesFromDisk() { Fimber.d("Loading Notes From Disk"); - noteRepo.listNotes().then((loadedNotes) { + _loadNotes().then((loadedNotes) { setState(() { appState.notes = loadedNotes; @@ -114,7 +124,7 @@ class StateContainerState extends State { await noteRepo.sync(); try { - var loadedNotes = await noteRepo.listNotes(); + var loadedNotes = await _loadNotes(); setState(() { appState.notes = loadedNotes; }); @@ -161,17 +171,6 @@ class StateContainerState extends State { }); } - /* - String _getGitDir(BuildContext context) { - var state = StateContainer.of(context).appState; - if (state.remoteGitRepoConfigured) { - return state.remoteGitRepoFolderName; - } else { - return state.localGitRepoPath; - } - } - */ - void undoRemoveNote(Note note, int index) { setState(() { appState.notes.insert(index, note); @@ -226,6 +225,7 @@ class StateContainerState extends State { baseDirectory: appState.gitBaseDirectory, dirName: appState.remoteGitRepoFolderName, ); + appState.noteFolder = NoteFolder(noteRepo.notesBasePath); await _persistConfig(); _loadNotesFromDisk(); diff --git a/lib/storage/git_storage.dart b/lib/storage/git_storage.dart index 77caee67..c3d07e12 100644 --- a/lib/storage/git_storage.dart +++ b/lib/storage/git_storage.dart @@ -4,7 +4,6 @@ import 'package:fimber/fimber.dart'; import 'package:flutter/foundation.dart'; import 'package:gitjournal/apis/git.dart'; import 'package:gitjournal/note.dart'; -import 'package:gitjournal/note_folder.dart'; import 'package:gitjournal/settings.dart'; import 'package:path/path.dart' as p; @@ -73,15 +72,6 @@ class GitNoteRepository { return _addNote(note, "Edited Journal Entry"); } - Future> listNotes() async { - var noteFolder = NoteFolder(notesBasePath); - await noteFolder.load(); - var notes = noteFolder.getAllNotes(); - notes.sort((a, b) => b.compareTo(a)); - - return notes; - } - Future sync() async { try { await _gitRepo.pull();