part of '../neon_files.dart'; class FilesBrowserView extends StatefulWidget { const FilesBrowserView({ required this.bloc, required this.filesBloc, this.onPickFile, this.enableFileActions = true, this.onlyShowDirectories = false, super.key, // ignore: prefer_asserts_with_message }) : assert((onPickFile == null) == onlyShowDirectories); final FilesBrowserBloc bloc; final FilesBloc filesBloc; final Function(FileDetails)? onPickFile; final bool enableFileActions; final bool onlyShowDirectories; @override State createState() => _FilesBrowserViewState(); } class _FilesBrowserViewState extends State { @override void initState() { super.initState(); widget.bloc.errors.listen((final error) { NeonException.showSnackbar(context, error); }); } @override Widget build(final BuildContext context) => ResultBuilder>.behaviorSubject( stream: widget.bloc.files, builder: (final context, final files) => StreamBuilder>( stream: widget.bloc.path, builder: (final context, final pathSnapshot) => StreamBuilder>( stream: widget.filesBloc.tasks, builder: (final context, final tasksSnapshot) => !pathSnapshot.hasData || !tasksSnapshot.hasData ? const SizedBox() : BackButtonListener( onBackButtonPressed: () async { final path = pathSnapshot.requireData; if (path.isNotEmpty) { widget.bloc.setPath(path.sublist(0, path.length - 1)); return true; } return false; }, child: SortBoxBuilder( sortBox: filesSortBox, sortPropertyOption: widget.bloc.options.filesSortPropertyOption, sortBoxOrderOption: widget.bloc.options.filesSortBoxOrderOption, input: files.data, builder: (final context, final sorted) => NeonListView( scrollKey: 'files-${pathSnapshot.requireData.join('/')}', withFloatingActionButton: true, items: [ for (final uploadTask in tasksSnapshot.requireData.whereType().where( (final task) => sorted.where((final file) => _pathMatchesFile(task.path, file.name)).isEmpty, )) ...[ FileListTile( bloc: widget.filesBloc, browserBloc: widget.bloc, details: FileDetails.fromUploadTask( task: uploadTask, ), enableFileActions: widget.enableFileActions, onPickFile: widget.onPickFile, ), ], 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, ); return FileListTile( bloc: widget.filesBloc, browserBloc: widget.bloc, details: details, enableFileActions: widget.enableFileActions, onPickFile: widget.onPickFile, ); }, ), ], ], ], isLoading: files.isLoading, error: files.error, onRefresh: widget.bloc.refresh, builder: (final context, final widget) => widget, topScrollingChildren: [ Align( alignment: Alignment.topLeft, child: Container( margin: const EdgeInsets.symmetric( horizontal: 10, ), child: Wrap( crossAxisAlignment: WrapCrossAlignment.center, children: [ IconButton( padding: EdgeInsets.zero, visualDensity: const VisualDensity( horizontal: VisualDensity.minimumDensity, vertical: VisualDensity.minimumDensity, ), tooltip: AppLocalizations.of(context).goToPath(''), icon: const Icon( Icons.house, size: 30, ), onPressed: () { widget.bloc.setPath([]); }, ), for (var i = 0; i < pathSnapshot.requireData.length; i++) ...[ Builder( builder: (final context) { final path = pathSnapshot.requireData.sublist(0, i + 1); return Tooltip( message: AppLocalizations.of(context).goToPath(path.join('/')), excludeFromSemantics: true, child: TextButton( onPressed: () { widget.bloc.setPath(path); }, child: Text( pathSnapshot.requireData[i], semanticsLabel: AppLocalizations.of(context).goToPath(path.join('/')), ), ), ); }, ), ], ] .intersperse( const Icon( Icons.keyboard_arrow_right, size: 30, ), ) .toList(), ), ), ), ], ), ), ), ), ), ); bool _pathMatchesFile(final List path, final String name) => const ListEquality().equals( [...widget.bloc.path.value, name], path, ); }