166 lines
4.6 KiB
Dart
166 lines
4.6 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import '../../../../core/api_gateway.dart';
|
|
import '../../../settings/settings_store.dart';
|
|
import '../../data/schedules_repository.dart';
|
|
import '../../domain/editor/schedule_draft.dart';
|
|
import '../../domain/saved_schedule.dart';
|
|
import '../list/schedule_list_format.dart';
|
|
import 'schedule_delete_dialog.dart';
|
|
import 'schedule_pipeline_controller.dart';
|
|
import 'schedule_pipeline_editor.dart';
|
|
import 'schedule_pipeline_list_body.dart';
|
|
|
|
/// The pipeline Summary's Schedules section.
|
|
///
|
|
/// Lists exactly this pipeline's saved run configurations and offers Add,
|
|
/// Edit, Pause/Enable and Delete. Every accepted mutation re-reads
|
|
/// `list_schedules`; nothing is updated optimistically. This section never
|
|
/// publishes, validates or runs a workflow and never starts a run.
|
|
class SchedulePipelineSection extends StatefulWidget {
|
|
const SchedulePipelineSection({
|
|
super.key,
|
|
required this.gateway,
|
|
required this.pipelineId,
|
|
required this.version,
|
|
this.settingsStore = const SharedPreferencesSettingsStore(),
|
|
});
|
|
|
|
final ApiGateway gateway;
|
|
final int pipelineId;
|
|
|
|
/// The resolved version currently shown, used as a new schedule's pin.
|
|
final int version;
|
|
|
|
/// The operator's persisted server workspace seeds a new schedule's absolute
|
|
/// workspace, matching the web client's configured workspace.
|
|
final SettingsStore settingsStore;
|
|
|
|
@override
|
|
State<SchedulePipelineSection> createState() =>
|
|
_SchedulePipelineSectionState();
|
|
}
|
|
|
|
class _SchedulePipelineSectionState extends State<SchedulePipelineSection> {
|
|
late SchedulePipelineController _controller;
|
|
final Map<int, String> _rowErrors = {};
|
|
final Set<int> _busyRows = {};
|
|
Future<SettingsValues>? _settings;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_controller = _createController()..refresh();
|
|
}
|
|
|
|
SchedulePipelineController _createController() => SchedulePipelineController(
|
|
repository: SchedulesRepository(widget.gateway),
|
|
pipelineId: widget.pipelineId,
|
|
);
|
|
|
|
@override
|
|
void didUpdateWidget(SchedulePipelineSection oldWidget) {
|
|
super.didUpdateWidget(oldWidget);
|
|
if (oldWidget.gateway != widget.gateway ||
|
|
oldWidget.pipelineId != widget.pipelineId) {
|
|
_controller.dispose();
|
|
_controller = _createController()..refresh();
|
|
_rowErrors.clear();
|
|
_busyRows.clear();
|
|
}
|
|
if (oldWidget.settingsStore != widget.settingsStore) _settings = null;
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_controller.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<String> _workspace() async {
|
|
final settings = _settings ??= widget.settingsStore.load();
|
|
try {
|
|
return (await settings).workspace.trim();
|
|
} catch (_) {
|
|
return '';
|
|
}
|
|
}
|
|
|
|
Future<void> _add() async {
|
|
final workspace = await _workspace();
|
|
if (!mounted) return;
|
|
final requestKey = newScheduleCreateRequestKey();
|
|
final draft = ScheduleDraft.create(
|
|
pipelineId: widget.pipelineId,
|
|
version: '${widget.version}',
|
|
workspace: workspace,
|
|
);
|
|
await openSchedulePipelineEditor(
|
|
context,
|
|
draft: draft,
|
|
onSubmit: (record) async {
|
|
await _controller.saveRecord(record, requestKey: requestKey);
|
|
},
|
|
);
|
|
}
|
|
|
|
Future<void> _edit(SavedSchedule schedule) {
|
|
return openSchedulePipelineEditor(
|
|
context,
|
|
draft: ScheduleDraft.fromSchedule(schedule),
|
|
onSubmit: (record) async {
|
|
await _controller.saveRecord(record);
|
|
},
|
|
);
|
|
}
|
|
|
|
Future<void> _toggle(SavedSchedule schedule) async {
|
|
final id = schedule.id;
|
|
if (id == null) return;
|
|
setState(() {
|
|
_busyRows.add(id);
|
|
_rowErrors.remove(id);
|
|
});
|
|
try {
|
|
await _controller.setEnabled(schedule);
|
|
} catch (error) {
|
|
if (mounted) {
|
|
setState(() => _rowErrors[id] = scheduleActionError(error));
|
|
}
|
|
} finally {
|
|
if (mounted) setState(() => _busyRows.remove(id));
|
|
}
|
|
}
|
|
|
|
Future<void> _delete(SavedSchedule schedule) {
|
|
return showScheduleDeleteDialog(
|
|
context: context,
|
|
name: scheduleDisplayName(schedule),
|
|
onSubmit: () async {
|
|
try {
|
|
await _controller.delete(schedule);
|
|
return null;
|
|
} catch (error) {
|
|
return scheduleActionError(error);
|
|
}
|
|
},
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return AnimatedBuilder(
|
|
animation: _controller,
|
|
builder: (context, _) => SchedulePipelineListBody(
|
|
controller: _controller,
|
|
busyRows: _busyRows,
|
|
rowErrors: _rowErrors,
|
|
onAdd: _add,
|
|
onEdit: _edit,
|
|
onToggle: _toggle,
|
|
onDelete: _delete,
|
|
),
|
|
);
|
|
}
|
|
}
|