Fix bad background on long text posts.

This commit is contained in:
Alex Hart
2023-01-04 12:45:54 -04:00
committed by Greyson Parrelli
parent c7bb0eadc2
commit eb6a14e686
3 changed files with 46 additions and 43 deletions

View File

@@ -12,12 +12,12 @@ import androidx.core.graphics.ColorUtils
import androidx.core.view.doOnNextLayout
import androidx.core.view.isVisible
import org.thoughtcrime.securesms.R
import org.thoughtcrime.securesms.components.ClippedCardView
import org.thoughtcrime.securesms.conversation.colors.ChatColors
import org.thoughtcrime.securesms.database.model.databaseprotos.StoryTextPost
import org.thoughtcrime.securesms.fonts.TextFont
import org.thoughtcrime.securesms.linkpreview.LinkPreview
import org.thoughtcrime.securesms.linkpreview.LinkPreviewViewModel
import org.thoughtcrime.securesms.mediasend.v2.text.TextAlignment
import org.thoughtcrime.securesms.mediasend.v2.text.TextStoryPostCreationState
import org.thoughtcrime.securesms.mediasend.v2.text.TextStoryScale
import org.thoughtcrime.securesms.mediasend.v2.text.TextStoryTextWatcher
@@ -35,9 +35,9 @@ class StoryTextPostView @JvmOverloads constructor(
inflate(context, R.layout.stories_text_post_view, this)
}
private var textAlignment: TextAlignment? = null
private val backgroundView: ImageView = findViewById(R.id.text_story_post_background)
private val textView: StoryTextView = findViewById(R.id.text_story_post_text)
private val textWrapperView: ClippedCardView = findViewById(R.id.text_story_text_background)
private val linkPreviewView: StoryLinkPreviewView = findViewById(R.id.text_story_post_link_preview)
private var isPlaceholder: Boolean = true
@@ -83,27 +83,21 @@ class StoryTextPostView @JvmOverloads constructor(
textView.text = text
}
private fun setTextGravity(textAlignment: TextAlignment) {
textView.gravity = textAlignment.gravity
}
private fun setTextScale(scalePercent: Int) {
val scale = TextStoryScale.convertToScale(scalePercent)
textView.scaleX = scale
textView.scaleY = scale
textWrapperView.scaleX = scale
textWrapperView.scaleY = scale
}
private fun setTextVisible(visible: Boolean) {
textView.visible = visible
textWrapperView.visible = visible
}
private fun setTextBackgroundColor(@ColorInt color: Int) {
textView.setWrappedBackgroundColor(color)
textWrapperView.setCardBackgroundColor(color)
}
fun bindFromCreationState(state: TextStoryPostCreationState) {
textAlignment = state.textAlignment
setPostBackground(state.backgroundColor.chatBubbleMask)
setText(
state.body.ifEmpty {
@@ -120,7 +114,6 @@ class StoryTextPostView @JvmOverloads constructor(
setTextColor(state.textForegroundColor, state.body.isEmpty())
setTextBackgroundColor(state.textBackgroundColor)
setTextGravity(state.textAlignment)
setTextScale(state.textScale)
postAdjustLinkPreviewTranslationY()
@@ -130,8 +123,6 @@ class StoryTextPostView @JvmOverloads constructor(
visible = true
linkPreviewView.visible = false
textAlignment = TextAlignment.CENTER
val font = TextFont.fromStyle(storyTextPost.style)
setPostBackground(ChatColors.forChatColor(ChatColors.Id.NotSet, storyTextPost.background).chatBubbleMask)
@@ -143,7 +134,6 @@ class StoryTextPostView @JvmOverloads constructor(
setTextColor(storyTextPost.textForegroundColor, false)
setTextBackgroundColor(storyTextPost.textBackgroundColor)
setTextGravity(TextAlignment.CENTER)
hideCloseButton()
@@ -182,12 +172,12 @@ class StoryTextPostView @JvmOverloads constructor(
}
fun showPostContent() {
textView.alpha = 1f
textWrapperView.alpha = 1f
linkPreviewView.alpha = 1f
}
fun hidePostContent() {
textView.alpha = 0f
textWrapperView.alpha = 0f
linkPreviewView.alpha = 0f
}
@@ -197,7 +187,7 @@ class StoryTextPostView @JvmOverloads constructor(
private fun adjustLinkPreviewTranslationY() {
val backgroundHeight = backgroundView.measuredHeight
val textHeight = if (canDisplayText()) textView.measuredHeight * textView.scaleY else 0f
val textHeight = if (canDisplayText()) textWrapperView.measuredHeight * textWrapperView.scaleY else 0f
val previewHeight = if (linkPreviewView.visible) linkPreviewView.measuredHeight else 0
val availableHeight = backgroundHeight - textHeight
@@ -208,17 +198,17 @@ class StoryTextPostView @JvmOverloads constructor(
linkPreviewView.translationY = -margin
val originPoint = textView.measuredHeight / 2f
val originPoint = textWrapperView.measuredHeight / 2f
val desiredPoint = (textHeight / 2f) + margin
textView.translationY = desiredPoint - originPoint
textWrapperView.translationY = desiredPoint - originPoint
} else {
linkPreviewView.translationY = 0f
val originPoint = textView.measuredHeight / 2f
val originPoint = textWrapperView.measuredHeight / 2f
val desiredPoint = backgroundHeight / 2f
textView.translationY = desiredPoint - originPoint
textWrapperView.translationY = desiredPoint - originPoint
}
}
}

View File

@@ -36,6 +36,10 @@ class StoryTextView @JvmOverloads constructor(
}
override fun onDraw(canvas: Canvas) {
if (layout == null) {
invalidate()
}
if (wrappedBackgroundPaint.color != Color.TRANSPARENT && layout != null) {
canvas.getClipBounds(canvasBounds)
textBounds.set(canvasBounds)