import 'package:flutter/foundation.dart'; import '../../../../core/api_gateway.dart'; import '../../data/schedules_repository.dart'; import '../../domain/saved_schedule.dart'; import '../list/schedule_list_controller.dart'; /// The message to show for a failed save/delete/pause, using the repository's /// older-server guidance when the reply itself is unsupported. String scheduleActionError(Object error) => ScheduleListFailure.from(error).message; /// Saved schedules for exactly one pipeline, with the two mutations the /// pipeline Summary offers. /// /// Every accepted save/delete is followed by `list_schedules`; the section never /// invents, edits or reorders a row locally. Saving does not start a run and /// nothing here publishes, validates or runs a workflow. class SchedulePipelineController extends ChangeNotifier { SchedulePipelineController({ required this.repository, required this.pipelineId, }); final SchedulesRepository repository; final int pipelineId; List _schedules = const []; bool _loading = false; bool _loaded = false; ScheduleListFailure? _failure; int _generation = 0; bool _disposed = false; List get schedules => _schedules; bool get isLoading => _loading; bool get hasLoaded => _loaded; bool get isInitialLoading => _loading && !_loaded; ScheduleListFailure? get failure => _failure; /// Re-reads server truth. A failed refresh keeps the rows already loaded so a /// refused mutation never silently empties the section. Future refresh() async { final generation = ++_generation; _loading = true; _failure = null; _notify(); try { final rows = await repository.listSchedules(pipelineId: pipelineId); if (!_owns(generation)) return; _schedules = List.unmodifiable(rows); _loaded = true; } catch (error) { if (!_owns(generation)) return; _failure = ScheduleListFailure.from(error); } finally { if (_owns(generation)) { _loading = false; _notify(); } } } /// Creates (with a stable create key) or updates from the editor's exact /// record. A server refusal is propagated unchanged; a later refresh failure /// is reported separately and does not pretend the accepted save failed. Future saveRecord(JsonMap record, {String? requestKey}) async { await repository.saveSchedule(record, requestKey: requestKey); await refresh(); } /// Reuses the exact loaded record, changing only `enabled`. Identity, /// revision and unknown fields stay in the payload. Future setEnabled(SavedSchedule schedule) => saveRecord(schedule.withEnabled(!schedule.enabled).toJson()); /// Sends the exact identity and compare-and-swap revision of the loaded row. Future delete(SavedSchedule schedule) async { final id = schedule.id; final revision = schedule.revision; if (id == null || revision == null) { throw const SchedulesRepositoryException( 'An exact schedule ID and revision are required.', ); } await repository.deleteSchedule(id, revision); await refresh(); } bool _owns(int generation) => !_disposed && generation == _generation; void _notify() { if (!_disposed) notifyListeners(); } @override void dispose() { _disposed = true; super.dispose(); } }