From 83160d5cfffa4145aeaffcd9ac106122b47a82b0 Mon Sep 17 00:00:00 2001 From: Diego Beraldin Date: Tue, 3 Oct 2023 19:39:16 +0200 Subject: [PATCH] fix: unneeded space after post text --- .../core/commonui/components/PostCard.kt | 33 +++-- .../commonui/components/PostCardFooter.kt | 2 +- .../markdown/compose/elements/MarkdownText.kt | 132 +++++++++--------- 3 files changed, 90 insertions(+), 77 deletions(-) diff --git a/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/components/PostCard.kt b/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/components/PostCard.kt index b7ed65a40..ef760f434 100644 --- a/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/components/PostCard.kt +++ b/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/components/PostCard.kt @@ -16,20 +16,25 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.CompositionLocalProvider 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.draw.clip import androidx.compose.ui.graphics.Brush import androidx.compose.ui.graphics.Color +import androidx.compose.ui.layout.onGloballyPositioned import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.unit.Density import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.toSize import com.github.diegoberaldin.raccoonforlemmy.core.appearance.data.PostLayout import com.github.diegoberaldin.raccoonforlemmy.core.appearance.di.getThemeRepository import com.github.diegoberaldin.raccoonforlemmy.core.appearance.theme.CornerSize import com.github.diegoberaldin.raccoonforlemmy.core.appearance.theme.Spacing +import com.github.diegoberaldin.raccoonforlemmy.core.utils.toLocalPixel import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.data.CommunityModel import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.data.PostModel import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.data.UserModel @@ -215,17 +220,22 @@ private fun ExtendedPost( onImageClick = onImageClick, ) Box { + val maxHeight = 200.dp + val maxHeightPx = maxHeight.toLocalPixel() + var textHeightPx by remember { mutableStateOf(0f) } PostCardBody( modifier = Modifier.let { if (withOverflowBlurred) { - it.heightIn(max = 200.dp) + it.heightIn(max = maxHeight) } else { it } - }.padding(horizontal = Spacing.xs), + }.padding(horizontal = Spacing.xs).onGloballyPositioned { + textHeightPx = it.size.toSize().height + }, text = post.text, ) - if (withOverflowBlurred) { + if (withOverflowBlurred && textHeightPx >= maxHeightPx) { Box( modifier = Modifier.height(Spacing.xxl).fillMaxWidth() .align(Alignment.BottomCenter).background( @@ -242,9 +252,7 @@ private fun ExtendedPost( if (post.url != post.imageUrl) { PostLinkBanner( modifier = Modifier.padding(vertical = Spacing.xs), - url = post.url.takeIf { - it?.contains("pictrs/image") == false - }.orEmpty(), + url = post.url?.takeIf { !it.looksLikeAnImage }.orEmpty(), ) } PostCardFooter( @@ -266,8 +274,11 @@ private fun ExtendedPost( private val PostModel.imageUrl: String get() = thumbnailUrl?.takeIf { it.isNotEmpty() } ?: run { - url?.takeIf { u -> - val imageExtensions = listOf(".jpeg", ".jpg", ".png") - imageExtensions.any { u.endsWith(it) } - } - }.orEmpty() \ No newline at end of file + url?.takeIf { it.looksLikeAnImage } + }.orEmpty() + +private val String.looksLikeAnImage: Boolean + get() { + val imageExtensions = listOf(".jpeg", ".jpg", ".png") + return imageExtensions.any { this.endsWith(it) } + } \ No newline at end of file diff --git a/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/components/PostCardFooter.kt b/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/components/PostCardFooter.kt index 7894787e8..6cd25182f 100644 --- a/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/components/PostCardFooter.kt +++ b/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/components/PostCardFooter.kt @@ -59,7 +59,7 @@ fun PostCardFooter( Box(modifier = modifier) { Row( - modifier = Modifier.padding(bottom = Spacing.xxxs), + modifier = Modifier.padding(vertical = Spacing.xxxs), verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(Spacing.xxs), ) { diff --git a/core-md/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/markdown/compose/elements/MarkdownText.kt b/core-md/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/markdown/compose/elements/MarkdownText.kt index 6e492b910..3c0f817de 100755 --- a/core-md/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/markdown/compose/elements/MarkdownText.kt +++ b/core-md/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/markdown/compose/elements/MarkdownText.kt @@ -97,72 +97,74 @@ internal fun MarkdownText( Column( modifier = Modifier.fillMaxWidth(), ) { - Text( - text = content, - modifier = textModifier, - style = style, - inlineContent = mapOf( - TAG_IMAGE_URL to InlineTextContent( - if (inlineImages) { - Placeholder( - 180.sp, - 180.sp, - PlaceholderVerticalAlign.Bottom, - ) // TODO: identify flexible scaling! - } else { - Placeholder(1.sp, 1.sp, PlaceholderVerticalAlign.Bottom) - } - ) { link -> - if (inlineImages) { - CustomImage( - modifier = Modifier - .fillMaxWidth() - .clickable( - interactionSource = remember { MutableInteractionSource() }, - indication = null - ) { - onOpenImage?.invoke(imageUrl) - }, - url = link, - quality = FilterQuality.Low, - contentDescription = null, - contentScale = ContentScale.FillWidth, - onFailure = { - Text( - modifier = Modifier.fillMaxWidth(), - textAlign = TextAlign.Center, - text = stringResource(MR.strings.message_image_loading_error), - style = LocalMarkdownTypography.current.text - ) - }, - onLoading = { progress -> - val prog = if (progress != null) { - progress - } else { - val transition = rememberInfiniteTransition() - val res by transition.animateFloat( - initialValue = 0f, - targetValue = 1f, - animationSpec = InfiniteRepeatableSpec( - animation = tween(1000) - ) + if (inlineImages || content.text != imageUrl) { + Text( + text = content, + modifier = textModifier, + style = style, + inlineContent = mapOf( + TAG_IMAGE_URL to InlineTextContent( + if (inlineImages) { + Placeholder( + 180.sp, + 180.sp, + PlaceholderVerticalAlign.Bottom, + ) // TODO: identify flexible scaling! + } else { + Placeholder(1.sp, 1.sp, PlaceholderVerticalAlign.Bottom) + } + ) { link -> + if (inlineImages) { + CustomImage( + modifier = Modifier + .fillMaxWidth() + .clickable( + interactionSource = remember { MutableInteractionSource() }, + indication = null + ) { + onOpenImage?.invoke(imageUrl) + }, + url = link, + quality = FilterQuality.Low, + contentDescription = null, + contentScale = ContentScale.FillWidth, + onFailure = { + Text( + modifier = Modifier.fillMaxWidth(), + textAlign = TextAlign.Center, + text = stringResource(MR.strings.message_image_loading_error), + style = LocalMarkdownTypography.current.text ) - res - } - CircularProgressIndicator( - progress = prog, - color = MaterialTheme.colorScheme.primary, - ) - }, - ) - } else { - imageUrl = link - } - }, - ), - color = LocalMarkdownColors.current.text, - onTextLayout = { layoutResult.value = it }, - ) + }, + onLoading = { progress -> + val prog = if (progress != null) { + progress + } else { + val transition = rememberInfiniteTransition() + val res by transition.animateFloat( + initialValue = 0f, + targetValue = 1f, + animationSpec = InfiniteRepeatableSpec( + animation = tween(1000) + ) + ) + res + } + CircularProgressIndicator( + progress = prog, + color = MaterialTheme.colorScheme.primary, + ) + }, + ) + } else { + imageUrl = link + } + }, + ), + color = LocalMarkdownColors.current.text, + onTextLayout = { layoutResult.value = it }, + ) + } if (!inlineImages && imageUrl.isNotEmpty()) { CustomImage( modifier = modifier.fillMaxWidth()