perf(mobile): avoid redundant history-row work during navigation

Signed-off-by: Tom Brow <tomb@block.xyz>
This commit is contained in:
Tom Brow
2026-10-17 16:51:44 -07:00
parent 6286a54321
commit 773020abae
8 changed files with 194 additions and 15 deletions

View File

@@ -56,6 +56,7 @@ class _MessageList extends HookConsumerWidget {
localMessageSendAnimationProvider(channelId),
);
final displayEntries = groupMembershipTimelineEntries(entries);
final calendarDays = TimelineCalendarDays();
final itemScrollController = useMemoized(ItemScrollController.new);
final itemPositionsListener = useMemoized(ItemPositionsListener.create);
final stickyDayTimestamp = useValueNotifier<int?>(null);
@@ -121,7 +122,7 @@ class _MessageList extends HookConsumerWidget {
: null;
final startsDay =
previousMessage == null ||
!isSameDay(previousMessage.createdAt, message.createdAt);
!calendarDays.isSameDay(previousMessage.createdAt, message.createdAt);
final reversedIndex = displayEntries.length - 1 - chronologicalIndex;
if (startsDay) {
currentDayTimestamp = message.createdAt;
@@ -911,7 +912,10 @@ class _MessageList extends HookConsumerWidget {
final prevMessage = prevEntry?.message;
final showDayDivider =
prevMessage == null ||
!isSameDay(prevMessage.createdAt, message.createdAt);
!calendarDays.isSameDay(
prevMessage.createdAt,
message.createdAt,
);
final showAuthor =
!message.isSystem &&
@@ -932,7 +936,10 @@ class _MessageList extends HookConsumerWidget {
nextMessage == null ||
nextMessage.isSystem ||
nextMessage.hasAttachments ||
!isSameDay(message.createdAt, nextMessage.createdAt) ||
!calendarDays.isSameDay(
message.createdAt,
nextMessage.createdAt,
) ||
nextMessage.pubkey.toLowerCase() !=
message.pubkey.toLowerCase() ||
nextMessage.createdAt - message.createdAt > 300;

View File

@@ -79,6 +79,23 @@ bool isSameDay(int a, int b) {
return dtA.year == dtB.year && dtA.month == dtB.month && dtA.day == dtB.day;
}
/// Reuses local calendar components while building one timeline.
///
/// Adjacent rows compare the same timestamps for dividers, author grouping,
/// avatars, and sticky headers. Resolve each timestamp only once. Create a new
/// instance on each build so a timezone change cannot leave stale cached days.
class TimelineCalendarDays {
final _days = <int, (int, int, int)>{};
(int, int, int) _day(int unixSeconds) => _days.putIfAbsent(unixSeconds, () {
final date = DateTime.fromMillisecondsSinceEpoch(unixSeconds * 1000);
return (date.year, date.month, date.day);
});
/// Whether two timestamps fall on the same local calendar day.
bool isSameDay(int a, int b) => _day(a) == _day(b);
}
/// Returns a compact relative time string like "just now", "5m ago", "3h ago",
/// "2d ago", or a short date for older timestamps.
String relativeTime(int unixSeconds) {

View File

@@ -68,12 +68,39 @@ class LocalMessageSendTransition extends HookConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final reduceMotion = MediaQuery.disableAnimationsOf(context);
final needsAnimation = useMemoized(
() => animate && !reduceMotion,
const [],
);
// Loaded history never needs a ticker, animation builder, or expanding row.
// Choose at mount: later claim or accessibility changes must not replay a
// settled entrance or remount media, selection, and other message state.
if (!needsAnimation) {
return LocalSendProgress(progress: 1, widthProgress: 1, child: child);
}
return _AnimatedLocalMessageSendTransition(transition: this);
}
}
class _AnimatedLocalMessageSendTransition extends HookConsumerWidget {
const _AnimatedLocalMessageSendTransition({required this.transition});
final LocalMessageSendTransition transition;
@override
Widget build(BuildContext context, WidgetRef ref) {
final animate = transition.animate;
final channelId = transition.channelId;
final messageId = transition.messageId;
final startOffsetFactor = transition.startOffsetFactor;
final child = transition.child;
final origin = useMemoized(
() => !animate || channelId == null || messageId == null
? null
: ref
.read(localMessageSendAnimationProvider(channelId!).notifier)
.originFor(messageId!),
.read(localMessageSendAnimationProvider(channelId).notifier)
.originFor(messageId),
const [],
);
final reduceMotion = MediaQuery.disableAnimationsOf(context);

View File

@@ -54,9 +54,7 @@ class _FooterLayout extends MultiChildRenderObjectWidget {
BuildContext context,
covariant RenderBox renderObject,
) {
(renderObject as _RenderMessageFooter)
..overlap = overlap
..markNeedsLayout();
(renderObject as _RenderMessageFooter).overlap = overlap;
}
}
@@ -70,9 +68,15 @@ class _RenderMessageFooter extends RenderBox
RenderBox,
ContainerBoxParentData<RenderBox>
> {
_RenderMessageFooter(this.overlap);
_RenderMessageFooter(this._overlap);
double overlap;
double _overlap;
double get overlap => _overlap;
set overlap(double value) {
if (_overlap == value) return;
_overlap = value;
markNeedsLayout();
}
@override
void setupParentData(RenderBox child) {

View File

@@ -144,6 +144,7 @@ class _ThreadMessageList extends HookWidget {
@override
Widget build(BuildContext context) {
final calendarDays = TimelineCalendarDays();
final headLink = useMemoized(LayerLink.new);
final loadingHeadLink = useMemoized(LayerLink.new);
final headScrollPosition = useRef<ScrollPosition?>(null);
@@ -237,7 +238,7 @@ class _ThreadMessageList extends HookWidget {
? replies[chronologicalIndex - 1]
: null;
final previousMessage = previousReply ?? head;
final showDayDivider = !isSameDay(
final showDayDivider = !calendarDays.isSameDay(
previousMessage.createdAt,
reply.createdAt,
);
@@ -258,7 +259,10 @@ class _ThreadMessageList extends HookWidget {
reply.hasAttachments ||
nextReply?.hasAttachments == true ||
nextReply == null ||
!isSameDay(reply.createdAt, nextReply.createdAt) ||
!calendarDays.isSameDay(
reply.createdAt,
nextReply.createdAt,
) ||
nextReply.pubkey.toLowerCase() !=
reply.pubkey.toLowerCase() ||
nextReply.createdAt - reply.createdAt > 300;