From 107388584b11ebcf018cc5fb5010328430240f00 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Mon, 29 Apr 2024 17:27:08 +0200 Subject: [PATCH] Also updated conversations participants selection to handle removal from add participant(s) fragment --- .../chat/fragment/ConversationInfoFragment.kt | 8 ++- .../ui/main/chat/model/ParticipantModel.kt | 1 + .../viewmodel/ConversationInfoViewModel.kt | 66 +++++++++++++++---- 3 files changed, 59 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/org/linphone/ui/main/chat/fragment/ConversationInfoFragment.kt b/app/src/main/java/org/linphone/ui/main/chat/fragment/ConversationInfoFragment.kt index 0a5c9b5d1..24c42ffb5 100644 --- a/app/src/main/java/org/linphone/ui/main/chat/fragment/ConversationInfoFragment.kt +++ b/app/src/main/java/org/linphone/ui/main/chat/fragment/ConversationInfoFragment.kt @@ -198,8 +198,7 @@ class ConversationInfoFragment : SlidingPaneChildFragment() { sharedViewModel.listOfSelectedSipUrisEvent.observe(viewLifecycleOwner) { it.consume { list -> Log.i("$TAG Found [${list.size}] new participants to add to the group, let's do it") - // TODO FIXME: instead of adding them, replace current list with new one - viewModel.addParticipants(list) + viewModel.setParticipants(list) } } @@ -221,7 +220,10 @@ class ConversationInfoFragment : SlidingPaneChildFragment() { Log.i("$TAG Going into participant picker fragment") val selection = arrayListOf() for (participant in viewModel.participants.value.orEmpty()) { - selection.add(participant.address.asStringUriOnly()) + if (!participant.isParticipantMyself) { + // Do not add ourselves to editable list + selection.add(participant.address.asStringUriOnly()) + } } Log.i("$TAG [${selection.size}] participants are already selected, keeping them") val action = diff --git a/app/src/main/java/org/linphone/ui/main/chat/model/ParticipantModel.kt b/app/src/main/java/org/linphone/ui/main/chat/model/ParticipantModel.kt index dc980b5d0..c966dc074 100644 --- a/app/src/main/java/org/linphone/ui/main/chat/model/ParticipantModel.kt +++ b/app/src/main/java/org/linphone/ui/main/chat/model/ParticipantModel.kt @@ -30,6 +30,7 @@ class ParticipantModel @WorkerThread constructor( val isMyselfAdmin: Boolean = false, val isParticipantAdmin: Boolean = false, val showMenu: Boolean = false, + val isParticipantMyself: Boolean = false, private val onClicked: ((model: ParticipantModel) -> Unit)? = null, private val onMenuClicked: ((view: View, model: ParticipantModel) -> Unit)? = null ) { diff --git a/app/src/main/java/org/linphone/ui/main/chat/viewmodel/ConversationInfoViewModel.kt b/app/src/main/java/org/linphone/ui/main/chat/viewmodel/ConversationInfoViewModel.kt index 4576749ab..968c5b8d5 100644 --- a/app/src/main/java/org/linphone/ui/main/chat/viewmodel/ConversationInfoViewModel.kt +++ b/app/src/main/java/org/linphone/ui/main/chat/viewmodel/ConversationInfoViewModel.kt @@ -376,7 +376,7 @@ class ConversationInfoViewModel @UiThread constructor() : AbstractConversationVi } @UiThread - fun addParticipants(toAdd: ArrayList) { + fun setParticipants(newList: ArrayList) { coreContext.postOnCoreThread { if (isChatRoomInitialized()) { if (!LinphoneUtils.isChatRoomAGroup(chatRoom)) { @@ -384,26 +384,63 @@ class ConversationInfoViewModel @UiThread constructor() : AbstractConversationVi return@postOnCoreThread } - val list = arrayListOf
() - for (participant in toAdd) { + for (participant in chatRoom.participants) { + val address = participant.address + // Do not remove ourselves if not in participants list anymore + if (LinphoneUtils.getDefaultAccount()?.params?.identityAddress?.weakEqual( + address + ) == true + ) { + continue + } + + val uri = address.asStringUriOnly() + val found = newList.find { + it == uri + } + if (found != null) { + Log.i( + "$TAG Participant [$uri] is still in new participants list, do nothing" + ) + } else { + Log.i("$TAG Removing participant [$uri] from this conversation") + chatRoom.removeParticipant(participant) + } + } + + val toAddList = arrayListOf
() + for (participant in newList) { val address = Factory.instance().createAddress(participant) if (address == null) { Log.e("$TAG Failed to parse [$participant] as address!") } else { - list.add(address) + val found = participants.value.orEmpty().find { + it.address.weakEqual(address) + } + if (found != null) { + Log.i( + "$TAG Participant [${address.asStringUriOnly()}] is already in group, do nothing" + ) + } else { + toAddList.add(address) + } } } - val participantsToAdd = arrayOfNulls
(list.size) - list.toArray(participantsToAdd) - Log.i("$TAG Adding [${participantsToAdd.size}] new participants to conversation") - val ok = chatRoom.addParticipants(participantsToAdd) - if (!ok) { - Log.w("$TAG Failed to add some/all participants to the group!") - val message = AppUtils.getString( - R.string.toast_failed_to_add_participant_to_group_conversation + if (toAddList.isNotEmpty()) { + val participantsToAdd = arrayOfNulls
(toAddList.size) + toAddList.toArray(participantsToAdd) + Log.i( + "$TAG Adding [${participantsToAdd.size}] new participants to conversation" ) - showRedToastEvent.postValue(Event(Pair(message, R.drawable.warning_circle))) + val ok = chatRoom.addParticipants(participantsToAdd) + if (!ok) { + Log.w("$TAG Failed to add some/all participants to the group!") + val message = AppUtils.getString( + R.string.toast_failed_to_add_participant_to_group_conversation + ) + showRedToastEvent.postValue(Event(Pair(message, R.drawable.warning_circle))) + } } } } @@ -484,6 +521,7 @@ class ConversationInfoViewModel @UiThread constructor() : AbstractConversationVi selfAdmin, isParticipantAdmin = false, showMenu = true, + isParticipantMyself = false, onMenuClicked = { view, model -> // openMenu showParticipantAdminPopupMenuEvent.postValue(Event(Pair(view, model))) @@ -499,6 +537,7 @@ class ConversationInfoViewModel @UiThread constructor() : AbstractConversationVi selfAdmin, isParticipantAdmin = isParticipantAdmin, showMenu = true, + isParticipantMyself = false, onMenuClicked = { view, model -> // openMenu showParticipantAdminPopupMenuEvent.postValue(Event(Pair(view, model))) @@ -516,6 +555,7 @@ class ConversationInfoViewModel @UiThread constructor() : AbstractConversationVi selfAdmin, isParticipantAdmin = selfAdmin, showMenu = false, + isParticipantMyself = true, onMenuClicked = { view, model -> // openMenu showParticipantAdminPopupMenuEvent.postValue(Event(Pair(view, model)))