From 4bac0c5eecf13c0e41ff263b042280e585960fc9 Mon Sep 17 00:00:00 2001 From: Nikolas Rimikis Date: Fri, 18 Aug 2023 09:27:25 +0200 Subject: [PATCH] refactor(neon,neon_files,neon_notes): improve nullability of SortBoxBuilder Signed-off-by: Nikolas Rimikis --- .../lib/src/sort_box/sort_box_builder.dart | 29 +++++++--- .../neon_files/lib/widgets/browser_view.dart | 53 +++++++++---------- .../neon_notes/lib/widgets/notes_view.dart | 4 +- 3 files changed, 48 insertions(+), 38 deletions(-) diff --git a/packages/neon/neon/lib/src/sort_box/sort_box_builder.dart b/packages/neon/neon/lib/src/sort_box/sort_box_builder.dart index 276338fb..6c256e78 100644 --- a/packages/neon/neon/lib/src/sort_box/sort_box_builder.dart +++ b/packages/neon/neon/lib/src/sort_box/sort_box_builder.dart @@ -2,26 +2,39 @@ import 'package:flutter/widgets.dart'; import 'package:neon/src/settings/models/select_option.dart'; import 'package:sort_box/sort_box.dart'; +/// Signature for a function that creates a widget for a given sorted list. +/// +/// Used by [SortBoxBuilder] to display a sorted list of items. +typedef SortBoxWidgetBuilder = Widget Function(BuildContext context, List sorted); + +/// Sorted list builder. +/// +/// Used together with a [SortBox] to sort a given list. class SortBoxBuilder extends StatelessWidget { - const SortBoxBuilder({ + SortBoxBuilder({ required this.sortBox, required this.sortPropertyOption, required this.sortBoxOrderOption, - required this.input, + required final List? input, required this.builder, super.key, - }); + }) : input = input ?? []; final SortBox sortBox; final SelectOption sortPropertyOption; final SelectOption sortBoxOrderOption; - final List? input; - final Widget Function(BuildContext, List?) builder; + + /// Input list to sort. + final List input; + + /// Child builder using the sorted list. + final SortBoxWidgetBuilder builder; @override Widget build(final BuildContext context) { - if (input == null || (input?.isEmpty ?? false)) { - return builder(context, null); + if (input.length <= 1) { + // input is already sorted. + return builder(context, input); } return ValueListenableBuilder( @@ -31,7 +44,7 @@ class SortBoxBuilder extends StatelessWidget { builder: (final context, final order, final _) { final box = Box(property, order); - return builder(context, sortBox.sort(input!, box)); + return builder(context, sortBox.sort(input, box)); }, ), ); diff --git a/packages/neon/neon_files/lib/widgets/browser_view.dart b/packages/neon/neon_files/lib/widgets/browser_view.dart index 912daeac..2d06b8aa 100644 --- a/packages/neon/neon_files/lib/widgets/browser_view.dart +++ b/packages/neon/neon_files/lib/widgets/browser_view.dart @@ -60,8 +60,7 @@ class _FilesBrowserViewState extends State { items: [ for (final uploadTask in tasksSnapshot.requireData.whereType().where( (final task) => - sorted?.where((final file) => _pathMatchesFile(task.path, file.name)).isEmpty ?? - false, + sorted.where((final file) => _pathMatchesFile(task.path, file.name)).isEmpty, )) ...[ FileListTile( bloc: widget.filesBloc, @@ -73,34 +72,32 @@ class _FilesBrowserViewState extends State { onPickFile: widget.onPickFile, ), ], - if (sorted != null) ...[ - for (final file in sorted) ...[ - if (!widget.onlyShowDirectories || file.isDirectory) ...[ - Builder( - builder: (final context) { - final matchingTask = tasksSnapshot.requireData - .firstWhereOrNull((final task) => _pathMatchesFile(task.path, file.name)); + for (final file in sorted) ...[ + if (!widget.onlyShowDirectories || file.isDirectory) ...[ + Builder( + builder: (final context) { + final matchingTask = tasksSnapshot.requireData + .firstWhereOrNull((final task) => _pathMatchesFile(task.path, file.name)); - final details = matchingTask != null - ? FileDetails.fromTask( - task: matchingTask, - file: file, - ) - : FileDetails.fromWebDav( - file: file, - path: widget.bloc.path.value, - ); + final details = matchingTask != null + ? FileDetails.fromTask( + task: matchingTask, + file: file, + ) + : FileDetails.fromWebDav( + file: file, + path: widget.bloc.path.value, + ); - return FileListTile( - bloc: widget.filesBloc, - browserBloc: widget.bloc, - details: details, - enableFileActions: widget.enableFileActions, - onPickFile: widget.onPickFile, - ); - }, - ), - ], + return FileListTile( + bloc: widget.filesBloc, + browserBloc: widget.bloc, + details: details, + enableFileActions: widget.enableFileActions, + onPickFile: widget.onPickFile, + ); + }, + ), ], ], ], diff --git a/packages/neon/neon_notes/lib/widgets/notes_view.dart b/packages/neon/neon_notes/lib/widgets/notes_view.dart index 8042fe5b..3926f975 100644 --- a/packages/neon/neon_notes/lib/widgets/notes_view.dart +++ b/packages/neon/neon_notes/lib/widgets/notes_view.dart @@ -31,8 +31,8 @@ class NotesView extends StatelessWidget { scrollKey: 'notes-notes', withFloatingActionButton: true, items: [ - ...?sortedFavorites, - ...?sortedNonFavorites, + ...sortedFavorites, + ...sortedNonFavorites, ], isLoading: notes.isLoading, error: notes.error,