From 6acc6b6bb008809952a68365a9eafd4ff232131a Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Thu, 30 Jun 2022 16:31:39 +0200 Subject: [PATCH] Keep scroll position in chat room when going into a sub view or upon rotation, improved how messages are marked as read --- CHANGELOG.md | 1 + .../chat/fragments/DetailChatRoomFragment.kt | 92 ++++++++++++------- 2 files changed, 61 insertions(+), 32 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1eb0d422e..e7470cc18 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,7 @@ Group changes to describe their impact on the project, as follows: - "Blinking" in some views when presence is being received - Trying to keep the preferred driver (OpenSLES / AAudio) when switching device - Issues when storing presence in native contacts + potentially duplicated SIP addresses in contact details +- Chat room scroll position lost when going into sub-view ## [4.6.11] - 2022-06-27 diff --git a/app/src/main/java/org/linphone/activities/main/chat/fragments/DetailChatRoomFragment.kt b/app/src/main/java/org/linphone/activities/main/chat/fragments/DetailChatRoomFragment.kt index e7fac8f02..4fc5fd9e5 100644 --- a/app/src/main/java/org/linphone/activities/main/chat/fragments/DetailChatRoomFragment.kt +++ b/app/src/main/java/org/linphone/activities/main/chat/fragments/DetailChatRoomFragment.kt @@ -86,24 +86,22 @@ class DetailChatRoomFragment : MasterFragment 0) { + Log.i("[Chat Room] Messages have been displayed, scrolling to first unread") + val notAllMessagesDisplayed = scrollToFirstUnreadMessageOrBottom(false) + if (notAllMessagesDisplayed) { + Log.w("[Chat Room] More unread messages than the screen can display, do not mark chat room as read now, wait for user to scroll to bottom") + } else { + Log.i("[Chat Room] Marking chat room as read") + viewModel.chatRoom.markAsRead() + } + } + } else { + Log.e("[Chat Room] Binding not available in onGlobalLayout callback!") + } + } + } + ) } else { Log.e("[Chat Room] Fragment resuming but viewModel lateinit property isn't initialized!") } } override fun onPause() { + if (::chatScrollListener.isInitialized) { + binding.chatMessagesList.removeOnScrollListener(chatScrollListener) + } + + if (_adapter != null) { + try { + adapter.unregisterAdapterDataObserver(observer) + } catch (ise: IllegalStateException) {} + } + // Conversation isn't visible anymore, any new message received in it will trigger a notification coreContext.notificationsManager.currentlyDisplayedChatRoomAddress = null @@ -1004,7 +1030,7 @@ class DetailChatRoomFragment : MasterFragment 0) { // Scroll to first unread message if any val firstUnreadMessagePosition = adapter.getFirstUnreadMessagePosition() @@ -1020,7 +1046,9 @@ class DetailChatRoomFragment : MasterFragment