feat(app): add native runs list interactions
This commit is contained in:
@@ -1,84 +1,247 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'runs_controller.dart';
|
||||
|
||||
import '../../../core/api_gateway.dart';
|
||||
import '../domain/run_models.dart';
|
||||
import 'runs_controller.dart';
|
||||
|
||||
class RunsList extends StatelessWidget {
|
||||
const RunsList({super.key, required this.controller, required this.onSelected});
|
||||
const RunsList({
|
||||
super.key,
|
||||
required this.controller,
|
||||
required this.onSelected,
|
||||
});
|
||||
|
||||
final RunsController controller;
|
||||
final ValueChanged<num> onSelected;
|
||||
|
||||
bool get _isCupertino =>
|
||||
defaultTargetPlatform == TargetPlatform.iOS ||
|
||||
defaultTargetPlatform == TargetPlatform.macOS;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (controller.loading && controller.runs.isEmpty) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
if (controller.error != null && controller.runs.isEmpty) {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Text(controller.error!),
|
||||
const SizedBox(height: 8),
|
||||
FilledButton(
|
||||
onPressed: controller.load,
|
||||
child: const Text('Retry'),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: _isCupertino
|
||||
? 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), child: Text('Usage: ${controller.usage}')),
|
||||
if (controller.error != null)
|
||||
MaterialBanner(
|
||||
content: Text(controller.error!),
|
||||
actions: [
|
||||
TextButton(onPressed: controller.load, child: const Text('Retry')),
|
||||
],
|
||||
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: TextField(
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Filter runs',
|
||||
prefixIcon: Icon(Icons.search),
|
||||
),
|
||||
onChanged: controller.setQuery,
|
||||
),
|
||||
child: _isCupertino
|
||||
? CupertinoSearchTextField(
|
||||
placeholder: 'Filter runs',
|
||||
onChanged: controller.setQuery,
|
||||
)
|
||||
: TextField(
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Filter runs',
|
||||
prefixIcon: Icon(Icons.search),
|
||||
),
|
||||
onChanged: controller.setQuery,
|
||||
),
|
||||
),
|
||||
Wrap(spacing: 8, children: [
|
||||
DropdownButton<String>(value: controller.sort, onChanged: (v) { if (v != null) controller.setSort(v); }, items: const [DropdownMenuItem(value: 'newest_first', child: Text('Newest')), DropdownMenuItem(value: 'oldest_first', child: Text('Oldest')), DropdownMenuItem(value: 'pipeline_name', child: Text('Pipeline')), DropdownMenuItem(value: 'status', child: Text('Status'))]),
|
||||
DropdownButton<String>(value: controller.scheduled, onChanged: (v) { if (v != null) controller.setScheduled(v); }, items: const [DropdownMenuItem(value: 'any', child: Text('Any schedule')), DropdownMenuItem(value: 'scheduled', child: Text('Scheduled')), DropdownMenuItem(value: 'unscheduled', child: Text('Unscheduled'))]),
|
||||
for (final status in ['queued', 'running', 'waiting', 'succeeded', 'failed'])
|
||||
FilterChip(label: Text(status), selected: controller.statuses.contains(status), onSelected: (_) => controller.setStatus(status)),
|
||||
]),
|
||||
_selectors(context),
|
||||
Expanded(
|
||||
child: controller.visibleRuns.isEmpty
|
||||
? const Center(child: Text('No runs found.'))
|
||||
: ListView.builder(
|
||||
itemCount: controller.visibleRuns.length,
|
||||
itemBuilder: (context, index) {
|
||||
final run = controller.visibleRuns[index];
|
||||
final identity = runIdentity(run['title'], run['name']);
|
||||
final created =
|
||||
run['created_at']?.toString() ?? 'Time unavailable';
|
||||
final id = run['id'];
|
||||
return ListTile(
|
||||
title: Text(
|
||||
identity.label.isEmpty ? 'Run $id' : identity.label,
|
||||
),
|
||||
subtitle: Text(
|
||||
'${run['name'] ?? 'Pipeline unavailable'} · $created${run['scheduled'] == true ? ' · Scheduled' : ''}',
|
||||
),
|
||||
leading: Chip(label: Text(runStatusLabel(run))),
|
||||
trailing: const Icon(Icons.chevron_right),
|
||||
onTap: id is num ? () => onSelected(id) : null,
|
||||
);
|
||||
},
|
||||
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),
|
||||
_isCupertino
|
||||
? 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 (_isCupertino)
|
||||
CupertinoButton(
|
||||
onPressed: controller.load,
|
||||
child: const Text('Retry'),
|
||||
)
|
||||
else
|
||||
TextButton(onPressed: controller.load, child: const Text('Retry')),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
Widget _selectors(BuildContext context) {
|
||||
if (_isCupertino) {
|
||||
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<String>(
|
||||
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<String>(
|
||||
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<void> _choose(
|
||||
BuildContext context,
|
||||
String title,
|
||||
List<(String, String)> values,
|
||||
String selected,
|
||||
ValueChanged<String> onSelected,
|
||||
) async {
|
||||
final value = await showCupertinoModalPopup<String>(
|
||||
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 (_isCupertino) {
|
||||
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)}';
|
||||
|
||||
Reference in New Issue
Block a user