Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
Expand All @@ -62,6 +63,7 @@ import io.getstream.chat.android.compose.ui.util.mirrorRtl
import io.getstream.chat.android.models.AttachmentType
import io.getstream.chat.android.ui.common.state.messages.composer.AttachmentMetaData
import io.getstream.chat.android.ui.common.utils.MediaStringUtil
import kotlinx.coroutines.launch

private const val DefaultNumberOfPicturesPerRow = 3

Expand Down Expand Up @@ -119,6 +121,7 @@ internal fun DefaultImagesPickerItem(
) {
val attachmentMetaData = imageItem.attachmentMetaData
val isVideo = attachmentMetaData.type == AttachmentType.VIDEO
val scope = rememberCoroutineScope()

val imageRequest = ImageRequest.Builder(LocalContext.current)
.data(attachmentMetaData.uri.toString())
Expand All @@ -134,7 +137,11 @@ internal fun DefaultImagesPickerItem(
modifier = Modifier
.height(125.dp)
.padding(2.dp)
.clickable { onImageSelected(imageItem) }
.clickable {
scope.launch {
onImageSelected(imageItem)
}
}
.testTag("Stream_AttachmentPickerSampleImage"),
) {
StreamAsyncImage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,12 @@ public class AttachmentsPickerViewModel(
public fun changeSelectedAttachments(attachmentItem: AttachmentPickerItemState) {
val dataSet = attachments

val itemIndex = dataSet.indexOf(attachmentItem)
val itemIndex = dataSet.indexOfFirst { it.attachmentMetaData == attachmentItem.attachmentMetaData }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sometimes isSelected might not be updated into the dataset so the index can be -1 if we use indexOf(). This kotlin function will never give us index -1.

val newFiles = dataSet.toMutableList()

val newItem = dataSet[itemIndex].copy(isSelected = !newFiles[itemIndex].isSelected)

newFiles.removeAt(itemIndex)
newFiles.add(itemIndex, newItem)
newFiles[itemIndex] = newItem
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optimize the O(n) + O(n) remove and add to O(1).


if (attachmentsPickerMode == Files) {
files = newFiles
Expand Down