b6aa820378
Keep the Material-only chat conversation implementation while preserving public Cupertino parameters for downstream compatibility. Narrow the matching tests to the Material path.
179 lines
6.0 KiB
Dart
179 lines
6.0 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../../../core/api_gateway.dart';
|
|
import 'chat_background_tasks_controller.dart';
|
|
import 'chat_background_tasks_dialog.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;
|
|
unawaited(controller.refreshAutomations());
|
|
FocusManager.instance.primaryFocus?.unfocus();
|
|
Widget dialog(BuildContext context) => ChatBackgroundTasksDialog(
|
|
key: const Key('chat-background-tasks-modal'),
|
|
controller: controller,
|
|
onClose: () => Navigator.of(context, rootNavigator: true).pop(),
|
|
);
|
|
|
|
final route = 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) => LayoutBuilder(
|
|
builder: (context, constraints) => ListenableBuilder(
|
|
listenable: controller,
|
|
builder: (context, _) {
|
|
final count = controller.running.length;
|
|
final automationCount = controller.automationStatus.count;
|
|
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 &&
|
|
(automationCount == null || automationCount == 0) &&
|
|
!failed) {
|
|
return const SizedBox.shrink();
|
|
}
|
|
final labels = <String>[];
|
|
if (count > 0) labels.add('$count ${count == 1 ? 'task' : 'tasks'}');
|
|
if (automationCount != null && automationCount > 0) {
|
|
labels.add(
|
|
'$automationCount ${automationCount == 1 ? 'automation' : 'automations'}',
|
|
);
|
|
}
|
|
final label = failed && labels.isEmpty ? 'Tasks' : labels.join(', ');
|
|
final labelStyle = Theme.of(context).textTheme.labelSmall;
|
|
final painter = TextPainter(
|
|
text: TextSpan(text: label, style: labelStyle),
|
|
textDirection: Directionality.of(context),
|
|
textScaler: MediaQuery.textScalerOf(context),
|
|
)..layout();
|
|
final compact =
|
|
constraints.maxWidth.isFinite &&
|
|
painter.width + 40 > constraints.maxWidth;
|
|
painter.dispose();
|
|
Widget caption() => compact
|
|
? Wrap(
|
|
spacing: 4,
|
|
crossAxisAlignment: WrapCrossAlignment.center,
|
|
children: [
|
|
if (count > 0) Text('$count', style: labelStyle),
|
|
if ((automationCount ?? 0) > 0)
|
|
Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
if (count > 0) ...[
|
|
Icon(Icons.schedule, size: 16),
|
|
const SizedBox(width: 2),
|
|
],
|
|
Text('$automationCount', style: labelStyle),
|
|
],
|
|
),
|
|
if (labels.isEmpty) Text('Tasks', style: labelStyle),
|
|
],
|
|
)
|
|
: Text(
|
|
label,
|
|
maxLines: 1,
|
|
overflow: TextOverflow.ellipsis,
|
|
style: labelStyle,
|
|
);
|
|
final automationOnly = count == 0 && (automationCount ?? 0) > 0;
|
|
final theme = Theme.of(context);
|
|
final button = TextButton.icon(
|
|
key: const Key('chat-background-tasks-button'),
|
|
style: TextButton.styleFrom(
|
|
foregroundColor: theme.colorScheme.onSurfaceVariant,
|
|
padding: EdgeInsets.symmetric(horizontal: compact ? 4 : 8),
|
|
visualDensity: VisualDensity.compact,
|
|
),
|
|
onPressed: _open,
|
|
icon: Icon(
|
|
automationOnly ? Icons.schedule : Icons.layers_outlined,
|
|
size: 16,
|
|
),
|
|
label: caption(),
|
|
);
|
|
return Padding(
|
|
// Level with the field's single line, beside Send.
|
|
padding: const EdgeInsetsDirectional.only(end: 4, bottom: 4),
|
|
child: Semantics(
|
|
identifier: 'chat-background-tasks-button',
|
|
button: true,
|
|
excludeSemantics: true,
|
|
onTap: _open,
|
|
label: failed
|
|
? (labels.isEmpty
|
|
? 'Background tasks unavailable'
|
|
: 'Background work, $label; task list unavailable')
|
|
: 'Background work, $label',
|
|
child: Tooltip(
|
|
message: label,
|
|
excludeFromSemantics: true,
|
|
child: button,
|
|
),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|