Performance improvements for chat

This commit is contained in:
Sylvain Berfini 2023-11-07 12:18:39 +01:00
parent fa78f7b9b3
commit 1fbad779af
14 changed files with 187 additions and 56 deletions

View file

@ -36,9 +36,9 @@ import org.linphone.ui.main.chat.model.EventLogModel
import org.linphone.ui.main.chat.model.EventModel
import org.linphone.utils.Event
class ConversationEventAdapter(
private val viewLifecycleOwner: LifecycleOwner
) : ListAdapter<EventLogModel, RecyclerView.ViewHolder>(EventLogDiffCallback()) {
class ConversationEventAdapter : ListAdapter<EventLogModel, RecyclerView.ViewHolder>(
EventLogDiffCallback()
) {
companion object {
const val INCOMING_CHAT_MESSAGE = 1
const val OUTGOING_CHAT_MESSAGE = 2
@ -57,6 +57,8 @@ class ConversationEventAdapter(
MutableLiveData<Event<ChatMessageModel>>()
}
lateinit var viewLifecycleOwner: LifecycleOwner
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return when (viewType) {
INCOMING_CHAT_MESSAGE -> createIncomingChatBubble(parent)

View file

@ -0,0 +1,57 @@
package org.linphone.ui.main.chat.adapter
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.annotation.UiThread
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import org.linphone.R
import org.linphone.databinding.ChatParticipantListCellBinding
import org.linphone.ui.main.chat.model.ParticipantModel
class ConversationParticipantsAdapter(
private val viewLifecycleOwner: LifecycleOwner
) : ListAdapter<ParticipantModel, RecyclerView.ViewHolder>(ChatRoomParticipantDiffCallback()) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val binding: ChatParticipantListCellBinding = DataBindingUtil.inflate(
LayoutInflater.from(parent.context),
R.layout.chat_participant_list_cell,
parent,
false
)
return ViewHolder(binding)
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
(holder as ViewHolder).bind(getItem(position))
}
inner class ViewHolder(
val binding: ChatParticipantListCellBinding
) : RecyclerView.ViewHolder(binding.root) {
@UiThread
fun bind(participantModel: ParticipantModel) {
with(binding) {
model = participantModel
lifecycleOwner = viewLifecycleOwner
executePendingBindings()
}
}
}
private class ChatRoomParticipantDiffCallback : DiffUtil.ItemCallback<ParticipantModel>() {
override fun areItemsTheSame(oldItem: ParticipantModel, newItem: ParticipantModel): Boolean {
return oldItem.sipUri == newItem.sipUri
}
override fun areContentsTheSame(oldItem: ParticipantModel, newItem: ParticipantModel): Boolean {
return oldItem.avatarModel.id == newItem.avatarModel.id
}
}
}

View file

@ -62,6 +62,7 @@ import org.linphone.ui.main.chat.model.ChatMessageDeliveryModel
import org.linphone.ui.main.chat.model.ChatMessageModel
import org.linphone.ui.main.chat.model.ChatMessageReactionsModel
import org.linphone.ui.main.chat.viewmodel.ConversationViewModel
import org.linphone.ui.main.chat.viewmodel.ConversationViewModel.Companion.SCROLLING_POSITION_NOT_SET
import org.linphone.ui.main.fragment.GenericFragment
import org.linphone.utils.AppUtils
import org.linphone.utils.Event
@ -80,12 +81,12 @@ class ConversationFragment : GenericFragment() {
private lateinit var viewModel: ConversationViewModel
private val args: ConversationFragmentArgs by navArgs()
private lateinit var adapter: ConversationEventAdapter
private lateinit var bottomSheetAdapter: ChatMessageBottomSheetAdapter
private val args: ConversationFragmentArgs by navArgs()
private val pickMedia = registerForActivityResult(
ActivityResultContracts.PickMultipleVisualMedia()
) { list ->
@ -98,6 +99,12 @@ class ConversationFragment : GenericFragment() {
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
adapter = ConversationEventAdapter()
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
@ -138,19 +145,12 @@ class ConversationFragment : GenericFragment() {
Log.i(
"$TAG Looking up for conversation with local SIP URI [$localSipUri] and remote SIP URI [$remoteSipUri]"
)
viewModel.findChatRoom(localSipUri, remoteSipUri)
val chatRoom = sharedViewModel.displayedChatRoom
viewModel.findChatRoom(chatRoom, localSipUri, remoteSipUri)
viewModel.chatRoomFoundEvent.observe(viewLifecycleOwner) {
it.consume { found ->
if (found) {
Log.i(
"$TAG Found matching chat room for local SIP URI [$localSipUri] and remote SIP URI [$remoteSipUri]"
)
(view.parent as? ViewGroup)?.doOnPreDraw {
startPostponedEnterTransition()
sharedViewModel.openSlidingPaneEvent.value = Event(true)
}
} else {
if (!found) {
(view.parent as? ViewGroup)?.doOnPreDraw {
Log.e("$TAG Failed to find chat room, going back")
goBack()
@ -160,9 +160,8 @@ class ConversationFragment : GenericFragment() {
}
}
adapter = ConversationEventAdapter(viewLifecycleOwner)
adapter.viewLifecycleOwner = viewLifecycleOwner
binding.eventsList.setHasFixedSize(true)
binding.eventsList.adapter = adapter
binding.eventsList.layoutManager = LinearLayoutManager(requireContext())
bottomSheetAdapter = ChatMessageBottomSheetAdapter(viewLifecycleOwner)
@ -172,17 +171,20 @@ class ConversationFragment : GenericFragment() {
val bottomSheetLayoutManager = LinearLayoutManager(requireContext())
binding.messageBottomSheet.bottomSheetList.layoutManager = bottomSheetLayoutManager
adapter.chatMessageLongPressEvent.observe(viewLifecycleOwner) {
it.consume { model ->
showChatMessageLongPressMenu(model)
}
}
viewModel.events.observe(viewLifecycleOwner) { items ->
val currentCount = adapter.itemCount
adapter.submitList(items)
Log.i("$TAG Events (messages) list updated with [${items.size}] items")
if (binding.eventsList.adapter != adapter) {
binding.eventsList.adapter = adapter
}
(view.parent as? ViewGroup)?.doOnPreDraw {
startPostponedEnterTransition()
sharedViewModel.openSlidingPaneEvent.value = Event(true)
}
if (currentCount < items.size) {
binding.eventsList.scrollToPosition(items.size - 1)
}
@ -192,6 +194,12 @@ class ConversationFragment : GenericFragment() {
emojisBottomSheetBehavior.state = BottomSheetBehavior.STATE_COLLAPSED
emojisBottomSheetBehavior.isDraggable = false // To allow scrolling through the emojis
adapter.chatMessageLongPressEvent.observe(viewLifecycleOwner) {
it.consume { model ->
showChatMessageLongPressMenu(model)
}
}
adapter.showDeliveryForChatMessageModelEvent.observe(viewLifecycleOwner) {
it.consume { model ->
showDeliveryBottomSheetDialog(model, showDelivery = true)
@ -284,7 +292,7 @@ class ConversationFragment : GenericFragment() {
Log.i("$TAG Asking notifications manager not to notify chat messages for chat room [$id]")
coreContext.notificationsManager.setCurrentlyDisplayedChatRoomId(id)
if (viewModel.scrollingPosition != -1) {
if (viewModel.scrollingPosition != SCROLLING_POSITION_NOT_SET) {
binding.eventsList.scrollToPosition(viewModel.scrollingPosition)
}
}

View file

@ -31,10 +31,12 @@ import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import androidx.recyclerview.widget.LinearLayoutManager
import org.linphone.R
import org.linphone.core.tools.Log
import org.linphone.databinding.ChatInfoFragmentBinding
import org.linphone.databinding.ChatParticipantAdminPopupMenuBinding
import org.linphone.ui.main.chat.adapter.ConversationParticipantsAdapter
import org.linphone.ui.main.chat.model.ConversationEditSubjectDialogModel
import org.linphone.ui.main.chat.model.ParticipantModel
import org.linphone.ui.main.chat.viewmodel.ConversationInfoViewModel
@ -51,6 +53,8 @@ class ConversationInfoFragment : GenericFragment() {
private lateinit var viewModel: ConversationInfoViewModel
private lateinit var adapter: ConversationParticipantsAdapter
private val args: ConversationInfoFragmentArgs by navArgs()
override fun onCreateView(
@ -71,6 +75,7 @@ class ConversationInfoFragment : GenericFragment() {
isSlidingPaneChild = true
super.onViewCreated(view, savedInstanceState)
postponeEnterTransition()
binding.lifecycleOwner = viewLifecycleOwner
@ -82,7 +87,13 @@ class ConversationInfoFragment : GenericFragment() {
Log.i(
"$TAG Looking up for conversation with local SIP URI [$localSipUri] and remote SIP URI [$remoteSipUri]"
)
viewModel.findChatRoom(localSipUri, remoteSipUri)
val chatRoom = sharedViewModel.displayedChatRoom
viewModel.findChatRoom(chatRoom, localSipUri, remoteSipUri)
adapter = ConversationParticipantsAdapter(viewLifecycleOwner)
binding.participants.setHasFixedSize(true)
binding.participants.layoutManager = LinearLayoutManager(requireContext())
binding.participants.adapter = adapter
viewModel.chatRoomFoundEvent.observe(viewLifecycleOwner) {
it.consume { found ->
@ -90,9 +101,6 @@ class ConversationInfoFragment : GenericFragment() {
Log.i(
"$TAG Found matching chat room for local SIP URI [$localSipUri] and remote SIP URI [$remoteSipUri]"
)
(view.parent as? ViewGroup)?.doOnPreDraw {
startPostponedEnterTransition()
}
} else {
(view.parent as? ViewGroup)?.doOnPreDraw {
Log.e("$TAG Failed to find chat room, going back")
@ -102,6 +110,15 @@ class ConversationInfoFragment : GenericFragment() {
}
}
viewModel.participants.observe(viewLifecycleOwner) { items ->
adapter.submitList(items)
Log.i("$TAG Participants list updated with [${items.size}] items")
(view.parent as? ViewGroup)?.doOnPreDraw {
startPostponedEnterTransition()
}
}
viewModel.groupLeftEvent.observe(viewLifecycleOwner) {
it.consume {
// TODO: show toast ?

View file

@ -95,8 +95,10 @@ class ConversationsFragment : GenericFragment() {
viewLifecycleOwner
) {
it.consume {
Log.i("$TAG Closing sliding pane")
binding.slidingPaneLayout.closePane()
if (binding.slidingPaneLayout.isOpen) {
Log.i("$TAG Closing sliding pane")
binding.slidingPaneLayout.closePane()
}
}
}
@ -104,8 +106,10 @@ class ConversationsFragment : GenericFragment() {
viewLifecycleOwner
) {
it.consume {
Log.i("$TAG Opening sliding pane")
binding.slidingPaneLayout.openPane()
if (!binding.slidingPaneLayout.isOpen) {
Log.i("$TAG Opening sliding pane")
binding.slidingPaneLayout.openPane()
}
}
}

View file

@ -108,6 +108,7 @@ class ConversationsListFragment : AbstractTopBarFragment() {
adapter.conversationClickedEvent.observe(viewLifecycleOwner) {
it.consume { model ->
Log.i("$TAG Show conversation with ID [${model.id}]")
sharedViewModel.displayedChatRoom = model.chatRoom
sharedViewModel.showConversationEvent.value = Event(
Pair(model.localSipUri, model.remoteSipUri)
)

View file

@ -38,7 +38,7 @@ import org.linphone.utils.AppUtils
import org.linphone.utils.LinphoneUtils
import org.linphone.utils.TimestampUtils
class ConversationModel @WorkerThread constructor(private val chatRoom: ChatRoom) {
class ConversationModel @WorkerThread constructor(val chatRoom: ChatRoom) {
companion object {
private const val TAG = "[Conversation Model]"
}

View file

@ -124,15 +124,30 @@ class ConversationInfoViewModel @UiThread constructor() : ViewModel() {
}
@UiThread
fun findChatRoom(localSipUri: String, remoteSipUri: String) {
fun findChatRoom(room: ChatRoom?, localSipUri: String, remoteSipUri: String) {
coreContext.postOnCoreThread { core ->
Log.i(
"$TAG Looking for chat room with local SIP URI [$localSipUri] and remote SIP URI [$remoteSipUri]"
)
if (room != null && ::chatRoom.isInitialized && chatRoom == room) {
Log.i("$TAG Chat room object already in memory, skipping")
chatRoomFoundEvent.postValue(Event(true))
return@postOnCoreThread
}
if (room != null && (!::chatRoom.isInitialized || chatRoom != room)) {
Log.i("$TAG Chat room object available in sharedViewModel, using it")
chatRoom = room
chatRoom.addListener(chatRoomListener)
configureChatRoom()
chatRoomFoundEvent.postValue(Event(true))
return@postOnCoreThread
}
val localAddress = Factory.instance().createAddress(localSipUri)
val remoteAddress = Factory.instance().createAddress(remoteSipUri)
if (localAddress != null && remoteAddress != null) {
Log.i("$TAG Searching for chat room in Core using local & peer SIP addresses")
val found = core.searchChatRoom(
null,
localAddress,

View file

@ -45,6 +45,7 @@ class ConversationViewModel @UiThread constructor() : ViewModel() {
private const val TAG = "[Conversation ViewModel]"
const val MAX_TIME_TO_GROUP_MESSAGES = 60 // 1 minute
const val SCROLLING_POSITION_NOT_SET = -1
}
val showBackButton = MutableLiveData<Boolean>()
@ -75,7 +76,7 @@ class ConversationViewModel @UiThread constructor() : ViewModel() {
val isReplyingToMessage = MutableLiveData<String>()
var scrollingPosition: Int = -1
var scrollingPosition: Int = SCROLLING_POSITION_NOT_SET
val requestKeyboardHidingEvent: MutableLiveData<Event<Boolean>> by lazy {
MutableLiveData<Event<Boolean>>()
@ -91,7 +92,7 @@ class ConversationViewModel @UiThread constructor() : ViewModel() {
val chatRoomFoundEvent = MutableLiveData<Event<Boolean>>()
private lateinit var chatRoom: ChatRoom
lateinit var chatRoom: ChatRoom
private var chatMessageToReplyTo: ChatMessage? = null
@ -202,15 +203,30 @@ class ConversationViewModel @UiThread constructor() : ViewModel() {
}
@UiThread
fun findChatRoom(localSipUri: String, remoteSipUri: String) {
fun findChatRoom(room: ChatRoom?, localSipUri: String, remoteSipUri: String) {
coreContext.postOnCoreThread { core ->
Log.i(
"$TAG Looking for chat room with local SIP URI [$localSipUri] and remote SIP URI [$remoteSipUri]"
)
if (room != null && ::chatRoom.isInitialized && chatRoom == room) {
Log.i("$TAG Chat room object already in memory, skipping")
chatRoomFoundEvent.postValue(Event(true))
return@postOnCoreThread
}
if (room != null && (!::chatRoom.isInitialized || chatRoom != room)) {
Log.i("$TAG Chat room object available in sharedViewModel, using it")
chatRoom = room
chatRoom.addListener(chatRoomListener)
configureChatRoom()
chatRoomFoundEvent.postValue(Event(true))
return@postOnCoreThread
}
val localAddress = Factory.instance().createAddress(localSipUri)
val remoteAddress = Factory.instance().createAddress(remoteSipUri)
if (localAddress != null && remoteAddress != null) {
Log.i("$TAG Searching for chat room in Core using local & peer SIP addresses")
val found = core.searchChatRoom(
null,
localAddress,
@ -324,6 +340,7 @@ class ConversationViewModel @UiThread constructor() : ViewModel() {
@WorkerThread
private fun configureChatRoom() {
scrollingPosition = SCROLLING_POSITION_NOT_SET
computeComposingLabel()
val empty = chatRoom.hasCapability(ChatRoom.Capabilities.Conference.toInt()) && chatRoom.participants.isEmpty()
@ -408,11 +425,11 @@ class ConversationViewModel @UiThread constructor() : ViewModel() {
@WorkerThread
private fun getEventsListFromHistory(history: Array<EventLog>, filter: String = ""): ArrayList<EventLogModel> {
var xFirstEventsSubmitted = false
val eventsList = arrayListOf<EventLogModel>()
val groupedEventLogs = arrayListOf<EventLog>()
for (event in history) {
if (filter.isNotEmpty()) {
// TODO: let the SDK do it
if (event.type == EventLog.Type.ConferenceChatMessage) {
val message = event.chatMessage ?: continue
val fromAddress = message.fromAddress

View file

@ -82,8 +82,10 @@ class ContactsFragment : GenericFragment() {
viewLifecycleOwner
) {
it.consume {
Log.i("$TAG Closing sliding pane")
binding.slidingPaneLayout.closePane()
if (binding.slidingPaneLayout.isOpen) {
Log.i("$TAG Closing sliding pane")
binding.slidingPaneLayout.closePane()
}
}
}
@ -91,8 +93,10 @@ class ContactsFragment : GenericFragment() {
viewLifecycleOwner
) {
it.consume {
Log.i("$TAG Opening sliding pane")
binding.slidingPaneLayout.openPane()
if (!binding.slidingPaneLayout.isOpen) {
Log.i("$TAG Opening sliding pane")
binding.slidingPaneLayout.openPane()
}
}
}

View file

@ -82,8 +82,10 @@ class HistoryFragment : GenericFragment() {
viewLifecycleOwner
) {
it.consume {
Log.i("$TAG Closing sliding pane")
binding.slidingPaneLayout.closePane()
if (binding.slidingPaneLayout.isOpen) {
Log.i("$TAG Closing sliding pane")
binding.slidingPaneLayout.closePane()
}
}
}
@ -91,8 +93,10 @@ class HistoryFragment : GenericFragment() {
viewLifecycleOwner
) {
it.consume {
Log.i("$TAG Opening sliding pane")
binding.slidingPaneLayout.openPane()
if (!binding.slidingPaneLayout.isOpen) {
Log.i("$TAG Opening sliding pane")
binding.slidingPaneLayout.openPane()
}
}
}

View file

@ -82,8 +82,10 @@ class MeetingsFragment : GenericFragment() {
viewLifecycleOwner
) {
it.consume {
Log.i("$TAG Closing sliding pane")
binding.slidingPaneLayout.closePane()
if (binding.slidingPaneLayout.isOpen) {
Log.i("$TAG Closing sliding pane")
binding.slidingPaneLayout.closePane()
}
}
}
@ -91,8 +93,10 @@ class MeetingsFragment : GenericFragment() {
viewLifecycleOwner
) {
it.consume {
Log.i("$TAG Opening sliding pane")
binding.slidingPaneLayout.openPane()
if (!binding.slidingPaneLayout.isOpen) {
Log.i("$TAG Opening sliding pane")
binding.slidingPaneLayout.openPane()
}
}
}

View file

@ -22,6 +22,7 @@ package org.linphone.ui.main.viewmodel
import androidx.annotation.UiThread
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import org.linphone.core.ChatRoom
import org.linphone.utils.Event
class SharedMainViewModel @UiThread constructor() : ViewModel() {
@ -105,6 +106,7 @@ class SharedMainViewModel @UiThread constructor() : ViewModel() {
MutableLiveData<Event<Boolean>>()
}
var displayedChatRoom: ChatRoom? = null
val showConversationEvent: MutableLiveData<Event<Pair<String, String>>> by lazy {
MutableLiveData<Event<Pair<String, String>>>()
}

View file

@ -220,19 +220,15 @@
app:layout_constraintTop_toTopOf="@id/participants"
app:layout_constraintBottom_toBottomOf="@id/participants_anchor" />
<LinearLayout
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/participants"
android:visibility="@{viewModel.expandParticipants ? View.VISIBLE : View.GONE}"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="16dp"
android:padding="10dp"
entries="@{viewModel.participants}"
layout="@{@layout/chat_participant_list_cell}"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/participants_label"