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 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 call(String type, [JsonMap fields = const {}]) { final copy = Map.from(fields); calls.add((type: type, fields: copy)); return handler(type, copy); } @override Stream 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 changed; @override State createState() => _TitleHostState(); } class _TitleHostState extends State { 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('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), ); }