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/cancel_run_action.dart'; typedef CancelHandler = Future Function(String type, JsonMap fields); /// A fake gateway that records every request and never owns a socket. class FakeCancelGateway implements ApiGateway { FakeCancelGateway(this.handler); final CancelHandler 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 executingRun({ num id = 7, bool executing = true, String status = 'running', int exportPending = 0, bool cancelRequested = false, bool cleanupUnconfirmed = false, }) => { 'id': id, 'title': 'Nightly build', 'status': status, 'executing': executing, 'export_pending_sequence': exportPending, 'cancel_requested': cancelRequested, 'cleanup_unconfirmed': cleanupUnconfirmed, }; JsonMap acceptedCancelRun() => executingRun(executing: false, status: 'canceled', cancelRequested: true); /// Owns the run like a run-detail screen would, so the callback's returned /// server detail is what the action renders next. class CancelHost extends StatefulWidget { const CancelHost({ super.key, required this.gateway, required this.initial, required this.changed, }); final FakeCancelGateway gateway; final JsonMap initial; final List changed; @override State createState() => _CancelHostState(); } class _CancelHostState extends State { late JsonMap _run = widget.initial; @override Widget build(BuildContext context) { return CancelRunAction( run: _run, gateway: widget.gateway, onRunChanged: (run) { widget.changed.add(run); setState(() => _run = run); }, ); } } Widget cancelApp( 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!, ); if (cupertino) { return CupertinoApp( builder: media, home: CupertinoPageScaffold(child: Center(child: child)), ); } return MaterialApp( builder: media, home: Scaffold(body: Center(child: child)), ); }