Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] TW-1399: counter update on scroll #1619

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 60 additions & 24 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 = '';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why you delete it?
It's related to display unread message

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not really deleted it: this variable was only used in _findUnreadReceivedMessages which I had to slightly modify:

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 (fullyRead != '' && fullyRead.isNotEmpty) {
final lastIndexReadEvent = events.indexWhere(
(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 <Event>[];
return unreadEvents;
}
} else {
return <Event>[];
}
return <Event>[];
}


String? unreadReceivedMessageLocation;

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

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

final ValueNotifier<bool> isScrollingForward = ValueNotifier(false);

double _lastScrollOffset = 0;

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

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

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

final lastUnread = unreadEvents.isEmpty ? null : unreadEvents.last.eventId;
Logs().d(
"Chat::getFirstUnreadEvent(): Last unread event ${lastUnread}",
);

return lastUnread;
}

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 @@ -346,6 +358,13 @@ class ChatController extends State<Chat>
return;
}
if (!scrollController.hasClients) return;

if (_lastScrollOffset == 0) _lastScrollOffset = scrollController.offset;

isScrollingForward.value = scrollController.position.userScrollDirection ==
ScrollDirection.forward ||
_lastScrollOffset < scrollController.offset;

if (timeline?.allowNewEvent == false ||
scrollController.position.pixels > 0) {
showScrollDownButtonNotifier.value = true;
Expand Down Expand Up @@ -397,32 +416,26 @@ class ChatController extends State<Chat>
}

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

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 @@ -460,7 +473,9 @@ class ChatController extends State<Chat>
// ignore: unawaited_futures
_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 @@ -836,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 @@ -914,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 @@ -1657,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 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
Loading