Skip to content

Commit

Permalink
fixup! TW-1399: counter update on scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
Te-Z committed Mar 25, 2024
1 parent 85d942e commit d74bd7c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 29 deletions.
52 changes: 26 additions & 26 deletions lib/pages/chat/chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,9 @@ class ChatController extends State<Chat>

final ValueNotifier<DateTime?> stickyTimestampNotifier = ValueNotifier(null);

final ValueNotifier<ScrollDirection> lastScrollDirection =
ValueNotifier(ScrollDirection.idle);
final ValueNotifier<bool> isScrollingForward = ValueNotifier(false);

double _lastScrollOffset = 0;

final ValueNotifier<ViewEventListUIState> openingChatViewStateNotifier =
ValueNotifier(ViewEventListInitial());
Expand Down Expand Up @@ -263,11 +264,12 @@ class ChatController extends State<Chat>
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<Event> findUnreadReceivedMessages(String fullyRead) {
Expand Down Expand Up @@ -355,13 +357,14 @@ class ChatController extends State<Chat>
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;
Expand Down Expand Up @@ -419,25 +422,20 @@ class ChatController extends State<Chat>

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) {
_initUnreadLocation(fullyRead);
}
if (timeline != null &&
timeline!.events.any((event) => event.eventId == fullyRead)) {
setReadMarker();
setReadMarker(eventId: fullyRead);
return;
}
if (!mounted) return;
Expand Down Expand Up @@ -473,12 +471,10 @@ class ChatController extends State<Chat>

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!);
Expand Down Expand Up @@ -855,7 +851,6 @@ class ChatController extends State<Chat>
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(
Expand Down Expand Up @@ -933,6 +928,7 @@ class ChatController extends State<Chat>

Future<void> 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(
Expand Down Expand Up @@ -1676,9 +1672,13 @@ class ChatController extends State<Chat>
_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',
);
Expand Down
11 changes: 8 additions & 3 deletions lib/pages/chat/chat_event_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit d74bd7c

Please sign in to comment.