import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../../../core/api_gateway.dart'; import '../../../ui/adaptive/host.dart'; import '../../changes/changes.dart'; /// The run detail's action and page label. The viewer itself owns the /// recorded summary and capture wording, so this bar stays neutral. const runChangesLabel = 'Changes'; const runChangesButtonKey = ValueKey('run-detail-changes'); const runVisitChangesButtonKey = ValueKey('run-visit-changes'); /// The detail's `steps` exactly as returned by the server. The pushed page /// snapshots the returned list once, so it never re-derives on each rebuild. List runDetailChangeSteps(JsonMap run) { final steps = run['steps']; if (steps is List) return steps; if (steps is List) return steps.cast(); return const []; } const _executingStatuses = {'queued', 'running', 'waiting'}; /// Whether the run detail offers the action at all. The opened page is a /// snapshot of the recorded steps, so it is offered only once nothing is /// executing: the server's own `executing` flag (which can stay true on a /// failed run whose step is still active) or a queued/running/waiting status /// would otherwise show a page that silently goes stale. bool showRunChangesAction(JsonMap run) => run['executing'] != true && !_executingStatuses.contains('${run['status'] ?? ''}'.trim().toLowerCase()); /// A standard native button for the run detail body. It opens the recorded /// changes in a platform page whose single navigation bar is the only chrome. class RunChangesButton extends StatelessWidget { const RunChangesButton({super.key, required this.run}); final JsonMap run; @override Widget build(BuildContext context) { final steps = runDetailChangeSteps(run); // The web client shows the run-wide aggregate only for a succeeded run; a // failed run still keeps each visit's own recorded capture. final summary = showRunChangesSummary(run['status']); void open() => showRunChanges(context, steps: steps, showSummary: summary); if (usesCupertinoHost(context)) { return CupertinoButton.tinted( key: runChangesButtonKey, onPressed: open, // The label keeps its intrinsic size until scaled text no longer fits // the detail's narrowest pane; then it wraps inside the native button // instead of overflowing the header row. child: const Row( mainAxisSize: MainAxisSize.min, children: [ Icon(CupertinoIcons.doc_text), SizedBox(width: 6), Flexible(child: Text(runChangesLabel)), ], ), ); } return OutlinedButton.icon( key: runChangesButtonKey, onPressed: open, icon: const Icon(Icons.difference_outlined), label: const Text(runChangesLabel), ); } } /// Opens the already-landed [ChangesViewer] for [steps] on a native page. /// The bar is the one title bar; the viewer's own summary and capture facts /// are passed through untouched, except for the caller's [showSummary] gate. /// /// [title] names the evidence the page shows. The run-level action keeps the /// neutral [runChangesLabel]; a visit passes [visitChangesTitle] so the pushed /// page cannot be mistaken for the run-wide aggregate. Future showRunChanges( BuildContext context, { required List steps, required bool showSummary, String title = runChangesLabel, }) { final cupertino = usesCupertinoHost(context); return Navigator.of(context).push( cupertino ? CupertinoPageRoute( builder: (_) => _CupertinoChangesPage( steps: steps, showSummary: showSummary, title: title, ), ) : MaterialPageRoute( builder: (_) => _MaterialChangesPage( steps: steps, showSummary: showSummary, title: title, ), ), ); } /// The page identity for exactly one recorded visit: the stage and visit the /// timeline row shows, so the page names its evidence rather than the run. String visitChangesTitle(String stage, int? sequence) { final name = stage.trim(); final visit = sequence == null ? 'visit unavailable' : 'visit $sequence'; return name.isEmpty ? '$runChangesLabel · $visit' : '$runChangesLabel · $name · $visit'; } /// Opens the Changes viewer for exactly one visit's recorded [step]. /// /// The page shows that step's own capture only - never the run's other visits - /// and no run-wide final-state header, which belongs to the run-level action. Future showVisitChanges( BuildContext context, { required String stage, required int? sequence, required JsonMap step, }) => showRunChanges( context, steps: [step], showSummary: false, title: visitChangesTitle(stage, sequence), ); /// The opened visit pane's own native action. It passes only the visit's step, /// so the viewer's per-stage section is its single evidence entry. class RunVisitChangesButton extends StatelessWidget { const RunVisitChangesButton({ super.key, required this.stage, required this.sequence, required this.step, }); final String stage; final int? sequence; final JsonMap step; @override Widget build(BuildContext context) { void open() => showVisitChanges(context, stage: stage, sequence: sequence, step: step); if (usesCupertinoHost(context)) { return CupertinoButton( key: runVisitChangesButtonKey, padding: EdgeInsets.zero, onPressed: open, child: const Row( mainAxisSize: MainAxisSize.min, children: [ Icon(CupertinoIcons.doc_text), SizedBox(width: 6), Flexible(child: Text(runChangesLabel)), ], ), ); } return TextButton.icon( key: runVisitChangesButtonKey, onPressed: open, icon: const Icon(Icons.difference_outlined), label: const Text(runChangesLabel), ); } } class _MaterialChangesPage extends StatelessWidget { const _MaterialChangesPage({ required this.steps, required this.showSummary, required this.title, }); final List steps; final bool showSummary; final String title; @override Widget build(BuildContext context) => Scaffold( appBar: AppBar(title: Text(title)), body: SafeArea( top: false, child: ChangesViewer(steps: steps, showSummary: showSummary), ), ); } class _CupertinoChangesPage extends StatelessWidget { const _CupertinoChangesPage({ required this.steps, required this.showSummary, required this.title, }); final List steps; final bool showSummary; final String title; @override Widget build(BuildContext context) => CupertinoPageScaffold( navigationBar: CupertinoNavigationBar(middle: Text(title)), child: SafeArea( // A CupertinoApp supplies no Material theme; follow the host brightness // so the viewer's Material-derived section text stays readable. child: Theme( data: hostTextTheme(context), child: ChangesViewer(steps: steps, showSummary: showSummary), ), ), ); }