Files
m8chat-app2/lib/features/chat/presentation/message_bubble.dart
help4bis b941cdfe4b refactor: /simplify — 22 fixes from 3-agent code review
Critical:
- Fix MXC URI resolution: all avatars/images now resolve mxc:// to HTTP
- Sync persistence: only write changed rooms, batch message upserts
- lastActivityAt uses room.lastEvent.originServerTs, not creation time

High:
- Shared MatrixAvatar widget replaces 6 duplicate implementations
- CallScreen decodes roomId before LiveKit JWT fetch
- Decline button actually dismisses incoming call overlay
- EventTypes constants replace raw string literals
- LiveKitService uses lazy auth reads, onDispose disconnects

Medium:
- CallController is keepAlive with timer/room cleanup
- authRepository is keepAlive (used from keepAlive notifier)
- StreamController not closed in stopListening (crash fix)
- Index on messages.roomId for query performance
- 400ms debounce on user search
- Static DateFormat in MessageBubble
- Hardcoded strings replaced with AppConfig refs
- Duplicate isDirectMessage field removed from RoomModel
- E2EE profile claim corrected to Phase 3

Shared utilities:
- lib/shared/widgets/matrix_avatar.dart
- lib/shared/utils/mxc_url.dart
- lib/shared/utils/room_preview.dart
- lib/shared/utils/matrix_id.dart

rawJson column removed (unused, caused main-thread jsonEncode)
Schema migrated to v2 with roomId index.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-02 13:19:22 +10:00

261 lines
7.3 KiB
Dart

// Version: 1.1.0 | Created: 2026-04-01 | Updated: 2026-04-02
// Message bubble widget. Handles text, images, files, redacted, replies.
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import '../../../shared/widgets/matrix_avatar.dart';
import '../domain/message_model.dart';
class MessageBubble extends StatelessWidget {
const MessageBubble({super.key, required this.message});
final MessageModel message;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final isMine = message.isMine;
return Padding(
padding: EdgeInsets.only(
top: 2,
bottom: 2,
left: isMine ? 48 : 8,
right: isMine ? 8 : 48,
),
child: Row(
crossAxisAlignment: CrossAxisAlignment.end,
mainAxisAlignment: isMine
? MainAxisAlignment.end
: MainAxisAlignment.start,
children: [
if (!isMine) ...[
MatrixAvatar(
name: message.senderDisplayName,
avatarUrl: message.senderAvatarUrl,
radius: 16,
),
const SizedBox(width: 8),
],
Flexible(
child: Column(
crossAxisAlignment: isMine
? CrossAxisAlignment.end
: CrossAxisAlignment.start,
children: [
if (!isMine)
Padding(
padding: const EdgeInsets.only(left: 4, bottom: 2),
child: Text(
message.senderDisplayName,
style: theme.textTheme.labelSmall?.copyWith(
color: theme.colorScheme.primary,
fontWeight: FontWeight.w600,
),
),
),
_BubbleContent(message: message, isMine: isMine),
if (message.reactions.isNotEmpty)
_ReactionsRow(reactions: message.reactions),
],
),
),
],
),
);
}
}
class _BubbleContent extends StatelessWidget {
const _BubbleContent({required this.message, required this.isMine});
final MessageModel message;
final bool isMine;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final bgColour = isMine
? theme.colorScheme.primary
: theme.colorScheme.surfaceContainerHighest;
final textColour = isMine ? Colors.white : theme.colorScheme.onSurface;
return Container(
constraints: const BoxConstraints(maxWidth: 320),
decoration: BoxDecoration(
color: bgColour,
borderRadius: BorderRadius.only(
topLeft: const Radius.circular(16),
topRight: const Radius.circular(16),
bottomLeft: Radius.circular(isMine ? 16 : 4),
bottomRight: Radius.circular(isMine ? 4 : 16),
),
),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
_MessageContentBody(message: message, textColour: textColour),
const SizedBox(height: 2),
_Timestamp(
timestamp: message.timestamp,
isEdited: message.isEdited,
textColour: textColour.withAlpha(153),
),
],
),
),
);
}
}
class _MessageContentBody extends StatelessWidget {
const _MessageContentBody({required this.message, required this.textColour});
final MessageModel message;
final Color textColour;
@override
Widget build(BuildContext context) {
return switch (message.type) {
MessageType.text => Text(
message.body ?? '',
style: TextStyle(color: textColour),
),
MessageType.image => _ImageContent(message: message),
MessageType.file => _FileContent(
message: message,
textColour: textColour,
),
MessageType.redacted => Text(
'This message was deleted.',
style: TextStyle(
color: textColour.withAlpha(153),
fontStyle: FontStyle.italic,
),
),
_ => Text(
message.body ?? 'Unsupported message type',
style: TextStyle(
color: textColour.withAlpha(153),
fontStyle: FontStyle.italic,
),
),
};
}
}
class _ImageContent extends StatelessWidget {
const _ImageContent({required this.message});
final MessageModel message;
@override
Widget build(BuildContext context) {
if (message.mediaUrl != null) {
return ClipRRect(
borderRadius: BorderRadius.circular(8),
child: CachedNetworkImage(
imageUrl: message.mediaUrl!,
width: 240,
fit: BoxFit.cover,
placeholder: (_, __) => const SizedBox(
height: 160,
child: Center(child: CircularProgressIndicator()),
),
errorWidget: (_, __, ___) => const SizedBox(
height: 80,
child: Center(child: Icon(Icons.broken_image)),
),
),
);
}
return const Icon(Icons.image_not_supported);
}
}
class _FileContent extends StatelessWidget {
const _FileContent({required this.message, required this.textColour});
final MessageModel message;
final Color textColour;
@override
Widget build(BuildContext context) {
return Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.attach_file, color: textColour, size: 18),
const SizedBox(width: 6),
Flexible(
child: Text(
message.body ?? 'File',
style: TextStyle(color: textColour),
overflow: TextOverflow.ellipsis,
),
),
],
);
}
}
class _Timestamp extends StatelessWidget {
const _Timestamp({
required this.timestamp,
required this.isEdited,
required this.textColour,
});
static final DateFormat _timeFormat = DateFormat('HH:mm');
final DateTime timestamp;
final bool isEdited;
final Color textColour;
@override
Widget build(BuildContext context) {
final formatted = _timeFormat.format(timestamp.toLocal());
final label = isEdited ? '$formatted (edited)' : formatted;
return Text(label, style: TextStyle(fontSize: 10, color: textColour));
}
}
class _ReactionsRow extends StatelessWidget {
const _ReactionsRow({required this.reactions});
final Map<String, List<String>> reactions;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return Padding(
padding: const EdgeInsets.only(top: 4),
child: Wrap(
spacing: 4,
children: reactions.entries.map((entry) {
return Container(
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 2),
decoration: BoxDecoration(
color: theme.colorScheme.surfaceContainerHighest,
borderRadius: BorderRadius.circular(12),
border: Border.all(
color: theme.colorScheme.outline.withAlpha(51),
),
),
child: Text(
'${entry.key} ${entry.value.length}',
style: const TextStyle(fontSize: 12),
),
);
}).toList(),
),
);
}
}