Skip to content

Commit 58cc4b7

Browse files
authored
Fix mobile jump-to-latest flicker (block#6807)
## Summary - Keep jump-to-latest hidden through composer and keyboard layout changes while the user is still following the tail. - Stabilize thread tail detection during lazy scroll updates. - Unmount inactive iOS Liquid Glass controls so the channel arrow cannot bleed into an opened thread. ## Testing - just mobile-check - flutter test (1,864 passed) - Signed iOS Release build and install on iPhone Air --------- Signed-off-by: kenny lopez <[email protected]>
1 parent 86b9142 commit 58cc4b7

7 files changed

Lines changed: 582 additions & 145 deletions

File tree

‎mobile/lib/features/channels/channel_detail_page/message_list.dart‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,13 @@ class _MessageList extends HookConsumerWidget {
437437
) -
438438
composerBottomInset,
439439
);
440+
final hidesJumpToLatest = shouldHideChannelJumpToLatest(
441+
isAtLatest: latestIsAtBoundary(),
442+
followsLatest: followsLatest.value,
443+
userHasDetached: hasUserScrolled.value,
444+
);
440445
final shouldShow =
441-
!latestIsAtBoundary() &&
446+
!hidesJumpToLatest &&
442447
(hasUnseenLatestEntry.value ||
443448
!latestIsVisible ||
444449
distanceFromLatest.value > visiblePageHeight);

