import 'package:flutter/widgets.dart'; import '../../../core/api_gateway.dart'; import '../data/runs_repository.dart'; import '../domain/run_deletion.dart'; import 'delete_runs_dialog.dart'; /// Confirms and deletes the finished runs among [runs]. /// /// Active runs are left out; nothing is sent unless the operator confirms. A /// transport or server error stays in the open prompt. The server's reply, /// including any refusals, goes to [onDeleted] exactly as received. Future confirmDeleteRuns( BuildContext context, { required Iterable runs, required ApiGateway gateway, required ValueChanged onDeleted, }) { final deletable = runs.where(runIsDeletable).toList(); final ids = deletableRunIds(deletable); if (ids.isEmpty) return Future.value(false); return showDeleteRunsPrompt( context: context, count: ids.length, size: runsSizeLabel(deletable), onSubmit: () async { try { onDeleted(await RunsRepository(gateway).deleteRuns(ids)); return null; } catch (error) { final message = '$error'.trim(); return message.isEmpty ? 'The server did not delete the runs.' : message; } }, ); }