Skip to content

Commit

Permalink
refactor: Event list
Browse files Browse the repository at this point in the history
  • Loading branch information
krille-chan committed Dec 1, 2023
1 parent aedc1c4 commit e6fa844
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 39 deletions.
71 changes: 36 additions & 35 deletions lib/pages/chat/chat_event_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@ class ChatEventList extends StatelessWidget {
Widget build(BuildContext context) {
final horizontalPadding = FluffyThemes.isColumnMode(context) ? 8.0 : 0.0;

final events = controller.timeline!.events
.where((event) => event.isVisibleInGui)
.toList();

// create a map of eventId --> index to greatly improve performance of
// ListView's findChildIndexCallback
final thisEventsKeyMap = <String, int>{};
for (var i = 0; i < controller.timeline!.events.length; i++) {
thisEventsKeyMap[controller.timeline!.events[i].eventId] = i;
for (var i = 0; i < events.length; i++) {
thisEventsKeyMap[events[i].eventId] = i;
}

return SelectionArea(
Expand Down Expand Up @@ -71,7 +75,7 @@ class ChatEventList extends StatelessWidget {
}

// Request history button or progress indicator:
if (i == controller.timeline!.events.length + 1) {
if (i == events.length + 1) {
if (controller.timeline!.isRequestingHistory) {
return const Center(
child: CircularProgressIndicator.adaptive(strokeWidth: 2),
Expand All @@ -87,46 +91,43 @@ class ChatEventList extends StatelessWidget {
}
return const SizedBox.shrink();
}
i--;

// The message at this index:
final event = controller.timeline!.events[i - 1];
final event = events[i];

return AutoScrollTag(
key: ValueKey(event.eventId),
index: i - 1,
index: i,
controller: controller.scrollController,
child: event.isVisibleInGui
? Message(
event,
onSwipe: () => controller.replyAction(replyTo: event),
onInfoTab: controller.showEventInfo,
onAvatarTab: (Event event) => showAdaptiveBottomSheet(
context: context,
builder: (c) => UserBottomSheet(
user: event.senderFromMemoryOrFallback,
outerContext: context,
onMention: () => controller.sendController.text +=
'${event.senderFromMemoryOrFallback.mention} ',
),
),
onSelect: controller.onSelectMessage,
scrollToEventId: (String eventId) =>
controller.scrollToEventId(eventId),
longPressSelect: controller.selectedEvents.isNotEmpty,
selected: controller.selectedEvents
.any((e) => e.eventId == event.eventId),
timeline: controller.timeline!,
displayReadMarker:
controller.readMarkerEventId == event.eventId &&
controller.timeline?.allowNewEvent == false,
nextEvent: i < controller.timeline!.events.length
? controller.timeline!.events[i]
: null,
)
: const SizedBox.shrink(),
child: Message(
event,
onSwipe: () => controller.replyAction(replyTo: event),
onInfoTab: controller.showEventInfo,
onAvatarTab: (Event event) => showAdaptiveBottomSheet(
context: context,
builder: (c) => UserBottomSheet(
user: event.senderFromMemoryOrFallback,
outerContext: context,
onMention: () => controller.sendController.text +=
'${event.senderFromMemoryOrFallback.mention} ',
),
),
onSelect: controller.onSelectMessage,
scrollToEventId: (String eventId) =>
controller.scrollToEventId(eventId),
longPressSelect: controller.selectedEvents.isNotEmpty,
selected: controller.selectedEvents
.any((e) => e.eventId == event.eventId),
timeline: controller.timeline!,
displayReadMarker:
controller.readMarkerEventId == event.eventId &&
controller.timeline?.allowNewEvent == false,
nextEvent: i + 1 < events.length ? events[i + 1] : null,
),
);
},
childCount: controller.timeline!.events.length + 2,
childCount: events.length + 2,
findChildIndexCallback: (key) =>
controller.findChildIndexCallback(key, thisEventsKeyMap),
),
Expand Down
6 changes: 2 additions & 4 deletions lib/pages/chat/events/message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class Message extends StatelessWidget {
final client = Matrix.of(context).client;
final ownMessage = event.senderId == client.userID;
final alignment = ownMessage ? Alignment.topRight : Alignment.topLeft;
var color = Theme.of(context).colorScheme.onInverseSurface;
var color = Theme.of(context).colorScheme.surfaceVariant;
final displayTime = event.type == EventTypes.RoomCreate ||
nextEvent == null ||
!event.originServerTs.sameEnvironment(nextEvent!.originServerTs);
Expand Down Expand Up @@ -294,9 +294,7 @@ class Message extends StatelessWidget {
if (displayTime || selected)
Padding(
padding: displayTime
? const EdgeInsets.symmetric(
vertical: 8.0,
)
? const EdgeInsets.symmetric(vertical: 8.0)
: EdgeInsets.zero,
child: Center(
child: Material(
Expand Down

0 comments on commit e6fa844

Please sign in to comment.