Skip to content

Commit c4ee9ab

Browse files
committed
Preserve thread reading position after non-drag head scrolling
Signed-off-by: Tom Brow <[email protected]>
1 parent a673032 commit c4ee9ab

3 files changed

Lines changed: 110 additions & 4 deletions

File tree

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,17 @@ class _ThreadHeadScrollInput extends HookWidget {
4545
final ScrollPosition? Function() scrollPosition;
4646
final ValueListenable<Iterable<ItemPosition>> positions;
4747
final double viewportTopEdge;
48+
final VoidCallback onUserScrollStart;
49+
final VoidCallback onUserScrollEnd;
4850
final Widget child;
4951

5052
const _ThreadHeadScrollInput({
5153
required this.enabled,
5254
required this.scrollPosition,
5355
required this.positions,
5456
required this.viewportTopEdge,
57+
required this.onUserScrollStart,
58+
required this.onUserScrollEnd,
5559
required this.child,
5660
});
5761

@@ -74,12 +78,20 @@ class _ThreadHeadScrollInput extends HookWidget {
7478
);
7579
final position = scrollPosition();
7680
useListenable(position);
81+
void userScroll(VoidCallback action) {
82+
onUserScrollStart();
83+
action();
84+
onUserScrollEnd();
85+
}
86+
7787
void scrollPage(double direction) {
7888
final current = scrollPosition();
7989
if (current != null) {
80-
current.moveTo(
81-
current.pixels + direction * current.viewportDimension * 0.8,
82-
);
90+
userScroll(() {
91+
current.moveTo(
92+
current.pixels + direction * current.viewportDimension * 0.8,
93+
);
94+
});
8395
}
8496
}
8597

@@ -110,7 +122,12 @@ class _ThreadHeadScrollInput extends HookWidget {
110122
GestureBinding.instance.pointerSignalResolver.register(
111123
event,
112124
(_) {
113-
scrollPosition()?.pointerScroll(event.scrollDelta.dy);
125+
final current = scrollPosition();
126+
if (current != null && event.scrollDelta.dy != 0) {
127+
userScroll(() {
128+
current.pointerScroll(event.scrollDelta.dy);
129+
});
130+
}
114131
},
115132
);
116133
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,8 @@ class _ThreadMessageList extends HookWidget {
312312
showWhenUnlinked: false,
313313
child: _ThreadHeadScrollInput(
314314
enabled: visible,
315+
onUserScrollStart: onUserScrollStart,
316+
onUserScrollEnd: onUserScrollEnd,
315317
scrollPosition: () => headScrollPosition.value,
316318
positions: itemPositionsListener.itemPositions,
317319
viewportTopEdge:

‎mobile/test/features/channels/channel_detail_page_test/loading_review_tests.dart‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,93 @@ void _loadingReviewTests() {
173173
);
174174
}
175175

176+
for (final useSemantics in [false, true]) {
177+
testWidgets(
178+
'head non-drag scroll detaches hydration tail follow semantics=$useSemantics',
179+
(tester) async {
180+
tester.view.physicalSize = const Size(400, 800);
181+
tester.view.devicePixelRatio = 1;
182+
addTearDown(tester.view.reset);
183+
final root = _textMsg(
184+
id: 'thread-root',
185+
pubkey: 'alice',
186+
content: 'Original message',
187+
createdAt: 1000,
188+
);
189+
final replies = [
190+
for (var i = 0; i < 65; i++)
191+
_textMsg(
192+
id: 'reply-$i',
193+
pubkey: 'bob',
194+
content: 'Reply $i',
195+
createdAt: 1100 + i,
196+
extraTags: const [
197+
['e', 'thread-root', '', 'reply'],
198+
],
199+
),
200+
];
201+
final query = Completer<List<NostrEvent>>();
202+
final provisional = formatTimeline([root, ...replies.take(60)]);
203+
await tester.pumpWidget(
204+
_buildTestable(
205+
messages: [root],
206+
pendingThreadReplies: {'thread-root': query.future},
207+
home: ThreadDetailPage(
208+
threadHead: provisional.first,
209+
allMessages: provisional,
210+
channelId: _channelId,
211+
currentPubkey: 'self',
212+
isMember: true,
213+
isArchived: false,
214+
),
215+
),
216+
);
217+
await tester.pumpAndSettle();
218+
final semantics = tester.ensureSemantics();
219+
await tester.pump();
220+
if (useSemantics) {
221+
final node = tester.getSemantics(
222+
find.byKey(const ValueKey('thread-head-scroll-semantics')),
223+
);
224+
node.owner!.performAction(node.id, SemanticsAction.scrollDown);
225+
} else {
226+
await tester.sendEventToBinding(
227+
PointerScrollEvent(
228+
position: tester.getCenter(
229+
find.text('Original message').hitTestable(),
230+
),
231+
scrollDelta: const Offset(0, 50),
232+
),
233+
);
234+
}
235+
await tester.pumpAndSettle();
236+
expect(find.text('Reply 59').hitTestable(), findsNothing);
237+
final list = tester.widget<ScrollablePositionedList>(
238+
find.byKey(const ValueKey('thread-message-list')),
239+
);
240+
final visibleReply = list.itemPositionsNotifier!.itemPositions.value
241+
.firstWhere(
242+
(item) =>
243+
item.index > 0 &&
244+
item.itemLeadingEdge > 0 &&
245+
item.itemTrailingEdge < 1,
246+
);
247+
final anchor = find.byKey(
248+
ValueKey('thread-message-group-reply-${visibleReply.index - 1}'),
249+
);
250+
final top = tester.getTopLeft(anchor).dy;
251+
query.complete(replies);
252+
await tester.pumpAndSettle();
253+
expect(anchor, findsOneWidget);
254+
expect(tester.getTopLeft(anchor).dy, closeTo(top, 0.5));
255+
tester.view.physicalSize = const Size(400, 720);
256+
await tester.pumpAndSettle();
257+
expect(tester.getTopLeft(anchor).dy, closeTo(top, 0.5));
258+
semantics.dispose();
259+
},
260+
);
261+
}
262+
176263
testWidgets('tall original message remains scrollable after hydration', (
177264
tester,
178265
) async {

0 commit comments

Comments
 (0)