feat(app): add native saved-schedule editor form

This commit is contained in:
2026-10-21 21:51:15 +01:00
parent 76fb2cdd87
commit 8760a01b17
12 changed files with 2101 additions and 0 deletions
@@ -0,0 +1,191 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../../ui/adaptive/host.dart';
/// Secondary text under a field, using the host's own colours and text theme.
TextStyle scheduleEditorSecondaryStyle(BuildContext context) {
if (usesCupertinoHost(context)) {
return CupertinoTheme.of(context).textTheme.textStyle.copyWith(
fontSize: 14,
color: CupertinoColors.secondaryLabel.resolveFrom(context),
);
}
final theme = Theme.of(context);
return (theme.textTheme.bodySmall ?? const TextStyle()).copyWith(
color: theme.colorScheme.onSurfaceVariant,
);
}
/// The host's semantic error colour, never a form-specific palette.
TextStyle scheduleEditorErrorStyle(BuildContext context) {
if (usesCupertinoHost(context)) {
return CupertinoTheme.of(context).textTheme.textStyle.copyWith(
color: CupertinoColors.systemRed.resolveFrom(context),
);
}
return TextStyle(color: Theme.of(context).colorScheme.error);
}
/// A wrapping label above its control, with optional helper and field error.
///
/// The browser puts the label in a sibling span, so a native floating label
/// would invent different words and truncate at large text sizes.
class ScheduleEditorField extends StatelessWidget {
const ScheduleEditorField({
super.key,
required this.label,
required this.child,
this.helper,
this.error,
});
final String label;
final Widget child;
final String? helper;
final String? error;
@override
Widget build(BuildContext context) {
final labelStyle = usesCupertinoHost(context)
? CupertinoTheme.of(context).textTheme.textStyle
: Theme.of(context).textTheme.bodyMedium;
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(label, style: labelStyle),
const SizedBox(height: 6),
child,
if (helper case final text?)
Padding(
padding: const EdgeInsets.only(top: 4),
child: Text(text, style: scheduleEditorSecondaryStyle(context)),
),
if (error case final text?)
Padding(
padding: const EdgeInsets.only(top: 4),
child: Text(text, style: scheduleEditorErrorStyle(context)),
),
],
);
}
}
/// One-line or multiline text input in the host's native field style.
class ScheduleEditorTextField extends StatelessWidget {
const ScheduleEditorTextField({
super.key,
required this.label,
required this.controller,
required this.onChanged,
this.fieldKey,
this.keyboardType,
this.minLines = 1,
this.maxLines = 1,
this.enabled = true,
this.helper,
this.error,
this.textInputAction,
});
final String label;
final TextEditingController controller;
final ValueChanged<String> onChanged;
final Key? fieldKey;
final TextInputType? keyboardType;
final int? minLines;
final int? maxLines;
final bool enabled;
final String? helper;
final String? error;
final TextInputAction? textInputAction;
@override
Widget build(BuildContext context) {
final field = usesCupertinoHost(context)
? CupertinoTextField(
key: fieldKey,
controller: controller,
enabled: enabled,
keyboardType: keyboardType,
minLines: minLines,
maxLines: maxLines,
textInputAction: textInputAction,
onChanged: onChanged,
)
: TextField(
key: fieldKey,
controller: controller,
enabled: enabled,
keyboardType: keyboardType,
minLines: minLines,
maxLines: maxLines,
textInputAction: textInputAction,
onChanged: onChanged,
);
return MergeSemantics(
child: ScheduleEditorField(
label: label,
helper: helper,
error: error,
child: field,
),
);
}
}
/// A native switch with a wrapping label and an explanatory helper.
class ScheduleEditorSwitchField extends StatelessWidget {
const ScheduleEditorSwitchField({
super.key,
required this.label,
required this.value,
required this.onChanged,
this.helper,
this.enabled = true,
this.switchKey,
});
final String label;
final bool value;
final ValueChanged<bool> onChanged;
final String? helper;
final bool enabled;
final Key? switchKey;
@override
Widget build(BuildContext context) {
final control = usesCupertinoHost(context)
? CupertinoSwitch(
key: switchKey,
value: value,
onChanged: enabled ? onChanged : null,
)
: Switch(
key: switchKey,
value: value,
onChanged: enabled ? onChanged : null,
);
return MergeSemantics(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Row(
children: [
Expanded(child: Text(label)),
const SizedBox(width: 12),
control,
],
),
if (helper case final text?)
Padding(
padding: const EdgeInsets.only(top: 4),
child: Text(text, style: scheduleEditorSecondaryStyle(context)),
),
],
),
);
}
}
@@ -0,0 +1,234 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../../core/api_gateway.dart';
import '../../../../ui/adaptive/host.dart';
import '../../domain/editor/schedule_draft.dart';
import '../../domain/schedule_rule.dart';
import 'schedule_editor_form_body.dart';
import 'schedule_editor_wording.dart';
/// The server's refusal, verbatim, plus the browser's preservation promise.
String scheduleEditorRefusal(Object error) {
final reason = '$error'.trim();
return '${reason.isEmpty ? 'The schedule was not saved.' : reason} '
'$scheduleEditorDraftPreserved';
}
/// One native form over a landed [ScheduleDraft].
///
/// The form owns only editable controls: it mutates the draft on submit, asks
/// the draft to validate and assemble the exact record, and calls [onSubmit]
/// only with a non-null record. It never talks to a repository, route or
/// scheduler, and an async refusal leaves every typed value in place.
class ScheduleEditorForm extends StatefulWidget {
const ScheduleEditorForm({
super.key,
required this.draft,
required this.onSubmit,
this.onCancel,
this.title,
});
final ScheduleDraft draft;
final Future<void> Function(JsonMap record) onSubmit;
final VoidCallback? onCancel;
final String? title;
@override
State<ScheduleEditorForm> createState() => _ScheduleEditorFormState();
}
class _ScheduleEditorFormState extends State<ScheduleEditorForm> {
late final TextEditingController _name;
late final TextEditingController _prompt;
late final TextEditingController _version;
late final TextEditingController _workspace;
late final TextEditingController _every;
late final TextEditingController _times;
late final TextEditingController _timezone;
late ScheduleTimerMode _mode;
late List<String> _days;
late bool _enabled;
late String _notifyMode;
Map<String, String> _errors = const {};
String? _refusal;
bool _sending = false;
@override
void initState() {
super.initState();
final draft = widget.draft;
_name = TextEditingController(text: draft.name);
_prompt = TextEditingController(text: draft.prompt);
_version = TextEditingController(text: draft.version);
_workspace = TextEditingController(text: draft.workspace);
_every = TextEditingController(text: draft.every);
_times = TextEditingController(text: draft.times);
_timezone = TextEditingController(text: draft.timezone);
_mode = draft.mode;
_days = [...draft.days];
_enabled = draft.enabled;
_notifyMode = draft.notifyMode;
}
@override
void dispose() {
for (final controller in [
_name,
_prompt,
_version,
_workspace,
_every,
_times,
_timezone,
]) {
controller.dispose();
}
super.dispose();
}
String get _heading =>
widget.title ??
(widget.draft.source == null
? scheduleEditorAddTitle
: scheduleEditorEditTitle);
void _clearError(String field) {
if (!_errors.containsKey(field)) return;
setState(() => _errors = {..._errors}..remove(field));
}
void _applyToDraft() {
widget.draft
..name = _name.text
..prompt = _prompt.text
..version = _version.text
..workspace = _workspace.text
..enabled = _enabled
..mode = _mode
..every = _every.text
..times = _times.text
..days = [
for (final day in scheduleDays)
if (_days.contains(day)) day,
]
..timezone = _timezone.text
..notifyMode = _notifyMode;
}
Future<void> _submit() async {
if (_sending) return;
setState(() => _refusal = null);
_applyToDraft();
final result = widget.draft.build();
if (!result.isValid) {
setState(() => _errors = result.errors);
return;
}
setState(() {
_errors = const {};
_sending = true;
});
try {
await widget.onSubmit(result.record!);
} catch (error) {
if (!mounted) return;
setState(() {
_sending = false;
_refusal = scheduleEditorRefusal(error);
});
return;
}
if (mounted) setState(() => _sending = false);
}
@override
Widget build(BuildContext context) {
final cupertino = usesCupertinoHost(context);
return ScheduleEditorFormBody(
heading: _heading,
name: _name,
prompt: _prompt,
version: _version,
workspace: _workspace,
every: _every,
times: _times,
timezone: _timezone,
mode: _mode,
days: _days,
enabled: _enabled,
notifyMode: _notifyMode,
errors: _errors,
sending: _sending,
refusal: _refusal,
onModeChanged: (mode) => setState(() {
_mode = mode;
_errors = {..._errors}
..remove('every')
..remove('times');
}),
onDayToggled: (day) => setState(() {
_days = [..._days];
_days.contains(day) ? _days.remove(day) : _days.add(day);
_errors = {..._errors}..remove('days');
}),
onEnabledChanged: (enabled) => setState(() => _enabled = enabled),
onNotifyChanged: (mode) => setState(() {
_notifyMode = mode;
_errors = {..._errors}..remove('notifyMode');
}),
onEdited: _clearError,
actions: Wrap(
alignment: WrapAlignment.end,
crossAxisAlignment: WrapCrossAlignment.center,
spacing: 12,
runSpacing: 8,
children: [
if (widget.onCancel != null)
_cancelButton(cupertino)
else
const SizedBox.shrink(),
_saveButton(cupertino),
],
),
);
}
Widget _cancelButton(bool cupertino) {
final onPressed = _sending ? null : widget.onCancel;
if (cupertino) {
return CupertinoButton(
key: scheduleEditorCancelKey,
padding: EdgeInsets.zero,
onPressed: onPressed,
child: const Text(scheduleEditorCancelLabel),
);
}
return TextButton(
key: scheduleEditorCancelKey,
onPressed: onPressed,
child: const Text(scheduleEditorCancelLabel),
);
}
Widget _saveButton(bool cupertino) {
final label = Text(
_sending ? scheduleEditorSendingLabel : scheduleEditorSubmitLabel,
);
final onPressed = _sending ? null : _submit;
if (cupertino) {
return CupertinoButton.filled(
key: scheduleEditorSubmitKey,
onPressed: onPressed,
child: label,
);
}
return FilledButton(
key: scheduleEditorSubmitKey,
onPressed: onPressed,
child: label,
);
}
}
@@ -0,0 +1,229 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../../ui/adaptive/host.dart';
import '../../domain/editor/schedule_draft.dart';
import 'schedule_editor_fields.dart';
import 'schedule_editor_selectors.dart';
import 'schedule_editor_wording.dart';
/// The visible schedule editor column: native fields, field errors and the
/// Cancel/Save actions. State stays in `ScheduleEditorForm`; this widget only
/// renders the values and forwards edits.
class ScheduleEditorFormBody extends StatelessWidget {
const ScheduleEditorFormBody({
super.key,
required this.heading,
required this.name,
required this.prompt,
required this.version,
required this.workspace,
required this.every,
required this.times,
required this.timezone,
required this.mode,
required this.days,
required this.enabled,
required this.notifyMode,
required this.errors,
required this.sending,
this.refusal,
required this.onModeChanged,
required this.onDayToggled,
required this.onEnabledChanged,
required this.onNotifyChanged,
required this.onEdited,
required this.actions,
});
final String heading;
final TextEditingController name;
final TextEditingController prompt;
final TextEditingController version;
final TextEditingController workspace;
final TextEditingController every;
final TextEditingController times;
final TextEditingController timezone;
final ScheduleTimerMode mode;
final List<String> days;
final bool enabled;
final String notifyMode;
final Map<String, String> errors;
final bool sending;
final String? refusal;
final ValueChanged<ScheduleTimerMode> onModeChanged;
final ValueChanged<String> onDayToggled;
final ValueChanged<bool> onEnabledChanged;
final ValueChanged<String> onNotifyChanged;
final ValueChanged<String> onEdited;
final Widget actions;
static const _gap = SizedBox(height: 16);
static const _timerOptions = [
ScheduleEditorChoice(ScheduleTimerMode.interval, 'Interval'),
ScheduleEditorChoice(ScheduleTimerMode.clock, 'Clock times'),
];
List<ScheduleEditorChoice<String>> get _notifyOptions {
const values = ['auto', 'on', 'off'];
return [
if (!values.contains(notifyMode))
ScheduleEditorChoice(notifyMode, notifyMode),
for (final value in values) ScheduleEditorChoice(value, value),
];
}
Widget _textField({
required Key key,
required String label,
required TextEditingController controller,
required String field,
String? error,
int minLines = 1,
int? maxLines = 1,
TextInputType? keyboardType,
TextInputAction? textInputAction,
}) => ScheduleEditorTextField(
fieldKey: key,
label: label,
controller: controller,
enabled: !sending,
minLines: minLines,
maxLines: maxLines,
keyboardType: keyboardType,
textInputAction: textInputAction,
error: error,
onChanged: (_) => onEdited(field),
);
@override
Widget build(BuildContext context) {
final cupertino = usesCupertinoHost(context);
final titleStyle = cupertino
? CupertinoTheme.of(context).textTheme.navLargeTitleTextStyle
: Theme.of(context).textTheme.titleLarge;
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(heading, style: titleStyle),
const SizedBox(height: 8),
Text(scheduleEditorNote, style: scheduleEditorSecondaryStyle(context)),
_gap,
_textField(
key: scheduleEditorNameKey,
label: scheduleEditorNameLabel,
controller: name,
field: 'name',
error: errors['name'],
textInputAction: TextInputAction.next,
),
_gap,
_textField(
key: scheduleEditorPromptKey,
label: scheduleEditorPromptLabel,
controller: prompt,
field: 'prompt',
error: errors['prompt'],
minLines: 3,
maxLines: null,
keyboardType: TextInputType.multiline,
),
_gap,
_textField(
key: scheduleEditorVersionKey,
label: scheduleEditorVersionLabel,
controller: version,
field: 'version',
error: errors['version'],
keyboardType: TextInputType.number,
textInputAction: TextInputAction.next,
),
_gap,
_textField(
key: scheduleEditorWorkspaceKey,
label: scheduleEditorWorkspaceLabel,
controller: workspace,
field: 'workspace',
error: errors['workspace'],
textInputAction: TextInputAction.next,
),
_gap,
ScheduleEditorChoiceField<ScheduleTimerMode>(
fieldKey: scheduleEditorTimerKey,
label: scheduleEditorTimerLabel,
value: mode,
options: _timerOptions,
enabled: !sending,
onChanged: onModeChanged,
),
_gap,
if (mode == ScheduleTimerMode.interval)
_textField(
key: scheduleEditorEveryKey,
label: scheduleEditorEveryLabel,
controller: every,
field: 'every',
error: errors['every'],
)
else
_textField(
key: scheduleEditorTimesKey,
label: scheduleEditorTimesLabel,
controller: times,
field: 'times',
error: errors['times'],
),
_gap,
ScheduleEditorDaysField(
label: scheduleEditorDaysLabel,
days: days,
enabled: !sending,
error: errors['days'],
onToggle: onDayToggled,
),
_gap,
_textField(
key: scheduleEditorTimezoneKey,
label: scheduleEditorTimezoneLabel,
controller: timezone,
field: 'timezone',
error: errors['timezone'],
),
_gap,
ScheduleEditorSwitchField(
switchKey: scheduleEditorEnabledKey,
label: scheduleEditorEnabledLabel,
helper: scheduleEditorEnabledNote,
value: enabled,
enabled: !sending,
onChanged: onEnabledChanged,
),
_gap,
ScheduleEditorChoiceField<String>(
fieldKey: scheduleEditorNotifyKey,
label: scheduleEditorNotificationsLabel,
value: notifyMode,
options: _notifyOptions,
enabled: !sending,
error: errors['notifyMode'],
onChanged: onNotifyChanged,
),
if (refusal ?? errors['record'] ?? errors['pipelineId']
case final error?) ...[
_gap,
Semantics(
liveRegion: true,
child: Text(
error,
key: scheduleEditorErrorKey,
style: scheduleEditorErrorStyle(context),
),
),
],
_gap,
actions,
],
);
}
}
@@ -0,0 +1,200 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../../ui/adaptive/host.dart';
import '../../domain/schedule_rule.dart';
import 'schedule_editor_fields.dart';
/// Day checkbox keys, shared with tests and the browser fixture.
ValueKey<String> scheduleEditorDayKey(String day) =>
ValueKey<String>('schedule-editor-day-$day');
/// One option of a [ScheduleEditorChoiceField].
class ScheduleEditorChoice<T> {
const ScheduleEditorChoice(this.value, this.label);
final T value;
final String label;
}
/// A native drop-down on Material and a native action sheet on Cupertino.
class ScheduleEditorChoiceField<T> extends StatelessWidget {
const ScheduleEditorChoiceField({
super.key,
required this.label,
required this.value,
required this.options,
required this.onChanged,
this.enabled = true,
this.helper,
this.error,
this.fieldKey,
});
final String label;
final T value;
final List<ScheduleEditorChoice<T>> options;
final ValueChanged<T> onChanged;
final bool enabled;
final String? helper;
final String? error;
final Key? fieldKey;
String get _selectedLabel {
for (final option in options) {
if (option.value == value) return option.label;
}
return '';
}
Future<void> _choose(BuildContext context) async {
final chosen = await showCupertinoModalPopup<T>(
context: context,
builder: (sheetContext) => CupertinoActionSheet(
title: Text(label),
actions: [
for (final option in options)
CupertinoActionSheetAction(
onPressed: () => Navigator.pop(sheetContext, option.value),
child: Text(option.label),
),
],
cancelButton: CupertinoActionSheetAction(
onPressed: () => Navigator.pop(sheetContext),
child: const Text('Cancel'),
),
),
);
if (chosen != null) onChanged(chosen);
}
@override
Widget build(BuildContext context) {
final control = usesCupertinoHost(context)
? CupertinoButton(
key: fieldKey,
padding: EdgeInsets.zero,
onPressed: enabled ? () => _choose(context) : null,
child: Row(
children: [
Expanded(child: Text(_selectedLabel)),
const Icon(CupertinoIcons.chevron_down),
],
),
)
: DropdownButtonFormField<T>(
key: fieldKey,
initialValue: value,
isExpanded: true,
items: [
for (final option in options)
DropdownMenuItem<T>(
value: option.value,
child: Text(option.label),
),
],
onChanged: enabled
? (chosen) {
if (chosen != null) onChanged(chosen);
}
: null,
);
return MergeSemantics(
child: ScheduleEditorField(
label: label,
helper: helper,
error: error,
child: control,
),
);
}
}
/// Weekday checkboxes in a wrapping row; no selection means every day.
class ScheduleEditorDaysField extends StatelessWidget {
const ScheduleEditorDaysField({
super.key,
required this.label,
required this.days,
required this.onToggle,
this.enabled = true,
this.error,
});
final String label;
final List<String> days;
final ValueChanged<String> onToggle;
final bool enabled;
final String? error;
@override
Widget build(BuildContext context) {
return ScheduleEditorField(
label: label,
error: error,
child: Wrap(
spacing: 12,
runSpacing: 8,
children: [
for (final day in scheduleDays)
_DayToggle(
day: day,
selected: days.contains(day),
enabled: enabled,
onTap: () => onToggle(day),
),
],
),
);
}
}
class _DayToggle extends StatelessWidget {
const _DayToggle({
required this.day,
required this.selected,
required this.enabled,
required this.onTap,
});
final String day;
final bool selected;
final bool enabled;
final VoidCallback onTap;
@override
Widget build(BuildContext context) {
final cupertino = usesCupertinoHost(context);
final checkbox = cupertino
? CupertinoCheckbox(
key: scheduleEditorDayKey(day),
value: selected,
onChanged: enabled ? (_) => onTap() : null,
)
: Checkbox(
key: scheduleEditorDayKey(day),
value: selected,
onChanged: enabled ? (_) => onTap() : null,
);
final content = Padding(
padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 4),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [checkbox, const SizedBox(width: 4), Text(day)],
),
);
return MergeSemantics(
child: cupertino
? GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: enabled ? onTap : null,
child: content,
)
: InkWell(
borderRadius: BorderRadius.circular(8),
onTap: enabled ? onTap : null,
child: content,
),
);
}
}
@@ -0,0 +1,45 @@
import 'package:flutter/widgets.dart';
/// Browser wording and stable keys for the saved-schedule editor, kept
/// verbatim from `web/src/views/schedules.js`.
const scheduleEditorAddTitle = 'Add schedule';
const scheduleEditorEditTitle = 'Edit schedule';
const scheduleEditorNote =
'Future runs only; does not publish or change the workflow. '
'Saving does not start a run.';
const scheduleEditorNameLabel = 'Name';
const scheduleEditorPromptLabel = 'Prompt';
const scheduleEditorVersionLabel = 'Pinned version';
const scheduleEditorWorkspaceLabel = 'Server workspace';
const scheduleEditorTimerLabel = 'Timer';
const scheduleEditorEveryLabel = 'Interval (at least 1m)';
const scheduleEditorTimesLabel = 'Clock times (24h, comma separated)';
const scheduleEditorDaysLabel = 'Weekdays (none means every day)';
const scheduleEditorTimezoneLabel = 'Timezone (blank uses server timezone)';
const scheduleEditorEnabledLabel = 'Enabled';
const scheduleEditorEnabledNote =
'Enabled schedules start future runs automatically, including model '
'usage and the workflow’s permitted tools.';
const scheduleEditorNotificationsLabel = 'Notifications';
const scheduleEditorSubmitLabel = 'Save schedule';
const scheduleEditorSendingLabel = 'Saving…';
const scheduleEditorCancelLabel = 'Cancel';
const scheduleEditorDraftPreserved =
'Your draft is preserved. If this schedule changed elsewhere, reopen it '
'before retrying.';
const scheduleEditorNameKey = ValueKey<String>('schedule-editor-name');
const scheduleEditorPromptKey = ValueKey<String>('schedule-editor-prompt');
const scheduleEditorVersionKey = ValueKey<String>('schedule-editor-version');
const scheduleEditorWorkspaceKey = ValueKey<String>(
'schedule-editor-workspace',
);
const scheduleEditorTimerKey = ValueKey<String>('schedule-editor-timer');
const scheduleEditorEveryKey = ValueKey<String>('schedule-editor-every');
const scheduleEditorTimesKey = ValueKey<String>('schedule-editor-times');
const scheduleEditorTimezoneKey = ValueKey<String>('schedule-editor-timezone');
const scheduleEditorEnabledKey = ValueKey<String>('schedule-editor-enabled');
const scheduleEditorNotifyKey = ValueKey<String>('schedule-editor-notify');
const scheduleEditorSubmitKey = ValueKey<String>('schedule-editor-submit');
const scheduleEditorCancelKey = ValueKey<String>('schedule-editor-cancel');
const scheduleEditorErrorKey = ValueKey<String>('schedule-editor-error');