mirror of
https://gitlab.linphone.org/BC/public/linphone-android.git
synced 2026-01-17 11:28:06 +00:00
Fixed chat rooms list not re-ordering properly
This commit is contained in:
parent
d480840353
commit
ee15b00533
2 changed files with 42 additions and 14 deletions
|
|
@ -31,6 +31,7 @@ import androidx.lifecycle.ViewModelProvider
|
|||
import androidx.navigation.findNavController
|
||||
import androidx.navigation.fragment.findNavController
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import org.linphone.R
|
||||
import org.linphone.core.tools.Log
|
||||
import org.linphone.databinding.ChatListFragmentBinding
|
||||
|
|
@ -53,6 +54,23 @@ class ConversationsListFragment : AbstractTopBarFragment() {
|
|||
|
||||
private lateinit var adapter: ConversationsListAdapter
|
||||
|
||||
private val dataObserver = object : RecyclerView.AdapterDataObserver() {
|
||||
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
|
||||
Log.i("$TAG [$itemCount] added, scrolling to top")
|
||||
binding.conversationsList.scrollToPosition(0)
|
||||
}
|
||||
|
||||
override fun onItemRangeMoved(fromPosition: Int, toPosition: Int, itemCount: Int) {
|
||||
Log.i("$TAG [$itemCount] moved, scrolling to top")
|
||||
binding.conversationsList.scrollToPosition(0)
|
||||
}
|
||||
|
||||
override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) {
|
||||
Log.i("$TAG [$itemCount] removed, scrolling to top")
|
||||
binding.conversationsList.scrollToPosition(0)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDefaultAccountChanged() {
|
||||
Log.i(
|
||||
"$TAG Default account changed, updating avatar in top bar & re-computing conversations"
|
||||
|
|
@ -170,13 +188,6 @@ class ConversationsListFragment : AbstractTopBarFragment() {
|
|||
}
|
||||
}
|
||||
|
||||
listViewModel.chatRoomsReOrderedEvent.observe(viewLifecycleOwner) {
|
||||
it.consume {
|
||||
Log.i("$TAG Conversations list have been re-ordered, scrolling to top")
|
||||
binding.conversationsList.scrollToPosition(0)
|
||||
}
|
||||
}
|
||||
|
||||
sharedViewModel.showConversationEvent.observe(viewLifecycleOwner) {
|
||||
it.consume { pair ->
|
||||
val localSipUri = pair.first
|
||||
|
|
@ -244,4 +255,27 @@ class ConversationsListFragment : AbstractTopBarFragment() {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
|
||||
try {
|
||||
adapter.registerAdapterDataObserver(dataObserver)
|
||||
} catch (e: IllegalStateException) {
|
||||
Log.e("$TAG Failed to unregister data observer to adapter: $e")
|
||||
}
|
||||
|
||||
// Scroll to top when fragment is resumed
|
||||
binding.conversationsList.scrollToPosition(0)
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
super.onPause()
|
||||
|
||||
try {
|
||||
adapter.unregisterAdapterDataObserver(dataObserver)
|
||||
} catch (e: IllegalStateException) {
|
||||
Log.e("$TAG Failed to unregister data observer to adapter: $e")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,7 +33,6 @@ import org.linphone.core.tools.Log
|
|||
import org.linphone.ui.main.chat.model.ConversationModel
|
||||
import org.linphone.ui.main.model.isInSecureMode
|
||||
import org.linphone.ui.main.viewmodel.AbstractTopBarViewModel
|
||||
import org.linphone.utils.Event
|
||||
import org.linphone.utils.LinphoneUtils
|
||||
|
||||
class ConversationsListViewModel @UiThread constructor() : AbstractTopBarViewModel() {
|
||||
|
|
@ -45,10 +44,6 @@ class ConversationsListViewModel @UiThread constructor() : AbstractTopBarViewMod
|
|||
|
||||
val fetchInProgress = MutableLiveData<Boolean>()
|
||||
|
||||
val chatRoomsReOrderedEvent: MutableLiveData<Event<Boolean>> by lazy {
|
||||
MutableLiveData<Event<Boolean>>()
|
||||
}
|
||||
|
||||
private val coreListener = object : CoreListenerStub() {
|
||||
@WorkerThread
|
||||
override fun onChatRoomStateChanged(
|
||||
|
|
@ -172,9 +167,8 @@ class ConversationsListViewModel @UiThread constructor() : AbstractTopBarViewMod
|
|||
val sortedList = arrayListOf<ConversationModel>()
|
||||
sortedList.addAll(conversations.value.orEmpty())
|
||||
sortedList.sortByDescending {
|
||||
it.lastUpdateTime.value
|
||||
it.chatRoom.lastUpdateTime
|
||||
}
|
||||
conversations.postValue(sortedList)
|
||||
chatRoomsReOrderedEvent.postValue(Event(true))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue