Skip to content

msglist: Fix excess fetching on scrolling past code blocks #508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions lib/widgets/message_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ class _MessageListState extends State<MessageList> with PerAccountStoreAwareStat
});
}

void _adjustButtonVisibility(ScrollMetrics scrollMetrics) {
void _handleScrollMetrics(ScrollMetrics scrollMetrics) {
if (scrollMetrics.extentAfter == 0) {
_scrollToBottomVisibleValue.value = false;
} else {
Expand All @@ -232,11 +232,17 @@ class _MessageListState extends State<MessageList> with PerAccountStoreAwareStat
}

void _scrollChanged() {
_adjustButtonVisibility(scrollController.position);
_handleScrollMetrics(scrollController.position);
}

bool _metricsChanged(ScrollMetricsNotification scrollMetricsNotification) {
_adjustButtonVisibility(scrollMetricsNotification.metrics);
bool _handleScrollMetricsNotification(ScrollMetricsNotification notification) {
if (notification.depth > 0) {
// This notification came from some Viewport nested more deeply than the
// one for the message list itself (e.g., from a CodeBlock). Ignore it.
return true;
}

_handleScrollMetrics(notification.metrics);
return true;
}

Expand All @@ -262,7 +268,7 @@ class _MessageListState extends State<MessageList> with PerAccountStoreAwareStat
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 760),
child: NotificationListener<ScrollMetricsNotification>(
onNotification: _metricsChanged,
onNotification: _handleScrollMetricsNotification,
child: Stack(
children: <Widget>[
_buildListView(context),
Expand Down
31 changes: 31 additions & 0 deletions test/widgets/message_list_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,37 @@ void main() {
check(itemCount(tester)).equals(301);
}, skip: true); // TODO this still reproduces manually, still needs debugging,
// but has become harder to reproduce in a test.

testWidgets("avoid getting distracted by nested viewports' metrics", (tester) async {
// Regression test for: https://github.com/zulip/zulip-flutter/issues/507
await setupMessageListPage(tester, foundOldest: false, messages: [
...List.generate(300, (i) => eg.streamMessage(id: 1000 + i)),
eg.streamMessage(id: 1301, content: '<div class="codehilite"><pre><span></span><code>verb\natim\n</code></pre></div>'),
...List.generate(100, (i) => eg.streamMessage(id: 1302 + i)),
]);
final lastRequest = connection.lastRequest;
check(itemCount(tester)).equals(404);

// Fling-scroll upward...
await tester.fling(find.byType(MessageListPage), const Offset(0, 300), 8000);
await tester.pump();

// ... in particular past the message with a [CodeBlockNode]...
bool sawCodeBlock = false;
for (int i = 0; i < 30; i++) {
await tester.pump(const Duration(milliseconds: 100));
if (tester.widgetList(find.byType(CodeBlock)).isNotEmpty) {
sawCodeBlock = true;
break;
}
}
check(sawCodeBlock).isTrue();

// ... and we should attempt no fetches. (This check isn't strictly
// necessary; a request would have thrown, as we prepared no response.)
await tester.pump();
check(connection.lastRequest).identicalTo(lastRequest);
});
});

group('ScrollToBottomButton interactions', () {
Expand Down