From 2c2c023eba217127d70d566b8391fd0c4d48677b Mon Sep 17 00:00:00 2001 From: sherlock Date: Wed, 15 Nov 2023 15:40:49 +0700 Subject: [PATCH] TW-849: refactor message widget --- lib/pages/chat/events/message/message.dart | 220 +++++++++++---------- 1 file changed, 116 insertions(+), 104 deletions(-) diff --git a/lib/pages/chat/events/message/message.dart b/lib/pages/chat/events/message/message.dart index 7d53ad779d..68b163b960 100644 --- a/lib/pages/chat/events/message/message.dart +++ b/lib/pages/chat/events/message/message.dart @@ -212,42 +212,10 @@ class Message extends StatelessWidget { hideDisplayName(ownMessage) || hideDisplayNameInBubbleChat ? const SizedBox(height: 0) - : FutureBuilder( - future: event.fetchSenderUser(), - builder: (context, snapshot) { - final displayName = snapshot - .data - ?.calcDisplayname() ?? - event - .senderFromMemoryOrFallback - .calcDisplayname(); - return Padding( - padding: EdgeInsets.only( - left: event.messageType == - MessageTypes.Image - ? 0 - : 8.0, - bottom: 4.0, - ), - child: Text( - displayName - .shortenDisplayName( - maxCharacters: - maxCharactersDisplayNameBubble, - ), - style: Theme.of(context) - .textTheme - .labelMedium - ?.copyWith( - color: Theme.of( - context, - ).colorScheme.primary, - ), - maxLines: 2, - overflow: TextOverflow.clip, - ), - ); - }, + : _DisplayNameWidget( + event: event, + maxCharactersDisplayNameBubble: + maxCharactersDisplayNameBubble, ), IntrinsicHeight( child: Stack( @@ -282,74 +250,13 @@ class Message extends StatelessWidget { if (event .relationshipType == RelationshipTypes.reply) - FutureBuilder( - future: - event.getReplyEvent( - timeline, - ), - builder: ( - BuildContext context, - snapshot, - ) { - final replyEvent = - snapshot.hasData - ? snapshot - .data! - : Event( - eventId: event - .relationshipEventId!, - content: { - 'msgtype': - 'm.text', - 'body': - '...', - }, - senderId: - event - .senderId, - type: - 'm.room.message', - room: event - .room, - status: EventStatus - .sent, - originServerTs: - DateTime - .now(), - ); - return InkWell( - onTap: () { - if (scrollToEventId != - null) { - scrollToEventId!( - replyEvent - .eventId, - ); - } - }, - child: - AbsorbPointer( - child: Container( - margin: EdgeInsets - .symmetric( - vertical: 4.0 * - AppConfig - .bubbleSizeFactor, - ), - child: - ReplyContent( - replyEvent, - ownMessage: - ownMessage, - timeline: - timeline, - chatController: - controller, - ), - ), - ), - ); - }, + _ReplyContent( + event: event, + timeline: timeline, + scrollToEventId: + scrollToEventId, + ownMessage: ownMessage, + controller: controller, ), Stack( children: [ @@ -808,6 +715,111 @@ class Message extends StatelessWidget { } } +class _DisplayNameWidget extends StatelessWidget { + const _DisplayNameWidget({ + required this.event, + required this.maxCharactersDisplayNameBubble, + }); + + final Event event; + final int maxCharactersDisplayNameBubble; + + @override + Widget build(BuildContext context) { + return FutureBuilder( + future: event.fetchSenderUser(), + builder: (context, snapshot) { + final displayName = snapshot.data?.calcDisplayname() ?? + event.senderFromMemoryOrFallback.calcDisplayname(); + return Padding( + padding: EdgeInsets.only( + left: event.messageType == MessageTypes.Image ? 0 : 8.0, + bottom: 4.0, + ), + child: Text( + displayName.shortenDisplayName( + maxCharacters: maxCharactersDisplayNameBubble, + ), + style: Theme.of(context).textTheme.labelMedium?.copyWith( + color: Theme.of( + context, + ).colorScheme.primary, + ), + maxLines: 2, + overflow: TextOverflow.clip, + ), + ); + }, + ); + } +} + +class _ReplyContent extends StatelessWidget { + const _ReplyContent({ + required this.event, + required this.timeline, + required this.scrollToEventId, + required this.ownMessage, + required this.controller, + }); + + final Event event; + final Timeline timeline; + final void Function(String p1)? scrollToEventId; + final bool ownMessage; + final ChatController controller; + + @override + Widget build(BuildContext context) { + return FutureBuilder( + future: event.getReplyEvent( + timeline, + ), + builder: ( + BuildContext context, + snapshot, + ) { + final replyEvent = snapshot.hasData + ? snapshot.data! + : Event( + eventId: event.relationshipEventId!, + content: { + 'msgtype': 'm.text', + 'body': '...', + }, + senderId: event.senderId, + type: 'm.room.message', + room: event.room, + status: EventStatus.sent, + originServerTs: DateTime.now(), + ); + return InkWell( + onTap: () { + if (scrollToEventId != null) { + scrollToEventId!( + replyEvent.eventId, + ); + } + }, + child: AbsorbPointer( + child: Container( + margin: EdgeInsets.symmetric( + vertical: 4.0 * AppConfig.bubbleSizeFactor, + ), + child: ReplyContent( + replyEvent, + ownMessage: ownMessage, + timeline: timeline, + chatController: controller, + ), + ), + ), + ); + }, + ); + } +} + class ReplyIconWidget extends StatelessWidget { final bool isOwnMessage;