FolderListing: Show the actual folders

Also don't load hidden folders
This commit is contained in:
Vishesh Handa
2019-12-04 00:35:29 +01:00
parent 3f8159a353
commit 56ccf00260
5 changed files with 66 additions and 57 deletions

View File

@ -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<Note> notes = [];
NoteFolder noteFolder;
AppState(SharedPreferences pref) {
localGitRepoConfigured = pref.getBool("localGitRepoConfigured") ?? false;

View File

@ -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<void> load() async {
// FIXME: This should not reconstruct the Notes or NotesFolders once constructed.
Future<void> 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);

View File

@ -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<Parent> parentList;
final bool hasScrollBar;
TreeView({
this.parentList = const <Parent>[],
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: <Widget>[
Parent(
parent: _buildFolderTile('documents'),
childList: ChildList(
children: <Widget>[
_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<Parent> _constructParentList(List<NoteFSEntity> entities) {
var parents = <Parent>[];
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<NoteFSEntity> entities) {
var children = <Widget>[];
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

View File

@ -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<StateContainer> {
dirName: appState.localGitRepoPath,
);
}
appState.noteFolder = NoteFolder(noteRepo.notesBasePath);
// Just a fail safe
if (!appState.remoteGitRepoConfigured) {
@ -77,9 +79,17 @@ class StateContainerState extends State<StateContainer> {
}
}
Future<List<Note>> _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<StateContainer> {
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<StateContainer> {
});
}
/*
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<StateContainer> {
baseDirectory: appState.gitBaseDirectory,
dirName: appState.remoteGitRepoFolderName,
);
appState.noteFolder = NoteFolder(noteRepo.notesBasePath);
await _persistConfig();
_loadNotesFromDisk();

View File

@ -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<List<Note>> listNotes() async {
var noteFolder = NoteFolder(notesBasePath);
await noteFolder.load();
var notes = noteFolder.getAllNotes();
notes.sort((a, b) => b.compareTo(a));
return notes;
}
Future<bool> sync() async {
try {
await _gitRepo.pull();