147 lines
4.4 KiB
Dart
147 lines
4.4 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_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;
|
|
|
|
bool _usesCupertino(BuildContext context) => _cupertinoLocale(context);
|
|
|
|
Future<void> _open() async {
|
|
if (_dialogOpen) return;
|
|
_dialogOpen = true;
|
|
try {
|
|
await openRetryRunPrompt(
|
|
context,
|
|
run: widget.run,
|
|
gateway: widget.gateway,
|
|
onRunChanged: widget.onRunChanged,
|
|
onRunOpened: widget.onRunOpened,
|
|
owns: (id) => mounted && retryRunId(widget.run) == id,
|
|
);
|
|
} finally {
|
|
_dialogOpen = false;
|
|
if (mounted) setState(() {});
|
|
}
|
|
}
|
|
|
|
@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),
|
|
);
|
|
}
|
|
}
|
|
|
|
bool _cupertinoLocale(BuildContext context) =>
|
|
Localizations.of<MaterialLocalizations>(context, MaterialLocalizations) ==
|
|
null;
|
|
|
|
/// Opens the retry prompt for a failed [run]; only its confirm sends
|
|
/// `retry_run`. An accepted reply reaches [onRunChanged] while [owns] still
|
|
/// holds, and a new run id opens through [onRunOpened]. A refusal re-reads the
|
|
/// run once so the prompt can say whether retrying is still possible. Shared
|
|
/// by the button and the run detail's more menu.
|
|
Future<void> openRetryRunPrompt(
|
|
BuildContext context, {
|
|
required JsonMap run,
|
|
required ApiGateway gateway,
|
|
required ValueChanged<JsonMap> onRunChanged,
|
|
required bool Function(num id) owns,
|
|
ValueChanged<num>? onRunOpened,
|
|
}) async {
|
|
final id = retryRunId(run);
|
|
if (id == null || !canRetryRun(run)) return;
|
|
final repository = RunsRepository(gateway);
|
|
Future<bool?>? resync;
|
|
Future<bool?> readRun() async {
|
|
try {
|
|
final current = await repository.getRun(id);
|
|
if (!owns(id)) return null;
|
|
onRunChanged(current);
|
|
return canRetryRun(current);
|
|
} catch (_) {
|
|
// Unknown server truth: keep the refusal and leave a deliberate retry.
|
|
return null;
|
|
} finally {
|
|
resync = null;
|
|
}
|
|
}
|
|
|
|
await showRetryRunPrompt(
|
|
context: context,
|
|
runId: id,
|
|
cupertino: _cupertinoLocale(context),
|
|
onSubmit: (intent) async {
|
|
try {
|
|
final updated = await repository.retryRun(
|
|
id,
|
|
requestKey: intent.requestKey,
|
|
mode: intent.mode.wireMode,
|
|
);
|
|
if (owns(id)) {
|
|
onRunChanged(updated);
|
|
final opened = retryRunId(updated);
|
|
if (opened != null && opened != id) onRunOpened?.call(opened);
|
|
}
|
|
return null;
|
|
} catch (error) {
|
|
return ActionRefusal(
|
|
describeRetryError(error),
|
|
stillPossible: resync ??= readRun(),
|
|
);
|
|
}
|
|
},
|
|
);
|
|
}
|