import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../../../core/api_gateway.dart'; import '../../../ui/adaptive/host.dart'; import '../domain/run_models.dart'; import 'runs_controller.dart'; class RunsList extends StatelessWidget { const RunsList({ super.key, required this.controller, required this.onSelected, }); final RunsController controller; final ValueChanged onSelected; @override Widget build(BuildContext context) { if (controller.loading && controller.runs.isEmpty) { return Center( child: usesCupertinoHost(context) ? const CupertinoActivityIndicator() : const CircularProgressIndicator(), ); } if (controller.error != null && controller.runs.isEmpty) { return _emptyError(context); } return Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ if (controller.usage != null) Padding( padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8), child: Text('Usage: ${controller.usage}'), ), if (controller.error != null) _inlineError(context), Padding( padding: const EdgeInsets.all(16), child: usesCupertinoHost(context) ? CupertinoSearchTextField( placeholder: 'Filter runs', onChanged: controller.setQuery, ) : TextField( decoration: const InputDecoration( labelText: 'Filter runs', prefixIcon: Icon(Icons.search), ), onChanged: controller.setQuery, ), ), _selectors(context), Expanded( child: controller.visibleRuns.isEmpty ? const Center(child: Text('No runs found.')) : ListView.builder( itemCount: controller.visibleRuns.length, itemBuilder: (context, index) => _runTile(context, controller.visibleRuns[index]), ), ), ], ); } Widget _emptyError(BuildContext context) => Center( child: Column( mainAxisSize: MainAxisSize.min, children: [ Text(controller.error!), const SizedBox(height: 8), usesCupertinoHost(context) ? CupertinoButton( onPressed: controller.load, child: const Text('Retry'), ) : FilledButton( onPressed: controller.load, child: const Text('Retry'), ), ], ), ); Widget _inlineError(BuildContext context) => Padding( padding: const EdgeInsets.symmetric(horizontal: 16), child: Row( children: [ Expanded(child: Text(controller.error!)), if (usesCupertinoHost(context)) CupertinoButton( onPressed: controller.load, child: const Text('Retry'), ) else TextButton(onPressed: controller.load, child: const Text('Retry')), ], ), ); Widget _selectors(BuildContext context) { if (usesCupertinoHost(context)) { return Wrap( spacing: 8, children: [ CupertinoButton( onPressed: () => _choose( context, 'Sort runs', _sorts, controller.sort, controller.setSort, ), child: Text('Sort: ${_sortLabel(controller.sort)}'), ), CupertinoButton( onPressed: () => _choose( context, 'Schedule', _schedules, controller.scheduled, controller.setScheduled, ), child: Text('Schedule: ${_scheduleLabel(controller.scheduled)}'), ), for (final status in _statuses) CupertinoButton( onPressed: () => controller.setStatus(status), child: Text( '${controller.statuses.contains(status) ? '✓ ' : ''}${_title(status)}', ), ), ], ); } return Wrap( spacing: 8, children: [ DropdownButton( value: controller.sort, isExpanded: true, onChanged: (v) { if (v != null) controller.setSort(v); }, items: _sorts .map((e) => DropdownMenuItem(value: e.$1, child: Text(e.$2))) .toList(), ), DropdownButton( value: controller.scheduled, isExpanded: true, onChanged: (v) { if (v != null) controller.setScheduled(v); }, items: _schedules .map((e) => DropdownMenuItem(value: e.$1, child: Text(e.$2))) .toList(), ), for (final status in _statuses) FilterChip( label: Text(_title(status)), selected: controller.statuses.contains(status), onSelected: (_) => controller.setStatus(status), ), ], ); } Future _choose( BuildContext context, String title, List<(String, String)> values, String selected, ValueChanged onSelected, ) async { final value = await showCupertinoModalPopup( context: context, builder: (context) => CupertinoActionSheet( title: Text(title), actions: [ for (final option in values) CupertinoActionSheetAction( onPressed: () => Navigator.pop(context, option.$1), child: Text(option.$2), ), ], cancelButton: CupertinoActionSheetAction( onPressed: () => Navigator.pop(context), child: const Text('Cancel'), ), ), ); if (value != null && value != selected) onSelected(value); } Widget _runTile(BuildContext context, JsonMap run) { final identity = runIdentity(run['title'], run['name']); final id = run['id']; final title = identity.label.isEmpty ? 'Run $id' : identity.label; final subtitle = '${run['name'] ?? 'Pipeline unavailable'} · ${run['created_at'] ?? 'Time unavailable'}${run['scheduled'] == true ? ' · Scheduled' : ''}'; if (usesCupertinoHost(context)) { return CupertinoListTile( title: Text(title), subtitle: Text(subtitle), additionalInfo: Text(runStatusLabel(run)), trailing: const CupertinoListTileChevron(), onTap: id is num ? () => onSelected(id) : null, ); } return ListTile( title: Text(title), subtitle: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [Text(subtitle), Text(runStatusLabel(run))], ), trailing: const Icon(Icons.chevron_right), onTap: id is num ? () => onSelected(id) : null, ); } } const _sorts = [ ('newest_first', 'Newest'), ('oldest_first', 'Oldest'), ('pipeline_name', 'Pipeline'), ('status', 'Status'), ]; const _schedules = [ ('any', 'Any schedule'), ('scheduled', 'Scheduled'), ('unscheduled', 'Unscheduled'), ]; const _statuses = ['queued', 'running', 'waiting', 'succeeded', 'failed']; String _sortLabel(String value) => _sorts.firstWhere((e) => e.$1 == value, orElse: () => _sorts.first).$2; String _scheduleLabel(String value) => _schedules .firstWhere((e) => e.$1 == value, orElse: () => _schedules.first) .$2; String _title(String value) => '${value[0].toUpperCase()}${value.substring(1)}';