Files
pipelines/app/lib/features/artifacts/actions/artifact_file_actions.dart
T

276 lines
8.1 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../ui/adaptive/host.dart';
import '../domain/artifact_record.dart';
import '../domain/artifact_url.dart';
import '../ui/artifact_viewer_fetch.dart';
import 'artifact_file.dart';
import 'artifact_file_bridge.dart';
import 'artifact_file_controller.dart';
/// Download and Share for one selected artifact, backed by its exact bytes.
///
/// The controls are standard native buttons. Download creates a named file;
/// Share passes a named file to the host share sheet and never falls back to
/// the artifact url. A host without a capability shows a disabled control and
/// the exact reason, while the other action keeps working. One tap starts at
/// most one transfer; a slow fetch shows progress and can be cancelled.
class ArtifactFileActions extends StatefulWidget {
const ArtifactFileActions({
super.key,
required this.artifact,
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;
final ArtifactFileBridge bridge;
@override
State<ArtifactFileActions> createState() => _ArtifactFileActionsState();
}
class _ArtifactFileActionsState extends State<ArtifactFileActions> {
late ArtifactFileActionsController _controller;
@override
void initState() {
super.initState();
_controller = _create();
}
@override
void didUpdateWidget(covariant ArtifactFileActions oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.resolved.url != widget.resolved.url ||
oldWidget.artifact.url != widget.artifact.url ||
oldWidget.artifact.name != widget.artifact.name ||
oldWidget.artifact.contentType != widget.artifact.contentType ||
oldWidget.fetch != widget.fetch ||
oldWidget.bridge != widget.bridge) {
_controller.dispose();
_controller = _create();
}
}
ArtifactFileActionsController _create() => ArtifactFileActionsController(
artifact: widget.artifact,
resolved: widget.resolved,
fetch: widget.fetch,
bridge: widget.bridge,
);
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return ListenableBuilder(
listenable: _controller,
builder: (context, _) => widget.compact
? _IconActions(controller: _controller)
: _ActionBar(
controller: _controller,
cupertino: usesCupertinoHost(context),
),
);
}
}
class _ActionBar extends StatelessWidget {
const _ActionBar({required this.controller, required this.cupertino});
final ArtifactFileActionsController controller;
final bool cupertino;
@override
Widget build(BuildContext context) {
final status = controller.status;
final notice = artifactFileRecordingNotice(
controller.artifact,
controller.resolved.uri,
);
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Wrap(
spacing: 8,
runSpacing: 4,
crossAxisAlignment: WrapCrossAlignment.center,
children: [
_button(
label: 'Download',
icon: Icons.download_outlined,
onPressed: controller.canDownload && !controller.busy
? controller.download
: null,
),
_button(
label: 'Share',
icon: Icons.ios_share,
onPressed: controller.canShare && !controller.busy
? controller.share
: null,
),
if (controller.canCancel)
_button(
label: 'Cancel',
icon: Icons.close,
onPressed: controller.cancel,
),
],
),
if (controller.busy && status != null)
Padding(
padding: const EdgeInsets.only(top: 6),
child: Semantics(
liveRegion: true,
container: true,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
if (cupertino)
const CupertinoActivityIndicator(radius: 8)
else
const SizedBox(
width: 14,
height: 14,
child: CircularProgressIndicator(strokeWidth: 2),
),
const SizedBox(width: 8),
Flexible(child: Text(status)),
],
),
),
)
else if (status != null)
Padding(
padding: const EdgeInsets.only(top: 6),
child: Semantics(liveRegion: true, child: Text(status)),
),
if (!controller.canDownload)
Padding(
padding: const EdgeInsets.only(top: 6),
child: Text(controller.bridge.downloadUnavailableMessage),
),
if (!controller.canShare)
Padding(
padding: const EdgeInsets.only(top: 6),
child: Text(controller.bridge.shareUnavailableMessage),
),
if (notice != null)
Padding(padding: const EdgeInsets.only(top: 6), child: Text(notice)),
],
);
}
Widget _button({
required String label,
required IconData icon,
required VoidCallback? onPressed,
}) {
if (cupertino) {
return CupertinoButton(onPressed: onPressed, child: Text(label));
}
return TextButton.icon(
onPressed: onPressed,
icon: Icon(icon, size: 18),
label: Text(label),
);
}
}
/// 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),
);
}
}