session-android/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/QuoteView.kt

202 lines
12 KiB
Kotlin
Raw Normal View History

2021-06-01 06:56:58 +02:00
package org.thoughtcrime.securesms.conversation.v2.messages
import android.content.Context
2021-06-21 06:24:00 +02:00
import android.content.res.ColorStateList
2021-06-01 06:56:58 +02:00
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.LinearLayout
2021-06-18 07:11:41 +02:00
import android.widget.RelativeLayout
2021-06-21 02:53:52 +02:00
import androidx.annotation.ColorInt
import androidx.core.content.res.ResourcesCompat
import androidx.core.text.toSpannable
2021-06-18 07:54:24 +02:00
import androidx.core.view.isVisible
import dagger.hilt.android.AndroidEntryPoint
2021-06-01 06:56:58 +02:00
import kotlinx.android.synthetic.main.view_quote.view.*
import network.loki.messenger.R
2021-06-18 07:54:24 +02:00
import org.session.libsession.messaging.contacts.Contact
2021-06-18 07:11:41 +02:00
import org.session.libsession.utilities.recipients.Recipient
2021-07-09 05:18:48 +02:00
import org.thoughtcrime.securesms.conversation.v2.utilities.MentionUtilities
2021-06-18 07:11:41 +02:00
import org.thoughtcrime.securesms.conversation.v2.utilities.TextUtilities
import org.thoughtcrime.securesms.database.SessionContactDatabase
2021-06-29 06:05:32 +02:00
import org.thoughtcrime.securesms.mms.GlideRequests
2021-06-18 07:11:41 +02:00
import org.thoughtcrime.securesms.mms.SlideDeck
2021-06-29 06:05:32 +02:00
import org.thoughtcrime.securesms.util.MediaUtil
2021-07-09 05:18:48 +02:00
import org.thoughtcrime.securesms.util.UiModeUtilities
import org.thoughtcrime.securesms.util.toPx
import javax.inject.Inject
2021-06-18 07:54:24 +02:00
import kotlin.math.max
import kotlin.math.min
2021-06-21 02:53:52 +02:00
import kotlin.math.roundToInt
2021-06-01 06:56:58 +02:00
2021-06-23 05:57:13 +02:00
// There's quite some calculation going on here. It's a bit complex so don't make changes
// if you don't need to. If you do then test:
// • Quoted text in both private chats and group chats
// • Quoted images and videos in both private chats and group chats
// • Quoted voice messages and documents in both private chats and group chats
// • All of the above in both dark mode and light mode
@AndroidEntryPoint
2021-06-01 06:56:58 +02:00
class QuoteView : LinearLayout {
@Inject lateinit var contactDb: SessionContactDatabase
2021-06-21 02:53:52 +02:00
private lateinit var mode: Mode
2021-06-18 07:54:24 +02:00
private val vPadding by lazy { toPx(6, resources) }
2021-06-18 08:04:22 +02:00
var delegate: QuoteViewDelegate? = null
2021-06-01 06:56:58 +02:00
2021-06-18 07:11:41 +02:00
enum class Mode { Regular, Draft }
2021-06-01 06:56:58 +02:00
2021-06-18 07:11:41 +02:00
// region Lifecycle
2021-06-21 02:53:52 +02:00
constructor(context: Context) : super(context) { throw IllegalAccessError("Use QuoteView(context:mode:) instead.") }
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) { throw IllegalAccessError("Use QuoteView(context:mode:) instead.") }
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) { throw IllegalAccessError("Use QuoteView(context:mode:) instead.") }
2021-06-01 06:56:58 +02:00
2021-06-21 02:53:52 +02:00
constructor(context: Context, mode: Mode) : super(context) {
this.mode = mode
2021-06-18 07:11:41 +02:00
LayoutInflater.from(context).inflate(R.layout.view_quote, this)
2021-06-23 05:57:13 +02:00
// Add padding here (not on mainQuoteViewContainer) to get a bit of a top inset while avoiding
// the clipping issue described in getIntrinsicHeight(maxContentWidth:).
2021-06-23 03:32:05 +02:00
setPadding(0, toPx(6, resources), 0, 0)
2021-06-21 02:53:52 +02:00
when (mode) {
Mode.Draft -> quoteViewCancelButton.setOnClickListener { delegate?.cancelQuoteDraft() }
Mode.Regular -> {
quoteViewCancelButton.isVisible = false
mainQuoteViewContainer.setBackgroundColor(ResourcesCompat.getColor(resources, R.color.transparent, context.theme))
val quoteViewMainContentContainerLayoutParams = quoteViewMainContentContainer.layoutParams as RelativeLayout.LayoutParams
2021-06-23 05:57:13 +02:00
// Since we're not showing the cancel button we can shorten the end margin
2021-06-21 02:53:52 +02:00
quoteViewMainContentContainerLayoutParams.marginEnd = resources.getDimension(R.dimen.medium_spacing).roundToInt()
quoteViewMainContentContainer.layoutParams = quoteViewMainContentContainerLayoutParams
}
}
2021-06-01 06:56:58 +02:00
}
2021-06-18 07:11:41 +02:00
// endregion
2021-06-01 06:56:58 +02:00
2021-06-18 07:11:41 +02:00
// region General
2021-06-23 03:32:05 +02:00
fun getIntrinsicContentHeight(maxContentWidth: Int): Int {
2021-06-23 05:57:13 +02:00
// If we're showing an attachment thumbnail, just constrain to the height of that
2021-06-21 06:24:00 +02:00
if (quoteViewAttachmentPreviewContainer.isVisible) { return toPx(40, resources) }
2021-06-18 07:11:41 +02:00
var result = 0
2021-06-21 05:43:49 +02:00
var authorTextViewIntrinsicHeight = 0
if (quoteViewAuthorTextView.isVisible) {
val author = quoteViewAuthorTextView.text
authorTextViewIntrinsicHeight = TextUtilities.getIntrinsicHeight(author, quoteViewAuthorTextView.paint, maxContentWidth)
2021-06-21 05:43:49 +02:00
result += authorTextViewIntrinsicHeight
}
2021-06-18 07:11:41 +02:00
val body = quoteViewBodyTextView.text
val bodyTextViewIntrinsicHeight = TextUtilities.getIntrinsicHeight(body, quoteViewBodyTextView.paint, maxContentWidth)
val staticLayout = TextUtilities.getIntrinsicLayout(body, quoteViewBodyTextView.paint, maxContentWidth)
2021-06-18 07:54:24 +02:00
result += bodyTextViewIntrinsicHeight
if (!quoteViewAuthorTextView.isVisible) {
// We want to at least be as high as the cancel button 36DP, and no higher than 3 lines of text.
// Height from intrinsic layout is the height of the text before truncation so we shorten
// proportionally to our max lines setting.
return max(toPx(32, resources) ,min((result / staticLayout.lineCount) * 3, result))
2021-06-18 07:54:24 +02:00
} else {
2021-06-23 05:57:13 +02:00
// Because we're showing the author text view, we should have a height of at least 32 DP
// anyway, so there's no need to constrain to that. We constrain to a max height of 56 DP
// because that's approximately the height of the author text view + 2 lines of the body
// text view.
2021-06-23 05:39:24 +02:00
return min(result, toPx(56, resources))
2021-06-18 07:54:24 +02:00
}
}
2021-06-23 03:32:05 +02:00
fun getIntrinsicHeight(maxContentWidth: Int): Int {
2021-06-23 05:57:13 +02:00
// The way all this works is that we just calculate the total height the quote view should be
// and then center everything inside vertically. This effectively means we're applying padding.
// Applying padding the regular way results in a clipping issue though due to a bug in
// RelativeLayout.
return getIntrinsicContentHeight(maxContentWidth) + (2 * vPadding )
2021-06-01 06:56:58 +02:00
}
// endregion
// region Updating
2021-06-23 03:32:05 +02:00
fun bind(authorPublicKey: String, body: String?, attachments: SlideDeck?, thread: Recipient,
isOutgoingMessage: Boolean, maxContentWidth: Int, isOpenGroupInvitation: Boolean, threadID: Long,
isOriginalMissing: Boolean, glide: GlideRequests) {
2021-06-23 05:57:13 +02:00
// Reduce the max body text view line count to 2 if this is a group thread because
// we'll be showing the author text view and we don't want the overall quote view height
// to get too big.
quoteViewBodyTextView.maxLines = if (thread.isGroupRecipient) 2 else 3
2021-06-18 07:54:24 +02:00
// Author
if (thread.isGroupRecipient) {
val author = contactDb.getContactWithSessionID(authorPublicKey)
2021-06-18 07:54:24 +02:00
val authorDisplayName = author?.displayName(Contact.contextForRecipient(thread)) ?: authorPublicKey
quoteViewAuthorTextView.text = authorDisplayName
2021-06-21 02:53:52 +02:00
quoteViewAuthorTextView.setTextColor(getTextColor(isOutgoingMessage))
2021-06-18 07:54:24 +02:00
}
quoteViewAuthorTextView.isVisible = thread.isGroupRecipient
// Body
quoteViewBodyTextView.text = if (isOpenGroupInvitation) resources.getString(R.string.open_group_invitation_view__open_group_invitation) else MentionUtilities.highlightMentions((body ?: "").toSpannable(), threadID, context);
2021-06-21 02:53:52 +02:00
quoteViewBodyTextView.setTextColor(getTextColor(isOutgoingMessage))
2021-06-21 06:24:00 +02:00
// Accent line / attachment preview
val hasAttachments = (attachments != null && attachments.asAttachments().isNotEmpty()) && !isOriginalMissing
2021-06-21 06:24:00 +02:00
quoteViewAccentLine.isVisible = !hasAttachments
quoteViewAttachmentPreviewContainer.isVisible = hasAttachments
if (!hasAttachments) {
val accentLineLayoutParams = quoteViewAccentLine.layoutParams as RelativeLayout.LayoutParams
2021-06-23 05:57:13 +02:00
accentLineLayoutParams.height = getIntrinsicContentHeight(maxContentWidth) // Match the intrinsic * content * height
2021-06-21 06:24:00 +02:00
quoteViewAccentLine.layoutParams = accentLineLayoutParams
quoteViewAccentLine.setBackgroundColor(getLineColor(isOutgoingMessage))
} else if (attachments != null) {
2021-06-21 06:24:00 +02:00
quoteViewAttachmentPreviewImageView.imageTintList = ColorStateList.valueOf(ResourcesCompat.getColor(resources, R.color.white, context.theme))
2021-06-21 07:26:09 +02:00
val backgroundColorID = if (UiModeUtilities.isDayUiMode(context)) R.color.black else R.color.accent
val backgroundColor = ResourcesCompat.getColor(resources, backgroundColorID, context.theme)
quoteViewAttachmentPreviewContainer.backgroundTintList = ColorStateList.valueOf(backgroundColor)
2021-06-29 06:05:32 +02:00
quoteViewAttachmentPreviewImageView.isVisible = false
quoteViewAttachmentThumbnailImageView.isVisible = false
2021-06-21 06:24:00 +02:00
if (attachments.audioSlide != null) {
quoteViewAttachmentPreviewImageView.setImageResource(R.drawable.ic_microphone)
2021-06-29 06:05:32 +02:00
quoteViewAttachmentPreviewImageView.isVisible = true
2021-06-21 06:24:00 +02:00
quoteViewBodyTextView.text = resources.getString(R.string.Slide_audio)
} else if (attachments.documentSlide != null) {
quoteViewAttachmentPreviewImageView.setImageResource(R.drawable.ic_document_large_light)
2021-06-29 06:05:32 +02:00
quoteViewAttachmentPreviewImageView.isVisible = true
2021-06-21 06:24:00 +02:00
quoteViewBodyTextView.text = resources.getString(R.string.document)
2021-06-29 06:05:32 +02:00
} else if (attachments.thumbnailSlide != null) {
val slide = attachments.thumbnailSlide!!
// This internally fetches the thumbnail
quoteViewAttachmentThumbnailImageView.radius = toPx(4, resources)
quoteViewAttachmentThumbnailImageView.setImageResource(glide, slide, false, false)
quoteViewAttachmentThumbnailImageView.isVisible = true
quoteViewBodyTextView.text = if (MediaUtil.isVideo(slide.asAttachment())) resources.getString(R.string.Slide_video) else resources.getString(R.string.Slide_image)
2021-06-21 06:24:00 +02:00
}
}
2021-06-23 03:32:05 +02:00
mainQuoteViewContainer.layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, getIntrinsicHeight(maxContentWidth))
2021-06-21 06:24:00 +02:00
val quoteViewMainContentContainerLayoutParams = quoteViewMainContentContainer.layoutParams as RelativeLayout.LayoutParams
2021-06-23 05:57:13 +02:00
// The start margin is different if we just show the accent line vs if we show an attachment thumbnail
2021-06-21 06:24:00 +02:00
quoteViewMainContentContainerLayoutParams.marginStart = if (!hasAttachments) toPx(16, resources) else toPx(48, resources)
quoteViewMainContentContainer.layoutParams = quoteViewMainContentContainerLayoutParams
2021-06-21 02:53:52 +02:00
}
// endregion
// region Convenience
@ColorInt private fun getLineColor(isOutgoingMessage: Boolean): Int {
val isLightMode = UiModeUtilities.isDayUiMode(context)
if ((mode == Mode.Regular && isLightMode) || (mode == Mode.Draft && isLightMode)) {
return ResourcesCompat.getColor(resources, R.color.black, context.theme)
} else if (mode == Mode.Regular && !isLightMode) {
if (isOutgoingMessage) {
return ResourcesCompat.getColor(resources, R.color.black, context.theme)
} else {
return ResourcesCompat.getColor(resources, R.color.accent, context.theme)
}
} else { // Draft & dark mode
return ResourcesCompat.getColor(resources, R.color.accent, context.theme)
}
}
@ColorInt private fun getTextColor(isOutgoingMessage: Boolean): Int {
if (mode == Mode.Draft) { return ResourcesCompat.getColor(resources, R.color.text, context.theme) }
val isLightMode = UiModeUtilities.isDayUiMode(context)
if ((isOutgoingMessage && !isLightMode) || (!isOutgoingMessage && isLightMode)) {
return ResourcesCompat.getColor(resources, R.color.black, context.theme)
} else {
return ResourcesCompat.getColor(resources, R.color.white, context.theme)
}
2021-06-01 06:56:58 +02:00
}
// endregion
2021-06-18 08:04:22 +02:00
}
interface QuoteViewDelegate {
fun cancelQuoteDraft()
2021-06-01 06:56:58 +02:00
}