From f3fb9667d8a33809a3aec78617dc73a0f76ee278 Mon Sep 17 00:00:00 2001 From: DarkKnight2122 <145188583+DarkKnight2122@users.noreply.github.com> Date: Sat, 25 Apr 2026 05:20:16 +0530 Subject: [PATCH] Chat Preview text bug fix --- .../impl/ui/FriendFeedMessagePreview.kt | 34 +++++++++++-------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/core/src/main/kotlin/me/eternal/purrfectsnap/core/features/impl/ui/FriendFeedMessagePreview.kt b/core/src/main/kotlin/me/eternal/purrfectsnap/core/features/impl/ui/FriendFeedMessagePreview.kt index 625c35fa..63b34e67 100644 --- a/core/src/main/kotlin/me/eternal/purrfectsnap/core/features/impl/ui/FriendFeedMessagePreview.kt +++ b/core/src/main/kotlin/me/eternal/purrfectsnap/core/features/impl/ui/FriendFeedMessagePreview.kt @@ -9,6 +9,8 @@ import android.text.TextPaint import android.view.View import android.view.ViewGroup import android.graphics.Typeface +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.graphics.toArgb import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.launch @@ -78,9 +80,10 @@ class FriendFeedMessagePreview : Feature("FriendFeedMessagePreview") { val ffSdlPrimaryTextStartMargin = 6 * density val feedEntryHeight = ffSdlAvatarSize + ffSdlAvatarMargin * 2 + (4 * density).toInt() - val separatorHeight = (density * 2).toInt() + val safetyGap = (12 * density).toInt() val textPaint = TextPaint().apply { textSize = secondaryTextSize + isAntiAlias = true } context.event.subscribe(BuildMessageEvent::class) { param -> @@ -105,14 +108,14 @@ class FriendFeedMessagePreview : Feature("FriendFeedMessagePreview") { } fetchMessages(conversationId) { - var maxTextHeight = 0 - val previewContainerHeight = messageCache[conversationId]?.sumOf { msg -> - val rect = Rect() - textPaint.getTextBounds(msg, 0, msg.length, rect) - rect.height().also { - if (it > maxTextHeight) maxTextHeight = it - }.plus(separatorHeight) - } ?: run { + val fontMetrics = textPaint.fontMetrics + val lineHeight = (fontMetrics.descent - fontMetrics.ascent).toInt() + val spacing = (2 * density).toInt() + + val messages = messageCache[conversationId] + val previewContainerHeight = if (messages.isNullOrEmpty()) 0 else (messages.size * (lineHeight + spacing)) + + if (previewContainerHeight == 0) { ffItem.layoutParams = ffItem.layoutParams.apply { height = ViewGroup.LayoutParams.MATCH_PARENT } @@ -120,22 +123,23 @@ class FriendFeedMessagePreview : Feature("FriendFeedMessagePreview") { } ffItem.layoutParams = ffItem.layoutParams.apply { - height = feedEntryHeight + previewContainerHeight + separatorHeight + height = feedEntryHeight + (safetyGap * 2) + previewContainerHeight } cachedLayouts[conversationId] = frameLayout frameLayout.addForegroundDrawable("ffItem", ShapeDrawable(object: Shape() { override fun draw(canvas: Canvas, paint: Paint) { - val offsetY = canvas.height.toFloat() - previewContainerHeight + val startY = feedEntryHeight.toFloat() + safetyGap paint.textSize = secondaryTextSize - paint.color = context.userInterface.colorPrimary + paint.color = Color(context.userInterface.colorPrimary).copy(alpha = 0.7f).toArgb() paint.typeface = Typeface.DEFAULT + paint.isAntiAlias = true - messageCache[conversationId]?.forEachIndexed { index, messageString -> + messages?.forEachIndexed { index, messageString -> canvas.drawText(messageString, - feedEntryHeight + ffSdlPrimaryTextStartMargin, - offsetY + index * maxTextHeight, + ffSdlAvatarSize + ffSdlAvatarMargin + (ffSdlPrimaryTextStartMargin * 2), + startY + (index + 1) * lineHeight + (index * spacing), paint ) }