‎mobile/lib/features/channels/jump_to_latest_button.dart‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class JumpToLatestButton extends HookWidget {
3333
final nativeChannel = useState<MethodChannel?>(null);
3434
final onPressedRef = useRef(onPressed)..value = onPressed;
3535
final brightness = context.theme.brightness.name;
36+
final routeIsCurrent = ModalRoute.isCurrentOf(context) ?? true;
3637

3738
useEffect(() {
3839
final channel = nativeChannel.value;
@@ -101,6 +102,7 @@ class JumpToLatestButton extends HookWidget {
101102
child: ValueListenableBuilder<bool>(
102103
valueListenable: messageActionBackdropActive,
103104
builder: (context, backdropActive, _) {
105+
if (!routeIsCurrent) return const SizedBox.shrink();
104106
if (!usesNativeIosGlass || backdropActive) {
105107
return buildFlutterSurface();
106108
}

‎mobile/lib/features/channels/jump_to_latest_switcher.dart‎

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@ import 'package:flutter/material.dart';
22

33
import 'jump_to_latest_button.dart';
44

5+
/// Whether channel Latest should remain hidden at the effective timeline tail.
6+
///
7+
/// Composer and keyboard layout updates can briefly make item-position
8+
/// measurements stale. Preserve an active tail-follow intent through those
9+
/// frames unless the user explicitly detached from the tail.
10+
bool shouldHideChannelJumpToLatest({
11+
required bool isAtLatest,
12+
required bool followsLatest,
13+
required bool userHasDetached,
14+
}) => isAtLatest || (followsLatest && !userHasDetached);
15+
516
/// Shared channel/thread visibility transition for [JumpToLatestButton].
617
class JumpToLatestSwitcher extends StatelessWidget {
718
final String id;
@@ -18,6 +29,19 @@ class JumpToLatestSwitcher extends StatelessWidget {
1829
@override
1930
Widget build(BuildContext context) {
2031
final reduceMotion = MediaQuery.disableAnimationsOf(context);
32+
final child = visible
33+
? JumpToLatestButton(
34+
key: ValueKey('$id-jump-to-latest'),
35+
id: id,
36+
onPressed: onPressed,
37+
)
38+
: SizedBox.shrink(key: ValueKey('$id-jump-to-latest-hidden'));
39+
if (Theme.of(context).platform == TargetPlatform.iOS) {
40+
return KeyedSubtree(
41+
key: ValueKey('$id-jump-to-latest-switcher'),
42+
child: child,
43+
);
44+
}
2145
return AnimatedSwitcher(
2246
key: ValueKey('$id-jump-to-latest-switcher'),
2347
duration: reduceMotion
@@ -36,13 +60,7 @@ class JumpToLatestSwitcher extends StatelessWidget {
3660
child: child,
3761
),
3862
),
39-
child: visible
40-
? JumpToLatestButton(
41-
key: ValueKey('$id-jump-to-latest'),
42-
id: id,
43-
onPressed: onPressed,
44-
)
45-
: SizedBox.shrink(key: ValueKey('$id-jump-to-latest-hidden')),
63+
child: child,
4664
);
4765
}
4866
}

‎mobile/lib/features/channels/thread_detail_helpers.dart‎

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,66 @@
11
part of 'thread_detail_page.dart';
22

3-
/// Returns whether a bounded tail correction reached the effective end.
3+
const _threadTailScrollTolerance = 0.5;
4+
5+
// Keep the direct-position correction finite in case the viewport cannot
6+
// expose its tail (for example, continuously changing media dimensions).
7+
const _latestTailCorrectionLimit = 8;
8+
9+
Widget _trackActiveThreadScrollPosition(
10+
Widget child,
11+
ObjectRef<ScrollPosition?> activePosition,
12+
) => Builder(
13+
builder: (context) {
14+
activePosition.value = Scrollable.of(context).position;
15+
return child;
16+
},
17+
);
18+
19+
bool _jumpActiveThreadScrollToTail(
20+
ObjectRef<ScrollPosition?> activePosition,
21+
bool Function()? testOverride,
22+
) {
23+
if (testOverride != null) return testOverride();
24+
final position = activePosition.value;
25+
if (position == null || !position.hasContentDimensions) return false;
26+
// Moving the one active viewport avoids a second list and its visible bounce.
27+
position.jumpTo(position.maxScrollExtent);
28+
return true;
29+
}
30+
31+
Future<bool> _animateActiveThreadScrollToTail(
32+
BuildContext context,
33+
ObjectRef<ScrollPosition?> activePosition,
34+
) async {
35+
final position = activePosition.value;
36+
if (position == null || !position.hasContentDimensions) return false;
37+
if (MediaQuery.disableAnimationsOf(context)) {
38+
position.jumpTo(position.maxScrollExtent);
39+
return true;
40+
}
41+
await position.animateTo(
42+
position.maxScrollExtent,
43+
duration: jumpToLatestScrollDuration,
44+
curve: jumpToLatestScrollCurve,
45+
);
46+
return true;
47+
}
48+
49+
/// Returns whether the thread is at its effective scroll end.
450
///
5-
/// Item positions can lag the active scroll position by a frame, so an exact
6-
/// end-of-scroll measurement is sufficient even while the tail item still
51+
/// Item positions can lag or briefly oscillate during lazy layout, so an exact
52+
/// end-of-scroll measurement remains authoritative even while the tail item
753
/// reports outside the visible boundary.
854
@visibleForTesting
9-
bool threadTailCorrectionReachedEnd({
55+
bool threadTailIsAtEffectiveEnd({
56+
required bool tailIsLaidOut,
1057
required bool tailIsVisible,
1158
required double? extentAfter,
1259
}) =>
1360
tailIsVisible ||
14-
(extentAfter != null && extentAfter <= _threadTailScrollTolerance);
61+
(tailIsLaidOut &&
62+
extentAfter != null &&
63+
extentAfter <= _threadTailScrollTolerance);
1564

1665
int _threadTailIndex(int replyCount) => replyCount;
1766

‎mobile/lib/features/channels/thread_detail_page.dart‎

Lines changed: 55 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,6 @@ const _landingHighlightDuration = Duration(seconds: 3);
6060
const _landingHighlightDelay = Duration(milliseconds: 50);
6161
const _landingHighlightTransitionDuration = Duration(milliseconds: 300);
6262
const _landingHighlightOpacity = 0.12;
63-
const _threadTailScrollTolerance = 0.5;
64-
65-
// Keep the direct-position correction finite in case the viewport cannot
66-
// expose its tail (for example, continuously changing media dimensions).
67-
const _latestTailCorrectionLimit = 8;
6863

6964
/// Full-screen thread detail page.
7065
///
@@ -79,6 +74,10 @@ class ThreadDetailPage extends HookConsumerWidget {
7974
final bool isArchived;
8075
final String? initialMessageId;
8176

77+
/// Overrides the tail jump only in deterministic lazy-layout tests.
78+
@visibleForTesting
79+
final bool Function()? jumpThreadTailForTesting;
80+
8281
const ThreadDetailPage({
8382
super.key,
8483
required this.threadHead,
@@ -88,6 +87,7 @@ class ThreadDetailPage extends HookConsumerWidget {
8887
required this.isMember,
8988
required this.isArchived,
9089
this.initialMessageId,
90+
this.jumpThreadTailForTesting,
9191
});
9292

9393
@override
@@ -252,9 +252,15 @@ class ThreadDetailPage extends HookConsumerWidget {
252252
final initialTailSettle = useMemoized(InitialThreadTailSettle.new);
253253
final isAtThreadTail = useState(true);
254254
final isNavigatingToThreadTail = useState(false);
255+
// Ordinary thread entry owns a tail-follow intent before lazy item
256+
// positions settle. Keep Latest suppressed through those initial layout
257+
// frames; a deep-link entry must expose it for its older target instead.
258+
final hidesLatestForInitialTailSettle = useState(initialMessageId == null);
259+
final hidesLatestForComposerTailCorrection = useState(false);
255260
final tailCorrectionInProgress = useRef(false);
256261
final tailCorrectionGeneration = useRef(0);
257262
final activeThreadScrollPosition = useRef<ScrollPosition?>(null);
263+
final composerHasFocus = useListenable(composerFocusNode).hasFocus;
258264
final viewportHeight = useListenable(listViewport.height).value;
259265
final previousViewportHeight = useRef(viewportHeight);
260266
final settledImeLift = usesFixedAndroidImeViewport
@@ -321,51 +327,34 @@ class ThreadDetailPage extends HookConsumerWidget {
321327
final trailingBoundary =
322328
1 - ((Grid.xs + timelineBottomInset) / viewportHeight) + 0.001;
323329
final lastMessageIndex = _threadTailIndex(replies.length);
324-
return itemPositionsListener.itemPositions.value.any(
325-
(position) =>
326-
position.index == lastMessageIndex &&
327-
position.itemTrailingEdge <= trailingBoundary,
330+
var tailItemIsLaidOut = false;
331+
var tailItemIsVisible = false;
332+
for (final item in itemPositionsListener.itemPositions.value) {
333+
if (item.index != lastMessageIndex) continue;
334+
tailItemIsLaidOut = true;
335+
tailItemIsVisible = item.itemTrailingEdge <= trailingBoundary;
336+
break;
337+
}
338+
final position = activeThreadScrollPosition.value;
339+
return threadTailIsAtEffectiveEnd(
340+
tailIsLaidOut: tailItemIsLaidOut,
341+
tailIsVisible: tailItemIsVisible,
342+
extentAfter: position != null && position.hasContentDimensions
343+
? position.extentAfter
344+
: null,
328345
);
329346
}
330347

331-
Widget trackActiveScrollPosition(Widget child) {
332-
return Builder(
333-
builder: (itemContext) {
334-
activeThreadScrollPosition.value = Scrollable.of(
335-
itemContext,
336-
).position;
337-
return child;
338-
},
339-
);
340-
}
348+
Widget trackActiveScrollPosition(Widget child) =>
349+
_trackActiveThreadScrollPosition(child, activeThreadScrollPosition);
341350

342-
bool jumpActiveScrollPositionToTail() {
343-
final position = activeThreadScrollPosition.value;
344-
if (position == null || !position.hasContentDimensions) return false;
345-
// Move the one active viewport to its exact end. Unlike indexed
346-
// jumpTo/scrollTo, this does not reset or cross-fade through a second
347-
// list, so iOS never exposes the intermediate top-of-thread frame.
348-
position.jumpTo(position.maxScrollExtent);
349-
return true;
350-
}
351+
bool jumpActiveScrollPositionToTail() => _jumpActiveThreadScrollToTail(
352+
activeThreadScrollPosition,
353+
jumpThreadTailForTesting,
354+
);
351355

352-
Future<bool> animateActiveScrollPositionToTail() async {
353-
final position = activeThreadScrollPosition.value;
354-
if (position == null || !position.hasContentDimensions) return false;
355-
if (MediaQuery.disableAnimationsOf(context)) {
356-
position.jumpTo(position.maxScrollExtent);
357-
return true;
358-
}
359-
// Match the channel's visible Latest glide while moving only the active
360-
// thread viewport. The indexed-list animation path can create a temporary
361-
// second list for distant targets, which caused the old top-frame bounce.
362-
await position.animateTo(
363-
position.maxScrollExtent,
364-
duration: jumpToLatestScrollDuration,
365-
curve: jumpToLatestScrollCurve,
366-
);
367-
return true;
368-
}
356+
Future<bool> animateActiveScrollPositionToTail() =>
357+
_animateActiveThreadScrollToTail(context, activeThreadScrollPosition);
369358

370359
void finishThreadTailCorrection({
371360
required bool revealViewport,
@@ -380,14 +369,14 @@ class ThreadDetailPage extends HookConsumerWidget {
380369
tailIntent.isDragging ||
381370
userOptedOutOfTailFollow.value) {
382371
tailCorrectionInProgress.value = false;
372+
hidesLatestForComposerTailCorrection.value = false;
383373
isNavigatingToThreadTail.value = false;
384374
isAtThreadTail.value = threadTailIsVisible();
385375
return;
386376
}
387377
final reachedTail = threadTailIsVisible();
388378
// Lazy children can revise maxScrollExtent for several frames. Keep
389379
// moving the same active position until the measured tail is visible;
390-
// the cap only guards pathological layouts that never stabilize.
391380
if (!reachedTail && corrections < _latestTailCorrectionLimit) {
392381
jumpActiveScrollPositionToTail();
393382
WidgetsBinding.instance.addPostFrameCallback(
@@ -401,19 +390,10 @@ class ThreadDetailPage extends HookConsumerWidget {
401390
return;
402391
}
403392
tailCorrectionInProgress.value = false;
393+
hidesLatestForComposerTailCorrection.value = false;
404394
isNavigatingToThreadTail.value = false;
405395
if (revealViewport) initialViewportReady.value = true;
406-
// Item positions can trail the ScrollPosition by a frame after an
407-
// animated jump. Once the bounded lazy-layout correction is exhausted,
408-
// trust an exact end-of-scroll position too: there is nowhere further
409-
// for Latest to navigate, so leaving the control visible is misleading.
410-
final position = activeThreadScrollPosition.value;
411-
isAtThreadTail.value = threadTailCorrectionReachedEnd(
412-
tailIsVisible: reachedTail,
413-
extentAfter: position != null && position.hasContentDimensions
414-
? position.extentAfter
415-
: null,
416-
);
396+
isAtThreadTail.value = reachedTail;
417397
}
418398

419399
void correctThreadTailInstantly() {
@@ -432,6 +412,7 @@ class ThreadDetailPage extends HookConsumerWidget {
432412

433413
void followThreadTailFromComposer() {
434414
if (userDragDetachedTailFollow.value) return;
415+
hidesLatestForComposerTailCorrection.value = true;
435416
initialTailSettle.abandon();
436417
initialViewportReady.value = true;
437418
tailIntent.endDrag();
@@ -440,9 +421,20 @@ class ThreadDetailPage extends HookConsumerWidget {
440421
followsThreadTail.value = true;
441422
final reachedTail = threadTailIsVisible();
442423
isAtThreadTail.value = reachedTail;
443-
if (!reachedTail) correctThreadTailInstantly();
424+
if (reachedTail) {
425+
hidesLatestForComposerTailCorrection.value = false;
426+
} else {
427+
correctThreadTailInstantly();
428+
}
444429
}
445430

431+
useEffect(() {
432+
if (!composerHasFocus) {
433+
hidesLatestForComposerTailCorrection.value = false;
434+
}
435+
return null;
436+
}, [composerHasFocus]);
437+
446438
useEffect(
447439
() {
448440
void onPositionsChanged() {
@@ -863,6 +855,8 @@ class ThreadDetailPage extends HookConsumerWidget {
863855
child: _ThreadMessageList(
864856
viewport: listViewport,
865857
onUserScrollStart: () {
858+
hidesLatestForInitialTailSettle.value = false;
859+
hidesLatestForComposerTailCorrection.value = false;
866860
initialTailSettle.abandon();
867861
initialViewportReady.value = true;
868862
tailCorrectionInProgress.value = false;
@@ -981,6 +975,9 @@ class ThreadDetailPage extends HookConsumerWidget {
981975
threadViewportVisible &&
982976
hasFetchedReplies &&
983977
!isNavigatingToThreadTail.value &&
978+
!hidesLatestForInitialTailSettle.value &&
979+
!hidesLatestForComposerTailCorrection.value &&
980+
!(composerHasFocus && !userDragDetachedTailFollow.value) &&
984981
!isAtThreadTail.value,
985982
onPressed: scrollToThreadLatest,
986983
),

0 commit comments

Comments
 (0)