From d74bd7c2622822fc4877fa24ea7a1912f3fbb30c Mon Sep 17 00:00:00 2001 From: Terence Zafindratafa Date: Mon, 25 Mar 2024 06:00:42 +0100 Subject: [PATCH] fixup! TW-1399: counter update on scroll --- lib/pages/chat/chat.dart | 52 ++++++++++++++--------------- lib/pages/chat/chat_event_list.dart | 11 ++++-- 2 files changed, 34 insertions(+), 29 deletions(-) diff --git a/lib/pages/chat/chat.dart b/lib/pages/chat/chat.dart index 245b37154f..fee3c255a9 100644 --- a/lib/pages/chat/chat.dart +++ b/lib/pages/chat/chat.dart @@ -177,8 +177,9 @@ class ChatController extends State final ValueNotifier stickyTimestampNotifier = ValueNotifier(null); - final ValueNotifier lastScrollDirection = - ValueNotifier(ScrollDirection.idle); + final ValueNotifier isScrollingForward = ValueNotifier(false); + + double _lastScrollOffset = 0; final ValueNotifier openingChatViewStateNotifier = ValueNotifier(ViewEventListInitial()); @@ -263,11 +264,12 @@ class ChatController extends State String? _findUnreadReceivedMessageId(String fullyRead) { final unreadEvents = findUnreadReceivedMessages(fullyRead); + final lastUnread = unreadEvents.isEmpty ? null : unreadEvents.last.eventId; Logs().d( - "Chat::getFirstUnreadEvent(): Last unread event ${unreadEvents.last}", + "Chat::getFirstUnreadEvent(): Last unread event ${lastUnread}", ); - - return unreadEvents.isEmpty ? null : unreadEvents.last.eventId; + + return lastUnread; } List findUnreadReceivedMessages(String fullyRead) { @@ -355,13 +357,14 @@ class ChatController extends State if (!mounted) { return; } + if (!scrollController.hasClients) return; - if (lastScrollDirection.value != - scrollController.position.userScrollDirection) { - lastScrollDirection.value = scrollController.position.userScrollDirection; - } + if (_lastScrollOffset == 0) _lastScrollOffset = scrollController.offset; + + isScrollingForward.value = scrollController.position.userScrollDirection == + ScrollDirection.forward || + _lastScrollOffset < scrollController.offset; - if (!scrollController.hasClients) return; if (timeline?.allowNewEvent == false || scrollController.position.pixels > 0) { showScrollDownButtonNotifier.value = true; @@ -419,17 +422,12 @@ class ChatController extends State void _tryLoadTimeline() async { _updateOpeningChatViewStateNotifier(ViewEventListLoading()); - loadTimelineFuture = _getTimeline( - onJumpToMessage: (event) { - scrollToEventId(event); - }, - ); + loadTimelineFuture = _getTimeline(); try { await loadTimelineFuture; await _tryRequestHistory(); final fullyRead = room?.fullyRead; if (fullyRead == null || fullyRead.isEmpty || fullyRead == '') { - setReadMarker(); return; } if (room?.hasNewMessages == true) { @@ -437,7 +435,7 @@ class ChatController extends State } if (timeline != null && timeline!.events.any((event) => event.eventId == fullyRead)) { - setReadMarker(); + setReadMarker(eventId: fullyRead); return; } if (!mounted) return; @@ -473,12 +471,10 @@ class ChatController extends State Logs().d('Set read marker...', eventId); // ignore: unawaited_futures - if (eventId != null) { - _setReadMarkerFuture = timeline.setReadMarker(eventId: eventId).then((_) { - _setReadMarkerFuture = null; - setState(() {}); - }); - } + _setReadMarkerFuture = timeline.setReadMarker(eventId: eventId).then((_) { + _setReadMarkerFuture = null; + setState(() {}); + }); if (eventId == null || eventId == timeline.room.lastEvent?.eventId) { Matrix.of(context).backgroundPush?.cancelNotification(roomId!); @@ -855,7 +851,6 @@ class ChatController extends State Logs().v('Chat::requestFuture(): Requesting future...'); try { await timeline.requestFuture(historyCount: _loadHistoryCount); - setReadMarker(eventId: timeline.events.first.eventId); } catch (err) { ScaffoldMessenger.of(context).showSnackBar( SnackBar( @@ -933,6 +928,7 @@ class ChatController extends State Future scrollToEventId(String eventId, {bool highlight = false}) async { final eventIndex = timeline!.events.indexWhere((e) => e.eventId == eventId); + if (eventIndex == -1) { timeline = null; loadTimelineFuture = _getTimeline(eventContextId: eventId).onError( @@ -1676,9 +1672,13 @@ class ChatController extends State _defaultEventCountDisplay; if (allMembershipEvents || canRequestHistory) { + final notificationCount = room?.notificationCount ?? 0; + final historyCount = notificationCount > _defaultEventCountDisplay + ? notificationCount + : _defaultEventCountDisplay; + try { - await requestHistory(historyCount: _defaultEventCountDisplay) - .then((response) { + await requestHistory(historyCount: historyCount).then((response) { Logs().d( 'Chat::_tryRequestHistory():: Try request history success', ); diff --git a/lib/pages/chat/chat_event_list.dart b/lib/pages/chat/chat_event_list.dart index 48dac543e4..2cfa359e8d 100644 --- a/lib/pages/chat/chat_event_list.dart +++ b/lib/pages/chat/chat_event_list.dart @@ -27,6 +27,8 @@ class ChatEventList extends StatelessWidget { required this.controller, }) : super(key: key); + static const _visiblePercentage = 80; + @override Widget build(BuildContext context) { final horizontalPadding = TwakeThemes.isColumnMode(context) ? 8.0 : 0.0; @@ -86,7 +88,7 @@ class ChatEventList extends StatelessWidget { chatController: controller, focusNode: controller.selectionFocusNode, child: ValueListenableBuilder( - valueListenable: controller.lastScrollDirection, + valueListenable: controller.isScrollingForward, builder: (context, lastScrollDirection, _) => InViewNotifierListCustom( isInViewPortCondition: controller.isInViewPortCondition, @@ -161,8 +163,11 @@ class ChatEventList extends StatelessWidget { final visiblePercentage = visibilityInfo.visibleFraction * 100; - if (lastScrollDirection == ScrollDirection.forward && - visiblePercentage == 100) { + final scrollCondition = + lastScrollDirection || previousEvent == null; + + if (scrollCondition && + visiblePercentage >= _visiblePercentage) { controller.updateReceipt( event: event, index: index,