Skip to content

Commit

Permalink
Properly select and deselect contact items
Browse files Browse the repository at this point in the history
Signed-off-by: sowjanyakch <sowjanya.kch@gmail.com>
  • Loading branch information
sowjanyakch committed Sep 2, 2024
1 parent 9d43d82 commit 6e5d1cc
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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()
Expand Down Expand Up @@ -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) }
Expand All @@ -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) {
Expand Down Expand Up @@ -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()
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* SPDX-License-Identifier: GPL-3.0-or-later
*/

@file:Suppress("DEPRECATION")

package com.nextcloud.talk.conversationcreation

import android.annotation.SuppressLint
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 6e5d1cc

Please sign in to comment.