import 'package:flutter/material.dart'; import 'action_refusal.dart'; import 'retry_intent.dart'; import 'retry_wording.dart'; /// Sends the confirmed retry intent. Returns null once the server has accepted /// it, or an [ActionRefusal] carrying the server's words verbatim; the prompt /// then stays open with dismissal available immediately. typedef RetrySubmit = Future Function(RetryIntent intent); /// Opens the native retry confirmation for [runId]. /// /// The first confirmation offers resume (the default) and a secondary /// `Restart from start` confirmation, exactly like the browser's retry dialog. /// Nothing is sent until the operator presses a confirm action; `Keep` only /// closes the prompt. Future showRetryRunPrompt({ required BuildContext context, required num runId, required RetrySubmit onSubmit, }) { final prompt = _RetryPrompt(runId: runId, onSubmit: onSubmit); return showDialog( context: context, barrierDismissible: false, builder: (_) => prompt, ); } class _RetryPrompt extends StatefulWidget { const _RetryPrompt({required this.runId, required this.onSubmit}); final num runId; final RetrySubmit onSubmit; @override State<_RetryPrompt> createState() => _RetryPromptState(); } class _RetryPromptState extends State<_RetryPrompt> { RetryMode _mode = RetryMode.resume; String? _requestKey; bool _sending = false; bool _checking = false; bool _blocked = false; bool _attempted = false; String? _error; void _keep() { if (!_sending) Navigator.of(context).pop(); } void _restart() { if (_sending || _checking || _attempted) return; setState(() { _mode = RetryMode.restart; _error = null; }); } Future _confirm() async { if (_sending || _checking || _blocked) return; setState(() { _sending = true; _attempted = true; _error = null; }); final intent = RetryIntent( mode: _mode, requestKey: _requestKey ??= newRetryRequestKey(widget.runId), ); final refusal = await widget.onSubmit(intent); if (!mounted) return; if (refusal == null) { Navigator.of(context).pop(); return; } final check = refusal.stillPossible; setState(() { _sending = false; _error = refusal.message; _checking = check != null; }); if (check == null) return; bool? stillPossible; try { stillPossible = await check; } catch (_) { // The action reports re-read failures as null; never fabricate one here. } if (!mounted) return; setState(() { _checking = false; _blocked = stillPossible == false; }); } @override Widget build(BuildContext context) { return PopScope(canPop: !_sending, child: _material(context)); } bool get _confirmDisabled => _sending || _checking || _blocked; bool get _showRestart => _mode == RetryMode.resume && !_attempted; Widget _material(BuildContext context) { final theme = Theme.of(context); return AlertDialog( scrollable: true, title: Text(_dialogTitle), content: _body(errorStyle: TextStyle(color: theme.colorScheme.error)), actions: [ if (_showRestart) TextButton( key: retryRestartKey, onPressed: _sending || _checking ? null : _restart, child: const Text(restartConfirmLabel), ), TextButton( key: retryKeepKey, onPressed: _sending ? null : _keep, child: const Text(retryKeepLabel), ), FilledButton( key: retryConfirmKey, onPressed: _confirmDisabled ? null : _confirm, child: Text(_confirmLabel), ), ], ); } Widget _body({required TextStyle errorStyle}) { return Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Text(_dialogMessage), if (_error case final error?) Padding( padding: const EdgeInsets.only(top: 8), child: Text(error, key: retryErrorKey, style: errorStyle), ), ], ); } String get _dialogTitle => _mode == RetryMode.restart ? restartDialogTitle(widget.runId) : retryDialogTitle(widget.runId); String get _dialogMessage => _mode == RetryMode.restart ? restartDialogMessage : retryDialogMessage; String get _confirmLabel => _mode == RetryMode.restart ? restartConfirmLabel : retryConfirmLabel; }