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

TW-1974: Fix cannot jump to reply #1995

Merged
merged 3 commits into from
Sep 4, 2024
Merged
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
48 changes: 36 additions & 12 deletions lib/pages/chat/chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import 'package:fluffychat/widgets/mixins/popup_menu_widget_style.dart';
import 'package:fluffychat/widgets/mixins/twake_context_menu_mixin.dart';
import 'package:flutter_typeahead/flutter_typeahead.dart';
import 'package:fluffychat/utils/extension/global_key_extension.dart';
import 'package:inview_notifier_list/inview_notifier_list.dart';
import 'package:universal_html/html.dart' as html;

import 'package:adaptive_dialog/adaptive_dialog.dart';
Expand Down Expand Up @@ -388,11 +387,20 @@ class ChatController extends State<Chat>
if (scrollController.position.pixels == 0 ||
scrollController.position.pixels == _isPortionAvailableToScroll) {
requestFuture();
} else if (scrollController.position.pixels ==
}

_handleRequestHistory();
}

void _handleRequestHistory() {
if (scrollController.position.pixels ==
scrollController.position.maxScrollExtent ||
scrollController.position.pixels + _isPortionAvailableToScroll ==
scrollController.position.maxScrollExtent) {
await requestHistory();
if (timeline?.isRequestingHistory == true) return;
if (timeline?.canRequestHistory == true) {
requestHistory();
}
}
}

Expand Down Expand Up @@ -934,6 +942,7 @@ class ChatController extends State<Chat>
if (timeline == null) return;
if (!timeline!.allowNewEvent) {
setState(() {
timeline = null;
loadTimelineFuture = _getTimeline().onError(
(e, s) {
Logs().e('Chat::scrollDown(): Unable to load timeline', e, s);
Expand All @@ -954,26 +963,42 @@ class ChatController extends State<Chat>
return eventIndex + addedHeadItemsInChat;
}

int _getEventIndex(String eventId) {
final foundEvent =
timeline!.events.firstWhereOrNull((event) => event.eventId == eventId);

final eventIndex = foundEvent == null
? -1
: timeline!.events.indexWhere(
(event) => event.eventId == foundEvent.eventId,
);

return eventIndex;
}

Future<void> scrollToEventId(String eventId, {bool highlight = true}) async {
final eventIndex = timeline!.events.indexWhere((e) => e.eventId == eventId);
final eventIndex = _getEventIndex(eventId);
if (eventIndex == -1) {
loadTimelineFuture = _getTimeline(eventContextId: eventId).onError(
(e, s) {
Logs().e('Chat::scrollToEventId(): Unable to load timeline', e, s);
},
);
setState(() {
timeline = null;
loadTimelineFuture = _getTimeline(eventContextId: eventId).onError(
(e, s) {
Logs().e('Chat::scrollToEventId(): Unable to load timeline', e, s);
},
);
});
await loadTimelineFuture;
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
scrollToEventId(eventId, highlight: highlight);
});
setState(() {});
return;
}

await scrollToIndex(getDisplayEventIndex(eventIndex), highlight: highlight);
_updateScrollController();
}

Future scrollToIndex(int index, {bool highlight = false}) async {
Future scrollToIndex(int index, {bool highlight = true}) async {
await scrollController.scrollToIndex(
index,
preferPosition: AutoScrollPosition.middle,
Expand Down Expand Up @@ -2001,7 +2026,6 @@ class ChatController extends State<Chat>
pinnedMessageScrollController.dispose();
onUpdateEventStreamSubcription?.cancel();
keyboardVisibilitySubscription?.cancel();
InViewNotifierListCustom.of(context)?.dispose();
hoangdat marked this conversation as resolved.
Show resolved Hide resolved
replyEventNotifier.dispose();
cachedPresenceStreamController.close();
cachedPresenceNotifier.dispose();
Expand Down
5 changes: 4 additions & 1 deletion lib/pages/chat/chat_event_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,10 @@ class ChatEventList extends StatelessWidget {
onSelect: controller.onSelectMessage,
selectMode: controller.selectMode,
scrollToEventId: (String eventId) =>
controller.scrollToEventId(eventId),
controller.scrollToEventId(
eventId,
highlight: true,
),
selected: controller.selectedEvents
.any((e) => e.eventId == event.eventId),
timeline: controller.timeline!,
Expand Down
18 changes: 12 additions & 6 deletions lib/pages/chat/chat_pinned_events/pinned_events_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,27 @@ class PinnedEventsController {
void Function(String)? scrollToEventId,
}) async {
final nextIndex = _nextIndexOfPinnedMessage(pinnedEvents);
final event = pinnedEvents[nextIndex];
final nextEvent = pinnedEvents[nextIndex];
final currentEvent = currentPinnedEventNotifier.value;
Logs().d(
"PinnedEventsController()::jumpToPinnedMessage(): eventID: ${event?.eventId}",
"PinnedEventsController()::jumpToPinnedMessage(): eventID: ${nextEvent?.eventId}",
);
if (event != null) {
currentPinnedEventNotifier.value = event;
if (currentEvent != null) {
currentPinnedEventNotifier.value = nextEvent;
if (scrollToEventId != null) {
scrollToEventId.call(event.eventId);
scrollToEventId.call(currentEvent.eventId);
}
}
}

int currentIndexOfPinnedMessage(List<Event?> pinnedEvents) {
final index = pinnedEvents.indexWhere(
(event) => event?.eventId == currentPinnedEventNotifier.value?.eventId,
(event) {
Logs().d(
"PinnedEventsController()::currentIndexOfPinnedMessage(): ${currentPinnedEventNotifier.value?.eventId}",
);
return event?.eventId == currentPinnedEventNotifier.value?.eventId;
},
);
if (index < 0) {
currentPinnedEventNotifier.value = pinnedEvents.first;
Expand Down