124 lines
4.2 KiB
Dart
124 lines
4.2 KiB
Dart
import 'dart:async';
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../domain/chat_shells.dart';
|
|
import '../ui/sessions/chat_adaptive.dart';
|
|
import 'chat_background_task_card.dart';
|
|
import 'chat_background_task_detail.dart';
|
|
import 'chat_background_tasks_controller.dart';
|
|
import 'chat_task_presentation.dart';
|
|
|
|
/// Scrollable modal content, backed by the same live controller as its badge.
|
|
class ChatBackgroundTasksPanel extends StatefulWidget {
|
|
const ChatBackgroundTasksPanel({
|
|
super.key,
|
|
required this.controller,
|
|
this.clock,
|
|
});
|
|
|
|
final ChatBackgroundTasksController controller;
|
|
final DateTime Function()? clock;
|
|
|
|
@override
|
|
State<ChatBackgroundTasksPanel> createState() =>
|
|
_ChatBackgroundTasksPanelState();
|
|
}
|
|
|
|
class _ChatBackgroundTasksPanelState extends State<ChatBackgroundTasksPanel> {
|
|
late final Timer _ticker;
|
|
ChatBackgroundTasksController get controller => widget.controller;
|
|
DateTime get now => (widget.clock ?? DateTime.now)();
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_ticker = Timer.periodic(const Duration(seconds: 1), (_) {
|
|
if (mounted && widget.controller.running.isNotEmpty) setState(() {});
|
|
});
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_ticker.cancel();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) => ListenableBuilder(
|
|
listenable: controller,
|
|
builder: (context, _) {
|
|
final at = now;
|
|
final cupertino =
|
|
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
|
|
final running = controller.running;
|
|
final shell = controller.selectedShell;
|
|
final error = controller.error;
|
|
final automation = controller.automationStatus;
|
|
return Column(
|
|
key: const Key('chat-background-tasks'),
|
|
mainAxisSize: MainAxisSize.min,
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
if (controller.loading)
|
|
cupertino
|
|
? const CupertinoActivityIndicator()
|
|
: const LinearProgressIndicator(),
|
|
if (automation.unavailable)
|
|
Text(
|
|
'Automation status is not available for this chat connection.',
|
|
key: const Key('chat-automations-unavailable'),
|
|
)
|
|
else ...[
|
|
Text(
|
|
automation.count == null
|
|
? 'Automation count is not available yet.'
|
|
: '${automation.count} ${automation.count == 1 ? 'automation' : 'automations'}',
|
|
key: const Key('chat-automations-count'),
|
|
),
|
|
if (automation.observedAt != null)
|
|
Text(
|
|
'Last reported: ${formatTaskDate(context, automation.observedAt!.toLocal())}',
|
|
key: const Key('chat-automations-observed-at'),
|
|
),
|
|
],
|
|
if (error != null) ...[
|
|
Text(
|
|
'Background tasks unavailable. $error',
|
|
key: const Key('chat-background-tasks-error'),
|
|
),
|
|
ChatBackgroundTaskButton(
|
|
label: 'Retry',
|
|
onPressed: controller.refresh,
|
|
),
|
|
] else if (!controller.loading && running.isEmpty)
|
|
const Text('No background tasks running.'),
|
|
if (running.isNotEmpty)
|
|
Text(backgroundTasksRunningLabel(running.length)),
|
|
for (final task in running)
|
|
Padding(
|
|
padding: const EdgeInsets.only(top: 12),
|
|
child: ChatBackgroundTaskCard(
|
|
shell: task,
|
|
now: at,
|
|
selected: shellIdOf(task) == controller.selectedId,
|
|
onDetails: () => controller.select(shellIdOf(task)),
|
|
detail:
|
|
shell != null && shellIdOf(task) == controller.selectedId
|
|
? ChatBackgroundTaskDetail(
|
|
key: const Key('chat-bg-task-detail'),
|
|
shell: shell,
|
|
now: at,
|
|
output: controller.output,
|
|
heightBudget: MediaQuery.sizeOf(context).height,
|
|
onRefresh: controller.readOutput,
|
|
)
|
|
: null,
|
|
),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
}
|