137 lines
4.0 KiB
Dart
137 lines
4.0 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../../../core/api_gateway.dart';
|
|
import '../data/runs_repository.dart';
|
|
import 'action_refusal.dart';
|
|
import 'retry_dialog.dart';
|
|
import 'retry_eligibility.dart';
|
|
import 'retry_intent.dart';
|
|
import 'retry_wording.dart';
|
|
|
|
/// The run detail's deliberate retry entry point.
|
|
///
|
|
/// Renders one native button only while [run] is a stopped `failed` run. Opening
|
|
/// the resume or restart confirmation sends nothing; only the confirm action
|
|
/// asks the server to retry. A refusal is returned to the kept-open prompt
|
|
/// immediately and the run is re-read separately, so [onRunChanged] still
|
|
/// receives server truth without locking the prompt while that read is pending.
|
|
class RetryRunAction extends StatefulWidget {
|
|
const RetryRunAction({
|
|
super.key,
|
|
required this.run,
|
|
required this.gateway,
|
|
required this.onRunChanged,
|
|
this.onRunOpened,
|
|
});
|
|
|
|
final JsonMap run;
|
|
final ApiGateway gateway;
|
|
final ValueChanged<JsonMap> onRunChanged;
|
|
|
|
/// Receives the id the accepted server reply names for the new retry run.
|
|
///
|
|
/// The reply is first delivered through [onRunChanged] like every other
|
|
/// accepted detail. This callback then opens that run; it is never called
|
|
/// with a guessed id, and a reply without a usable id changes no selection.
|
|
final ValueChanged<num>? onRunOpened;
|
|
|
|
@override
|
|
State<RetryRunAction> createState() => _RetryRunActionState();
|
|
}
|
|
|
|
class _RetryRunActionState extends State<RetryRunAction> {
|
|
bool _dialogOpen = false;
|
|
Future<bool?>? _resync;
|
|
|
|
bool _usesCupertino(BuildContext context) {
|
|
final material = Localizations.of<MaterialLocalizations>(
|
|
context,
|
|
MaterialLocalizations,
|
|
);
|
|
return material == null;
|
|
}
|
|
|
|
Future<void> _open() async {
|
|
final id = retryRunId(widget.run);
|
|
if (id == null || !canRetryRun(widget.run) || _dialogOpen) return;
|
|
_dialogOpen = true;
|
|
try {
|
|
await showRetryRunPrompt(
|
|
context: context,
|
|
runId: id,
|
|
cupertino: _usesCupertino(context),
|
|
onSubmit: (intent) => _submit(id, intent),
|
|
);
|
|
} finally {
|
|
_dialogOpen = false;
|
|
if (mounted) setState(() {});
|
|
}
|
|
}
|
|
|
|
Future<ActionRefusal?> _submit(num id, RetryIntent intent) async {
|
|
final repository = RunsRepository(widget.gateway);
|
|
try {
|
|
final run = await repository.retryRun(
|
|
id,
|
|
requestKey: intent.requestKey,
|
|
mode: intent.mode.wireMode,
|
|
);
|
|
if (!mounted) return null;
|
|
if (_owns(id)) {
|
|
widget.onRunChanged(run);
|
|
final opened = retryRunId(run);
|
|
if (opened != null && opened != id) widget.onRunOpened?.call(opened);
|
|
}
|
|
return null;
|
|
} catch (error) {
|
|
return ActionRefusal(
|
|
describeRetryError(error),
|
|
stillPossible: _resyncRun(id),
|
|
);
|
|
}
|
|
}
|
|
|
|
bool _owns(num id) => mounted && retryRunId(widget.run) == id;
|
|
|
|
/// One bounded re-read per refusal. A refusal that arrives while a read is
|
|
/// already in flight observes that same read instead of starting another.
|
|
Future<bool?> _resyncRun(num id) {
|
|
return _resync ??= _readRun(id);
|
|
}
|
|
|
|
Future<bool?> _readRun(num id) async {
|
|
try {
|
|
final run = await RunsRepository(widget.gateway).getRun(id);
|
|
if (!_owns(id)) return null;
|
|
widget.onRunChanged(run);
|
|
return canRetryRun(run);
|
|
} catch (_) {
|
|
// Unknown server truth: keep the refusal and leave a deliberate retry.
|
|
return null;
|
|
} finally {
|
|
_resync = null;
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
if (retryRunId(widget.run) == null || !canRetryRun(widget.run)) {
|
|
return const SizedBox.shrink();
|
|
}
|
|
if (_usesCupertino(context)) {
|
|
return CupertinoButton.tinted(
|
|
sizeStyle: CupertinoButtonSize.small,
|
|
key: retryActionKey,
|
|
onPressed: _open,
|
|
child: const Text(retryActionLabel),
|
|
);
|
|
}
|
|
return OutlinedButton(
|
|
key: retryActionKey,
|
|
onPressed: _open,
|
|
child: const Text(retryActionLabel),
|
|
);
|
|
}
|
|
}
|