Fix link preview corner rounding

This commit is contained in:
Niels Andriesse 2021-06-22 09:34:23 +10:00
parent 74330c931a
commit 252f344d93
3 changed files with 61 additions and 75 deletions

View File

@ -1,9 +1,11 @@
package org.thoughtcrime.securesms.conversation.v2.messages package org.thoughtcrime.securesms.conversation.v2.messages
import android.content.Context import android.content.Context
import android.graphics.Canvas
import android.graphics.Outline import android.graphics.Outline
import android.graphics.Path import android.graphics.Path
import android.graphics.RectF import android.graphics.RectF
import android.graphics.drawable.Drawable
import android.os.Build import android.os.Build
import android.util.AttributeSet import android.util.AttributeSet
import android.util.Log import android.util.Log
@ -29,8 +31,9 @@ class LinkPreviewView : LinearLayout {
// endregion // endregion
// region Updating // region Updating
fun bind(message: MmsMessageRecord, glide: GlideRequests, isStartOfMessageCluster: Boolean, isEndOfMessageCluster: Boolean) { fun bind(message: MmsMessageRecord, glide: GlideRequests, background: Drawable) {
mainLinkPreviewContainer.outlineProvider = getOutlineProvider(message.isOutgoing, isStartOfMessageCluster, isEndOfMessageCluster) mainLinkPreviewContainer.background = background
mainLinkPreviewContainer.outlineProvider = ViewOutlineProvider.BACKGROUND
mainLinkPreviewContainer.clipToOutline = true mainLinkPreviewContainer.clipToOutline = true
// Thumbnail // Thumbnail
val linkPreview = message.linkPreviews.first() val linkPreview = message.linkPreviews.first()
@ -40,38 +43,12 @@ class LinkPreviewView : LinearLayout {
// TODO: Properly use glide and the actual thumbnail // TODO: Properly use glide and the actual thumbnail
// Title // Title
titleTextView.text = linkPreview.title titleTextView.text = linkPreview.title
// Body
mainLinkPreviewContainer.addView(VisibleMessageContentView.getBodyTextView(context, message))
} }
fun recycle() { fun recycle() {
// TODO: Implement // TODO: Implement
} }
// endregion // endregion
// region Convenience
private fun getOutlineProvider(isOutgoing: Boolean, isStartOfMessageCluster: Boolean, isEndOfMessageCluster: Boolean): ViewOutlineProvider {
return object : ViewOutlineProvider() {
override fun getOutline(view: View, outline: Outline) {
val path = Path()
val rect = RectF(0.0f, 0.0f, view.width.toFloat(), view.height.toFloat())
val topLeft = if (isOutgoing) {
resources.getDimension(R.dimen.message_corner_radius)
} else {
if (isStartOfMessageCluster) resources.getDimension(R.dimen.message_corner_radius) else resources.getDimension(R.dimen.message_corner_collapse_radius)
}
val topRight = if (isOutgoing) {
if (isStartOfMessageCluster) resources.getDimension(R.dimen.message_corner_radius) else resources.getDimension(R.dimen.message_corner_collapse_radius)
} else {
resources.getDimension(R.dimen.message_corner_radius)
}
path.addRoundRect(rect, floatArrayOf( topLeft, topLeft, topRight, topRight, 0.0f, 0.0f, 0.0f, 0.0f ), Path.Direction.CW)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
outline.setPath(path)
} else {
@Suppress("DEPRECATION") outline.setConvexPath(path)
}
}
}
}
// endregion
} }

View File

