From efe6c7c0d30906f9177b204eb3a38afcf92b64e5 Mon Sep 17 00:00:00 2001 From: Sorunome Date: Mon, 16 Aug 2021 18:13:31 +0200 Subject: [PATCH] fix: Cause a re-render of the message widget once the member state is fully loaded --- lib/widgets/event_content/message.dart | 91 ++++++++++++++++---------- 1 file changed, 57 insertions(+), 34 deletions(-) diff --git a/lib/widgets/event_content/message.dart b/lib/widgets/event_content/message.dart index e5d04311..1705e296 100644 --- a/lib/widgets/event_content/message.dart +++ b/lib/widgets/event_content/message.dart @@ -14,7 +14,7 @@ import 'message_reactions.dart'; import 'state_message.dart'; import 'verification_request_content.dart'; -class Message extends StatelessWidget { +class Message extends StatefulWidget { final Event event; final Event nextEvent; final void Function(Event) onSelect; @@ -35,29 +35,47 @@ class Message extends StatelessWidget { this.selected, this.timeline}); + @override + _MessageState createState() => _MessageState(); +} + +class _MessageState extends State { /// Indicates wheither the user may use a mouse instead /// of touchscreen. static bool useMouse = false; @override Widget build(BuildContext context) { + // if the sender isn't in-memory yet we want to queue a re-rendering of the widget for + // when it is in-memory, for nicer displaying + if (widget.event.room + .getState(EventTypes.RoomMember, widget.event.senderId) == + null) { + widget.event.room.requestUser(widget.event.senderId).then((_) { + WidgetsBinding.instance.addPostFrameCallback((_) { + setState(() => null); + }); + }); + } if (![EventTypes.Message, EventTypes.Sticker, EventTypes.Encrypted] - .contains(event.type)) { - return StateMessage(event, unfold: unfold); + .contains(widget.event.type)) { + return StateMessage(widget.event, unfold: widget.unfold); } - if (event.type == EventTypes.Message && - event.messageType == EventTypes.KeyVerificationRequest) { - return VerificationRequestContent(event: event, timeline: timeline); + if (widget.event.type == EventTypes.Message && + widget.event.messageType == EventTypes.KeyVerificationRequest) { + return VerificationRequestContent( + event: widget.event, timeline: widget.timeline); } final client = Matrix.of(context).client; - final ownMessage = event.senderId == client.userID; + final ownMessage = widget.event.senderId == client.userID; final alignment = ownMessage ? Alignment.topRight : Alignment.topLeft; var color = Theme.of(context).secondaryHeaderColor; - final sameSender = nextEvent != null && - [EventTypes.Message, EventTypes.Sticker].contains(nextEvent.type) - ? nextEvent.sender.id == event.sender.id + final sameSender = widget.nextEvent != null && + [EventTypes.Message, EventTypes.Sticker] + .contains(widget.nextEvent.type) + ? widget.nextEvent.sender.id == widget.event.sender.id : false; var textColor = ownMessage ? Colors.white @@ -67,9 +85,9 @@ class Message extends StatelessWidget { final rowMainAxisAlignment = ownMessage ? MainAxisAlignment.end : MainAxisAlignment.start; - final displayEvent = event.getDisplayEvent(timeline); + final displayEvent = widget.event.getDisplayEvent(widget.timeline); - if (event.showThumbnail) { + if (widget.event.showThumbnail) { color = Theme.of(context).scaffoldBackgroundColor; textColor = Theme.of(context).textTheme.bodyText2.color; } else if (ownMessage) { @@ -88,10 +106,12 @@ class Message extends StatelessWidget { borderRadius: BorderRadius.circular(AppConfig.borderRadius), child: InkWell( onHover: (b) => useMouse = true, - onTap: !useMouse && longPressSelect + onTap: !useMouse && widget.longPressSelect ? () => null - : () => onSelect(event), - onLongPress: !longPressSelect ? null : () => onSelect(event), + : () => widget.onSelect(widget.event), + onLongPress: !widget.longPressSelect + ? null + : () => widget.onSelect(widget.event), borderRadius: BorderRadius.circular(AppConfig.borderRadius), child: Container( padding: @@ -104,29 +124,30 @@ class Message extends StatelessWidget { mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, children: [ - if (event.relationshipType == RelationshipTypes.reply) + if (widget.event.relationshipType == + RelationshipTypes.reply) FutureBuilder( - future: event.getReplyEvent(timeline), + future: widget.event.getReplyEvent(widget.timeline), builder: (BuildContext context, snapshot) { final replyEvent = snapshot.hasData ? snapshot.data : Event( - eventId: event.relationshipEventId, + eventId: widget.event.relationshipEventId, content: { 'msgtype': 'm.text', 'body': '...' }, - senderId: event.senderId, + senderId: widget.event.senderId, type: 'm.room.message', - room: event.room, - roomId: event.roomId, + room: widget.event.room, + roomId: widget.event.roomId, status: 1, originServerTs: DateTime.now(), ); return InkWell( onTap: () { - if (scrollToEventId != null) { - scrollToEventId(replyEvent.eventId); + if (widget.scrollToEventId != null) { + widget.scrollToEventId(replyEvent.eventId); } }, child: AbsorbPointer( @@ -134,7 +155,7 @@ class Message extends StatelessWidget { margin: EdgeInsets.symmetric(vertical: 4.0), child: ReplyContent(replyEvent, lightText: ownMessage, - timeline: timeline), + timeline: widget.timeline), ), ), ); @@ -148,10 +169,11 @@ class Message extends StatelessWidget { Opacity( opacity: 0, child: _MetaRow( - event, // meta information should be from the unedited event + widget + .event, // meta information should be from the unedited event ownMessage, textColor, - timeline, + widget.timeline, displayEvent, ), ), @@ -162,10 +184,10 @@ class Message extends StatelessWidget { right: ownMessage ? 0 : null, left: !ownMessage ? 0 : null, child: _MetaRow( - event, + widget.event, ownMessage, textColor, - timeline, + widget.timeline, displayEvent, ), ), @@ -180,9 +202,9 @@ class Message extends StatelessWidget { final avatarOrSizedBox = sameSender ? SizedBox(width: Avatar.defaultSize) : Avatar( - event.sender.avatarUrl, - event.sender.calcDisplayname(), - onTap: () => onAvatarTab(event), + widget.event.sender.avatarUrl, + widget.event.sender.calcDisplayname(), + onTap: () => widget.onAvatarTab(widget.event), ); if (ownMessage) { rowChildren.add(avatarOrSizedBox); @@ -195,7 +217,8 @@ class Message extends StatelessWidget { children: rowChildren, ); Widget container; - if (event.hasAggregatedEvents(timeline, RelationshipTypes.reaction)) { + if (widget.event + .hasAggregatedEvents(widget.timeline, RelationshipTypes.reaction)) { container = Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: @@ -208,7 +231,7 @@ class Message extends StatelessWidget { left: (ownMessage ? 0 : Avatar.defaultSize) + 12.0, right: (ownMessage ? Avatar.defaultSize : 0) + 12.0, ), - child: MessageReactions(event, timeline), + child: MessageReactions(widget.event, widget.timeline), ), ], ); @@ -218,7 +241,7 @@ class Message extends StatelessWidget { return Center( child: Container( - color: selected + color: widget.selected ? Theme.of(context).primaryColor.withAlpha(100) : Theme.of(context).primaryColor.withAlpha(0), constraints: BoxConstraints(maxWidth: FluffyThemes.columnWidth * 2.5),