2021-06-01 06:56:58 +02:00
|
|
|
package org.thoughtcrime.securesms.conversation.v2.messages
|
|
|
|
|
|
|
|
import android.content.Context
|
2021-06-18 07:11:41 +02:00
|
|
|
import android.content.res.Resources
|
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-18 07:54:24 +02:00
|
|
|
import androidx.core.view.isVisible
|
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
|
|
|
|
import org.thoughtcrime.securesms.conversation.v2.utilities.TextUtilities
|
2021-06-18 07:54:24 +02:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory
|
2021-06-01 06:56:58 +02:00
|
|
|
import org.thoughtcrime.securesms.database.model.MessageRecord
|
2021-06-18 07:11:41 +02:00
|
|
|
import org.thoughtcrime.securesms.loki.utilities.toPx
|
|
|
|
import org.thoughtcrime.securesms.mms.SlideDeck
|
2021-06-18 07:54:24 +02:00
|
|
|
import kotlin.math.max
|
|
|
|
import kotlin.math.min
|
2021-06-01 06:56:58 +02:00
|
|
|
|
|
|
|
class QuoteView : LinearLayout {
|
2021-06-18 07:11:41 +02:00
|
|
|
private val screenWidth by lazy { Resources.getSystem().displayMetrics.widthPixels }
|
2021-06-18 07:54:24 +02:00
|
|
|
private val vPadding by lazy { toPx(6, resources) }
|
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
|
|
|
|
constructor(context: Context) : super(context) { initialize() }
|
|
|
|
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) { initialize() }
|
|
|
|
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) { initialize() }
|
2021-06-01 06:56:58 +02:00
|
|
|
|
2021-06-18 07:11:41 +02:00
|
|
|
private fun initialize() {
|
|
|
|
LayoutInflater.from(context).inflate(R.layout.view_quote, this)
|
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-18 07:54:24 +02:00
|
|
|
fun getIntrinsicContentHeight(): Int {
|
2021-06-18 07:11:41 +02:00
|
|
|
var result = 0
|
|
|
|
val width = screenWidth
|
|
|
|
val author = quoteViewAuthorTextView.text
|
2021-06-18 07:54:24 +02:00
|
|
|
val authorTextViewIntrinsicHeight = TextUtilities.getIntrinsicHeight(author, quoteViewAuthorTextView.paint, width)
|
|
|
|
result += authorTextViewIntrinsicHeight
|
2021-06-18 07:11:41 +02:00
|
|
|
val body = quoteViewBodyTextView.text
|
2021-06-18 07:54:24 +02:00
|
|
|
val bodyTextViewIntrinsicHeight = TextUtilities.getIntrinsicHeight(body, quoteViewBodyTextView.paint, width)
|
|
|
|
result += bodyTextViewIntrinsicHeight
|
|
|
|
if (!quoteViewAuthorTextView.isVisible) {
|
|
|
|
return min(max(result, toPx(32, resources)), toPx(54, resources))
|
|
|
|
} else {
|
|
|
|
return min(result, toPx(54, resources) + authorTextViewIntrinsicHeight)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun getIntrinsicHeight(): Int {
|
|
|
|
return getIntrinsicContentHeight() + 2 * vPadding
|
2021-06-01 06:56:58 +02:00
|
|
|
}
|
|
|
|
// endregion
|
|
|
|
|
|
|
|
// region Updating
|
2021-06-18 07:11:41 +02:00
|
|
|
fun bind(authorPublicKey: String, body: String, attachments: SlideDeck?, thread: Recipient) {
|
2021-06-18 07:54:24 +02:00
|
|
|
val contactDB = DatabaseFactory.getSessionContactDatabase(context)
|
|
|
|
// Author
|
|
|
|
if (thread.isGroupRecipient) {
|
|
|
|
val author = contactDB.getContactWithSessionID(authorPublicKey)
|
|
|
|
val authorDisplayName = author?.displayName(Contact.contextForRecipient(thread)) ?: authorPublicKey
|
|
|
|
quoteViewAuthorTextView.text = authorDisplayName
|
|
|
|
}
|
|
|
|
quoteViewAuthorTextView.isVisible = thread.isGroupRecipient
|
|
|
|
// Body
|
|
|
|
quoteViewBodyTextView.text = body
|
|
|
|
// Accent line
|
2021-06-18 07:11:41 +02:00
|
|
|
val accentLineLayoutParams = quoteViewAccentLine.layoutParams as RelativeLayout.LayoutParams
|
2021-06-18 07:54:24 +02:00
|
|
|
accentLineLayoutParams.height = getIntrinsicContentHeight()
|
2021-06-18 07:11:41 +02:00
|
|
|
quoteViewAccentLine.layoutParams = accentLineLayoutParams
|
2021-06-01 06:56:58 +02:00
|
|
|
}
|
|
|
|
// endregion
|
|
|
|
}
|