Files
pipelines/app/lib/features/chat/conversation/chat_background_tasks.dart
T

153 lines
4.7 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../core/api_gateway.dart';
import '../domain/chat_shells.dart';
import '../ui/sessions/chat_adaptive.dart';
import 'chat_background_tasks_controller.dart';
import 'chat_background_tasks_panel.dart';
/// Compact live task count, mounted above Send. Details live only in its modal.
class ChatBackgroundTasks extends StatefulWidget {
const ChatBackgroundTasks({
super.key,
required this.gateway,
required this.sessionId,
this.syncToken = 0,
});
final ApiGateway gateway;
final String sessionId;
final int syncToken;
@override
State<ChatBackgroundTasks> createState() => _ChatBackgroundTasksState();
}
class _ChatBackgroundTasksState extends State<ChatBackgroundTasks> {
late final controller = ChatBackgroundTasksController(
widget.gateway,
widget.sessionId,
);
TransitionRoute<void>? _modal;
@override
void didUpdateWidget(covariant ChatBackgroundTasks oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.gateway != widget.gateway ||
oldWidget.sessionId != widget.sessionId) {
_closeModal();
controller.replace(widget.gateway, widget.sessionId, notify: false);
} else if (oldWidget.syncToken != widget.syncToken) {
controller.reconnect(notify: false);
}
}
void _closeModal() {
final route = _modal;
_modal = null;
if (route == null || !route.isActive) return;
WidgetsBinding.instance.addPostFrameCallback((_) {
if (route.isActive) route.navigator?.removeRoute(route);
});
}
void _open() {
if (_modal != null) return;
FocusManager.instance.primaryFocus?.unfocus();
final cupertino =
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
Widget dialog(BuildContext context) {
final content = ChatBackgroundTasksPanel(controller: controller);
void close() => Navigator.of(context).pop();
return cupertino
? CupertinoAlertDialog(
key: const Key('chat-background-tasks-modal'),
title: const Text(backgroundTasksTitle),
content: content,
actions: [
CupertinoDialogAction(
onPressed: close,
child: const Text('Done'),
),
],
)
: AlertDialog(
key: const Key('chat-background-tasks-modal'),
title: const Text(backgroundTasksTitle),
scrollable: true,
content: content,
actions: [
TextButton(onPressed: close, child: const Text('Done')),
],
);
}
final TransitionRoute<void> route = cupertino
? CupertinoDialogRoute<void>(
context: context,
barrierDismissible: true,
builder: dialog,
)
: DialogRoute<void>(context: context, builder: dialog);
_modal = route;
Navigator.of(context, rootNavigator: true).push(route);
route.completed.then((_) {
if (_modal == route) _modal = null;
});
}
@override
void dispose() {
_closeModal();
controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) => ListenableBuilder(
listenable: controller,
builder: (context, _) {
final count = controller.running.length;
final label = controller.error != null
? 'Tasks'
: controller.loading && count == 0
? 'Tasks…'
: '$count ${count == 1 ? 'task' : 'tasks'}';
final cupertino =
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
final button = cupertino
? CupertinoButton(
key: const Key('chat-background-tasks-button'),
sizeStyle: CupertinoButtonSize.small,
padding: EdgeInsets.zero,
onPressed: _open,
child: Text(
label,
style: CupertinoTheme.of(context).textTheme.tabLabelTextStyle
.copyWith(color: CupertinoTheme.of(context).primaryColor),
),
)
: TextButton(
key: const Key('chat-background-tasks-button'),
onPressed: _open,
child: Text(
label,
style: Theme.of(context).textTheme.labelSmall?.copyWith(
color: Theme.of(context).colorScheme.primary,
),
),
);
return Semantics(
button: true,
excludeSemantics: true,
onTap: _open,
label: controller.error != null
? 'Background tasks unavailable'
: 'Background tasks, $label',
child: button,
);
},
);
}