feat(app): add native create-from-template prompt
Validate the template source first, then submit the typed name create-only, matching the browser's P5 flow and wording. Refusals keep the prompt and the typed name; the accepted detail is reported once through the callback. The labelled field, visible feedback and native Material/Cupertino controls are covered by focused widget tests and a loopback browser probe.
This commit is contained in:
@@ -0,0 +1,287 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/rendering.dart';
|
||||
|
||||
import '../../../core/api_gateway.dart';
|
||||
import '../data/template_repository.dart';
|
||||
import '../domain/template_entry.dart';
|
||||
import 'template_create_wording.dart';
|
||||
|
||||
/// Opens the native create prompt; resolves when it closes. Opening, typing and
|
||||
/// Cancel send nothing; Create validates the source first and only a passing
|
||||
/// validate submits it create-only. A refusal keeps the typed name; an accepted
|
||||
/// `detail` is delivered through [onCreated] once.
|
||||
Future<void> showTemplateCreateDialog({
|
||||
required BuildContext context,
|
||||
required TemplateEntry template,
|
||||
required TemplateRepository repository,
|
||||
required bool cupertino,
|
||||
required ValueChanged<JsonMap> onCreated,
|
||||
}) {
|
||||
final dialog = _TemplateCreateDialog(
|
||||
template: template,
|
||||
repository: repository,
|
||||
cupertino: cupertino,
|
||||
onCreated: onCreated,
|
||||
);
|
||||
return cupertino
|
||||
? showCupertinoDialog<void>(
|
||||
context: context,
|
||||
barrierDismissible: false,
|
||||
builder: (_) => dialog,
|
||||
)
|
||||
: showDialog<void>(
|
||||
context: context,
|
||||
barrierDismissible: false,
|
||||
builder: (_) => dialog,
|
||||
);
|
||||
}
|
||||
|
||||
class _TemplateCreateDialog extends StatefulWidget {
|
||||
const _TemplateCreateDialog({
|
||||
required this.template,
|
||||
required this.repository,
|
||||
required this.cupertino,
|
||||
required this.onCreated,
|
||||
});
|
||||
|
||||
final TemplateEntry template;
|
||||
final TemplateRepository repository;
|
||||
final bool cupertino;
|
||||
final ValueChanged<JsonMap> onCreated;
|
||||
|
||||
@override
|
||||
State<_TemplateCreateDialog> createState() => _TemplateCreateDialogState();
|
||||
}
|
||||
|
||||
class _TemplateCreateDialogState extends State<_TemplateCreateDialog> {
|
||||
final _name = TextEditingController();
|
||||
final _feedback = GlobalKey();
|
||||
bool _sending = false;
|
||||
String? _error;
|
||||
String? _created;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_name.addListener(_onNameChanged);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_name.removeListener(_onNameChanged);
|
||||
_name.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _onNameChanged() {
|
||||
if (mounted) setState(() {});
|
||||
}
|
||||
|
||||
bool get _fieldEnabled => !_sending && _created == null;
|
||||
bool get _canSubmit => _fieldEnabled && _name.text.trim().isNotEmpty;
|
||||
|
||||
void _cancel() {
|
||||
if (!_sending) Navigator.of(context).pop();
|
||||
}
|
||||
|
||||
Future<void> _create() async {
|
||||
if (!_canSubmit) return;
|
||||
final submitted = _name.text;
|
||||
setState(() {
|
||||
_sending = true;
|
||||
_error = null;
|
||||
});
|
||||
JsonMap? detail;
|
||||
try {
|
||||
await widget.repository.validate(widget.template.source);
|
||||
detail = await widget.repository.submit(
|
||||
submitted,
|
||||
widget.template.source,
|
||||
);
|
||||
} catch (error) {
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_sending = false;
|
||||
_error = describeTemplateCreateError(error);
|
||||
});
|
||||
_revealFeedback();
|
||||
return;
|
||||
}
|
||||
if (!mounted) return;
|
||||
final created = templateCreatedName(detail, submitted);
|
||||
setState(() {
|
||||
_sending = false;
|
||||
_created = created;
|
||||
});
|
||||
_revealFeedback();
|
||||
widget.onCreated(detail);
|
||||
}
|
||||
|
||||
/// The refusal/success line sits directly under the field, but a tall dialog
|
||||
/// can still be scrolled away; bring it into view when it appears so a
|
||||
/// refusal or an accepted create is never a silent no-op. Scrolling only when
|
||||
/// it does not already fit keeps the title and field in place.
|
||||
void _revealFeedback() {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
final feedback = _feedback.currentContext;
|
||||
if (feedback == null) return;
|
||||
final box = feedback.findRenderObject();
|
||||
final scrollable = Scrollable.maybeOf(feedback);
|
||||
final viewport = RenderAbstractViewport.maybeOf(box);
|
||||
if (box is! RenderBox || !box.attached || scrollable == null) return;
|
||||
if (viewport == null) return;
|
||||
final position = scrollable.position;
|
||||
final top = viewport.getOffsetToReveal(box, 0).offset;
|
||||
final start = position.pixels;
|
||||
final end = start + position.viewportDimension;
|
||||
if (top >= start - 1 && top + box.size.height <= end + 1) return;
|
||||
final target = top < start
|
||||
? top
|
||||
: viewport.getOffsetToReveal(box, 1).offset;
|
||||
position.jumpTo(
|
||||
target
|
||||
.clamp(position.minScrollExtent, position.maxScrollExtent)
|
||||
.toDouble(),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final color = widget.cupertino
|
||||
? CupertinoColors.systemRed.resolveFrom(context)
|
||||
: Theme.of(context).colorScheme.error;
|
||||
final content = Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
_field(),
|
||||
// Feedback stays next to the field it belongs to. The note below can
|
||||
// be pushed out of the dialog's scroll viewport on a short, large-text
|
||||
// screen; a refusal or success line must not be.
|
||||
Column(
|
||||
key: _feedback,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
if (_error case final error?)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
child: Text(
|
||||
error,
|
||||
key: templateCreateErrorKey,
|
||||
style: TextStyle(color: color),
|
||||
),
|
||||
),
|
||||
if (_created case final created?)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
child: Text(
|
||||
templateCreateSuccess(created),
|
||||
key: templateCreateSuccessKey,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
const Text(templateCreateNote),
|
||||
],
|
||||
);
|
||||
final actions = [
|
||||
_dialogAction(
|
||||
key: templateCreateCancelKey,
|
||||
onPressed: _sending ? null : _cancel,
|
||||
child: Text(
|
||||
_created == null
|
||||
? templateCreateCancelLabel
|
||||
: templateCreateCloseLabel,
|
||||
),
|
||||
),
|
||||
_dialogAction(
|
||||
key: templateCreateConfirmKey,
|
||||
primary: true,
|
||||
onPressed: _canSubmit ? _create : null,
|
||||
child: Text(
|
||||
_sending ? templateCreateSendingLabel : templateCreateConfirmLabel,
|
||||
),
|
||||
),
|
||||
];
|
||||
final title = Text(templateCreateTitle(widget.template));
|
||||
if (!widget.cupertino) {
|
||||
return PopScope(
|
||||
canPop: !_sending,
|
||||
child: AlertDialog(
|
||||
scrollable: true,
|
||||
title: title,
|
||||
content: content,
|
||||
actions: actions,
|
||||
),
|
||||
);
|
||||
}
|
||||
return PopScope(
|
||||
canPop: !_sending,
|
||||
child: CupertinoAlertDialog(
|
||||
title: title,
|
||||
content: content,
|
||||
actions: actions,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _field() {
|
||||
// A wrapping label above the field, like the browser's label span: the
|
||||
// Material floating label is ellipsized to `New pipeline name (requ…` at 2x
|
||||
// text, and a Cupertino placeholder is not exposed as the field's name.
|
||||
// Merging the visible label into the field gives both hosts one node with
|
||||
// the exact wording and the field's value.
|
||||
final field = widget.cupertino
|
||||
? CupertinoTextField(
|
||||
key: templateCreateNameKey,
|
||||
controller: _name,
|
||||
enabled: _fieldEnabled,
|
||||
autofocus: true,
|
||||
textInputAction: TextInputAction.done,
|
||||
onSubmitted: (_) => _create(),
|
||||
)
|
||||
: TextField(
|
||||
key: templateCreateNameKey,
|
||||
controller: _name,
|
||||
enabled: _fieldEnabled,
|
||||
autofocus: true,
|
||||
textInputAction: TextInputAction.done,
|
||||
onSubmitted: (_) => _create(),
|
||||
);
|
||||
return MergeSemantics(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
const Text(templateCreateNameLabel),
|
||||
const SizedBox(height: 8),
|
||||
field,
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _dialogAction({
|
||||
required Key key,
|
||||
required VoidCallback? onPressed,
|
||||
required Widget child,
|
||||
bool primary = false,
|
||||
}) {
|
||||
if (widget.cupertino) {
|
||||
return CupertinoDialogAction(
|
||||
key: key,
|
||||
isDefaultAction: primary,
|
||||
onPressed: onPressed,
|
||||
child: child,
|
||||
);
|
||||
}
|
||||
if (primary) {
|
||||
return FilledButton(key: key, onPressed: onPressed, child: child);
|
||||
}
|
||||
return TextButton(key: key, onPressed: onPressed, child: child);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
import '../../../core/api_gateway.dart';
|
||||
import '../domain/template_entry.dart';
|
||||
|
||||
/// The browser client's create-from-template wording, kept verbatim.
|
||||
const templateCreateNameLabel = 'New pipeline name (required)';
|
||||
const templateCreateNote =
|
||||
"Creates a new draft pipeline on the server from this template's source; it is not run.";
|
||||
const templateCreateConfirmLabel = 'Create';
|
||||
const templateCreateSendingLabel = 'Creating…';
|
||||
const templateCreateCancelLabel = 'Cancel';
|
||||
const templateCreateCloseLabel = 'Close';
|
||||
|
||||
/// Stable test/semantics keys for the prompt.
|
||||
const templateCreateNameKey = ValueKey<String>('template-create-name');
|
||||
const templateCreateConfirmKey = ValueKey<String>('template-create-confirm');
|
||||
const templateCreateCancelKey = ValueKey<String>('template-create-cancel');
|
||||
const templateCreateErrorKey = ValueKey<String>('template-create-error');
|
||||
const templateCreateSuccessKey = ValueKey<String>('template-create-success');
|
||||
|
||||
/// `Create from {name}`, a blank name falling back to the template key.
|
||||
String templateCreateTitle(TemplateEntry template) =>
|
||||
'Create from ${template.name.trim().isNotEmpty ? template.name : template.key}';
|
||||
|
||||
/// The server's own created name, else the submitted name, trimmed.
|
||||
String templateCreatedName(JsonMap detail, String submittedName) {
|
||||
final name = '${detail['name'] ?? ''}'.trim();
|
||||
return name.isNotEmpty ? name : submittedName.trim();
|
||||
}
|
||||
|
||||
/// `Created "{name}". It is in the Pipelines list.` — the browser's exact line.
|
||||
String templateCreateSuccess(String created) =>
|
||||
'Created "$created". It is in the Pipelines list.';
|
||||
|
||||
/// The server's refusal, verbatim; only a blank reason gets a generic sentence.
|
||||
String describeTemplateCreateError(Object error) {
|
||||
final message = '$error';
|
||||
return message.trim().isEmpty
|
||||
? 'The server did not create the pipeline.'
|
||||
: message;
|
||||
}
|
||||
Reference in New Issue
Block a user