feat(app): artifact viewer actions are bottom icons; swipe pages; native download, share and open in browser
This commit is contained in:
@@ -23,8 +23,12 @@ class ArtifactFileActions extends StatefulWidget {
|
||||
required this.resolved,
|
||||
this.fetch = fetchArtifact,
|
||||
this.bridge = const ArtifactFileBridge(),
|
||||
this.compact = false,
|
||||
});
|
||||
|
||||
/// Icon buttons only (a viewer's bottom bar): no labels and no notices.
|
||||
final bool compact;
|
||||
|
||||
final ArtifactRecord artifact;
|
||||
final ResolvedArtifactUrl resolved;
|
||||
final ArtifactFetch fetch;
|
||||
@@ -74,10 +78,12 @@ class _ArtifactFileActionsState extends State<ArtifactFileActions> {
|
||||
Widget build(BuildContext context) {
|
||||
return ListenableBuilder(
|
||||
listenable: _controller,
|
||||
builder: (context, _) => _ActionBar(
|
||||
controller: _controller,
|
||||
cupertino: usesCupertinoHost(context),
|
||||
),
|
||||
builder: (context, _) => widget.compact
|
||||
? _IconActions(controller: _controller)
|
||||
: _ActionBar(
|
||||
controller: _controller,
|
||||
cupertino: usesCupertinoHost(context),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -185,3 +191,85 @@ class _ActionBar extends StatelessWidget {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Download and Share as icon buttons; a spinner stands in for the one in
|
||||
/// progress. A host that cannot download or share simply has no button.
|
||||
class _IconActions extends StatelessWidget {
|
||||
const _IconActions({required this.controller});
|
||||
|
||||
final ArtifactFileActionsController controller;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final busy = controller.busy;
|
||||
return Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
if (controller.canDownload)
|
||||
ArtifactIconAction(
|
||||
key: const Key('artifact-download'),
|
||||
label: 'Download',
|
||||
icon: Icons.download_outlined,
|
||||
cupertinoIcon: CupertinoIcons.arrow_down_to_line,
|
||||
busy: busy,
|
||||
onPressed: busy ? null : controller.download,
|
||||
),
|
||||
if (controller.canShare)
|
||||
ArtifactIconAction(
|
||||
key: const Key('artifact-share'),
|
||||
label: 'Share',
|
||||
icon: Icons.share_outlined,
|
||||
cupertinoIcon: CupertinoIcons.share,
|
||||
onPressed: busy ? null : controller.share,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// One icon-only viewer action with its name as tooltip and accessible label.
|
||||
class ArtifactIconAction extends StatelessWidget {
|
||||
const ArtifactIconAction({
|
||||
super.key,
|
||||
required this.label,
|
||||
required this.icon,
|
||||
required this.cupertinoIcon,
|
||||
required this.onPressed,
|
||||
this.busy = false,
|
||||
});
|
||||
|
||||
final String label;
|
||||
final IconData icon;
|
||||
final IconData cupertinoIcon;
|
||||
final VoidCallback? onPressed;
|
||||
final bool busy;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (usesCupertinoHost(context)) {
|
||||
return Semantics(
|
||||
button: true,
|
||||
label: label,
|
||||
child: CupertinoButton(
|
||||
padding: const EdgeInsets.all(8),
|
||||
onPressed: onPressed,
|
||||
child: busy
|
||||
? const CupertinoActivityIndicator(radius: 9)
|
||||
: Icon(cupertinoIcon),
|
||||
),
|
||||
);
|
||||
}
|
||||
return IconButton(
|
||||
tooltip: label,
|
||||
visualDensity: VisualDensity.compact,
|
||||
onPressed: onPressed,
|
||||
icon: busy
|
||||
? const SizedBox(
|
||||
width: 18,
|
||||
height: 18,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: Icon(icon),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import 'artifact_file.dart';
|
||||
import 'artifact_transfer_stub.dart'
|
||||
if (dart.library.js_interop) 'artifact_transfer_web.dart'
|
||||
if (dart.library.io) 'artifact_transfer_io.dart'
|
||||
as impl;
|
||||
|
||||
/// The outcome of a host file transfer.
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:share_plus/share_plus.dart';
|
||||
|
||||
import 'artifact_file.dart';
|
||||
import 'artifact_transfer.dart';
|
||||
|
||||
/// Phones and desktops: Download asks where to save the exact bytes (the
|
||||
/// system "Save as"), Share hands the named file to the share sheet.
|
||||
bool get artifactDownloadSupported => true;
|
||||
|
||||
bool get artifactShareSupported => true;
|
||||
|
||||
String get artifactDownloadUnsupportedMessage => '';
|
||||
|
||||
String get artifactShareUnsupportedMessage => '';
|
||||
|
||||
Future<ArtifactTransferResult> saveArtifactFile(ArtifactFile file) async {
|
||||
try {
|
||||
final saved = await FilePicker.saveFile(
|
||||
fileName: file.name,
|
||||
bytes: file.bytes,
|
||||
mimeType: file.mimeType,
|
||||
);
|
||||
if (saved == null) {
|
||||
return const ArtifactTransferResult(ArtifactTransferState.canceled, '');
|
||||
}
|
||||
return ArtifactTransferResult(
|
||||
ArtifactTransferState.saved,
|
||||
'Saved ${file.name}.',
|
||||
);
|
||||
} catch (error) {
|
||||
return ArtifactTransferResult(
|
||||
ArtifactTransferState.failed,
|
||||
'Could not save ${file.name}: $error',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<ArtifactTransferResult> shareArtifactFile(ArtifactFile file) async {
|
||||
try {
|
||||
final result = await SharePlus.instance.share(
|
||||
ShareParams(
|
||||
files: [XFile.fromData(file.bytes, mimeType: file.mimeType)],
|
||||
fileNameOverrides: [file.name],
|
||||
),
|
||||
);
|
||||
return result.status == ShareResultStatus.dismissed
|
||||
? const ArtifactTransferResult(ArtifactTransferState.canceled, '')
|
||||
: const ArtifactTransferResult(ArtifactTransferState.shared, '');
|
||||
} catch (error) {
|
||||
return ArtifactTransferResult(
|
||||
ArtifactTransferState.failed,
|
||||
'Could not share ${file.name}: $error',
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import '../domain/artifact_url.dart';
|
||||
import 'artifact_external_open_stub.dart'
|
||||
if (dart.library.js_interop) 'artifact_external_open_web.dart'
|
||||
if (dart.library.io) 'artifact_external_open_io.dart'
|
||||
as impl;
|
||||
|
||||
/// Opens an already validated artifact url with the host's external handler.
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
|
||||
/// Phones and desktops: the link opens in the system browser, which renders
|
||||
/// what it can and offers to download the rest.
|
||||
Future<bool> openArtifactExternally(Uri uri) async {
|
||||
try {
|
||||
return await launchUrl(uri, mode: LaunchMode.externalApplication);
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -62,27 +62,10 @@ class ArtifactViewerBody extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final resolved = plan.resolved;
|
||||
final controls = <Widget>[
|
||||
if (resolved != null && !resolved.external)
|
||||
ArtifactFileActions(
|
||||
key: ValueKey<String>(
|
||||
'viewer-files-${page.group.stageKey}\u0000'
|
||||
'${page.group.visit}\u0000${page.artifact.url}',
|
||||
),
|
||||
artifact: page.artifact,
|
||||
resolved: resolved,
|
||||
fetch: fetch,
|
||||
bridge: fileBridge,
|
||||
),
|
||||
if (resolved != null)
|
||||
_Actions(resolved: resolved, onOpen: onOpen, onCopy: onCopy),
|
||||
];
|
||||
final content = SingleChildScrollView(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
// Room for the controls bar, so it never covers the first line.
|
||||
if (controls.isNotEmpty) const SizedBox(height: 56),
|
||||
Padding(
|
||||
padding: const EdgeInsetsDirectional.fromSTEB(16, 12, 16, 4),
|
||||
child: ArtifactSelectionFacts(
|
||||
@@ -102,11 +85,6 @@ class ArtifactViewerBody extends StatelessWidget {
|
||||
),
|
||||
),
|
||||
),
|
||||
if (status != null)
|
||||
Padding(
|
||||
padding: const EdgeInsetsDirectional.fromSTEB(16, 0, 16, 4),
|
||||
child: Semantics(liveRegion: true, child: Text(status!)),
|
||||
),
|
||||
ArtifactPreviewBody(
|
||||
plan: plan,
|
||||
loading: loading,
|
||||
@@ -118,23 +96,60 @@ class ArtifactViewerBody extends StatelessWidget {
|
||||
],
|
||||
),
|
||||
);
|
||||
final hasPrevious = pagedIndex(index, -1, count) != index;
|
||||
final hasNext = pagedIndex(index, 1, count) != index;
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Expanded(
|
||||
child: controls.isEmpty
|
||||
? content
|
||||
: ArtifactControlsOverlay(
|
||||
key: ValueKey<String>('viewer-overlay-${page.artifact.url}'),
|
||||
controls: controls,
|
||||
child: content,
|
||||
),
|
||||
// A horizontal swipe moves to the previous or next artifact.
|
||||
child: GestureDetector(
|
||||
key: const Key('artifact-viewer-swipe'),
|
||||
behavior: HitTestBehavior.translucent,
|
||||
onHorizontalDragEnd: (details) {
|
||||
final velocity = details.primaryVelocity ?? 0;
|
||||
if (velocity < -250 && hasNext) onNext();
|
||||
if (velocity > 250 && hasPrevious) onPrevious();
|
||||
},
|
||||
child: content,
|
||||
),
|
||||
),
|
||||
_Pager(
|
||||
index: index,
|
||||
count: count,
|
||||
onPrevious: onPrevious,
|
||||
onNext: onNext,
|
||||
// Every action as an icon at the bottom, beside the pager.
|
||||
SafeArea(
|
||||
top: false,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 4),
|
||||
child: Row(
|
||||
key: const Key('artifact-viewer-actions'),
|
||||
children: [
|
||||
if (resolved != null && !resolved.external)
|
||||
ArtifactFileActions(
|
||||
key: ValueKey<String>(
|
||||
'viewer-files-${page.group.stageKey}\u0000'
|
||||
'${page.group.visit}\u0000${page.artifact.url}',
|
||||
),
|
||||
artifact: page.artifact,
|
||||
resolved: resolved,
|
||||
fetch: fetch,
|
||||
bridge: fileBridge,
|
||||
compact: true,
|
||||
),
|
||||
if (resolved != null)
|
||||
_Actions(resolved: resolved, onOpen: onOpen, onCopy: onCopy),
|
||||
Expanded(
|
||||
child: Align(
|
||||
alignment: AlignmentDirectional.centerEnd,
|
||||
child: _Pager(
|
||||
index: index,
|
||||
count: count,
|
||||
onPrevious: onPrevious,
|
||||
onNext: onNext,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
@@ -263,40 +278,25 @@ class _Actions extends StatelessWidget {
|
||||
final VoidCallback onCopy;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (usesCupertinoHost(context)) {
|
||||
return SafeArea(
|
||||
top: false,
|
||||
child: Wrap(
|
||||
alignment: WrapAlignment.center,
|
||||
spacing: 8,
|
||||
children: [
|
||||
CupertinoButton(onPressed: onOpen, child: const Text('Open')),
|
||||
CupertinoButton(onPressed: onCopy, child: const Text('Copy link')),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
return SafeArea(
|
||||
top: false,
|
||||
child: Wrap(
|
||||
alignment: WrapAlignment.center,
|
||||
spacing: 8,
|
||||
children: [
|
||||
TextButton.icon(
|
||||
onPressed: onOpen,
|
||||
icon: const Icon(Icons.open_in_new),
|
||||
label: const Text('Open'),
|
||||
),
|
||||
TextButton.icon(
|
||||
onPressed: onCopy,
|
||||
icon: const Icon(Icons.content_copy),
|
||||
label: const Text('Copy link'),
|
||||
),
|
||||
],
|
||||
Widget build(BuildContext context) => Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
ArtifactIconAction(
|
||||
key: const Key('artifact-open'),
|
||||
label: 'Open',
|
||||
icon: Icons.open_in_new,
|
||||
cupertinoIcon: CupertinoIcons.arrow_up_right_square,
|
||||
onPressed: onOpen,
|
||||
),
|
||||
);
|
||||
}
|
||||
ArtifactIconAction(
|
||||
key: const Key('artifact-copy-link'),
|
||||
label: 'Copy link',
|
||||
icon: Icons.link,
|
||||
cupertinoIcon: CupertinoIcons.link,
|
||||
onPressed: onCopy,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
const artifactPreviousKey = ValueKey<String>('artifact-previous');
|
||||
@@ -320,18 +320,15 @@ class _Pager extends StatelessWidget {
|
||||
if (count <= 1) return const SizedBox.shrink();
|
||||
final hasPrevious = pagedIndex(index, -1, count) != index;
|
||||
final hasNext = pagedIndex(index, 1, count) != index;
|
||||
return SafeArea(
|
||||
top: false,
|
||||
child: ChevronPager(
|
||||
index: index,
|
||||
count: count,
|
||||
previousLabel: 'Previous artifact',
|
||||
nextLabel: 'Next artifact',
|
||||
previousKey: artifactPreviousKey,
|
||||
nextKey: artifactNextKey,
|
||||
onPrevious: hasPrevious ? onPrevious : null,
|
||||
onNext: hasNext ? onNext : null,
|
||||
),
|
||||
return ChevronPager(
|
||||
index: index,
|
||||
count: count,
|
||||
previousLabel: 'Previous artifact',
|
||||
nextLabel: 'Next artifact',
|
||||
previousKey: artifactPreviousKey,
|
||||
nextKey: artifactNextKey,
|
||||
onPrevious: hasPrevious ? onPrevious : null,
|
||||
onNext: hasNext ? onNext : null,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user