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 @@ -31,12 +31,16 @@
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.derivedStateOf
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.draw.clip
import androidx.compose.ui.geometry.isSpecified
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.layout.ContentScale
Expand All @@ -52,6 +56,7 @@
import io.getstream.chat.android.client.utils.attachment.isGiphy
import io.getstream.chat.android.compose.R
import io.getstream.chat.android.compose.state.messages.attachments.AttachmentState
import io.getstream.chat.android.compose.ui.components.ShimmerProgressIndicator
import io.getstream.chat.android.compose.ui.theme.ChatTheme
import io.getstream.chat.android.compose.ui.theme.StreamDimens
import io.getstream.chat.android.compose.ui.util.AsyncImagePreviewHandler
Expand Down Expand Up @@ -144,7 +149,7 @@
@OptIn(ExperimentalFoundationApi::class)
@Suppress("LongMethod")
@Composable
public fun GiphyAttachmentContent(

Check failure on line 152 in stream-chat-android-compose/src/main/java/io/getstream/chat/android/compose/ui/attachments/content/GiphyAttachmentContent.kt

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 28 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=GetStream_stream-chat-android&issues=AZ88H3HeZ3Nc1zah1KCL&open=AZ88H3HeZ3Nc1zah1KCL&pullRequest=6551
state: AttachmentState,
modifier: Modifier = Modifier,
giphyInfoType: GiphyInfoType = GiphyInfoType.ORIGINAL,
Expand Down Expand Up @@ -178,6 +183,8 @@
val width = ChatTheme.dimens.attachmentsContentGiphyWidth
val height = ChatTheme.dimens.attachmentsContentGiphyHeight

var downloadedRatio by remember(previewUrl) { mutableStateOf<Float?>(null) }

val giphyDimensions: DpSize by remember(key1 = giphyInfo) {
derivedStateOf {
if (giphyInfo != null) {
Expand Down Expand Up @@ -208,7 +215,18 @@
}
}
} else {
DpSize(maxWidth, maxHeight)
val ratio = downloadedRatio
if (ratio == null) {
val side = minOf(maxWidth, maxHeight)
DpSize(side, side)
} else {
calculateResultingDimensions(
maxWidth = maxWidth,
maxHeight = maxHeight,
giphyWidth = ratio.dp,
giphyHeight = 1.dp,
)
}
}
}
}
Expand All @@ -233,12 +251,38 @@
onLongClick = { onLongItemClick(message) },
),
) {
StreamAsyncImage(
data = giphyInfo?.url ?: attachment.giphyFallbackPreviewUrl,
modifier = Modifier.fillMaxSize(),
contentDescription = null,
contentScale = contentScale,
)
if (giphyInfo == null) {
StreamAsyncImage(
data = attachment.giphyFallbackPreviewUrl,
modifier = Modifier.fillMaxSize(),
contentScale = contentScale,
) { imageState ->
val painter = imageState.painter
val intrinsicSize = painter?.intrinsicSize
LaunchedEffect(intrinsicSize) {
if (intrinsicSize != null && intrinsicSize.isSpecified && intrinsicSize.height > 0f) {
downloadedRatio = intrinsicSize.width / intrinsicSize.height
}
}
if (painter == null) {
ShimmerProgressIndicator(modifier = Modifier.matchParentSize())
} else {
Image(
painter = painter,
contentDescription = null,
modifier = Modifier.matchParentSize(),
contentScale = contentScale,
)
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
} else {
StreamAsyncImage(
data = giphyInfo.url,
modifier = Modifier.fillMaxSize(),
contentDescription = null,
contentScale = contentScale,
)
}

Image(
modifier = Modifier
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading