enable add button only when you select contacts

Signed-off-by: sowjanyakch <sowjanya.kch@gmail.com>
This commit is contained in:
sowjanyakch 2025-05-26 13:02:27 +02:00 committed by Marcel Hibbe
parent e689e4f7f8
commit 96541b7ad8
No known key found for this signature in database
GPG Key ID: C793F8B59F43CE7B
5 changed files with 24 additions and 10 deletions

View File

@ -29,6 +29,7 @@ fun ContactsScreen(contactsViewModel: ContactsViewModel, uiState: ContactsUiStat
val isSearchActive by contactsViewModel.isSearchActive.collectAsStateWithLifecycle() val isSearchActive by contactsViewModel.isSearchActive.collectAsStateWithLifecycle()
val isAddParticipants by contactsViewModel.isAddParticipantsView.collectAsStateWithLifecycle() val isAddParticipants by contactsViewModel.isAddParticipantsView.collectAsStateWithLifecycle()
val autocompleteUsers by contactsViewModel.selectedParticipantsList.collectAsStateWithLifecycle() val autocompleteUsers by contactsViewModel.selectedParticipantsList.collectAsStateWithLifecycle()
val enableAddButton by contactsViewModel.enableAddButton.collectAsStateWithLifecycle()
Scaffold( Scaffold(
topBar = { topBar = {
@ -49,7 +50,8 @@ fun ContactsScreen(contactsViewModel: ContactsViewModel, uiState: ContactsUiStat
}, },
onUpdateAutocompleteUsers = { onUpdateAutocompleteUsers = {
contactsViewModel.getContactsFromSearchParams() contactsViewModel.getContactsFromSearchParams()
} },
enableAddButton = enableAddButton
) )
}, },
content = { content = {

View File

@ -36,6 +36,11 @@ class ContactsViewModel @Inject constructor(
private val _isAddParticipantsView = MutableStateFlow(false) private val _isAddParticipantsView = MutableStateFlow(false)
val isAddParticipantsView: StateFlow<Boolean> = _isAddParticipantsView val isAddParticipantsView: StateFlow<Boolean> = _isAddParticipantsView
private val _enableAddButton = MutableStateFlow(false)
val enableAddButton: StateFlow<Boolean> = _enableAddButton
private val _selectedContacts = MutableStateFlow<List<AutocompleteUser>>(emptyList())
private var hideAlreadyAddedParticipants: Boolean = false private var hideAlreadyAddedParticipants: Boolean = false
init { init {
@ -49,11 +54,21 @@ class ContactsViewModel @Inject constructor(
fun selectContact(contact: AutocompleteUser) { fun selectContact(contact: AutocompleteUser) {
val updatedParticipants = selectedParticipants.value + contact val updatedParticipants = selectedParticipants.value + contact
selectedParticipants.value = updatedParticipants selectedParticipants.value = updatedParticipants
_selectedContacts.value = _selectedContacts.value + contact
}
fun updateAddButtonState() {
if (_selectedContacts.value.isEmpty()) {
_enableAddButton.value = false
} else {
_enableAddButton.value = true
}
} }
fun deselectContact(contact: AutocompleteUser) { fun deselectContact(contact: AutocompleteUser) {
val updatedParticipants = selectedParticipants.value - contact val updatedParticipants = selectedParticipants.value - contact
selectedParticipants.value = updatedParticipants selectedParticipants.value = updatedParticipants
_selectedContacts.value = _selectedContacts.value - contact
} }
fun updateSelectedParticipants(participants: List<AutocompleteUser>) { fun updateSelectedParticipants(participants: List<AutocompleteUser>) {

View File

@ -42,7 +42,8 @@ fun AppBar(
onEnableSearch: () -> Unit, onEnableSearch: () -> Unit,
onDisableSearch: () -> Unit, onDisableSearch: () -> Unit,
onUpdateSearchQuery: (String) -> Unit, onUpdateSearchQuery: (String) -> Unit,
onUpdateAutocompleteUsers: () -> Unit onUpdateAutocompleteUsers: () -> Unit,
enableAddButton: Boolean
) { ) {
val context = LocalContext.current val context = LocalContext.current
@ -96,11 +97,11 @@ fun AppBar(
onClick = { onClick = {
onDisableSearch() onDisableSearch()
onUpdateSearchQuery("") onUpdateSearchQuery("")
} },
enabled = enableAddButton
) { ) {
Text(text = context.getString(R.string.add_participants)) Text(text = context.getString(R.string.add_participants))
} }
} else {
} }
} }
} }

View File

@ -65,8 +65,10 @@ fun ContactItemRow(contact: AutocompleteUser, contactsViewModel: ContactsViewMod
isSelected = !isSelected isSelected = !isSelected
if (isSelected) { if (isSelected) {
contactsViewModel.selectContact(contact) contactsViewModel.selectContact(contact)
contactsViewModel.updateAddButtonState()
} else { } else {
contactsViewModel.deselectContact(contact) contactsViewModel.deselectContact(contact)
contactsViewModel.updateAddButtonState()
} }
} }
} }

View File

@ -109,9 +109,3 @@ fun searchKeyboardActions(text: String, keyboardController: SoftwareKeyboardCont
} }
} }
) )
class SearchComponentCompanionClass {
companion object {
const val WIDTH_RATIO = 0.85f
}
}