Skip to content

Commit

Permalink
Combine list views into one paging controller
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed May 9, 2024
1 parent 09cd6ff commit d5caa30
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 86 deletions.
51 changes: 14 additions & 37 deletions app/lib/pages/notes/navigator/drawer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<NoteFilter>? onFilterChanged;
final NoteFilter filter;
final bool isSearching;
final SourcedPagingController<Note> controller;

const NotesNavigatorDrawer({
super.key,
this.note,
this.onFilterChanged,
required this.filter,
required this.controller,
required this.isSearching,
});

@override
State<NotesNavigatorDrawer> createState() => _NotesNavigatorDrawerState();
}

class _NotesNavigatorDrawerState extends State<NotesNavigatorDrawer> {
late final SourcedPagingController<Note> _controller;

@override
void initState() {
super.initState();
_controller = SourcedPagingController(context.read<FlowCubit>());
_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(
Expand All @@ -75,29 +52,29 @@ class _NotesNavigatorDrawerState extends State<NotesNavigatorDrawer> {
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,
)),
),
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,
),
),
],
Expand Down
83 changes: 37 additions & 46 deletions app/lib/pages/notes/page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<Multihash>? parent;

Expand All @@ -25,46 +25,11 @@ class NotesPage extends StatefulWidget {
this.filter = const NoteFilter(),
});

@override
_NotesPageState createState() => _NotesPageState();
}

class _NotesPageState extends State<NotesPage> {
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,
);
}
}
Expand Down Expand Up @@ -103,6 +68,7 @@ class _NotesSearchDelegate extends SearchDelegate {
search: query,
filter: filter,
parent: parent,
showAppBar: false,
);
}

Expand All @@ -116,12 +82,14 @@ class NotesBodyView extends StatefulWidget {
final String search;
final NoteFilter filter;
final SourcedModel<Multihash>? parent;
final bool showAppBar;

const NotesBodyView({
super.key,
this.search = '',
this.filter = const NoteFilter(),
this.parent,
this.showAppBar = true,
});

@override
Expand All @@ -141,7 +109,6 @@ class _NotesBodyViewState extends State<NotesBodyView> {
_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!,
Expand Down Expand Up @@ -201,7 +168,29 @@ class _NotesBodyViewState extends State<NotesBodyView> {

@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(
Expand Down Expand Up @@ -230,12 +219,14 @@ class _NotesBodyViewState extends State<NotesBodyView> {
],
);
}),
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(
Expand Down
6 changes: 3 additions & 3 deletions app/lib/widgets/navigation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit d5caa30

Please sign in to comment.