import 'package:flutter/foundation.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_transfer.dart'; /// Which host file action is currently in flight. enum ArtifactFileAction { download, share } /// The words shown while bytes are being fetched. const String artifactPreparingFile = 'Preparing file…'; /// Saves or shares one selected artifact's fetched bytes. /// /// The controller owns exactly one transfer at a time: a tap while [busy] is /// ignored, so a double tap cannot start two fetches or two share sheets. The /// same fetch seam the viewer uses obtains the bytes, so the same-origin /// credential policy and HTTP status handling apply. Fetched bytes are kept /// after a ready/blocked/canceled share so the next tap can retry with a fresh /// user activation without another transfer. /// /// Cancelling stops the action from continuing: a late fetch result is ignored /// by generation, and the host transfer is never started. The underlying HTTP /// request is not aborted because the shared fetch seam has no cancellation /// token; the app never reports those bytes as a completed transfer. class ArtifactFileActionsController extends ChangeNotifier { ArtifactFileActionsController({ required this.artifact, required this.resolved, required this.fetch, this.bridge = const ArtifactFileBridge(), }); final ArtifactRecord artifact; final ResolvedArtifactUrl resolved; final ArtifactFileBridge bridge; final ArtifactFetch fetch; ArtifactFile? _prepared; String? _preparedUrl; ArtifactFileAction? _active; bool _preparing = false; int _generation = 0; bool _disposed = false; String? _status; String? get status => _status; bool get busy => _active != null; /// Cancel is offered only while the fetch is still running; once the host /// share sheet is open the OS owns the transfer. bool get canCancel => _active != null && _preparing; bool get canDownload => bridge.canDownload; bool get canShare => bridge.canShare; Future download() async { if (busy) return; if (!canDownload) { _setStatus(bridge.downloadUnavailableMessage); return; } final generation = _begin(ArtifactFileAction.download); final file = await _prepare(generation); if (file == null || generation != _generation) return; _preparing = false; _notify(); ArtifactTransferResult result; try { result = await bridge.saveFile(file); } catch (_) { _fail(generation, 'Could not download the file. Try again.'); return; } if (generation != _generation) return; _active = null; _status = result.state == ArtifactTransferState.saved ? 'Downloaded ${file.name}.' : result.message; _notify(); } Future share() async { if (busy) return; if (!canShare) { _setStatus(bridge.shareUnavailableMessage); return; } final generation = _begin(ArtifactFileAction.share); final file = await _prepare(generation); if (file == null || generation != _generation) return; _preparing = false; _notify(); ArtifactTransferResult result; try { result = await bridge.shareFile(file); } catch (_) { _fail( generation, 'Could not share the file. Use Download to save it, or try Share again.', ); return; } if (generation != _generation) return; _active = null; _status = result.state == ArtifactTransferState.shared ? 'File shared.' : result.message; _notify(); } /// Stops waiting for the fetch. Nothing is handed to the host afterwards. void cancel() { if (!canCancel) return; final action = _active; _generation += 1; _active = null; _preparing = false; _status = action == ArtifactFileAction.share ? 'Sharing canceled.' : 'Download canceled.'; _notify(); } int _begin(ArtifactFileAction action) { final generation = ++_generation; _active = action; _preparing = true; _status = artifactPreparingFile; _notify(); return generation; } Future _prepare(int generation) async { final cached = _prepared; if (cached != null && _preparedUrl == resolved.url) { _preparing = false; _notify(); return cached; } try { final fetched = await fetch(resolved.uri); if (generation != _generation) return null; if (!fetched.ok) { _fail( generation, 'Could not fetch the artifact (HTTP ${fetched.status}).', ); return null; } final file = artifactFileFor( artifact: artifact, url: resolved.uri, bytes: fetched.bytes, ); _prepared = file; _preparedUrl = resolved.url; return file; } on ArtifactFetchException catch (error) { _fail(generation, 'Could not fetch the artifact: ${error.message}'); return null; } catch (error) { _fail(generation, 'Could not fetch the artifact: $error'); return null; } } void _fail(int generation, String message) { if (generation != _generation) return; _active = null; _preparing = false; _status = message; _notify(); } void _setStatus(String message) { _status = message; _notify(); } void _notify() { if (!_disposed) notifyListeners(); } @override void dispose() { _disposed = true; _generation += 1; super.dispose(); } }