@ -2,10 +2,10 @@ package org.thoughtcrime.securesms.conversation.v2.messages
import android.content.Context
import android.content.res.ColorStateList
import android.text.StaticLayout
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.LinearLayout
import android.widget.RelativeLayout
import androidx.annotation.ColorInt
import androidx.core.content.res.ResourcesCompat
import androidx.core.text.toSpannable
@ -18,6 +18,8 @@ import org.session.libsession.utilities.recipients.Recipient
import org.thoughtcrime.securesms.conversation.v2.utilities.MentionUtilities
import org.thoughtcrime.securesms.conversation.v2.utilities.TextUtilities
import org.thoughtcrime.securesms.database.SessionContactDatabase
import org.thoughtcrime.securesms.database.model.Quote
import org.thoughtcrime.securesms.database.model.ThreadRecord
import org.thoughtcrime.securesms.mms.GlideRequests
import org.thoughtcrime.securesms.mms.SlideDeck
import org.thoughtcrime.securesms.util.MediaUtil
@ -26,7 +28,6 @@ import org.thoughtcrime.securesms.util.toPx
import javax.inject.Inject
import kotlin.math.max
import kotlin.math.min
import kotlin.math.roundToInt
// 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:
@ -61,10 +62,6 @@ class QuoteView : LinearLayout {
Mode . Regular -> {
binding . quoteViewCancelButton . isVisible = false
binding . mainQuoteViewContainer . setBackgroundColor ( ResourcesCompat . getColor ( resources , R . color . transparent , context . theme ) )
val quoteViewMainContentContainerLayoutParams = binding . quoteViewMainContentContainer . layoutParams as RelativeLayout . LayoutParams
// Since we're not showing the cancel button we can shorten the end margin
quoteViewMainContentContainerLayoutParams . marginEnd = resources . getDimension ( R . dimen . medium _spacing ) . roundToInt ( )
// binding.quoteViewMainContentContainer.layoutParams = quoteViewMainContentContainerLayoutParams
}
}
}
@ -75,7 +72,7 @@ class QuoteView : LinearLayout {
// If we're showing an attachment thumbnail, just constrain to the height of that
if ( binding . quoteViewAttachmentPreviewContainer . isVisible ) { return toPx ( 40 , resources ) }
var result = 0
va r authorTextViewIntrinsicHeight = 0
va l authorTextViewIntrinsicHeight : Int
if ( binding . quoteViewAuthorTextView . isVisible ) {
val author = binding . quoteViewAuthorTextView . text
authorTextViewIntrinsicHeight = TextUtilities . getIntrinsicHeight ( author , binding . quoteViewAuthorTextView . paint , maxContentWidth )
@ -110,7 +107,7 @@ class QuoteView : LinearLayout {
// region Updating
fun bind ( authorPublicKey : String , body : String ? , attachments : SlideDeck ? , thread : Recipient ,
isOutgoingMessage : Boolean , maxContentWidth: Int , isOpenGroupInvitation: Boolean , threadID : Long ,
isOutgoingMessage : Boolean , isOpenGroupInvitation: Boolean , threadID : Long ,
isOriginalMissing : Boolean , glide : GlideRequests ) {
// 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
@ -125,16 +122,13 @@ class QuoteView : LinearLayout {
}
binding . quoteViewAuthorTextView . isVisible = thread . isGroupRecipient
// Body
binding . quoteViewBodyTextView . text = if ( isOpenGroupInvitation ) resources . getString ( R . string . open _group _invitation _view _ _open _group _invitation ) else MentionUtilities . highlightMentions ( ( body ?: " " ) . toSpannable ( ) , threadID , context ) ;
binding . quoteViewBodyTextView . text = if ( isOpenGroupInvitation ) resources . getString ( R . string . open _group _invitation _view _ _open _group _invitation ) else MentionUtilities . highlightMentions ( ( body ?: " " ) . toSpannable ( ) , threadID , context )
binding . quoteViewBodyTextView . setTextColor ( getTextColor ( isOutgoingMessage ) )
// Accent line / attachment preview
val hasAttachments = ( attachments != null && attachments . asAttachments ( ) . isNotEmpty ( ) ) && !is OriginalMissing
binding . quoteViewAccentLine . isVisible = ! hasAttachments
binding . quoteViewAttachmentPreviewContainer . isVisible = hasAttachments
if ( ! hasAttachments ) {
val accentLineLayoutParams = binding . quoteViewAccentLine . layoutParams as RelativeLayout . LayoutParams
accentLineLayoutParams . height = getIntrinsicContentHeight ( maxContentWidth ) // Match the intrinsic * content * height
// binding.quoteViewAccentLine.layoutParams = accentLineLayoutParams
binding . quoteViewAccentLine . setBackgroundColor ( getLineColor ( isOutgoingMessage ) )
} else if ( attachments != null ) {
binding . quoteViewAttachmentPreviewImageView . imageTintList = ColorStateList . valueOf ( ResourcesCompat . getColor ( resources , R . color . white , context . theme ) )
@ -164,11 +158,6 @@ class QuoteView : LinearLayout {
}
}
}
//mainQuoteViewContainer.layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, getIntrinsicHeight(maxContentWidth))
// val quoteViewMainContentContainerLayoutParams = quoteViewMainContentContainer.layoutParams as RelativeLayout.LayoutParams
// The start margin is different if we just show the accent line vs if we show an attachment thumbnail
// quoteViewMainContentContainerLayoutParams.marginStart = if (!hasAttachments) toPx(16, resources) else toPx(48, resources)
// quoteViewMainContentContainer.layoutParams = quoteViewMainContentContainerLayoutParams
}
// endregion
@ -201,6 +190,31 @@ class QuoteView : LinearLayout {
ResourcesCompat . getColor ( resources , R . color . white , context . theme )
}
}
fun calculateWidth ( quote : Quote , bodyWidth : Int , maxContentWidth : Int , thread : Recipient ) : Int {
binding . quoteViewAuthorTextView . isVisible = thread . isGroupRecipient
var paddingWidth = resources . getDimensionPixelSize ( R . dimen . medium _spacing ) * 5 // initial horizontal padding
with ( binding ) {
if ( quoteViewAttachmentPreviewContainer . isVisible ) {
paddingWidth += toPx ( 40 , resources )
}
if ( quoteViewAccentLine . isVisible ) {
paddingWidth += resources . getDimensionPixelSize ( R . dimen . accent _line _thickness )
}
}
val quoteBodyWidth = StaticLayout . getDesiredWidth ( binding . quoteViewBodyTextView . text , binding . quoteViewBodyTextView . paint ) . toInt ( ) + paddingWidth
val quoteAuthorWidth = if ( thread . isGroupRecipient ) {
val authorPublicKey = quote . author . serialize ( )
val author = contactDb . getContactWithSessionID ( authorPublicKey )
val authorDisplayName = author ?. displayName ( Contact . contextForRecipient ( thread ) ) ?: authorPublicKey
StaticLayout . getDesiredWidth ( authorDisplayName , binding . quoteViewBodyTextView . paint ) . toInt ( ) + paddingWidth
} else 0
val quoteWidth = max ( quoteBodyWidth , quoteAuthorWidth )
val usedWidth = max ( quoteWidth , bodyWidth )
return min ( maxContentWidth , usedWidth )
}
// endregion
}