@ -55,16 +55,15 @@ class VisibleMessageContentView : LinearLayout {
onContentClick = null onContentClick = null
if (message is MmsMessageRecord && message.linkPreviews.isNotEmpty()) { if (message is MmsMessageRecord && message.linkPreviews.isNotEmpty()) {
val linkPreviewView = LinkPreviewView(context) val linkPreviewView = LinkPreviewView(context)
linkPreviewView.bind(message, glide, isStartOfMessageCluster, isEndOfMessageCluster) linkPreviewView.bind(message, glide, background)
mainContainer.addView(linkPreviewView) mainContainer.addView(linkPreviewView)
val bodyTextView = getBodyTextView(message) // Body text view is inside the link preview for layout convenience
mainContainer.addView(bodyTextView)
} else if (message is MmsMessageRecord && message.quote != null) { } else if (message is MmsMessageRecord && message.quote != null) {
val quote = message.quote!! val quote = message.quote!!
val quoteView = QuoteView(context, QuoteView.Mode.Regular) val quoteView = QuoteView(context, QuoteView.Mode.Regular)
quoteView.bind(quote.author.toString(), quote.text, quote.attachment, message.recipient, message.isOutgoing) quoteView.bind(quote.author.toString(), quote.text, quote.attachment, message.recipient, message.isOutgoing)
mainContainer.addView(quoteView) mainContainer.addView(quoteView)
val bodyTextView = getBodyTextView(message) val bodyTextView = VisibleMessageContentView.getBodyTextView(context, message)
ViewUtil.setPaddingTop(bodyTextView, 0) ViewUtil.setPaddingTop(bodyTextView, 0)
mainContainer.addView(bodyTextView) mainContainer.addView(bodyTextView)
} else if (message is MmsMessageRecord && message.slideDeck.audioSlide != null) { } else if (message is MmsMessageRecord && message.slideDeck.audioSlide != null) {
@ -74,12 +73,12 @@ class VisibleMessageContentView : LinearLayout {
onContentClick = { voiceMessageView.togglePlayback() } onContentClick = { voiceMessageView.togglePlayback() }
} else if (message is MmsMessageRecord && message.slideDeck.documentSlide != null) { } else if (message is MmsMessageRecord && message.slideDeck.documentSlide != null) {
val documentView = DocumentView(context) val documentView = DocumentView(context)
documentView.bind(message, getTextColor(message)) documentView.bind(message, VisibleMessageContentView.getTextColor(context, message))
mainContainer.addView(documentView) mainContainer.addView(documentView)
} else if (message is MmsMessageRecord && message.slideDeck.asAttachments().isNotEmpty()) { } else if (message is MmsMessageRecord && message.slideDeck.asAttachments().isNotEmpty()) {
throw IllegalStateException("Not yet implemented; we may want to use Signal's album view here.") throw IllegalStateException("Not yet implemented; we may want to use Signal's album view here.")
} else { } else {
val bodyTextView = getBodyTextView(message) val bodyTextView = VisibleMessageContentView.getBodyTextView(context, message)
mainContainer.addView(bodyTextView) mainContainer.addView(bodyTextView)
} }
} }
@ -105,27 +104,30 @@ class VisibleMessageContentView : LinearLayout {
// endregion // endregion
// region Convenience // region Convenience
private fun getBodyTextView(message: MessageRecord): TextView { companion object {
val result = TextView(context)
val vPadding = resources.getDimension(R.dimen.small_spacing).toInt()
val hPadding = toPx(12, resources)
result.setPadding(hPadding, vPadding, hPadding, vPadding)
result.text = message.body
result.setTextSize(TypedValue.COMPLEX_UNIT_PX, resources.getDimension(R.dimen.small_font_size))
val color = getTextColor(message)
result.setTextColor(color)
return result
}
@ColorInt fun getBodyTextView(context: Context, message: MessageRecord): TextView {
private fun getTextColor(message: MessageRecord): Int { val result = TextView(context)
val uiMode = UiModeUtilities.getUserSelectedUiMode(context) val vPadding = context.resources.getDimension(R.dimen.small_spacing).toInt()
val colorID = if (message.isOutgoing) { val hPadding = toPx(12, context.resources)
if (uiMode == UiMode.NIGHT) R.color.black else R.color.white result.setPadding(hPadding, vPadding, hPadding, vPadding)
} else { result.text = message.body
if (uiMode == UiMode.NIGHT) R.color.white else R.color.black result.setTextSize(TypedValue.COMPLEX_UNIT_PX, context.resources.getDimension(R.dimen.small_font_size))
val color = getTextColor(context, message)
result.setTextColor(color)
return result
}
@ColorInt
fun getTextColor(context: Context, message: MessageRecord): Int {
val uiMode = UiModeUtilities.getUserSelectedUiMode(context)
val colorID = if (message.isOutgoing) {
if (uiMode == UiMode.NIGHT) R.color.black else R.color.white
} else {
if (uiMode == UiMode.NIGHT) R.color.white else R.color.black
}
return context.resources.getColorWithID(colorID, context.theme)
} }
return resources.getColorWithID(colorID, context.theme)
} }
// endregion // endregion
} }

View File

@ -5,27 +5,34 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal" android:orientation="vertical">
android:gravity="center">
<ImageView <LinearLayout
android:id="@+id/thumbnailImageView"
android:layout_width="96dp"
android:layout_height="96dp"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/titleTextView"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="wrap_content"
android:paddingStart="12dp" android:orientation="horizontal"
android:gravity="center_vertical" android:gravity="center">
android:textSize="@dimen/small_font_size"
android:textStyle="bold" <ImageView
tools:text="The Day The Dinosaurs Died - Minute by Minute" android:id="@+id/thumbnailImageView"
android:maxLines="3" android:layout_width="96dp"
android:ellipsize="end" android:layout_height="96dp"
android:textColor="@color/text" android:scaleType="centerCrop" />
android:background="@color/link_preview_background" />
<TextView
android:id="@+id/titleTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingHorizontal="12dp"
android:gravity="center_vertical"
android:textSize="@dimen/small_font_size"
android:textStyle="bold"
tools:text="The Day The Dinosaurs Died - Minute by Minute"
android:maxLines="3"
android:ellipsize="end"
android:textColor="@color/text"
android:background="@color/link_preview_background" />
</LinearLayout>
</LinearLayout> </LinearLayout>