From 9fcb1427dbcb0f785ab9b73a726ffa731e7f753f Mon Sep 17 00:00:00 2001 From: sowjanyakch Date: Mon, 9 Sep 2024 22:39:06 +0200 Subject: [PATCH] Make contacts select and deselect via search Signed-off-by: sowjanyakch --- .../talk/contacts/ContactsActivityCompose.kt | 50 ++++++------------- .../talk/contacts/ContactsViewModel.kt | 13 ++++- .../talk/contacts/SearchComponent.kt | 2 - 3 files changed, 26 insertions(+), 39 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/contacts/ContactsActivityCompose.kt b/app/src/main/java/com/nextcloud/talk/contacts/ContactsActivityCompose.kt index 5fdbc3c73..b57e87061 100644 --- a/app/src/main/java/com/nextcloud/talk/contacts/ContactsActivityCompose.kt +++ b/app/src/main/java/com/nextcloud/talk/contacts/ContactsActivityCompose.kt @@ -52,9 +52,7 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.DisposableEffect import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember -import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color @@ -116,9 +114,9 @@ class ContactsActivityCompose : BaseActivity() { @Suppress("DEPRECATION") intent.getParcelableArrayListExtra("selectedParticipants") ?: emptyList() } - } - val participants = selectedParticipants.toSet().toMutableList() - contactsViewModel.updateSelectedParticipants(participants) + }.toSet().toMutableList() + contactsViewModel.updateSelectedParticipants(selectedParticipants) + MaterialTheme( colorScheme = colorScheme ) { @@ -137,8 +135,7 @@ class ContactsActivityCompose : BaseActivity() { ContactsList( contactsUiState = uiState.value, contactsViewModel = contactsViewModel, - context = context, - selectedParticipants = selectedParticipants.toMutableList() + context = context ) } } @@ -168,13 +165,8 @@ class ContactsActivityCompose : BaseActivity() { } @Composable -fun ContactItemRow( - contact: AutocompleteUser, - contactsViewModel: ContactsViewModel, - context: Context, - selectedContacts: MutableList -) { - var isSelected by remember { mutableStateOf(selectedContacts.contains(contact)) } +fun ContactItemRow(contact: AutocompleteUser, contactsViewModel: ContactsViewModel, context: Context) { + var isSelected = remember(contact) { contactsViewModel.selectedParticipantsList.value.contains(contact) } val roomUiState by contactsViewModel.roomViewState.collectAsState() val isAddParticipants = contactsViewModel.isAddParticipantsView.collectAsState() Row( @@ -191,14 +183,11 @@ fun ContactItemRow( ) } else { isSelected = !isSelected - selectedContacts.apply { - if (isSelected) { - add(contact) - } else { - remove(contact) - } + if (isSelected) { + contactsViewModel.selectContact(contact) + } else { + contactsViewModel.deselectContact(contact) } - contactsViewModel.updateSelectedParticipants(selectedContacts) } } ), @@ -363,12 +352,7 @@ fun ConversationCreationOptions(context: Context, contactsViewModel: ContactsVie } @Composable -fun ContactsList( - contactsUiState: ContactsUiState, - contactsViewModel: ContactsViewModel, - context: Context, - selectedParticipants: MutableList -) { +fun ContactsList(contactsUiState: ContactsUiState, contactsViewModel: ContactsViewModel, context: Context) { when (contactsUiState) { is ContactsUiState.None -> { } @@ -381,7 +365,7 @@ fun ContactsList( val contacts = contactsUiState.contacts Log.d(CompanionClass.TAG, "Contacts:$contacts") if (contacts != null) { - ContactsItem(contacts, contactsViewModel, context, selectedParticipants) + ContactsItem(contacts, contactsViewModel, context) } } is ContactsUiState.Error -> { @@ -395,12 +379,7 @@ fun ContactsList( @OptIn(ExperimentalFoundationApi::class) @Composable -fun ContactsItem( - contacts: List, - contactsViewModel: ContactsViewModel, - context: Context, - selectedParticipants: MutableList -) { +fun ContactsItem(contacts: List, contactsViewModel: ContactsViewModel, context: Context) { val groupedContacts: Map> = contacts.groupBy { contact -> ( if (contact.source == "users") { @@ -432,8 +411,7 @@ fun ContactsItem( ContactItemRow( contact = contact, contactsViewModel = contactsViewModel, - context = context, - selectedContacts = selectedParticipants + context = context ) Log.d(CompanionClass.TAG, "Contacts:$contact") } diff --git a/app/src/main/java/com/nextcloud/talk/contacts/ContactsViewModel.kt b/app/src/main/java/com/nextcloud/talk/contacts/ContactsViewModel.kt index 7dda67591..b045dd11a 100644 --- a/app/src/main/java/com/nextcloud/talk/contacts/ContactsViewModel.kt +++ b/app/src/main/java/com/nextcloud/talk/contacts/ContactsViewModel.kt @@ -13,6 +13,7 @@ import com.nextcloud.talk.models.json.autocomplete.AutocompleteUser import com.nextcloud.talk.models.json.conversations.Conversation import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.launch import javax.inject.Inject @@ -31,7 +32,7 @@ class ContactsViewModel @Inject constructor( private val _searchState = MutableStateFlow(false) val searchState: StateFlow = _searchState private val selectedParticipants = MutableStateFlow>(emptyList()) - val selectedParticipantsList: StateFlow> = selectedParticipants + val selectedParticipantsList: StateFlow> = selectedParticipants.asStateFlow() private val _isAddParticipantsView = MutableStateFlow(false) val isAddParticipantsView: StateFlow = _isAddParticipantsView @@ -43,6 +44,16 @@ class ContactsViewModel @Inject constructor( _searchQuery.value = query } + fun selectContact(contact: AutocompleteUser) { + val updatedParticipants = selectedParticipants.value + contact + selectedParticipants.value = updatedParticipants + } + + fun deselectContact(contact: AutocompleteUser) { + val updatedParticipants = selectedParticipants.value - contact + selectedParticipants.value = updatedParticipants + } + fun updateSelectedParticipants(participants: List) { selectedParticipants.value = participants } diff --git a/app/src/main/java/com/nextcloud/talk/contacts/SearchComponent.kt b/app/src/main/java/com/nextcloud/talk/contacts/SearchComponent.kt index 5a793d40e..0e73a7afd 100644 --- a/app/src/main/java/com/nextcloud/talk/contacts/SearchComponent.kt +++ b/app/src/main/java/com/nextcloud/talk/contacts/SearchComponent.kt @@ -19,7 +19,6 @@ import androidx.compose.material3.IconButton import androidx.compose.material3.Text import androidx.compose.material3.TextField import androidx.compose.runtime.Composable -import androidx.compose.runtime.collectAsState import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalSoftwareKeyboardController import androidx.compose.ui.res.stringResource @@ -31,7 +30,6 @@ import com.nextcloud.talk.R @Composable fun DisplaySearch(text: String, onTextChange: (String) -> Unit, contactsViewModel: ContactsViewModel) { - val isAddParticipants = contactsViewModel.isAddParticipantsView.collectAsState() val keyboardController = LocalSoftwareKeyboardController.current TextField( modifier = Modifier