Properly select and deselect contact items

Signed-off-by: sowjanyakch <sowjanya.kch@gmail.com>
This commit is contained in:
sowjanyakch 2024-09-02 21:00:22 +02:00 committed by Marcel Hibbe
parent 5f3d73efce
commit ff63bcd855
No known key found for this signature in database
GPG Key ID: C793F8B59F43CE7B
4 changed files with 58 additions and 57 deletions

View File

@ -11,6 +11,7 @@ import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.util.Log
import androidx.activity.compose.setContent
@ -49,7 +50,9 @@ import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
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
@ -88,7 +91,6 @@ class ContactsActivityCompose : BaseActivity() {
contactsViewModel = ViewModelProvider(this, viewModelFactory)[ContactsViewModel::class.java]
setContent {
val isAddParticipants = intent.getBooleanExtra("isAddParticipants", false)
val isAddParticipantsEdit = intent.getBooleanExtra("isAddParticipantsEdit", false)
contactsViewModel.updateIsAddParticipants(isAddParticipants)
if (isAddParticipants) {
contactsViewModel.updateShareTypes(
@ -102,11 +104,12 @@ class ContactsActivityCompose : BaseActivity() {
}
val colorScheme = viewThemeUtils.getColorScheme(this)
val uiState = contactsViewModel.contactsViewState.collectAsState()
val selectedParticipants: List<AutocompleteUser> = remember {
if (isAddParticipants) {
intent.getParcelableArrayListExtra("selected participants") ?: emptyList()
val selectedParticipants = remember {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
intent.getParcelableArrayListExtra("selectedParticipants", AutocompleteUser::class.java) ?: emptyList()
} else {
emptyList()
@Suppress("DEPRECATION")
intent.getParcelableArrayListExtra("selectedParticipants") ?: emptyList()
}
}
val participants = selectedParticipants.toSet().toMutableList()
@ -147,29 +150,34 @@ fun ContactItemRow(
context: Context,
selectedContacts: MutableList<AutocompleteUser>
) {
val isSelected = selectedContacts.contains(contact)
var isSelected by remember { mutableStateOf(selectedContacts.contains(contact)) }
val roomUiState by contactsViewModel.roomViewState.collectAsState()
val isAddParticipants = contactsViewModel.isAddParticipantsView.collectAsState()
Row(
modifier = Modifier
.fillMaxWidth()
.clickable {
if (!isAddParticipants.value) {
contactsViewModel.createRoom(
CompanionClass.ROOM_TYPE_ONE_ONE,
contact.source!!,
contact.id!!,
null
)
} else {
if (isSelected) {
selectedContacts.remove(contact)
.clickable(
onClick = {
if (!isAddParticipants.value) {
contactsViewModel.createRoom(
CompanionClass.ROOM_TYPE_ONE_ONE,
contact.source!!,
contact.id!!,
null
)
} else {
selectedContacts.add(contact)
isSelected = !isSelected
selectedContacts.apply {
if (isSelected) {
add(contact)
} else {
remove(contact)
}
}
contactsViewModel.updateSelectedParticipants(selectedContacts)
}
contactsViewModel.updateSelectedParticipants(selectedContacts)
}
},
),
verticalAlignment = Alignment.CenterVertically
) {
val imageUri = contact.id?.let { contactsViewModel.getImageUri(it, true) }
@ -181,14 +189,16 @@ fun ContactItemRow(
modifier = Modifier.size(width = 45.dp, height = 45.dp)
)
Text(modifier = Modifier.padding(16.dp), text = contact.label!!)
if (isAddParticipants.value && isSelected) {
Spacer(modifier = Modifier.weight(1f))
Icon(
imageVector = ImageVector.vectorResource(id = R.drawable.ic_check_circle),
contentDescription = "Selected",
tint = Color.Blue,
modifier = Modifier.padding(end = 8.dp)
)
if (isAddParticipants.value) {
if (isSelected) {
Spacer(modifier = Modifier.weight(1f))
Icon(
imageVector = ImageVector.vectorResource(id = R.drawable.ic_check_circle),
contentDescription = "Selected",
tint = Color.Blue,
modifier = Modifier.padding(end = 8.dp)
)
}
}
}
when (roomUiState) {
@ -238,17 +248,17 @@ fun AppBar(title: String, context: Context, contactsViewModel: ContactsViewModel
Text(
text = stringResource(id = R.string.nc_contacts_done),
modifier = Modifier.clickable {
val activity = context as? Activity
val resultIntent = Intent().apply {
putParcelableArrayListExtra(
"selectedParticipants",
contactsViewModel
.selectedParticipantsList as
ArrayList<AutocompleteUser>
ArrayList(
contactsViewModel
.selectedParticipantsList.value
)
)
}
activity?.setResult(Activity.RESULT_OK, resultIntent)
activity?.finish()
(context as? Activity)?.setResult(Activity.RESULT_OK, resultIntent)
(context as? Activity)?.finish()
}
)
}

View File

@ -30,8 +30,8 @@ class ContactsViewModel @Inject constructor(
val shareTypeList: List<String> = shareTypes
private val _searchState = MutableStateFlow(false)
val searchState: StateFlow<Boolean> = _searchState
private val selectedParticipants = mutableListOf<AutocompleteUser>()
val selectedParticipantsList: List<AutocompleteUser> = selectedParticipants
private val _selectedParticipants = MutableStateFlow<List<AutocompleteUser>>(emptyList())
val selectedParticipantsList: StateFlow<List<AutocompleteUser>> = _selectedParticipants
private val _isAddParticipantsView = MutableStateFlow(false)
val isAddParticipantsView: StateFlow<Boolean> = _isAddParticipantsView
@ -44,7 +44,7 @@ class ContactsViewModel @Inject constructor(
}
fun updateSelectedParticipants(participants: List<AutocompleteUser>) {
selectedParticipants.addAll(participants)
_selectedParticipants.value = participants
}
fun updateSearchState(searchState: Boolean) {
_searchState.value = searchState

View File

@ -5,6 +5,8 @@
* SPDX-License-Identifier: GPL-3.0-or-later
*/
@file:Suppress("DEPRECATION")
package com.nextcloud.talk.conversationcreation
import android.annotation.SuppressLint
@ -267,7 +269,7 @@ fun AddParticipants(
.clickable {
val intent = Intent(context, ContactsActivityCompose::class.java)
intent.putParcelableArrayListExtra(
"selected participants",
"selectedParticipants",
participants as ArrayList<AutocompleteUser>
)
intent.putExtra("isAddParticipants", true)
@ -278,10 +280,7 @@ fun AddParticipants(
)
}
}
val participant = participants.toSet()
participant.forEach { participant ->
participants.toSet().forEach { participant ->
Row(modifier = Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {
val imageUri = participant.id?.let { conversationCreationViewModel.getImageUri(it, true) }
val errorPlaceholderImage: Int = R.drawable.account_circle_96dp
@ -312,7 +311,7 @@ fun AddParticipants(
},
verticalAlignment = Alignment.CenterVertically
) {
if (participant.isEmpty()) {
if (participants.isEmpty()) {
Icon(
painter = painterResource(id = R.drawable.ic_account_plus),
contentDescription = null,
@ -329,17 +328,16 @@ fun AddParticipants(
@Composable
fun RoomCreationOptions(conversationCreationViewModel: ConversationCreationViewModel) {
var isGuestsAllowed = conversationCreationViewModel.isGuestsAllowed.value
var isConversationAvailableForRegisteredUsers = conversationCreationViewModel
val isGuestsAllowed = conversationCreationViewModel.isGuestsAllowed.value
val isConversationAvailableForRegisteredUsers = conversationCreationViewModel
.isConversationAvailableForRegisteredUsers.value
var isOpenForGuestAppUsers = conversationCreationViewModel.openForGuestAppUsers.value
val isOpenForGuestAppUsers = conversationCreationViewModel.openForGuestAppUsers.value
Text(
text = stringResource(id = R.string.nc_visible).uppercase(),
fontSize = 14.sp,
modifier = Modifier.padding(top = 24.dp, start = 16.dp, end = 16.dp)
)
ConversationOptions(
icon = R.drawable.ic_avatar_link,
text = R.string.nc_guest_access_allow_title,
@ -458,17 +456,17 @@ fun ShowPasswordDialog(onDismiss: () -> Unit, conversationCreationViewModel: Con
conversationCreationViewModel.updatePassword(password)
onDismiss()
}) {
Text(text = "Save")
Text(text = stringResource(id = R.string.save))
}
},
title = { Text(text = "Set Password") },
title = { Text(text = stringResource(id = R.string.nc_set_password)) },
text = {
TextField(
value = password,
onValueChange = {
password = it
},
label = { Text(text = "Enter a password") }
label = { Text(text = stringResource(id = R.string.nc_guest_access_password_dialog_hint)) }
)
},
dismissButton = {

View File

@ -8,7 +8,6 @@
package com.nextcloud.talk.conversationcreation
import android.util.Log
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
@ -42,14 +41,8 @@ class ConversationCreationViewModel @Inject constructor(
var isGuestsAllowed = mutableStateOf(false)
var isConversationAvailableForRegisteredUsers = mutableStateOf(false)
var openForGuestAppUsers = mutableStateOf(false)
private val scope: MutableState<Int?> = mutableStateOf(null)
private val addParticipantsViewState = MutableStateFlow<AddParticipantsUiState>(AddParticipantsUiState.None)
val addParticipantsUiState: StateFlow<AddParticipantsUiState> = addParticipantsViewState
private val _allowGuestsResult = MutableStateFlow<AllowGuestsUiState>(AllowGuestsUiState.None)
val allowGuestsResult: StateFlow<AllowGuestsUiState> = _allowGuestsResult
fun updateRoomName(roomName: String) {
_roomName.value = roomName
}