feat(app): add native rename-run action
Native Material/Cupertino prompt seeded from the run's stored title. Only
Save sends set_run_title {run_id, title} with the text untrimmed; typing and
Cancel send nothing, refusals stay verbatim with the draft kept, and only the
accepted run_detail is delivered once.
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../core/api_gateway.dart';
|
||||
import '../data/runs_repository.dart';
|
||||
import 'action_refusal.dart';
|
||||
import 'title_dialog.dart';
|
||||
import 'title_wording.dart';
|
||||
|
||||
/// The run detail's rename entry point, mirroring the browser's rename row.
|
||||
///
|
||||
/// The prompt starts with [run]'s stored title exactly as the server sent it
|
||||
/// (never a prompt or output fallback). Typing and Cancel send nothing; only
|
||||
/// Save sends `set_run_title` with the exact run id and title text. A refusal is
|
||||
/// returned verbatim to the kept-open prompt, and only the server's returned
|
||||
/// `run_detail` is delivered through [onRunChanged], once.
|
||||
class RunTitleAction extends StatefulWidget {
|
||||
const RunTitleAction({
|
||||
super.key,
|
||||
required this.run,
|
||||
required this.gateway,
|
||||
required this.onRunChanged,
|
||||
});
|
||||
|
||||
final JsonMap run;
|
||||
final ApiGateway gateway;
|
||||
final ValueChanged<JsonMap> onRunChanged;
|
||||
|
||||
@override
|
||||
State<RunTitleAction> createState() => _RunTitleActionState();
|
||||
}
|
||||
|
||||
class _RunTitleActionState extends State<RunTitleAction> {
|
||||
bool _dialogOpen = false;
|
||||
bool _saving = false;
|
||||
|
||||
bool _usesCupertino(BuildContext context) {
|
||||
final material = Localizations.of<MaterialLocalizations>(
|
||||
context,
|
||||
MaterialLocalizations,
|
||||
);
|
||||
return material == null;
|
||||
}
|
||||
|
||||
Future<void> _open() async {
|
||||
final id = runTitleId(widget.run);
|
||||
if (id == null || _dialogOpen) return;
|
||||
_dialogOpen = true;
|
||||
try {
|
||||
await showRunTitlePrompt(
|
||||
context: context,
|
||||
runId: id,
|
||||
initialTitle: storedRunTitle(widget.run),
|
||||
cupertino: _usesCupertino(context),
|
||||
onSubmit: (title) => _submit(id, title),
|
||||
);
|
||||
} finally {
|
||||
_dialogOpen = false;
|
||||
if (mounted) setState(() {});
|
||||
}
|
||||
}
|
||||
|
||||
Future<ActionRefusal?> _submit(num id, String title) async {
|
||||
if (_saving) return const ActionRefusal(actionAlreadySending);
|
||||
_saving = true;
|
||||
try {
|
||||
final run = await RunsRepository(widget.gateway).setRunTitle(id, title);
|
||||
if (!mounted) return null;
|
||||
if (_owns(id)) widget.onRunChanged(run);
|
||||
return null;
|
||||
} catch (error) {
|
||||
return ActionRefusal(describeTitleError(error));
|
||||
} finally {
|
||||
_saving = false;
|
||||
}
|
||||
}
|
||||
|
||||
bool _owns(num id) => mounted && runTitleId(widget.run) == id;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (runTitleId(widget.run) == null) return const SizedBox.shrink();
|
||||
if (_usesCupertino(context)) {
|
||||
return CupertinoButton(
|
||||
key: runTitleActionKey,
|
||||
onPressed: _open,
|
||||
child: const Text(runTitleActionLabel),
|
||||
);
|
||||
}
|
||||
return OutlinedButton(
|
||||
key: runTitleActionKey,
|
||||
onPressed: _open,
|
||||
child: const Text(runTitleActionLabel),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// The run id used by the rename request, or null for a malformed run.
|
||||
num? runTitleId(JsonMap run) {
|
||||
final id = run['id'];
|
||||
return id is num ? id : null;
|
||||
}
|
||||
|
||||
/// The run's stored title exactly as the browser reads it
|
||||
/// (`String(d.title ?? "")`); never inferred from the prompt or output.
|
||||
String storedRunTitle(JsonMap run) {
|
||||
final title = run['title'];
|
||||
return title == null ? '' : '$title';
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'action_refusal.dart';
|
||||
import 'title_wording.dart';
|
||||
|
||||
/// Sends the typed title. Returns null once the server has accepted it, or an
|
||||
/// [ActionRefusal] carrying the server's words verbatim; the prompt then stays
|
||||
/// open with the typed title intact.
|
||||
typedef TitleSubmit = Future<ActionRefusal?> Function(String title);
|
||||
|
||||
/// Opens the native rename prompt for [runId], seeded with the run's stored
|
||||
/// [initialTitle].
|
||||
///
|
||||
/// Typing and the dismiss action send nothing; only Save sends the title. The
|
||||
/// title is passed through exactly as typed, including surrounding whitespace,
|
||||
/// matching `setRunTitle` in `web/src/repository.js`; the server decides whether
|
||||
/// a blank title is acceptable.
|
||||
Future<void> showRunTitlePrompt({
|
||||
required BuildContext context,
|
||||
required num runId,
|
||||
required String initialTitle,
|
||||
required bool cupertino,
|
||||
required TitleSubmit onSubmit,
|
||||
}) {
|
||||
final prompt = _TitlePrompt(
|
||||
runId: runId,
|
||||
initialTitle: initialTitle,
|
||||
cupertino: cupertino,
|
||||
onSubmit: onSubmit,
|
||||
);
|
||||
if (cupertino) {
|
||||
return showCupertinoDialog<void>(
|
||||
context: context,
|
||||
barrierDismissible: false,
|
||||
builder: (_) => prompt,
|
||||
);
|
||||
}
|
||||
return showDialog<void>(
|
||||
context: context,
|
||||
barrierDismissible: false,
|
||||
builder: (_) => prompt,
|
||||
);
|
||||
}
|
||||
|
||||
class _TitlePrompt extends StatefulWidget {
|
||||
const _TitlePrompt({
|
||||
required this.runId,
|
||||
required this.initialTitle,
|
||||
required this.cupertino,
|
||||
required this.onSubmit,
|
||||
});
|
||||
|
||||
final num runId;
|
||||
final String initialTitle;
|
||||
final bool cupertino;
|
||||
final TitleSubmit onSubmit;
|
||||
|
||||
@override
|
||||
State<_TitlePrompt> createState() => _TitlePromptState();
|
||||
}
|
||||
|
||||
class _TitlePromptState extends State<_TitlePrompt> {
|
||||
late final TextEditingController _title = TextEditingController(
|
||||
text: widget.initialTitle,
|
||||
);
|
||||
bool _sending = false;
|
||||
String? _error;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_title.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _cancel() {
|
||||
if (!_sending) Navigator.of(context).pop();
|
||||
}
|
||||
|
||||
Future<void> _save() async {
|
||||
if (_sending) return;
|
||||
setState(() {
|
||||
_sending = true;
|
||||
_error = null;
|
||||
});
|
||||
final refusal = await widget.onSubmit(_title.text);
|
||||
if (!mounted) return;
|
||||
if (refusal == null) {
|
||||
Navigator.of(context).pop();
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
_sending = false;
|
||||
_error = refusal.message;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final dialog = widget.cupertino ? _cupertino(context) : _material(context);
|
||||
return PopScope(canPop: !_sending, child: dialog);
|
||||
}
|
||||
|
||||
Widget _material(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
return AlertDialog(
|
||||
scrollable: true,
|
||||
title: const Text(runTitleDialogTitle),
|
||||
content: _body(
|
||||
field: TextField(
|
||||
key: runTitleFieldKey,
|
||||
controller: _title,
|
||||
enabled: !_sending,
|
||||
autofocus: true,
|
||||
decoration: const InputDecoration(labelText: runTitleFieldLabel),
|
||||
textInputAction: TextInputAction.done,
|
||||
onSubmitted: (_) => _save(),
|
||||
),
|
||||
errorStyle: TextStyle(color: theme.colorScheme.error),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
key: runTitleCancelKey,
|
||||
onPressed: _sending ? null : _cancel,
|
||||
child: const Text(runTitleCancelLabel),
|
||||
),
|
||||
FilledButton(
|
||||
key: runTitleSaveKey,
|
||||
onPressed: _sending ? null : _save,
|
||||
child: const Text(runTitleSaveLabel),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _cupertino(BuildContext context) {
|
||||
return CupertinoAlertDialog(
|
||||
title: const Text(runTitleDialogTitle),
|
||||
content: _body(
|
||||
field: CupertinoTextField(
|
||||
key: runTitleFieldKey,
|
||||
controller: _title,
|
||||
enabled: !_sending,
|
||||
autofocus: true,
|
||||
placeholder: runTitleFieldLabel,
|
||||
textInputAction: TextInputAction.done,
|
||||
onSubmitted: (_) => _save(),
|
||||
),
|
||||
errorStyle: TextStyle(
|
||||
color: CupertinoColors.systemRed.resolveFrom(context),
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
CupertinoDialogAction(
|
||||
key: runTitleCancelKey,
|
||||
onPressed: _sending ? null : _cancel,
|
||||
child: const Text(runTitleCancelLabel),
|
||||
),
|
||||
CupertinoDialogAction(
|
||||
key: runTitleSaveKey,
|
||||
isDefaultAction: true,
|
||||
onPressed: _sending ? null : _save,
|
||||
child: const Text(runTitleSaveLabel),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _body({required Widget field, required TextStyle errorStyle}) {
|
||||
return Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
field,
|
||||
if (_error case final error?)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
child: Text(error, key: runTitleErrorKey, style: errorStyle),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
/// The browser client's rename wording, kept verbatim.
|
||||
const runTitleActionLabel = 'Rename';
|
||||
const runTitleDialogTitle = 'Rename';
|
||||
const runTitleFieldLabel = 'Run title';
|
||||
const runTitleSaveLabel = 'Save';
|
||||
const runTitleCancelLabel = 'Cancel';
|
||||
|
||||
/// Stable test/semantics keys shared by the action and its prompt.
|
||||
const runTitleActionKey = ValueKey<String>('rename-run-action');
|
||||
const runTitleFieldKey = ValueKey<String>('rename-run-field');
|
||||
const runTitleSaveKey = ValueKey<String>('rename-run-save');
|
||||
const runTitleCancelKey = ValueKey<String>('rename-run-cancel');
|
||||
const runTitleErrorKey = ValueKey<String>('rename-run-error');
|
||||
|
||||
/// The server's refusal, verbatim. Only a blank error gets a generic sentence
|
||||
/// so the prompt never claims a rename that did not happen.
|
||||
String describeTitleError(Object error) {
|
||||
final message = '$error';
|
||||
return message.trim().isEmpty
|
||||
? 'The server did not rename the run.'
|
||||
: message;
|
||||
}
|
||||
Reference in New Issue
Block a user