Skip to content

Commit

Permalink
TW-1399: counter update on scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
Te-Z committed Mar 21, 2024
1 parent 6608983 commit 85d942e
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 128 deletions.
70 changes: 53 additions & 17 deletions lib/pages/chat/chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:fluffychat/utils/extension/basic_event_extension.dart';
import 'package:fluffychat/utils/extension/event_status_custom_extension.dart';
import 'package:fluffychat/utils/matrix_sdk_extensions/filtered_timeline_extension.dart';
import 'package:fluffychat/widgets/mixins/twake_context_menu_mixin.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_typeahead/flutter_typeahead.dart';
import 'package:fluffychat/utils/extension/global_key_extension.dart';
import 'package:universal_html/html.dart' as html;
Expand Down Expand Up @@ -139,8 +140,6 @@ class ChatController extends State<Chat>

Timer? _timestampTimer;

String _markerReadLocation = '';

String? unreadReceivedMessageLocation;

List<MatrixFile?>? get shareFiles => widget.shareFiles;
Expand Down Expand Up @@ -178,6 +177,9 @@ class ChatController extends State<Chat>

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

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

final ValueNotifier<ViewEventListUIState> openingChatViewStateNotifier =
ValueNotifier(ViewEventListInitial());

Expand Down Expand Up @@ -258,27 +260,35 @@ class ChatController extends State<Chat>
(e) => e.eventId == event.eventId,
);

String? _findUnreadReceivedMessageLocation() {
String? _findUnreadReceivedMessageId(String fullyRead) {
final unreadEvents = findUnreadReceivedMessages(fullyRead);

Logs().d(
"Chat::getFirstUnreadEvent(): Last unread event ${unreadEvents.last}",
);

return unreadEvents.isEmpty ? null : unreadEvents.last.eventId;
}

List<Event> findUnreadReceivedMessages(String fullyRead) {
final events = timeline!.events;
if (_markerReadLocation != '' && _markerReadLocation.isNotEmpty) {
if (fullyRead != '' && fullyRead.isNotEmpty) {
final lastIndexReadEvent = events.indexWhere(
(event) => event.eventId == _markerReadLocation,
(event) => event.eventId == fullyRead,
);
if (lastIndexReadEvent > 0) {
final afterFullyRead = events.getRange(0, lastIndexReadEvent);
final unreadEvents = afterFullyRead
.where((event) => event.senderId != client.userID)
.toList();
if (unreadEvents.isEmpty) return null;
Logs().d(
"Chat::getFirstUnreadEvent(): Last unread event ${unreadEvents.last}",
);
return unreadEvents.last.eventId;
if (unreadEvents.isEmpty) return <Event>[];

return unreadEvents;
}
} else {
return null;
return <Event>[];
}
return null;
return <Event>[];
}

void recreateChat() async {
Expand Down Expand Up @@ -345,6 +355,12 @@ class ChatController extends State<Chat>
if (!mounted) {
return;
}

if (lastScrollDirection.value !=
scrollController.position.userScrollDirection) {
lastScrollDirection.value = scrollController.position.userScrollDirection;
}

if (!scrollController.hasClients) return;
if (timeline?.allowNewEvent == false ||
scrollController.position.pixels > 0) {
Expand Down Expand Up @@ -397,8 +413,7 @@ class ChatController extends State<Chat>
}

void _initUnreadLocation(String fullyRead) {
_markerReadLocation = fullyRead;
unreadReceivedMessageLocation = _findUnreadReceivedMessageLocation();
unreadReceivedMessageLocation = _findUnreadReceivedMessageId(fullyRead);
scrollToEventId(fullyRead);
}

Expand Down Expand Up @@ -458,9 +473,13 @@ class ChatController extends State<Chat>

Logs().d('Set read marker...', eventId);
// ignore: unawaited_futures
_setReadMarkerFuture = timeline.setReadMarker(eventId: eventId).then((_) {
_setReadMarkerFuture = null;
});
if (eventId != null) {
_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 @@ -1768,6 +1787,23 @@ class ChatController extends State<Chat>
}
}

void updateReceipt({
required int index,
required Event event,
}) {
final fullyRead = room?.fullyRead;
if (fullyRead == null) return;
final unreadMessagesIds = findUnreadReceivedMessages(fullyRead)
.map(
(e) => e.eventId,
)
.toList();

if (roomId != null && unreadMessagesIds.contains(event.eventId)) {
setReadMarker(eventId: event.eventId);
}
}

@override
void initState() {
_initializePinnedEvents();
Expand Down
Loading

0 comments on commit 85d942e

Please sign in to comment.