From 882c4eb0247527ece3949ededf3a19798a7624a7 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Thu, 14 Feb 2019 11:56:26 +0100 Subject: [PATCH] GitSetup: Add a screen to choose a subFolder This is only shows if there are subFolders with '.md' files. This feature is super important for me as it allows me to finally dogfood this app and use it This patch just adds the screen, but the subFolder selection currently doesn't do anything. --- lib/appstate.dart | 1 + lib/screens/githostsetup_folder.dart | 76 ++++++++++++++++++ lib/screens/githostsetup_screens.dart | 109 ++++++++++++++++++++++---- 3 files changed, 171 insertions(+), 15 deletions(-) create mode 100644 lib/screens/githostsetup_folder.dart diff --git a/lib/appstate.dart b/lib/appstate.dart index 86431ed8..63b490c8 100644 --- a/lib/appstate.dart +++ b/lib/appstate.dart @@ -13,6 +13,7 @@ class AppState { bool onBoardingCompleted = false; // FIXME: Make final + /// This is the directory where all the git repos are stored String gitBaseDirectory = ""; bool isLoadingFromDisk; diff --git a/lib/screens/githostsetup_folder.dart b/lib/screens/githostsetup_folder.dart new file mode 100644 index 00000000..1f981298 --- /dev/null +++ b/lib/screens/githostsetup_folder.dart @@ -0,0 +1,76 @@ +import 'package:flutter/material.dart'; + +import 'githostsetup_button.dart'; + +class GitHostSetupFolderPage extends StatelessWidget { + final List folders; + final Function rootFolderSelected; + final Function subFolderSelected; + + GitHostSetupFolderPage({ + @required this.folders, + @required this.rootFolderSelected, + @required this.subFolderSelected, + }); + + @override + Widget build(BuildContext context) { + var columns = Column( + mainAxisAlignment: MainAxisAlignment.center, + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + 'Would you like to store your journal entries in an existing folder?', + style: Theme.of(context).textTheme.title, + ), + SizedBox(height: 32.0), + FolderListWidget( + folders: folders, + onSelected: subFolderSelected, + ), + SizedBox(height: 16.0), + GitHostSetupButton( + text: "Ignore", + onPressed: rootFolderSelected, + ), + ], + ); + + return Center( + child: SingleChildScrollView( + child: columns, + ), + ); + } +} + +// FIXME: This needs to be made much much prettier! +class FolderListWidget extends StatelessWidget { + final List folders; + final Function onSelected; + + FolderListWidget({ + @required this.folders, + @required this.onSelected, + }); + + @override + Widget build(BuildContext context) { + var buttons = []; + for (var folderName in folders) { + var button = GitHostSetupButton( + text: folderName, + onPressed: () { + onSelected(folderName); + }, + ); + buttons.add(button); + } + + return Column( + mainAxisAlignment: MainAxisAlignment.center, + crossAxisAlignment: CrossAxisAlignment.start, + children: buttons, + ); + } +} diff --git a/lib/screens/githostsetup_screens.dart b/lib/screens/githostsetup_screens.dart index 33842f71..fccb2ccd 100644 --- a/lib/screens/githostsetup_screens.dart +++ b/lib/screens/githostsetup_screens.dart @@ -14,6 +14,7 @@ import 'githostsetup_autoconfigure.dart'; import 'githostsetup_button.dart'; import 'githostsetup_clone.dart'; import 'githostsetup_clone_url.dart'; +import 'githostsetup_folder.dart'; import 'githostsetup_sshkey.dart'; class GitHostSetupScreen extends StatefulWidget { @@ -38,17 +39,16 @@ class GitHostSetupScreenState extends State { PageChoice1.Unknown, ]; - GitHostType _gitHostType = GitHostType.Unknown; - + var _gitHostType = GitHostType.Unknown; var _gitCloneUrl = ""; - String gitCloneErrorMessage = ""; - - String publicKey = ""; + var gitCloneErrorMessage = ""; + var publicKey = ""; + var _subFolders = []; var pageController = PageController(); int _currentPageIndex = 0; - final GlobalKey _scaffoldKey = GlobalKey(); + final _scaffoldKey = GlobalKey(); Widget _buildPage(BuildContext context, int pos) { print("_buildPage " + pos.toString()); @@ -186,11 +186,35 @@ class GitHostSetupScreenState extends State { assert(false); } - assert(_pageChoice[0] != PageChoice0.CustomProvider); - if (pos == 4) { + if (_pageChoice[0] == PageChoice0.CustomProvider) { + return GitHostSetupFolderPage( + folders: _subFolders, + subFolderSelected: _subFolderSelected, + rootFolderSelected: _finish, + ); + } + if (_pageChoice[1] == PageChoice1.Manual) { return GitHostSetupGitClone(errorMessage: gitCloneErrorMessage); + } else if (_pageChoice[1] == PageChoice1.Auto) { + return GitHostSetupFolderPage( + folders: _subFolders, + subFolderSelected: _subFolderSelected, + rootFolderSelected: _finish, + ); + } + } + + assert(_pageChoice[0] != PageChoice0.CustomProvider); + + if (pos == 5) { + if (_pageChoice[1] == PageChoice1.Manual) { + return GitHostSetupFolderPage( + folders: _subFolders, + subFolderSelected: _subFolderSelected, + rootFolderSelected: _finish, + ); } } @@ -371,14 +395,21 @@ class GitHostSetupScreenState extends State { ); gitCloneErrorMessage = error; }); - } else { - getAnalytics().logEvent( - name: "onboarding_complete", - parameters: {}, - ); - Navigator.pop(context); - this.widget.onCompletedFunction(); + return; } + + List subFolders = await _getSubFoldersWithMdFiles(basePath); + if (subFolders.isEmpty) { + print("Found no subfolders with md files"); + _finish(); + return; + } + + setState(() { + _subFolders = subFolders; + _pageCount += 1; + _nextPage(); + }); } Future _removeExistingClone(String baseDirPath) async { @@ -391,6 +422,54 @@ class GitHostSetupScreenState extends State { await baseDir.create(); } } + + Future> _getSubFoldersWithMdFiles(String baseDirPath) async { + var subFolders = []; + + var gitRootDir = Directory(p.join(baseDirPath, "journal")); + var lister = gitRootDir.list(recursive: false); + await for (var fileEntity in lister) { + if (fileEntity is! Directory) { + continue; + } + + Directory dir = fileEntity; + var hasMdFiles = await _hasMdFiles(dir); + if (hasMdFiles) { + subFolders.add(p.basename(dir.path)); + } + } + + return subFolders; + } + + Future _hasMdFiles(Directory dir) async { + var lister = dir.list(recursive: false); + await for (var fileEntity in lister) { + if (fileEntity is! File) { + continue; + } + + if (fileEntity.path.toLowerCase().endsWith('.md')) { + return true; + } + } + + return false; + } + + void _finish() { + getAnalytics().logEvent( + name: "onboarding_complete", + parameters: {}, + ); + Navigator.pop(context); + this.widget.onCompletedFunction(); + } + + void _subFolderSelected(String folder) { + _finish(); + } } class GitHostChoicePage extends StatelessWidget {