177 lines
5.2 KiB
Dart
177 lines
5.2 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../../../core/api_gateway.dart';
|
|
import '../data/runs_repository.dart';
|
|
import '../domain/run_decisions.dart';
|
|
import '../domain/run_models.dart';
|
|
import 'action_refusal.dart';
|
|
import 'decision_dialog.dart';
|
|
import 'decision_wording.dart';
|
|
|
|
/// The run detail's deliberate human-decision entry point.
|
|
///
|
|
/// Renders one native button only while [run] is genuinely waiting on a person.
|
|
/// Nothing is sent until the operator opens the prompt, chooses an option and
|
|
/// presses Confirm; cancel, selection and optional feedback stay local. A
|
|
/// server refusal is shown verbatim in the prompt immediately, the draft is
|
|
/// preserved and the run is re-read separately so [onRunChanged] still receives
|
|
/// server truth without locking the prompt while that read is pending.
|
|
class HumanDecisionAction extends StatefulWidget {
|
|
const HumanDecisionAction({
|
|
super.key,
|
|
required this.run,
|
|
required this.gateway,
|
|
required this.onRunChanged,
|
|
this.cupertino,
|
|
});
|
|
|
|
final JsonMap run;
|
|
final ApiGateway gateway;
|
|
final ValueChanged<JsonMap> onRunChanged;
|
|
|
|
/// Overrides the platform style; by default it follows the enclosing app.
|
|
final bool? cupertino;
|
|
|
|
@override
|
|
State<HumanDecisionAction> createState() => _HumanDecisionActionState();
|
|
}
|
|
|
|
class _HumanDecisionActionState extends State<HumanDecisionAction> {
|
|
PendingDecision? _pending;
|
|
bool _dialogOpen = false;
|
|
bool _sending = false;
|
|
Future<bool?>? _resync;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_pending = pendingDecision(widget.run);
|
|
}
|
|
|
|
@override
|
|
void didUpdateWidget(covariant HumanDecisionAction oldWidget) {
|
|
super.didUpdateWidget(oldWidget);
|
|
_pending = pendingDecision(widget.run);
|
|
}
|
|
|
|
bool _usesCupertino(BuildContext context) {
|
|
if (widget.cupertino != null) return widget.cupertino!;
|
|
final material = Localizations.of<MaterialLocalizations>(
|
|
context,
|
|
MaterialLocalizations,
|
|
);
|
|
return material == null;
|
|
}
|
|
|
|
Future<void> _open() async {
|
|
final decision = _pending;
|
|
if (decision == null || _dialogOpen) return;
|
|
_dialogOpen = true;
|
|
try {
|
|
await showHumanDecisionPrompt(
|
|
context: context,
|
|
decision: decision,
|
|
cupertino: _usesCupertino(context),
|
|
onSubmit: (draft) => _submit(decision, draft),
|
|
);
|
|
} finally {
|
|
_dialogOpen = false;
|
|
if (mounted) setState(() {});
|
|
}
|
|
}
|
|
|
|
Future<ActionRefusal?> _submit(
|
|
PendingDecision decision,
|
|
DecisionDraft draft,
|
|
) async {
|
|
final option = draft.selected?.trim() ?? '';
|
|
if (option.isEmpty) return const ActionRefusal(actionOptionRequired);
|
|
if (_sending) return const ActionRefusal(actionAlreadySending);
|
|
_sending = true;
|
|
final repository = RunsRepository(widget.gateway);
|
|
try {
|
|
final JsonMap run;
|
|
try {
|
|
run = await repository.decideRun(
|
|
decision.runId,
|
|
stage: decision.stageKey,
|
|
option: option,
|
|
feedback: draft.feedback,
|
|
);
|
|
} catch (error) {
|
|
return ActionRefusal(
|
|
_describe(error),
|
|
stillPossible: _resyncRun(decision),
|
|
);
|
|
}
|
|
if (!mounted) return null;
|
|
if (_owns(decision.runId)) {
|
|
setState(() => _pending = pendingDecision(run));
|
|
widget.onRunChanged(run);
|
|
}
|
|
return null;
|
|
} finally {
|
|
_sending = false;
|
|
}
|
|
}
|
|
|
|
bool _owns(num id) => mounted && widget.run['id'] == 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(PendingDecision decision) {
|
|
return _resync ??= _readRun(decision);
|
|
}
|
|
|
|
Future<bool?> _readRun(PendingDecision expected) async {
|
|
try {
|
|
final run = await RunsRepository(widget.gateway).getRun(expected.runId);
|
|
if (!_owns(expected.runId)) return null;
|
|
final next = pendingDecision(run);
|
|
setState(() => _pending = next);
|
|
widget.onRunChanged(run);
|
|
return _stillDecidable(next, expected);
|
|
} catch (_) {
|
|
// Unknown server truth: keep the refusal and leave a deliberate retry.
|
|
return null;
|
|
} finally {
|
|
_resync = null;
|
|
}
|
|
}
|
|
|
|
/// Whether the refreshed run offers the same decision the prompt is showing.
|
|
bool _stillDecidable(PendingDecision? next, PendingDecision expected) {
|
|
if (next == null ||
|
|
next.runId != expected.runId ||
|
|
next.stageKey != expected.stageKey) {
|
|
return false;
|
|
}
|
|
final keys = next.options.map((option) => option.key).toSet();
|
|
return expected.options.every((option) => keys.contains(option.key));
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final decision = _pending;
|
|
if (decision == null) return const SizedBox.shrink();
|
|
if (_usesCupertino(context)) {
|
|
return CupertinoButton.filled(
|
|
key: decisionActionKey,
|
|
onPressed: _open,
|
|
child: const Text(decisionActionLabel),
|
|
);
|
|
}
|
|
return FilledButton(
|
|
key: decisionActionKey,
|
|
onPressed: _open,
|
|
child: const Text(decisionActionLabel),
|
|
);
|
|
}
|
|
}
|
|
|
|
String _describe(Object error) {
|
|
final message = '$error';
|
|
return message.trim().isEmpty ? 'The server refused the decision.' : message;
|
|
}
|