From d5caa3041862261571ed4058bd2679626ca185ff Mon Sep 17 00:00:00 2001 From: CodeDoctorDE Date: Thu, 9 May 2024 22:14:44 +0200 Subject: [PATCH] Combine list views into one paging controller --- app/lib/pages/notes/navigator/drawer.dart | 51 ++++---------- app/lib/pages/notes/page.dart | 83 ++++++++++------------- app/lib/widgets/navigation.dart | 6 +- 3 files changed, 54 insertions(+), 86 deletions(-) diff --git a/app/lib/pages/notes/navigator/drawer.dart b/app/lib/pages/notes/navigator/drawer.dart index 26d66ed8561..731501e5875 100644 --- a/app/lib/pages/notes/navigator/drawer.dart +++ b/app/lib/pages/notes/navigator/drawer.dart @@ -22,50 +22,27 @@ part '../list.dart'; part 'labels.dart'; part 'notebooks.dart'; -class NotesNavigatorDrawer extends StatefulWidget { +class NotesNavigatorDrawer extends StatelessWidget { final Multihash? note; final ValueChanged? onFilterChanged; final NoteFilter filter; + final bool isSearching; + final SourcedPagingController controller; const NotesNavigatorDrawer({ super.key, this.note, this.onFilterChanged, required this.filter, + required this.controller, + required this.isSearching, }); - @override - State createState() => _NotesNavigatorDrawerState(); -} - -class _NotesNavigatorDrawerState extends State { - late final SourcedPagingController _controller; - - @override - void initState() { - super.initState(); - _controller = SourcedPagingController(context.read()); - _controller.addFetchListener( - (source, service, offset, limit) async => service.note?.getNotes( - offset: offset, - limit: limit, - notebook: widget.filter.notebook, - parent: widget.note, - )); - } - - @override - void dispose() { - super.dispose(); - _controller.dispose(); - } - @override Widget build(BuildContext context) { - final sourcedNotebook = - widget.filter.notebook == null || widget.filter.source == null - ? null - : SourcedModel(widget.filter.source!, widget.filter.notebook); + final sourcedNotebook = filter.notebook == null || filter.source == null + ? null + : SourcedModel(filter.source!, filter.notebook); return DefaultTabController( length: 2, child: Column( @@ -75,11 +52,11 @@ class _NotesNavigatorDrawerState extends State { padding: const EdgeInsets.all(8), child: Column( children: [ - if (widget.note == null) ...[ + if (note == null) ...[ _NotebooksView( model: sourcedNotebook, onChanged: (value) => - widget.onFilterChanged?.call(widget.filter.copyWith( + onFilterChanged?.call(filter.copyWith( notebook: value?.model, source: value?.source, )), @@ -87,17 +64,17 @@ class _NotesNavigatorDrawerState extends State { const Divider(height: 32), ], _NoteLabelsView( - onChanged: widget.onFilterChanged, - filter: widget.filter, + onChanged: onFilterChanged, + filter: filter, ), ], ), ), ), - if (widget.note != null) + if (note != null && !isSearching) Expanded( child: NotesListView( - controller: _controller, + controller: controller, ), ), ], diff --git a/app/lib/pages/notes/page.dart b/app/lib/pages/notes/page.dart index cd915420250..6d77338e58b 100644 --- a/app/lib/pages/notes/page.dart +++ b/app/lib/pages/notes/page.dart @@ -15,7 +15,7 @@ import '../../helpers/sourced_paging_controller.dart'; import 'filter.dart'; import 'view.dart'; -class NotesPage extends StatefulWidget { +class NotesPage extends StatelessWidget { final NoteFilter filter; final SourcedModel? parent; @@ -25,46 +25,11 @@ class NotesPage extends StatefulWidget { this.filter = const NoteFilter(), }); - @override - _NotesPageState createState() => _NotesPageState(); -} - -class _NotesPageState extends State { - late NoteFilter _filter; - - @override - void initState() { - super.initState(); - _filter = widget.filter; - } - @override Widget build(BuildContext context) { - return FlowNavigation( - title: AppLocalizations.of(context).notes, - endDrawer: NotesNavigatorDrawer( - note: widget.parent?.model, - filter: _filter, - onFilterChanged: (value) => setState(() { - _filter = value; - }), - ), - actions: [ - IconButton( - icon: const PhosphorIcon(PhosphorIconsLight.magnifyingGlass), - onPressed: () => showSearch( - context: context, - delegate: _NotesSearchDelegate( - _filter, - widget.parent, - ), - ), - ), - ], - body: NotesBodyView( - filter: _filter, - parent: widget.parent, - ), + return NotesBodyView( + filter: filter, + parent: parent, ); } } @@ -103,6 +68,7 @@ class _NotesSearchDelegate extends SearchDelegate { search: query, filter: filter, parent: parent, + showAppBar: false, ); } @@ -116,12 +82,14 @@ class NotesBodyView extends StatefulWidget { final String search; final NoteFilter filter; final SourcedModel? parent; + final bool showAppBar; const NotesBodyView({ super.key, this.search = '', this.filter = const NoteFilter(), this.parent, + this.showAppBar = true, }); @override @@ -141,7 +109,6 @@ class _NotesBodyViewState extends State { _controller = SourcedPagingController(_flowCubit); _controller.addFetchListener((source, service, offset, limit) async { if (_filter.source != null && _filter.source != source) return null; - if (widget.parent != null && widget.search.isEmpty) return null; final notes = _filter.selectedLabel != null ? await service.labelNote?.getNotes( _filter.selectedLabel!, @@ -201,7 +168,29 @@ class _NotesBodyViewState extends State { @override Widget build(BuildContext context) { - return Scaffold( + return FlowNavigation( + title: widget.showAppBar ? AppLocalizations.of(context).notes : null, + endDrawer: NotesNavigatorDrawer( + note: widget.parent?.model, + filter: _filter, + controller: _controller, + isSearching: widget.search.isNotEmpty, + onFilterChanged: (value) => setState(() { + _filter = value; + }), + ), + actions: [ + IconButton( + icon: const PhosphorIcon(PhosphorIconsLight.magnifyingGlass), + onPressed: () => showSearch( + context: context, + delegate: _NotesSearchDelegate( + _filter, + widget.parent, + ), + ), + ), + ], body: Column( children: [ NoteFilterView( @@ -230,12 +219,14 @@ class _NotesBodyViewState extends State { ], ); }), - const SizedBox(height: 8), - Expanded( - child: NotesListView( - controller: _controller, + if (widget.parent == null || widget.search.isNotEmpty) ...[ + const SizedBox(height: 8), + Expanded( + child: NotesListView( + controller: _controller, + ), ), - ), + ], ], ), floatingActionButton: FloatingActionButton.extended( diff --git a/app/lib/widgets/navigation.dart b/app/lib/widgets/navigation.dart index 3ed697bce6e..4d658434ec1 100644 --- a/app/lib/widgets/navigation.dart +++ b/app/lib/widgets/navigation.dart @@ -119,7 +119,7 @@ class _NativeWindowArea extends StatelessWidget { } class FlowNavigation extends StatelessWidget { - final String title; + final String? title; final Widget body; final PreferredSizeWidget? bottom; final Widget? endDrawer; @@ -145,7 +145,7 @@ class FlowNavigation extends StatelessWidget { const drawer = _FlowDrawer(); PreferredSizeWidget appBar = AppBar( bottom: bottom, - title: Text(title), + title: title == null ? null : Text(title!), toolbarHeight: kAppBarHeight, actions: [ ...actions, @@ -183,7 +183,7 @@ class FlowNavigation extends StatelessWidget { children: [ Expanded( child: Scaffold( - appBar: appBar, + appBar: title == null ? null : appBar, key: _scaffoldKey, drawer: isMobile ? const Drawer(