42b21b86cd
Adds HumanDecisionAction plus its native prompt, wording constants and tests. The action renders only while pendingDecision() reports a genuine human wait, sends decide_run only after an option is chosen and Confirm pressed, sends the pending stage/trimmed option/optional feedback, blocks double submits, shows server refusals verbatim with the draft intact, and re-reads the run so the owner always receives server truth.
142 lines
4.0 KiB
Dart
142 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 '../domain/run_decisions.dart';
|
|
import '../domain/run_models.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, the draft is preserved and
|
|
/// the run is re-read so [onRunChanged] always receives server truth.
|
|
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;
|
|
|
|
@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<String?> _submit(PendingDecision decision, DecisionDraft draft) async {
|
|
final option = draft.selected?.trim() ?? '';
|
|
if (option.isEmpty || _sending) return null;
|
|
_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) {
|
|
final refusal = _describe(error);
|
|
if (!mounted) return refusal;
|
|
JsonMap? refreshed;
|
|
try {
|
|
refreshed = await repository.getRun(decision.runId);
|
|
} catch (_) {
|
|
// A failed re-read must not hide or replace the server's refusal.
|
|
}
|
|
if (refreshed != null && mounted) {
|
|
setState(() => _pending = pendingDecision(refreshed));
|
|
widget.onRunChanged(refreshed);
|
|
}
|
|
return refusal;
|
|
}
|
|
if (!mounted) return null;
|
|
setState(() => _pending = pendingDecision(run));
|
|
widget.onRunChanged(run);
|
|
return null;
|
|
} finally {
|
|
_sending = false;
|
|
}
|
|
}
|
|
|
|
@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;
|
|
}
|