import 'dart:async'; import 'package:flutter/foundation.dart'; import '../../../core/api_gateway.dart'; import '../data/runs_repository.dart'; import '../domain/run_events.dart'; import '../domain/run_models.dart'; class RunsController extends ChangeNotifier { RunsController(this.repository); final RunsRepository repository; StreamSubscription? _subscription; List runs = []; JsonMap? detail, usage; String? error; bool loading = false, detailLoading = false; String query = '', sort = 'newest_first', scheduled = 'any', timeRange = 'any_time'; final Set statuses = {}; int? pipelineId; int _selection = 0; num? _selectedId; Timer? _refresh; bool _refreshInFlight = false, _refreshAgain = false, _disposed = false; void listen(Stream frames) { _subscription?.cancel(); _subscription = frames .where((f) => f['type'] == 'run_event') .listen(_event); } Future load([int? initialId]) async { final token = ++_selection; loading = true; error = null; notifyListeners(); try { final result = await repository.listRuns(); if (_disposed || token != _selection) return; runs = result.runs; usage = result.usage; if (initialId != null) { // Keep list and detail request generations independent: selecting the // initial row must not make this list request's completion stale. await select(initialId); } } catch (e) { if (!_disposed && token == _selection) error = '$e'; } if (!_disposed && token == _selection) { loading = false; notifyListeners(); } } Future select(num id) async { final token = ++_selection; _refresh?.cancel(); _refresh = null; _refreshAgain = false; _selectedId = id; detailLoading = true; error = null; notifyListeners(); try { final value = await repository.getRun(id); if (!_disposed && token == _selection) detail = value; } catch (e) { if (!_disposed && token == _selection) error = '$e'; } if (!_disposed && token == _selection) { detailLoading = false; notifyListeners(); } } void back() { ++_selection; _selectedId = null; _refresh?.cancel(); _refresh = null; _refreshAgain = false; detail = null; detailLoading = false; error = null; notifyListeners(); } void setQuery(String value) { query = value; notifyListeners(); } void setStatus(String? value) { if (value == null) { statuses.clear(); } else if (!statuses.add(value)) { statuses.remove(value); } notifyListeners(); } void setPipeline(int? value) { pipelineId = value; notifyListeners(); } void setScheduled(String value) { scheduled = value; notifyListeners(); } void setSort(String value) { sort = value; notifyListeners(); } void _event(JsonMap frame) { final folded = foldRunEvent(runs, frame); runs = folded.runs; if (detail?['id'] == frame['run_id']) { detail = foldRunEventDetail(detail, frame); } if (frame['run_id'] == null || folded.needsResync) { _scheduleListRefresh(); } if (_selectedId != null && frame['run_id'] == _selectedId && (folded.needsResync || runDetailNeedsFullSync(frame))) { if (_refreshInFlight) { _refreshAgain = true; } else { _refresh?.cancel(); final id = _selectedId!; final generation = _selection; _refresh = Timer( const Duration(milliseconds: 150), () => _refreshDetail(id, generation), ); } } notifyListeners(); } void _scheduleListRefresh() { if (_disposed || loading) return; loading = true; notifyListeners(); unawaited(_reloadList()); } Future _reloadList() async { try { final result = await repository.listRuns(); if (_disposed) return; runs = result.runs; usage = result.usage; error = null; } catch (e) { if (!_disposed) error = '$e'; } finally { if (!_disposed) { loading = false; notifyListeners(); } } } Future _refreshDetail(num id, int generation) async { if (_disposed || generation != _selection || _selectedId != id) return; _refreshInFlight = true; try { final value = await repository.getRun(id); if (!_disposed && generation == _selection && _selectedId == id) { detail = value; notifyListeners(); } } catch (e) { if (!_disposed && generation == _selection && _selectedId == id) { error = '$e'; notifyListeners(); } } finally { _refreshInFlight = false; if (!_disposed && _refreshAgain && generation == _selection && _selectedId == id) { _refreshAgain = false; _refresh = Timer( const Duration(milliseconds: 150), () => _refreshDetail(id, generation), ); } } } List get visibleRuns => sortRuns( filterRuns( runs, statuses: statuses, pipelineId: pipelineId, scheduled: scheduled, timeRange: timeRange, ) .where( (r) => '${r['title'] ?? r['name'] ?? ''}'.toLowerCase().contains( query.toLowerCase(), ), ) .toList(), sort: sort, ); @override void dispose() { _disposed = true; _subscription?.cancel(); _refresh?.cancel(); super.dispose(); } }