Browse Source

refactor(neon_files): add FilesBrowserMode for cleaner apis

Signed-off-by: Nikolas Rimikis <rimikis.nikolas@gmail.com>
pull/604/head
Nikolas Rimikis 1 year ago
parent
commit
edc6f58df8
No known key found for this signature in database
GPG Key ID: 85ED1DE9786A4FF2
  1. 3
      packages/neon/neon_files/lib/dialogs/choose_folder.dart
  2. 15
      packages/neon/neon_files/lib/pages/main.dart
  3. 37
      packages/neon/neon_files/lib/widgets/browser_view.dart
  4. 110
      packages/neon/neon_files/lib/widgets/file_list_tile.dart

3
packages/neon/neon_files/lib/dialogs/choose_folder.dart

@ -25,8 +25,7 @@ class FilesChooseFolderDialog extends StatelessWidget {
child: FilesBrowserView( child: FilesBrowserView(
bloc: bloc, bloc: bloc,
filesBloc: filesBloc, filesBloc: filesBloc,
enableFileActions: false, mode: FilesBrowserMode.selectDirectory,
onlyShowDirectories: true,
), ),
), ),
StreamBuilder<List<String>>( StreamBuilder<List<String>>(

15
packages/neon/neon_files/lib/pages/main.dart

@ -27,21 +27,6 @@ class _FilesMainPageState extends State<FilesMainPage> {
body: FilesBrowserView( body: FilesBrowserView(
bloc: bloc.browser, bloc: bloc.browser,
filesBloc: bloc, filesBloc: bloc,
onPickFile: (final details) async {
final sizeWarning = bloc.options.downloadSizeWarning.value;
if (sizeWarning != null && details.size != null && details.size! > sizeWarning) {
if (!(await showConfirmationDialog(
context,
AppLocalizations.of(context).downloadConfirmSizeWarning(
filesize(sizeWarning),
filesize(details.size),
),
))) {
return;
}
}
bloc.openFile(details.path, details.etag!, details.mimeType);
},
), ),
floatingActionButton: FloatingActionButton( floatingActionButton: FloatingActionButton(
onPressed: () async { onPressed: () async {

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

@ -1,21 +1,30 @@
part of '../neon_files.dart'; part of '../neon_files.dart';
/// Mode to operate the [FilesBrowserView] in.
enum FilesBrowserMode {
/// Default file browser mode.
///
/// When a file is selecteed it will be opened or downloaded.
browser,
/// Select directory.
selectDirectory,
/// Don't show file actions.
noActions,
}
class FilesBrowserView extends StatefulWidget { class FilesBrowserView extends StatefulWidget {
const FilesBrowserView({ const FilesBrowserView({
required this.bloc, required this.bloc,
required this.filesBloc, required this.filesBloc,
this.onPickFile, this.mode = FilesBrowserMode.browser,
this.enableFileActions = true,
this.onlyShowDirectories = false,
super.key, super.key,
// ignore: prefer_asserts_with_message });
}) : assert((onPickFile == null) == onlyShowDirectories);
final FilesBrowserBloc bloc; final FilesBrowserBloc bloc;
final FilesBloc filesBloc; final FilesBloc filesBloc;
final Function(FileDetails)? onPickFile; final FilesBrowserMode mode;
final bool enableFileActions;
final bool onlyShowDirectories;
@override @override
State<FilesBrowserView> createState() => _FilesBrowserViewState(); State<FilesBrowserView> createState() => _FilesBrowserViewState();
@ -24,11 +33,11 @@ class FilesBrowserView extends StatefulWidget {
class _FilesBrowserViewState extends State<FilesBrowserView> { class _FilesBrowserViewState extends State<FilesBrowserView> {
@override @override
void initState() { void initState() {
super.initState();
widget.bloc.errors.listen((final error) { widget.bloc.errors.listen((final error) {
NeonException.showSnackbar(context, error); NeonException.showSnackbar(context, error);
}); });
super.initState();
} }
@override @override
@ -68,12 +77,11 @@ class _FilesBrowserViewState extends State<FilesBrowserView> {
details: FileDetails.fromUploadTask( details: FileDetails.fromUploadTask(
task: uploadTask, task: uploadTask,
), ),
enableFileActions: widget.enableFileActions, mode: widget.mode,
onPickFile: widget.onPickFile,
), ),
], ],
for (final file in sorted) ...[ for (final file in sorted) ...[
if (!widget.onlyShowDirectories || file.isDirectory) ...[ if (widget.mode != FilesBrowserMode.selectDirectory || file.isDirectory) ...[
Builder( Builder(
builder: (final context) { builder: (final context) {
final matchingTask = tasksSnapshot.requireData final matchingTask = tasksSnapshot.requireData
@ -93,8 +101,7 @@ class _FilesBrowserViewState extends State<FilesBrowserView> {
bloc: widget.filesBloc, bloc: widget.filesBloc,
browserBloc: widget.bloc, browserBloc: widget.bloc,
details: details, details: details,
enableFileActions: widget.enableFileActions, mode: widget.mode,
onPickFile: widget.onPickFile,
); );
}, },
), ),

110
packages/neon/neon_files/lib/widgets/file_list_tile.dart

@ -1,7 +1,9 @@
import 'package:filesize/filesize.dart'; import 'package:filesize/filesize.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:material_design_icons_flutter/material_design_icons_flutter.dart'; import 'package:material_design_icons_flutter/material_design_icons_flutter.dart';
import 'package:neon/utils.dart';
import 'package:neon/widgets.dart'; import 'package:neon/widgets.dart';
import 'package:neon_files/l10n/localizations.dart';
import 'package:neon_files/neon_files.dart'; import 'package:neon_files/neon_files.dart';
import 'package:neon_files/widgets/actions.dart'; import 'package:neon_files/widgets/actions.dart';
@ -10,66 +12,72 @@ class FileListTile extends StatelessWidget {
required this.bloc, required this.bloc,
required this.browserBloc, required this.browserBloc,
required this.details, required this.details,
this.enableFileActions = true, this.mode = FilesBrowserMode.browser,
this.onPickFile,
super.key, super.key,
}); });
final FilesBloc bloc; final FilesBloc bloc;
final FilesBrowserBloc browserBloc; final FilesBrowserBloc browserBloc;
final FileDetails details; final FileDetails details;
final bool enableFileActions; final FilesBrowserMode mode;
final Function(FileDetails)? onPickFile;
@override Future<void> _onTap(final BuildContext context, final FileDetails details) async {
Widget build(final BuildContext context) { if (details.isDirectory) {
// When the ETag is null it means we are uploading this file right now browserBloc.setPath(details.path);
final onTap = details.isDirectory || details.etag != null } else if (mode == FilesBrowserMode.browser) {
? () { final sizeWarning = bloc.options.downloadSizeWarning.value;
if (details.isDirectory) { if (sizeWarning != null && details.size != null && details.size! > sizeWarning) {
browserBloc.setPath(details.path); if (!(await showConfirmationDialog(
} else { context,
onPickFile?.call(details); AppLocalizations.of(context).downloadConfirmSizeWarning(
} filesize(sizeWarning),
} filesize(details.size),
: null; ),
))) {
return;
}
}
bloc.openFile(details.path, details.etag!, details.mimeType);
}
}
return ListTile( @override
onTap: onTap, Widget build(final BuildContext context) => ListTile(
title: Text( // When the ETag is null it means we are uploading this file right now
details.name, onTap: details.isDirectory || details.etag != null ? () async => _onTap(context, details) : null,
overflow: TextOverflow.ellipsis, title: Text(
), details.name,
subtitle: Row( overflow: TextOverflow.ellipsis,
children: [ ),
if (details.lastModified != null) subtitle: Row(
RelativeTime( children: [
date: details.lastModified!, if (details.lastModified != null)
), RelativeTime(
if (details.size != null && details.size! > 0) ...[ date: details.lastModified!,
const SizedBox( ),
width: 10, if (details.size != null && details.size! > 0) ...[
), const SizedBox(
Text( width: 10,
filesize(details.size, 1), ),
style: DefaultTextStyle.of(context).style.copyWith( Text(
color: Colors.grey, filesize(details.size, 1),
), style: DefaultTextStyle.of(context).style.copyWith(
), color: Colors.grey,
),
),
],
], ],
], ),
), leading: _FileIcon(
leading: _FileIcon( details: details,
details: details, bloc: bloc,
bloc: bloc, ),
), trailing: !details.hasTask && mode != FilesBrowserMode.noActions
trailing: !details.hasTask && enableFileActions ? FileActions(details: details)
? FileActions(details: details) : const SizedBox.square(
: const SizedBox.square( dimension: 48,
dimension: 48, ),
), );
);
}
} }
class _FileIcon extends StatelessWidget { class _FileIcon extends StatelessWidget {

Loading…
Cancel
Save