From b85f9028febfadf4985cef1d56325bb066439dfa Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Fri, 31 Jul 2020 20:36:20 +0200 Subject: [PATCH] Half implement Note selection in a FolderView This allows you to long press on a note and select it, thereby allowing you to perform actions on it, without opening the note. This is disabled for note, as it isn't completely implemented. I'm not sure how to pass down the informatin on which Note is selected. --- lib/folder_views/common.dart | 36 +++--- lib/screens/folder_view.dart | 154 +++++++++++++++++--------- lib/widgets/note_search_delegate.dart | 13 ++- 3 files changed, 125 insertions(+), 78 deletions(-) diff --git a/lib/folder_views/common.dart b/lib/folder_views/common.dart index 3460b070..ed541e7e 100644 --- a/lib/folder_views/common.dart +++ b/lib/folder_views/common.dart @@ -20,23 +20,23 @@ enum FolderViewType { Grid, } -Widget buildFolderView( - BuildContext context, - FolderViewType viewType, - NotesFolder folder, - String emptyText, - StandardViewHeader header, - bool showSummary, -) { - var noteSelectionFn = (Note note) => openNoteEditor(context, note); - var noteLongPressedFn = (Note note) {}; +typedef void NoteSelectedFunction(Note note); +Widget buildFolderView({ + @required FolderViewType viewType, + @required NotesFolder folder, + @required String emptyText, + @required StandardViewHeader header, + @required bool showSummary, + @required NoteSelectedFunction noteTapped, + @required NoteSelectedFunction noteLongPressed, +}) { switch (viewType) { case FolderViewType.Standard: return StandardView( folder: folder, - noteTapped: noteSelectionFn, - noteLongPressed: noteLongPressedFn, + noteTapped: noteTapped, + noteLongPressed: noteLongPressed, emptyText: emptyText, headerType: header, showSummary: showSummary, @@ -44,22 +44,22 @@ Widget buildFolderView( case FolderViewType.Journal: return JournalView( folder: folder, - noteTapped: noteSelectionFn, - noteLongPressed: noteLongPressedFn, + noteTapped: noteTapped, + noteLongPressed: noteLongPressed, emptyText: emptyText, ); case FolderViewType.Card: return CardView( folder: folder, - noteTapped: noteSelectionFn, - noteLongPressed: noteLongPressedFn, + noteTapped: noteTapped, + noteLongPressed: noteLongPressed, emptyText: emptyText, ); case FolderViewType.Grid: return GridFolderView( folder: folder, - noteTapped: noteSelectionFn, - noteLongPressed: noteLongPressedFn, + noteTapped: noteTapped, + noteLongPressed: noteLongPressed, emptyText: emptyText, ); } diff --git a/lib/screens/folder_view.dart b/lib/screens/folder_view.dart index 5e5a10c1..daf27bf2 100644 --- a/lib/screens/folder_view.dart +++ b/lib/screens/folder_view.dart @@ -2,6 +2,7 @@ import 'package:flutter/material.dart'; import 'package:font_awesome_flutter/font_awesome_flutter.dart'; import 'package:git_bindings/git_bindings.dart'; +import 'package:gitjournal/core/note.dart'; import 'package:provider/provider.dart'; import 'package:gitjournal/core/notes_folder.dart'; @@ -45,6 +46,9 @@ class _FolderViewState extends State { StandardViewHeader _headerType = StandardViewHeader.TitleGenerated; bool _showSummary = true; + bool inSelectionMode = false; + Note selectedNote; + var _scaffoldKey = GlobalKey(); @override @@ -62,9 +66,6 @@ class _FolderViewState extends State { @override Widget build(BuildContext context) { - var container = Provider.of(context); - final appState = container.appState; - var createButton = FloatingActionButton( key: const ValueKey("FAB"), onPressed: () => @@ -72,17 +73,40 @@ class _FolderViewState extends State { child: const Icon(Icons.add), ); - String title = widget.notesFolder.publicName; + var title = widget.notesFolder.publicName; + if (inSelectionMode) { + title = "Note Selected"; + } + Widget folderView = Builder( builder: (BuildContext context) { const emptyText = "Let's add some notes?"; return buildFolderView( - context, - _viewType, - sortedNotesFolder, - emptyText, - _headerType, - _showSummary, + viewType: _viewType, + folder: sortedNotesFolder, + emptyText: emptyText, + header: _headerType, + showSummary: _showSummary, + noteTapped: (Note note) { + if (!inSelectionMode) { + openNoteEditor(context, note); + } else { + setState(() { + inSelectionMode = false; + selectedNote = null; + }); + } + }, + noteLongPressed: (Note note) { + // Disabled for now, until I figure out how to render + // the selected note differently + /* + setState(() { + inSelectionMode = true; + selectedNote = note; + }); + */ + }, ); }, ); @@ -93,57 +117,24 @@ class _FolderViewState extends State { padding: const EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 48.0), ); - var extraAction = PopupMenuButton( - onSelected: (DropDownChoices choice) { - switch (choice) { - case DropDownChoices.SortingOptions: - _sortButtonPressed(); - break; - - case DropDownChoices.ViewOptions: - _configureViewButtonPressed(); - break; - } + var backButton = IconButton( + icon: const Icon(Icons.arrow_back), + onPressed: () { + setState(() { + inSelectionMode = false; + selectedNote = null; + }); }, - itemBuilder: (BuildContext context) => >[ - const PopupMenuItem( - value: DropDownChoices.SortingOptions, - child: Text('Sorting Options'), - ), - if (_viewType == FolderViewType.Standard) - const PopupMenuItem( - value: DropDownChoices.ViewOptions, - child: Text('View Options'), - ), - ], ); return Scaffold( key: _scaffoldKey, appBar: AppBar( title: Text(title), - leading: GJAppBarMenuButton(), - actions: [ - IconButton( - icon: const Icon(Icons.library_books), - onPressed: _folderViewChooserSelected, - key: const ValueKey("FolderViewSelector"), - ), - if (appState.remoteGitRepoConfigured) SyncButton(), - IconButton( - icon: const Icon(Icons.search), - onPressed: () { - showSearch( - context: context, - delegate: NoteSearchDelegate( - sortedNotesFolder.notes, - _viewType, - ), - ); - }, - ), - extraAction, - ], + leading: inSelectionMode ? backButton : GJAppBarMenuButton(), + actions: inSelectionMode + ? _buildInSelectionNoteActions() + : _buildNoteActions(), ), body: Center( child: Builder( @@ -412,4 +403,59 @@ class _FolderViewState extends State { container.saveFolderConfig(widget.notesFolder.config); } } + + List _buildNoteActions() { + final appState = Provider.of(context).appState; + + var extraActions = PopupMenuButton( + onSelected: (DropDownChoices choice) { + switch (choice) { + case DropDownChoices.SortingOptions: + _sortButtonPressed(); + break; + + case DropDownChoices.ViewOptions: + _configureViewButtonPressed(); + break; + } + }, + itemBuilder: (BuildContext context) => >[ + const PopupMenuItem( + value: DropDownChoices.SortingOptions, + child: Text('Sorting Options'), + ), + if (_viewType == FolderViewType.Standard) + const PopupMenuItem( + value: DropDownChoices.ViewOptions, + child: Text('View Options'), + ), + ], + ); + + return [ + IconButton( + icon: const Icon(Icons.library_books), + onPressed: _folderViewChooserSelected, + key: const ValueKey("FolderViewSelector"), + ), + if (appState.remoteGitRepoConfigured) SyncButton(), + IconButton( + icon: const Icon(Icons.search), + onPressed: () { + showSearch( + context: context, + delegate: NoteSearchDelegate( + sortedNotesFolder.notes, + _viewType, + ), + ); + }, + ), + extraActions, + ]; + } + + List _buildInSelectionNoteActions() { + return []; + } } diff --git a/lib/widgets/note_search_delegate.dart b/lib/widgets/note_search_delegate.dart index 2f644aa5..5761f699 100644 --- a/lib/widgets/note_search_delegate.dart +++ b/lib/widgets/note_search_delegate.dart @@ -78,12 +78,13 @@ class NoteSearchDelegate extends SearchDelegate { const emptyText = "No Search Results Found"; return buildFolderView( - context, - viewType, - folder, - emptyText, - StandardViewHeader.TitleOrFileName, - true, + viewType: viewType, + folder: folder, + emptyText: emptyText, + header: StandardViewHeader.TitleOrFileName, + showSummary: true, + noteTapped: (Note note) => openNoteEditor(context, note), + noteLongPressed: (Note note) {}, ); } }