Browse Source

refactor(neon,neon_files,neon_notes): improve nullability of SortBoxBuilder

Signed-off-by: Nikolas Rimikis <rimikis.nikolas@gmail.com>
pull/591/head
Nikolas Rimikis 1 year ago
parent
commit
4bac0c5eec
No known key found for this signature in database
GPG Key ID: 85ED1DE9786A4FF2
  1. 29
      packages/neon/neon/lib/src/sort_box/sort_box_builder.dart
  2. 53
      packages/neon/neon_files/lib/widgets/browser_view.dart
  3. 4
      packages/neon/neon_notes/lib/widgets/notes_view.dart

29
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:neon/src/settings/models/select_option.dart';
import 'package:sort_box/sort_box.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<T> = Widget Function(BuildContext context, List<T> sorted);
/// Sorted list builder.
///
/// Used together with a [SortBox] to sort a given list.
class SortBoxBuilder<T extends Enum, R> extends StatelessWidget { class SortBoxBuilder<T extends Enum, R> extends StatelessWidget {
const SortBoxBuilder({ SortBoxBuilder({
required this.sortBox, required this.sortBox,
required this.sortPropertyOption, required this.sortPropertyOption,
required this.sortBoxOrderOption, required this.sortBoxOrderOption,
required this.input, required final List<R>? input,
required this.builder, required this.builder,
super.key, super.key,
}); }) : input = input ?? [];
final SortBox<T, R> sortBox; final SortBox<T, R> sortBox;
final SelectOption<T> sortPropertyOption; final SelectOption<T> sortPropertyOption;
final SelectOption<SortBoxOrder> sortBoxOrderOption; final SelectOption<SortBoxOrder> sortBoxOrderOption;
final List<R>? input;
final Widget Function(BuildContext, List<R>?) builder; /// Input list to sort.
final List<R> input;
/// Child builder using the sorted list.
final SortBoxWidgetBuilder<R> builder;
@override @override
Widget build(final BuildContext context) { Widget build(final BuildContext context) {
if (input == null || (input?.isEmpty ?? false)) { if (input.length <= 1) {
return builder(context, null); // input is already sorted.
return builder(context, input);
} }
return ValueListenableBuilder<T>( return ValueListenableBuilder<T>(
@ -31,7 +44,7 @@ class SortBoxBuilder<T extends Enum, R> extends StatelessWidget {
builder: (final context, final order, final _) { builder: (final context, final order, final _) {
final box = Box(property, order); final box = Box(property, order);
return builder(context, sortBox.sort(input!, box)); return builder(context, sortBox.sort(input, box));
}, },
), ),
); );

53
packages/neon/neon_files/lib/widgets/browser_view.dart

@ -60,8 +60,7 @@ class _FilesBrowserViewState extends State<FilesBrowserView> {
items: [ items: [
for (final uploadTask in tasksSnapshot.requireData.whereType<FilesUploadTask>().where( for (final uploadTask in tasksSnapshot.requireData.whereType<FilesUploadTask>().where(
(final task) => (final task) =>
sorted?.where((final file) => _pathMatchesFile(task.path, file.name)).isEmpty ?? sorted.where((final file) => _pathMatchesFile(task.path, file.name)).isEmpty,
false,
)) ...[ )) ...[
FileListTile( FileListTile(
bloc: widget.filesBloc, bloc: widget.filesBloc,
@ -73,34 +72,32 @@ class _FilesBrowserViewState extends State<FilesBrowserView> {
onPickFile: widget.onPickFile, onPickFile: widget.onPickFile,
), ),
], ],
if (sorted != null) ...[ for (final file in sorted) ...[
for (final file in sorted) ...[ if (!widget.onlyShowDirectories || file.isDirectory) ...[
if (!widget.onlyShowDirectories || file.isDirectory) ...[ Builder(
Builder( builder: (final context) {
builder: (final context) { final matchingTask = tasksSnapshot.requireData
final matchingTask = tasksSnapshot.requireData .firstWhereOrNull((final task) => _pathMatchesFile(task.path, file.name));
.firstWhereOrNull((final task) => _pathMatchesFile(task.path, file.name));
final details = matchingTask != null final details = matchingTask != null
? FileDetails.fromTask( ? FileDetails.fromTask(
task: matchingTask, task: matchingTask,
file: file, file: file,
) )
: FileDetails.fromWebDav( : FileDetails.fromWebDav(
file: file, file: file,
path: widget.bloc.path.value, path: widget.bloc.path.value,
); );
return FileListTile( return FileListTile(
bloc: widget.filesBloc, bloc: widget.filesBloc,
browserBloc: widget.bloc, browserBloc: widget.bloc,
details: details, details: details,
enableFileActions: widget.enableFileActions, enableFileActions: widget.enableFileActions,
onPickFile: widget.onPickFile, onPickFile: widget.onPickFile,
); );
}, },
), ),
],
], ],
], ],
], ],

4
packages/neon/neon_notes/lib/widgets/notes_view.dart

@ -31,8 +31,8 @@ class NotesView extends StatelessWidget {
scrollKey: 'notes-notes', scrollKey: 'notes-notes',
withFloatingActionButton: true, withFloatingActionButton: true,
items: [ items: [
...?sortedFavorites, ...sortedFavorites,
...?sortedNonFavorites, ...sortedNonFavorites,
], ],
isLoading: notes.isLoading, isLoading: notes.isLoading,
error: notes.error, error: notes.error,

Loading…
Cancel
Save