364fd5b538
Keep the Material-only run action behavior while preserving public Cupertino parameters for callers that will be cleaned up separately. Narrow the related tests to the supported Material path.
186 lines
5.3 KiB
Dart
186 lines
5.3 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import '../domain/run_decisions.dart';
|
|
import '../domain/run_models.dart';
|
|
import 'action_refusal.dart';
|
|
import 'decision_wording.dart';
|
|
|
|
/// Sends the draft to the server. Returns null on success, or an
|
|
/// [ActionRefusal] with the server's words verbatim; the prompt stays open
|
|
/// with the draft intact and dismissal available immediately.
|
|
typedef DecisionSubmit = Future<ActionRefusal?> Function(DecisionDraft draft);
|
|
|
|
/// Opens the native decision prompt for [decision].
|
|
Future<void> showHumanDecisionPrompt({
|
|
required BuildContext context,
|
|
required PendingDecision decision,
|
|
required bool cupertino,
|
|
required DecisionSubmit onSubmit,
|
|
}) {
|
|
final prompt = _DecisionPrompt(decision: decision, onSubmit: onSubmit);
|
|
return showDialog<void>(
|
|
context: context,
|
|
barrierDismissible: false,
|
|
builder: (_) => prompt,
|
|
);
|
|
}
|
|
|
|
class _DecisionPrompt extends StatefulWidget {
|
|
const _DecisionPrompt({required this.decision, required this.onSubmit});
|
|
|
|
final PendingDecision decision;
|
|
final DecisionSubmit onSubmit;
|
|
|
|
@override
|
|
State<_DecisionPrompt> createState() => _DecisionPromptState();
|
|
}
|
|
|
|
class _DecisionPromptState extends State<_DecisionPrompt> {
|
|
DecisionDraft _draft = const DecisionDraft();
|
|
late final TextEditingController _feedback = TextEditingController();
|
|
bool _sending = false;
|
|
bool _checking = false;
|
|
bool _blocked = false;
|
|
String? _error;
|
|
|
|
@override
|
|
void dispose() {
|
|
_feedback.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
void _select(String option) {
|
|
if (_sending) return;
|
|
setState(() => _draft = _draft.select(option));
|
|
}
|
|
|
|
void _feedbackChanged(String value) => _draft = _draft.withFeedback(value);
|
|
|
|
void _cancel() {
|
|
if (!_sending) Navigator.of(context).pop();
|
|
}
|
|
|
|
Future<void> _confirm() async {
|
|
if (_sending || _checking || _blocked || !_draft.canSubmit) return;
|
|
setState(() {
|
|
_sending = true;
|
|
_error = null;
|
|
});
|
|
final refusal = await widget.onSubmit(_draft);
|
|
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;
|
|
|
|
Widget _material(BuildContext context) {
|
|
final theme = Theme.of(context);
|
|
return AlertDialog(
|
|
scrollable: true,
|
|
title: const Text(decisionWaitingTitle),
|
|
content: _body(
|
|
errorStyle: TextStyle(color: theme.colorScheme.error),
|
|
options: RadioGroup<String>(
|
|
groupValue: _draft.selected,
|
|
onChanged: (value) {
|
|
if (value != null) _select(value);
|
|
},
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
for (final option in widget.decision.options)
|
|
RadioListTile<String>(
|
|
value: option.key,
|
|
enabled: !_sending,
|
|
title: Text(option.key),
|
|
subtitle: _target(option),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
feedback: TextField(
|
|
key: decisionFeedbackKey,
|
|
controller: _feedback,
|
|
enabled: !_sending,
|
|
decoration: const InputDecoration(labelText: decisionFeedbackLabel),
|
|
textInputAction: TextInputAction.done,
|
|
onChanged: _feedbackChanged,
|
|
onSubmitted: (_) => _confirm(),
|
|
),
|
|
),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: _sending ? null : _cancel,
|
|
child: const Text(decisionCancelLabel),
|
|
),
|
|
FilledButton(
|
|
onPressed: _confirmDisabled || !_draft.canSubmit ? null : _confirm,
|
|
child: Text(_sending ? decisionSendingLabel : decisionConfirmLabel),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _body({
|
|
required TextStyle errorStyle,
|
|
required Widget options,
|
|
required Widget feedback,
|
|
}) {
|
|
return Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
Text(decisionMeta(widget.decision)),
|
|
if (widget.decision.instructions.trim().isNotEmpty)
|
|
Padding(
|
|
padding: const EdgeInsets.only(top: 8),
|
|
child: Text(widget.decision.instructions),
|
|
),
|
|
Padding(padding: const EdgeInsets.only(top: 8), child: options),
|
|
Padding(padding: const EdgeInsets.only(top: 8), child: feedback),
|
|
if (_error case final error?)
|
|
Padding(
|
|
padding: const EdgeInsets.only(top: 8),
|
|
child: Text(error, key: decisionErrorKey, style: errorStyle),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget? _target(DecisionOption option) {
|
|
final text = _targetText(option);
|
|
return text == null ? null : Text(text);
|
|
}
|
|
|
|
String? _targetText(DecisionOption option) {
|
|
final text = decisionTargetText(option.target);
|
|
return text.isEmpty ? null : text;
|
|
}
|
|
}
|