174 lines
5.6 KiB
Dart
174 lines
5.6 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: a quiet control inside the composer beside Send,
|
|
/// shown only while tasks run. 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 failed = controller.error != null;
|
|
// Nothing to show without running tasks: never a "0 tasks" caption. A
|
|
// list that cannot be read stays reachable so its refusal is reported.
|
|
if (count == 0 && !failed) return const SizedBox.shrink();
|
|
final label = failed
|
|
? 'Tasks'
|
|
: '$count ${count == 1 ? 'task' : 'tasks'}';
|
|
final cupertino =
|
|
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
|
|
final Widget button;
|
|
if (cupertino) {
|
|
final color = CupertinoColors.secondaryLabel.resolveFrom(context);
|
|
button = CupertinoButton(
|
|
key: const Key('chat-background-tasks-button'),
|
|
sizeStyle: CupertinoButtonSize.small,
|
|
padding: const EdgeInsets.symmetric(horizontal: 6),
|
|
onPressed: _open,
|
|
child: Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Icon(CupertinoIcons.square_stack_3d_up, size: 16, color: color),
|
|
const SizedBox(width: 4),
|
|
Text(
|
|
label,
|
|
style: CupertinoTheme.of(
|
|
context,
|
|
).textTheme.tabLabelTextStyle.copyWith(color: color),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
} else {
|
|
final theme = Theme.of(context);
|
|
button = TextButton.icon(
|
|
key: const Key('chat-background-tasks-button'),
|
|
style: TextButton.styleFrom(
|
|
foregroundColor: theme.colorScheme.onSurfaceVariant,
|
|
padding: const EdgeInsets.symmetric(horizontal: 8),
|
|
visualDensity: VisualDensity.compact,
|
|
),
|
|
onPressed: _open,
|
|
icon: const Icon(Icons.layers_outlined, size: 16),
|
|
label: Text(label, style: theme.textTheme.labelSmall),
|
|
);
|
|
}
|
|
return Padding(
|
|
// Level with the field's single line, beside Send.
|
|
padding: const EdgeInsetsDirectional.only(end: 4, bottom: 4),
|
|
child: Semantics(
|
|
button: true,
|
|
excludeSemantics: true,
|
|
onTap: _open,
|
|
label: failed
|
|
? 'Background tasks unavailable'
|
|
: 'Background tasks, $label',
|
|
child: button,
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|