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: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 {
const SortBoxBuilder({
SortBoxBuilder({
required this.sortBox,
required this.sortPropertyOption,
required this.sortBoxOrderOption,
required this.input,
required final List<R>? input,
required this.builder,
super.key,
});
}) : input = input ?? [];
final SortBox<T, R> sortBox;
final SelectOption<T> sortPropertyOption;
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
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<T>(
@ -31,7 +44,7 @@ class SortBoxBuilder<T extends Enum, R> 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));
},
),
);

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

@ -60,8 +60,7 @@ class _FilesBrowserViewState extends State<FilesBrowserView> {
items: [
for (final uploadTask in tasksSnapshot.requireData.whereType<FilesUploadTask>().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<FilesBrowserView> {
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,
);
},
),
],
],
],

4
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,

Loading…
Cancel
Save