2021-05-31 06:06:02 +02:00
|
|
|
package org.thoughtcrime.securesms.conversation.v2.messages
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.util.AttributeSet
|
2021-06-01 06:28:14 +02:00
|
|
|
import android.view.Gravity
|
2021-05-31 06:29:11 +02:00
|
|
|
import android.view.LayoutInflater
|
2021-06-01 05:01:03 +02:00
|
|
|
import android.view.View
|
2021-06-01 06:28:14 +02:00
|
|
|
import android.view.ViewGroup
|
2021-05-31 06:06:02 +02:00
|
|
|
import android.widget.LinearLayout
|
2021-06-01 01:48:02 +02:00
|
|
|
import kotlinx.android.synthetic.main.view_visible_message.view.*
|
2021-05-31 06:29:11 +02:00
|
|
|
import network.loki.messenger.R
|
2021-06-01 05:01:03 +02:00
|
|
|
import org.session.libsession.messaging.contacts.Contact.ContactContext
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory
|
2021-05-31 06:06:02 +02:00
|
|
|
import org.thoughtcrime.securesms.database.model.MessageRecord
|
|
|
|
|
2021-06-01 01:48:02 +02:00
|
|
|
class VisibleMessageView : LinearLayout {
|
2021-05-31 06:06:02 +02:00
|
|
|
|
|
|
|
// region Lifecycle
|
|
|
|
constructor(context: Context) : super(context) {
|
|
|
|
setUpViewHierarchy()
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
|
|
|
|
setUpViewHierarchy()
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
|
|
|
|
setUpViewHierarchy()
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun setUpViewHierarchy() {
|
2021-06-01 01:48:02 +02:00
|
|
|
LayoutInflater.from(context).inflate(R.layout.view_visible_message, this)
|
2021-06-01 06:28:14 +02:00
|
|
|
layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
|
2021-05-31 06:06:02 +02:00
|
|
|
}
|
|
|
|
// endregion
|
|
|
|
|
|
|
|
// region Updating
|
|
|
|
fun bind(message: MessageRecord) {
|
2021-06-01 05:01:03 +02:00
|
|
|
val sender = message.individualRecipient
|
|
|
|
val senderSessionID = sender.address.serialize()
|
|
|
|
val threadID = message.threadId
|
|
|
|
val threadDB = DatabaseFactory.getThreadDatabase(context)
|
|
|
|
val thread = threadDB.getRecipientForThreadId(threadID)
|
|
|
|
val contactDB = DatabaseFactory.getSessionContactDatabase(context)
|
|
|
|
val isGroupThread = (thread?.isGroupRecipient == true)
|
2021-06-01 06:28:14 +02:00
|
|
|
// Show profile picture and sender name if this is a group thread AND
|
|
|
|
// the message is incoming
|
|
|
|
if (isGroupThread && !message.isOutgoing) {
|
2021-06-01 05:01:03 +02:00
|
|
|
profilePictureContainer.visibility = View.VISIBLE
|
|
|
|
profilePictureView.publicKey = senderSessionID
|
|
|
|
// TODO: Set glide on the profile picture view and update it
|
|
|
|
// TODO: Show crown if this is an open group and the user is a moderator; otherwise hide it
|
|
|
|
senderNameTextView.visibility = View.VISIBLE
|
|
|
|
val context = if (thread?.isOpenGroupRecipient == true) ContactContext.OPEN_GROUP else ContactContext.REGULAR
|
|
|
|
senderNameTextView.text = contactDB.getContactWithSessionID(senderSessionID)?.displayName(context) ?: senderSessionID
|
|
|
|
} else {
|
|
|
|
profilePictureContainer.visibility = View.GONE
|
|
|
|
senderNameTextView.visibility = View.GONE
|
|
|
|
}
|
2021-06-01 06:28:14 +02:00
|
|
|
// Margins
|
|
|
|
val messageContentViewLayoutParams = messageContentView.layoutParams as LinearLayout.LayoutParams
|
|
|
|
messageContentViewLayoutParams.leftMargin = if (message.isOutgoing) resources.getDimension(R.dimen.very_large_spacing).toInt() else 0
|
|
|
|
messageContentViewLayoutParams.rightMargin = if (message.isOutgoing) 0 else resources.getDimension(R.dimen.very_large_spacing).toInt()
|
|
|
|
messageContentView.layoutParams = messageContentViewLayoutParams
|
|
|
|
// Gravity
|
|
|
|
gravity = if (message.isOutgoing) Gravity.RIGHT else Gravity.LEFT
|
2021-06-01 05:26:57 +02:00
|
|
|
// Populate content view
|
|
|
|
messageContentView.bind(message)
|
2021-05-31 06:06:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fun recycle() {
|
2021-06-01 05:01:03 +02:00
|
|
|
profilePictureView.recycle()
|
2021-05-31 06:06:02 +02:00
|
|
|
}
|
|
|
|
// endregion
|
|
|
|
}
|