f2643a0ff4
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.
111 lines
2.9 KiB
Dart
111 lines
2.9 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:pipelines_app/core/api_gateway.dart';
|
|
import 'package:pipelines_app/features/runs/actions/title_action.dart';
|
|
|
|
typedef TitleHandler = Future<JsonMap> Function(String type, JsonMap fields);
|
|
|
|
/// A fake gateway that records every request and never owns a socket.
|
|
class FakeTitleGateway implements ApiGateway {
|
|
FakeTitleGateway(this.handler);
|
|
|
|
final TitleHandler handler;
|
|
final List<({String type, JsonMap fields})> calls = [];
|
|
|
|
@override
|
|
Future<JsonMap> call(String type, [JsonMap fields = const {}]) {
|
|
final copy = Map<String, dynamic>.from(fields);
|
|
calls.add((type: type, fields: copy));
|
|
return handler(type, copy);
|
|
}
|
|
|
|
@override
|
|
Stream<JsonMap> get frames => const Stream.empty();
|
|
}
|
|
|
|
JsonMap titledRun({
|
|
num id = 7,
|
|
String? title = 'Nightly build',
|
|
String status = 'succeeded',
|
|
String prompt = 'the submitted prompt',
|
|
}) => {
|
|
'id': id,
|
|
'title': title,
|
|
'status': status,
|
|
'input': {'prompt': prompt},
|
|
};
|
|
|
|
JsonMap renamedRun({num id = 7, String title = 'Release build'}) =>
|
|
titledRun(id: id, title: title, status: 'succeeded');
|
|
|
|
/// Owns the run like a run-detail screen would, so the callback's returned
|
|
/// server detail is what the action renders next. Re-pumping with a different
|
|
/// initial run updates the action in place, as a refreshed detail does.
|
|
class TitleHost extends StatefulWidget {
|
|
const TitleHost({
|
|
super.key,
|
|
required this.gateway,
|
|
required this.initial,
|
|
required this.changed,
|
|
});
|
|
|
|
final FakeTitleGateway gateway;
|
|
final JsonMap initial;
|
|
final List<JsonMap> changed;
|
|
|
|
@override
|
|
State<TitleHost> createState() => _TitleHostState();
|
|
}
|
|
|
|
class _TitleHostState extends State<TitleHost> {
|
|
late JsonMap _run = widget.initial;
|
|
|
|
@override
|
|
void didUpdateWidget(covariant TitleHost oldWidget) {
|
|
super.didUpdateWidget(oldWidget);
|
|
if (!identical(oldWidget.initial, widget.initial)) {
|
|
_run = widget.initial;
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Text('${_run['title'] ?? ''}', key: titleHostTitleKey),
|
|
RunTitleAction(
|
|
run: _run,
|
|
gateway: widget.gateway,
|
|
onRunChanged: (run) {
|
|
widget.changed.add(run);
|
|
setState(() => _run = run);
|
|
},
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|
|
|
|
const titleHostTitleKey = ValueKey<String>('rename-host-title');
|
|
|
|
Widget titleApp(Widget child, {required bool cupertino, double textScale = 1}) {
|
|
Widget media(BuildContext context, Widget? app) => MediaQuery(
|
|
data: MediaQuery.of(
|
|
context,
|
|
).copyWith(textScaler: TextScaler.linear(textScale)),
|
|
child: app!,
|
|
);
|
|
final body = Center(child: child);
|
|
if (cupertino) {
|
|
return CupertinoApp(
|
|
builder: media,
|
|
home: CupertinoPageScaffold(child: body),
|
|
);
|
|
}
|
|
return MaterialApp(
|
|
builder: media,
|
|
home: Scaffold(body: body),
|
|
);
|
|
}
|