Latest message scroll issue

This commit is contained in:
adarshs
2022-04-05 15:36:25 +05:30
parent 499dc9723c
commit d4fc32bcb5
2 changed files with 21 additions and 11 deletions

View File

@@ -9,6 +9,7 @@ package com.navi.chat.ui.fragments
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.View.VISIBLE
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Lifecycle
@@ -54,6 +55,7 @@ import com.navi.naviwidgets.utils.NAVI_CHAT_MESSAGE_WITH_ACTION_ITEM_SELECTED
import com.navi.naviwidgets.viewholder.NaviChatViewHolderFactoryImpl
import dagger.hilt.android.EntryPointAccessors
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import timber.log.Timber
@@ -172,9 +174,7 @@ class NaviChatFragment : ChatBaseFragment(), WidgetCallback, MessageOperation, T
chatPagingRVAdapter.registerAdapterDataObserver(object :
RecyclerView.AdapterDataObserver() {
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
viewLifecycleOwner.lifecycleScope.launch(Dispatchers.Main) {
scrollChatToLatestMessageReceived()
}
scrollChatToLatestMessageReceived()
}
override fun onItemRangeChanged(positionStart: Int, itemCount: Int) {
@@ -247,6 +247,7 @@ class NaviChatFragment : ChatBaseFragment(), WidgetCallback, MessageOperation, T
writePath = naviChatInitiateResponse.writePath.orEmpty()
)
)
binding.rvChat.visibility = VISIBLE
}
naviChatSharedViewModel.ratingMetaData.observeNonNull(viewLifecycleOwner) { metadata ->
@@ -292,13 +293,21 @@ class NaviChatFragment : ChatBaseFragment(), WidgetCallback, MessageOperation, T
@ExperimentalPagingApi
private fun scrollChatToLatestMessageReceived() {
if (naviChatViewModel.getIsScrollReachedEnd().not() ||
naviChatViewModel.getFirstCompletelyVisibleMessagePosition() in 0..1
naviChatViewModel.getFirstCompletelyVisibleMessagePosition() == 0
) {
linearLayoutManager.smoothScrollToPosition(
binding.rvChat,
null,
0
)
viewLifecycleOwner.lifecycleScope.launch(Dispatchers.Main) {
linearLayoutManager.smoothScrollToPosition(
binding.rvChat,
null,
0
)
viewLifecycleOwner.lifecycleScope.launch(Dispatchers.IO) {
delay(DELAY_TO_SCROLL)
viewLifecycleOwner.lifecycleScope.launch(Dispatchers.Main) {
binding.rvChat.smoothScrollToPosition(0)
}
}
}
}
}
@@ -366,5 +375,6 @@ class NaviChatFragment : ChatBaseFragment(), WidgetCallback, MessageOperation, T
companion object {
const val TAG = "NaviChatFragment"
private const val DELAY_TO_SCROLL = 100L
}
}

View File

@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
<?xml version="1.0" encoding="utf-8"?><!--
~ /*
~ * Copyright © 2022 by Navi Technologies Private Limited
~ * All rights reserved. Strictly confidential
@@ -25,6 +24,7 @@
android:id="@+id/rvChat"
android:layout_width="match_parent"
android:layout_height="@dimen/layout_dp_0"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"