chore: Revert "refactor: Make ChatListItem cache lasteventbody for better performance"

This reverts commit 42438052b1.
pull/2234/head
Christian Kußowski 1 month ago
parent 42438052b1
commit 0be267e8a4
No known key found for this signature in database
GPG Key ID: E067ECD60F1A0652

@ -15,7 +15,7 @@ import '../../widgets/avatar.dart';
enum ArchivedRoomAction { delete, rejoin } enum ArchivedRoomAction { delete, rejoin }
class ChatListItem extends StatefulWidget { class ChatListItem extends StatelessWidget {
final Room room; final Room room;
final Room? space; final Room? space;
final bool activeChat; final bool activeChat;
@ -35,87 +35,37 @@ class ChatListItem extends StatefulWidget {
super.key, super.key,
}); });
@override
State<ChatListItem> createState() => _ChatListItemState();
}
class _ChatListItemState extends State<ChatListItem> {
String? lastEventKey;
String? lastEventBody;
late final bool isDirectChat;
late final String? directChatMatrixId;
String _calcLastEventKey() =>
'${widget.room.lastEvent?.eventId}_${widget.room.lastEvent?.type}_${widget.room.lastEvent?.redacted}';
@override
void initState() {
isDirectChat = widget.room.isDirectChat;
directChatMatrixId = widget.room.directChatMatrixID;
super.initState();
lastEventKey = _calcLastEventKey();
lastEventBody = widget.room.lastEvent?.calcLocalizedBodyFallback(
MatrixLocals(L10n.of(context)),
hideReply: true,
hideEdit: true,
plaintextBody: true,
removeMarkdown: true,
withSenderNamePrefix: (!isDirectChat ||
directChatMatrixId != widget.room.lastEvent?.senderId),
);
if (!widget.room.participantListComplete) {
widget.room.loadHeroUsers().then((_) {
setState(() {});
});
}
}
void _maybeUpdateLastEventBody() async {
final newLastEventKey = _calcLastEventKey();
if (newLastEventKey == lastEventKey) return;
final newLastEventBody = await widget.room.lastEvent?.calcLocalizedBody(
MatrixLocals(L10n.of(context)),
hideReply: true,
hideEdit: true,
plaintextBody: true,
removeMarkdown: true,
withSenderNamePrefix: (!isDirectChat ||
directChatMatrixId != widget.room.lastEvent?.senderId),
);
if (lastEventBody != newLastEventBody) {
setState(() {
lastEventBody = newLastEventBody;
});
}
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
_maybeUpdateLastEventBody();
final theme = Theme.of(context); final theme = Theme.of(context);
final isMuted = widget.room.pushRuleState != PushRuleState.notify; final isMuted = room.pushRuleState != PushRuleState.notify;
final typingText = widget.room.getLocalizedTypingText(context); final typingText = room.getLocalizedTypingText(context);
final lastEvent = widget.room.lastEvent; final lastEvent = room.lastEvent;
final ownMessage = lastEvent?.senderId == widget.room.client.userID; final ownMessage = lastEvent?.senderId == room.client.userID;
final unread = widget.room.isUnread; final unread = room.isUnread;
final unreadBubbleSize = unread || widget.room.hasNewMessages final directChatMatrixId = room.directChatMatrixID;
? widget.room.notificationCount > 0 final isDirectChat = directChatMatrixId != null;
final unreadBubbleSize = unread || room.hasNewMessages
? room.notificationCount > 0
? 20.0 ? 20.0
: 14.0 : 14.0
: 0.0; : 0.0;
final hasNotifications = widget.room.notificationCount > 0; final hasNotifications = room.notificationCount > 0;
final backgroundColor = final backgroundColor =
widget.activeChat ? theme.colorScheme.secondaryContainer : null; activeChat ? theme.colorScheme.secondaryContainer : null;
final displayname = widget.room.getLocalizedDisplayname( final displayname = room.getLocalizedDisplayname(
MatrixLocals(L10n.of(context)), MatrixLocals(L10n.of(context)),
); );
final filter = widget.filter; final filter = this.filter;
if (filter != null && !displayname.toLowerCase().contains(filter)) { if (filter != null && !displayname.toLowerCase().contains(filter)) {
return const SizedBox.shrink(); return const SizedBox.shrink();
} }
final space = widget.space;
final needLastEventSender = lastEvent == null
? false
: room.getState(EventTypes.RoomMember, lastEvent.senderId) == null;
final space = this.space;
return Padding( return Padding(
padding: const EdgeInsets.symmetric( padding: const EdgeInsets.symmetric(
@ -126,11 +76,13 @@ class _ChatListItemState extends State<ChatListItem> {
borderRadius: BorderRadius.circular(AppConfig.borderRadius), borderRadius: BorderRadius.circular(AppConfig.borderRadius),
clipBehavior: Clip.hardEdge, clipBehavior: Clip.hardEdge,
color: backgroundColor, color: backgroundColor,
child: HoverBuilder( child: FutureBuilder(
future: room.loadHeroUsers(),
builder: (context, snapshot) => HoverBuilder(
builder: (context, listTileHovered) => ListTile( builder: (context, listTileHovered) => ListTile(
visualDensity: const VisualDensity(vertical: -0.5), visualDensity: const VisualDensity(vertical: -0.5),
contentPadding: const EdgeInsets.symmetric(horizontal: 8), contentPadding: const EdgeInsets.symmetric(horizontal: 8),
onLongPress: () => widget.onLongPress?.call(context), onLongPress: () => onLongPress?.call(context),
leading: HoverBuilder( leading: HoverBuilder(
builder: (context, hovered) => AnimatedScale( builder: (context, hovered) => AnimatedScale(
duration: FluffyThemes.animationDuration, duration: FluffyThemes.animationDuration,
@ -148,8 +100,8 @@ class _ChatListItemState extends State<ChatListItem> {
child: Avatar( child: Avatar(
border: BorderSide( border: BorderSide(
width: 2, width: 2,
color: color: backgroundColor ??
backgroundColor ?? theme.colorScheme.surface, theme.colorScheme.surface,
), ),
borderRadius: BorderRadius.circular( borderRadius: BorderRadius.circular(
AppConfig.borderRadius / 4, AppConfig.borderRadius / 4,
@ -157,7 +109,7 @@ class _ChatListItemState extends State<ChatListItem> {
mxContent: space.avatar, mxContent: space.avatar,
size: Avatar.defaultSize * 0.75, size: Avatar.defaultSize * 0.75,
name: space.getLocalizedDisplayname(), name: space.getLocalizedDisplayname(),
onTap: () => widget.onLongPress?.call(context), onTap: () => onLongPress?.call(context),
), ),
), ),
Positioned( Positioned(
@ -165,7 +117,7 @@ class _ChatListItemState extends State<ChatListItem> {
right: 0, right: 0,
child: Avatar( child: Avatar(
border: space == null border: space == null
? widget.room.isSpace ? room.isSpace
? BorderSide( ? BorderSide(
width: 1, width: 1,
color: theme.dividerColor, color: theme.dividerColor,
@ -176,26 +128,26 @@ class _ChatListItemState extends State<ChatListItem> {
color: backgroundColor ?? color: backgroundColor ??
theme.colorScheme.surface, theme.colorScheme.surface,
), ),
borderRadius: widget.room.isSpace borderRadius: room.isSpace
? BorderRadius.circular( ? BorderRadius.circular(
AppConfig.borderRadius / 4, AppConfig.borderRadius / 4,
) )
: null, : null,
mxContent: widget.room.avatar, mxContent: room.avatar,
size: space != null size: space != null
? Avatar.defaultSize * 0.75 ? Avatar.defaultSize * 0.75
: Avatar.defaultSize, : Avatar.defaultSize,
name: displayname, name: displayname,
presenceUserId: directChatMatrixId, presenceUserId: directChatMatrixId,
presenceBackgroundColor: backgroundColor, presenceBackgroundColor: backgroundColor,
onTap: () => widget.onLongPress?.call(context), onTap: () => onLongPress?.call(context),
), ),
), ),
Positioned( Positioned(
top: 0, top: 0,
right: 0, right: 0,
child: GestureDetector( child: GestureDetector(
onTap: () => widget.onLongPress?.call(context), onTap: () => onLongPress?.call(context),
child: AnimatedScale( child: AnimatedScale(
duration: FluffyThemes.animationDuration, duration: FluffyThemes.animationDuration,
curve: FluffyThemes.animationCurve, curve: FluffyThemes.animationCurve,
@ -225,7 +177,7 @@ class _ChatListItemState extends State<ChatListItem> {
overflow: TextOverflow.ellipsis, overflow: TextOverflow.ellipsis,
softWrap: false, softWrap: false,
style: TextStyle( style: TextStyle(
fontWeight: unread || widget.room.hasNewMessages fontWeight: unread || room.hasNewMessages
? FontWeight.w500 ? FontWeight.w500
: null, : null,
), ),
@ -239,7 +191,7 @@ class _ChatListItemState extends State<ChatListItem> {
size: 16, size: 16,
), ),
), ),
if (widget.room.isFavourite) if (room.isFavourite)
Padding( Padding(
padding: EdgeInsets.only( padding: EdgeInsets.only(
right: hasNotifications ? 4.0 : 0.0, right: hasNotifications ? 4.0 : 0.0,
@ -250,12 +202,11 @@ class _ChatListItemState extends State<ChatListItem> {
color: theme.colorScheme.primary, color: theme.colorScheme.primary,
), ),
), ),
if (!widget.room.isSpace && if (!room.isSpace && room.membership != Membership.invite)
widget.room.membership != Membership.invite)
Padding( Padding(
padding: const EdgeInsets.only(left: 4.0), padding: const EdgeInsets.only(left: 4.0),
child: Text( child: Text(
widget.room.latestEventReceivedTime room.latestEventReceivedTime
.localizedTimeShort(context), .localizedTimeShort(context),
style: TextStyle( style: TextStyle(
fontSize: 12, fontSize: 12,
@ -271,7 +222,7 @@ class _ChatListItemState extends State<ChatListItem> {
children: <Widget>[ children: <Widget>[
if (typingText.isEmpty && if (typingText.isEmpty &&
ownMessage && ownMessage &&
widget.room.lastEvent!.status.isSending) ...[ room.lastEvent!.status.isSending) ...[
const SizedBox( const SizedBox(
width: 16, width: 16,
height: 16, height: 16,
@ -293,12 +244,11 @@ class _ChatListItemState extends State<ChatListItem> {
), ),
), ),
Expanded( Expanded(
child: widget.room.isSpace && child: room.isSpace && room.membership == Membership.join
widget.room.membership == Membership.join
? Text( ? Text(
L10n.of(context).countChatsAndCountParticipants( L10n.of(context).countChatsAndCountParticipants(
widget.room.spaceChildren.length, room.spaceChildren.length,
(widget.room.summary.mJoinedMemberCount ?? 1), (room.summary.mJoinedMemberCount ?? 1),
), ),
style: TextStyle(color: theme.colorScheme.outline), style: TextStyle(color: theme.colorScheme.outline),
) )
@ -311,35 +261,62 @@ class _ChatListItemState extends State<ChatListItem> {
maxLines: 1, maxLines: 1,
softWrap: false, softWrap: false,
) )
: Text( : FutureBuilder(
widget.room.membership == Membership.invite key: ValueKey(
? widget.room '${lastEvent?.eventId}_${lastEvent?.type}_${lastEvent?.redacted}',
),
future: needLastEventSender
? lastEvent.calcLocalizedBody(
MatrixLocals(L10n.of(context)),
hideReply: true,
hideEdit: true,
plaintextBody: true,
removeMarkdown: true,
withSenderNamePrefix: (!isDirectChat ||
directChatMatrixId !=
room.lastEvent?.senderId),
)
: null,
initialData:
lastEvent?.calcLocalizedBodyFallback(
MatrixLocals(L10n.of(context)),
hideReply: true,
hideEdit: true,
plaintextBody: true,
removeMarkdown: true,
withSenderNamePrefix: (!isDirectChat ||
directChatMatrixId !=
room.lastEvent?.senderId),
),
builder: (context, snapshot) => Text(
room.membership == Membership.invite
? room
.getState( .getState(
EventTypes.RoomMember, EventTypes.RoomMember,
widget.room.client.userID!, room.client.userID!,
) )
?.content ?.content
.tryGet<String>('reason') ?? .tryGet<String>('reason') ??
(isDirectChat (isDirectChat
? L10n.of(context).newChatRequest ? L10n.of(context).newChatRequest
: L10n.of(context).inviteGroupChat) : L10n.of(context)
: lastEventBody ?? .inviteGroupChat)
: snapshot.data ??
L10n.of(context).noMessagesYet, L10n.of(context).noMessagesYet,
softWrap: false, softWrap: false,
maxLines: maxLines: room.notificationCount >= 1 ? 2 : 1,
widget.room.notificationCount >= 1 ? 2 : 1,
overflow: TextOverflow.ellipsis, overflow: TextOverflow.ellipsis,
style: TextStyle( style: TextStyle(
color: unread || widget.room.hasNewMessages color: unread || room.hasNewMessages
? theme.colorScheme.onSurface ? theme.colorScheme.onSurface
: theme.colorScheme.outline, : theme.colorScheme.outline,
decoration: decoration: room.lastEvent?.redacted == true
widget.room.lastEvent?.redacted == true
? TextDecoration.lineThrough ? TextDecoration.lineThrough
: null, : null,
), ),
), ),
), ),
),
const SizedBox(width: 8), const SizedBox(width: 8),
AnimatedContainer( AnimatedContainer(
duration: FluffyThemes.animationDuration, duration: FluffyThemes.animationDuration,
@ -347,26 +324,24 @@ class _ChatListItemState extends State<ChatListItem> {
alignment: Alignment.center, alignment: Alignment.center,
padding: const EdgeInsets.symmetric(horizontal: 7), padding: const EdgeInsets.symmetric(horizontal: 7),
height: unreadBubbleSize, height: unreadBubbleSize,
width: !hasNotifications && width: !hasNotifications && !unread && !room.hasNewMessages
!unread &&
!widget.room.hasNewMessages
? 0 ? 0
: (unreadBubbleSize - 9) * : (unreadBubbleSize - 9) *
widget.room.notificationCount.toString().length + room.notificationCount.toString().length +
9, 9,
decoration: BoxDecoration( decoration: BoxDecoration(
color: widget.room.highlightCount > 0 color: room.highlightCount > 0
? theme.colorScheme.error ? theme.colorScheme.error
: hasNotifications || widget.room.markedUnread : hasNotifications || room.markedUnread
? theme.colorScheme.primary ? theme.colorScheme.primary
: theme.colorScheme.primaryContainer, : theme.colorScheme.primaryContainer,
borderRadius: BorderRadius.circular(7), borderRadius: BorderRadius.circular(7),
), ),
child: hasNotifications child: hasNotifications
? Text( ? Text(
widget.room.notificationCount.toString(), room.notificationCount.toString(),
style: TextStyle( style: TextStyle(
color: widget.room.highlightCount > 0 color: room.highlightCount > 0
? theme.colorScheme.onError ? theme.colorScheme.onError
: hasNotifications : hasNotifications
? theme.colorScheme.onPrimary ? theme.colorScheme.onPrimary
@ -380,9 +355,9 @@ class _ChatListItemState extends State<ChatListItem> {
), ),
], ],
), ),
onTap: widget.onTap, onTap: onTap,
trailing: widget.onForget == null trailing: onForget == null
? widget.room.membership == Membership.invite ? room.membership == Membership.invite
? IconButton( ? IconButton(
tooltip: L10n.of(context).declineInvitation, tooltip: L10n.of(context).declineInvitation,
icon: const Icon(Icons.delete_forever_outlined), icon: const Icon(Icons.delete_forever_outlined),
@ -399,14 +374,15 @@ class _ChatListItemState extends State<ChatListItem> {
if (!context.mounted) return; if (!context.mounted) return;
await showFutureLoadingDialog( await showFutureLoadingDialog(
context: context, context: context,
future: widget.room.leave, future: room.leave,
); );
}, },
) )
: null : null
: IconButton( : IconButton(
icon: const Icon(Icons.delete_outlined), icon: const Icon(Icons.delete_outlined),
onPressed: widget.onForget, onPressed: onForget,
),
), ),
), ),
), ),

Loading…
Cancel
Save