Make contacts select and deselect via search

Signed-off-by: sowjanyakch <sowjanya.kch@gmail.com>
This commit is contained in:
sowjanyakch 2024-09-09 22:39:06 +02:00 committed by Marcel Hibbe
parent f374e6fe50
commit 9fcb1427db
No known key found for this signature in database
GPG key ID: C793F8B59F43CE7B
3 changed files with 26 additions and 39 deletions

View file

@ -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<AutocompleteUser>
) {
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<AutocompleteUser>
) {
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<AutocompleteUser>,
contactsViewModel: ContactsViewModel,
context: Context,
selectedParticipants: MutableList<AutocompleteUser>
) {
fun ContactsItem(contacts: List<AutocompleteUser>, contactsViewModel: ContactsViewModel, context: Context) {
val groupedContacts: Map<String, List<AutocompleteUser>> = 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")
}

View file

@ -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<Boolean> = _searchState
private val selectedParticipants = MutableStateFlow<List<AutocompleteUser>>(emptyList())
val selectedParticipantsList: StateFlow<List<AutocompleteUser>> = selectedParticipants
val selectedParticipantsList: StateFlow<List<AutocompleteUser>> = selectedParticipants.asStateFlow()
private val _isAddParticipantsView = MutableStateFlow(false)
val isAddParticipantsView: StateFlow<Boolean> = _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<AutocompleteUser>) {
selectedParticipants.value = participants
}

View file

@ -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