From fbbad55a0fbe40dc126c715208d59c6b85c308fe Mon Sep 17 00:00:00 2001 From: Stanislav Dmitrenko <7953703+avently@users.noreply.github.com> Date: Thu, 2 Feb 2023 19:46:33 +0000 Subject: [PATCH] android: Fix constraints of Compose that could crash the app (#1876) * android: Fix constraints of Compose that could crash the app * made constant --- .../simplex/app/views/chat/item/FramedItemView.kt | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/apps/android/app/src/main/java/chat/simplex/app/views/chat/item/FramedItemView.kt b/apps/android/app/src/main/java/chat/simplex/app/views/chat/item/FramedItemView.kt index 1234799172..b6639709c1 100644 --- a/apps/android/app/src/main/java/chat/simplex/app/views/chat/item/FramedItemView.kt +++ b/apps/android/app/src/main/java/chat/simplex/app/views/chat/item/FramedItemView.kt @@ -28,6 +28,7 @@ import chat.simplex.app.model.* import chat.simplex.app.ui.theme.* import chat.simplex.app.views.helpers.* import kotlinx.datetime.Clock +import kotlin.math.min val SentColorLight = Color(0x1E45B8FF) val ReceivedColorLight = Color(0x20B1B0B5) @@ -241,6 +242,7 @@ fun CIMarkdownText( } const val CHAT_IMAGE_LAYOUT_ID = "chatImage" +const val MAX_SAFE_WIDTH_HEIGHT = 100_000 @Composable fun PriorityLayout( @@ -259,9 +261,15 @@ fun PriorityLayout( if (it.layoutId == priorityLayoutId) imagePlaceable!! else - it.measure(constraints.copy(maxWidth = imagePlaceable?.width ?: constraints.maxWidth)) } - // Limit width for every other element to width of important element and height for a sum of all elements - layout(imagePlaceable?.measuredWidth ?: placeables.maxOf { it.width }, placeables.sumOf { it.height }) { + it.measure(constraints.copy(maxWidth = imagePlaceable?.width ?: min(MAX_SAFE_WIDTH_HEIGHT, constraints.maxWidth))) } + /** + * Limit width for every other element to width of important element and height for a sum of all elements. + * + * min(MAX_SAFE_WIDTH_HEIGHT, ...) is here because of exception (related to width of long text): + * java.lang.IllegalArgumentException: Can't represent a size of 324314 in Constraints + * at androidx.compose.ui.unit.Constraints$Companion.bitsNeedForSize(Constraints.kt:403) + * */ + layout(imagePlaceable?.measuredWidth ?: min(MAX_SAFE_WIDTH_HEIGHT, placeables.maxOf { it.width }), min(MAX_SAFE_WIDTH_HEIGHT, placeables.sumOf { it.height })) { var y = 0 placeables.forEach { it.place(0, y)