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

114 lines
3.8 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'chat_deliveries.dart';
/// The words each delivery state reads as, beside its icon.
String chatDeliveryLabel(ChatDelivery state) => switch (state) {
ChatDelivery.sending => 'Sending…',
ChatDelivery.sent => 'Sent',
ChatDelivery.queued => 'Queued',
ChatDelivery.steered => 'Delivered to the running reply',
ChatDelivery.failed => 'Not sent',
};
/// A small muted line under the operator's own bubble saying whether the
/// server has the message. "Not sent" takes the error colour and offers the
/// native Retry (same operation id) and Edit (back into the composer) — or
/// Discard when the stored intent can no longer be sent as it was. "Queued"
/// offers Remove, which takes the message off the server's queue.
class ChatDeliveryIndicator extends StatelessWidget {
const ChatDeliveryIndicator({
super.key,
required this.state,
required this.cupertino,
this.onRetry,
this.onEdit,
this.editLabel = 'Edit',
this.onRemove,
});
final ChatDelivery state;
final bool cupertino;
final VoidCallback? onRetry;
final VoidCallback? onEdit;
final String editLabel;
/// Takes a "Queued" message back off the server's queue before it is sent.
final VoidCallback? onRemove;
@override
Widget build(BuildContext context) {
final failed = state == ChatDelivery.failed;
final color = failed
? (cupertino
? CupertinoColors.systemRed.resolveFrom(context)
: Theme.of(context).colorScheme.error)
: (cupertino
? CupertinoColors.secondaryLabel.resolveFrom(context)
: Theme.of(context).colorScheme.onSurfaceVariant);
final style = DefaultTextStyle.of(
context,
).style.merge(TextStyle(fontSize: 12, color: color));
final label = chatDeliveryLabel(state);
return Semantics(
container: true,
label: 'Delivery: $label',
child: Wrap(
key: Key('chat-delivery-${state.name}'),
spacing: 4,
crossAxisAlignment: WrapCrossAlignment.center,
children: [
ExcludeSemantics(
child: Icon(_icon, size: style.fontSize! + 2, color: color),
),
ExcludeSemantics(child: Text(label, style: style)),
if (failed && onRetry != null)
_action('chat-delivery-retry', 'Retry', onRetry!, style),
if (failed && onEdit != null)
_action('chat-delivery-edit', editLabel, onEdit!, style),
if (state == ChatDelivery.queued && onRemove != null)
_action('chat-delivery-remove', 'Remove', onRemove!, style),
],
),
);
}
IconData get _icon => switch (state) {
ChatDelivery.sending => cupertino ? CupertinoIcons.clock : Icons.schedule,
ChatDelivery.sent => cupertino ? CupertinoIcons.checkmark : Icons.done,
ChatDelivery.queued =>
cupertino ? CupertinoIcons.hourglass : Icons.hourglass_empty,
ChatDelivery.steered => cupertino ? CupertinoIcons.checkmark : Icons.done,
ChatDelivery.failed =>
cupertino ? CupertinoIcons.exclamationmark_circle : Icons.error_outline,
};
Widget _action(
String key,
String text,
VoidCallback onPressed,
TextStyle style,
) {
if (cupertino) {
return CupertinoButton(
key: Key(key),
padding: const EdgeInsets.symmetric(horizontal: 6),
minimumSize: Size.zero,
onPressed: onPressed,
child: Text(text, style: style.copyWith(fontWeight: FontWeight.w600)),
);
}
return TextButton(
key: Key(key),
style: TextButton.styleFrom(
foregroundColor: style.color,
visualDensity: VisualDensity.compact,
textStyle: style.copyWith(fontWeight: FontWeight.w600),
),
onPressed: onPressed,
child: Text(text),
);
}
}