diff --git a/client/lib/ui/explorer/explorer_actions.dart b/client/lib/ui/explorer/explorer_actions.dart index c5bb555b..af8d1fde 100644 --- a/client/lib/ui/explorer/explorer_actions.dart +++ b/client/lib/ui/explorer/explorer_actions.dart @@ -45,7 +45,15 @@ class ExplorerActions extends StatelessWidget { final state = context.read(); final r = state.selectedSingle ?? state.focussed; if (r == null) return; - _openResource(context, r); + if (r.dir) { + Actions.maybeInvoke(context, NavToFolderIntent(folderId: r.id)); + } else { + final resources = state.resources.where((r) => !r.dir).toList(growable: false); + final index = resources.indexOf(r); + if (index >= 0) { + ResourcePreview.showPreview(context, resources, index); + } + } return; }), DeleteIntent: CallbackAction(onInvoke: (i) { @@ -95,14 +103,6 @@ class ExplorerActions extends StatelessWidget { ); } - void _openResource(BuildContext context, Resource r) { - if (r.dir) { - Actions.maybeInvoke(context, NavToFolderIntent(folderId: r.id)); - } else { - ResourcePreview.showPreview(context, r); - } - } - void _deleteResources(BuildContext context, Iterable resources) async { final account = context.read(); final confirm = await showAlertDialog( diff --git a/client/lib/ui/preview/resource_preview.dart b/client/lib/ui/preview/resource_preview.dart index 98e3368a..aa8ed06f 100644 --- a/client/lib/ui/preview/resource_preview.dart +++ b/client/lib/ui/preview/resource_preview.dart @@ -9,7 +9,6 @@ import 'package:offtheline/offtheline.dart'; import 'package:phylum/app_shortcuts.dart'; import 'package:phylum/integrations/download_manager.dart'; import 'package:phylum/libphylum/db/db.dart'; -import 'package:phylum/libphylum/explorer/explorer_controller.dart'; import 'package:phylum/libphylum/phylum_account.dart'; import 'package:phylum/libphylum/phylum_api_types.dart'; import 'package:phylum/libphylum/requests/resource_contents_request.dart'; @@ -19,13 +18,14 @@ import 'package:provider/provider.dart'; const maxPreviewSize = 1 * 1024 * 1024; class ResourcePreview extends StatefulWidget { - final Resource resource; + final List resources; + final int index; - ResourcePreview({required this.resource}) : super(key: ValueKey(resource.id)); + const ResourcePreview({super.key, required this.resources, required this.index}); - static Future showPreview(BuildContext context, Resource r) async { - context.read().myListsRepository.markResourceAccessed(r.id); - return showDialog(context: context, builder: (context) => ResourcePreview(resource: r)); + static Future showPreview(BuildContext context, List resources, int index) async { + context.read().myListsRepository.markResourceAccessed(resources[index].id); + return showDialog(context: context, builder: (context) => ResourcePreview(resources: resources, index: index)); } @override @@ -33,6 +33,7 @@ class ResourcePreview extends StatefulWidget { } class _ResourcePreviewState extends State { + late int index; late Resource _resource; final _focusNode = FocusNode(); String? _error; @@ -41,7 +42,8 @@ class _ResourcePreviewState extends State { @override void initState() { super.initState(); - _updateResource(widget.resource); + index = widget.index; + _updateResource(); } @override @@ -50,24 +52,24 @@ class _ResourcePreviewState extends State { super.dispose(); } - void _updateResource(Resource resource) { - this._resource = resource; - if (resource.dir) { + void _updateResource() { + _resource = widget.resources[index]; + if (_resource.dir) { _error = 'Cannot preview directory'; - } else if (resource.contentLength > maxPreviewSize) { + } else if (_resource.contentLength > maxPreviewSize) { _error = 'Resource too large to preview'; } else { - if (resource.contentType.startsWith('image/')) { + if (_resource.contentType.startsWith('image/')) { _buildPreview = buildImagePreview; } - if (resource.contentType.startsWith('text/')) { + if (_resource.contentType.startsWith('text/')) { _buildPreview = buildTextPreview; } - if (resource.contentType == 'application/pdf') { + if (_resource.contentType == 'application/pdf') { _buildPreview = buildPdfPreview; } if (_buildPreview == null) { - _error = 'Unable to preview ${resource.contentType}'; + _error = 'Unable to preview ${_resource.contentType}'; } } } @@ -77,40 +79,26 @@ class _ResourcePreviewState extends State { final theme = Theme.of(context); return Actions( actions: { - DismissIntent: CallbackAction(onInvoke: (i) { - Navigator.of(context).pop(); - }), + DismissIntent: CallbackAction( + onInvoke: (i) => Navigator.of(context).pop(), + ), FocusLeftIntent: CallbackAction(onInvoke: (FocusLeftIntent i) { - final state = context.read(); - final index = state.resources.indexOf(_resource); - if (index == -1) { - return; - } - for (int i = index; i >= 0; i--) { - final r = state.resources[i]; - if (!r.dir) { - return setState(() { - _updateResource(r); - }); + return setState(() { + if (index <= 0) { + index = widget.resources.length; } - } - return; + index--; + _updateResource(); + }); }), FocusRightIntent: CallbackAction(onInvoke: (FocusRightIntent i) { - final state = context.read(); - final index = state.resources.indexOf(_resource); - if (index == -1) { - return; - } - for (int i = index; i < state.resources.length; i++) { - final r = state.resources[i]; - if (!r.dir) { - return setState(() { - _updateResource(r); - }); + return setState(() { + index++; + if (index >= widget.resources.length) { + index = 0; } - } - return; + _updateResource(); + }); }), }, child: GestureDetector(