session-android/app/src/main/java/org/thoughtcrime/securesms/home/ConversationView.kt

108 lines
5.3 KiB
Kotlin
Raw Normal View History

2021-07-09 03:14:21 +02:00
package org.thoughtcrime.securesms.home
2019-12-17 14:27:59 +01:00
import android.content.Context
2021-06-21 01:45:09 +02:00
import android.content.res.Resources
2019-12-17 15:15:13 +01:00
import android.graphics.Typeface
2019-12-17 14:27:59 +01:00
import android.util.AttributeSet
2021-06-24 02:18:52 +02:00
import android.util.TypedValue
2019-12-17 14:27:59 +01:00
import android.view.LayoutInflater
2019-12-17 15:15:13 +01:00
import android.view.View
2019-12-17 14:27:59 +01:00
import android.widget.LinearLayout
import androidx.core.content.ContextCompat
2021-06-24 02:18:52 +02:00
import androidx.core.view.isVisible
2021-06-21 01:45:09 +02:00
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.view_conversation.view.*
2019-12-17 14:27:59 +01:00
import network.loki.messenger.R
import org.session.libsession.utilities.recipients.Recipient
2021-07-09 05:18:48 +02:00
import org.thoughtcrime.securesms.conversation.v2.utilities.MentionManagerUtilities.populateUserPublicKeyCacheIfNeeded
import org.thoughtcrime.securesms.conversation.v2.utilities.MentionUtilities.highlightMentions
import org.thoughtcrime.securesms.database.RecipientDatabase
import org.thoughtcrime.securesms.database.model.ThreadRecord
2019-12-19 11:15:58 +01:00
import org.thoughtcrime.securesms.mms.GlideRequests
2019-12-17 15:15:13 +01:00
import org.thoughtcrime.securesms.util.DateUtils
import java.util.*
2019-12-17 14:27:59 +01:00
class ConversationView : LinearLayout {
2021-06-21 01:45:09 +02:00
private val screenWidth = Resources.getSystem().displayMetrics.widthPixels
2019-12-17 15:15:13 +01:00
var thread: ThreadRecord? = null
2019-12-17 14:27:59 +01:00
// region Lifecycle
2021-06-21 01:40:23 +02:00
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() }
2019-12-17 14:27:59 +01:00
2021-06-21 01:40:23 +02:00
private fun initialize() {
2021-05-27 07:00:16 +02:00
LayoutInflater.from(context).inflate(R.layout.view_conversation, this)
2021-06-21 01:45:09 +02:00
layoutParams = RecyclerView.LayoutParams(screenWidth, RecyclerView.LayoutParams.WRAP_CONTENT)
2019-12-17 16:24:42 +01:00
}
2019-12-17 14:27:59 +01:00
// endregion
// region Updating
2019-12-19 11:15:58 +01:00
fun bind(thread: ThreadRecord, isTyping: Boolean, glide: GlideRequests) {
2019-12-17 15:15:13 +01:00
this.thread = thread
2020-07-16 03:20:39 +02:00
populateUserPublicKeyCacheIfNeeded(thread.threadId, context) // FIXME: This is a bad place to do this
2021-06-24 02:18:52 +02:00
val unreadCount = thread.unreadCount
2020-07-16 03:20:39 +02:00
if (thread.recipient.isBlocked) {
accentView.setBackgroundResource(R.color.destructive)
accentView.visibility = View.VISIBLE
} else {
accentView.setBackgroundResource(R.color.accent)
2021-06-24 02:18:52 +02:00
accentView.visibility = if (unreadCount > 0) View.VISIBLE else View.INVISIBLE
2020-07-16 03:20:39 +02:00
}
2021-06-24 02:18:52 +02:00
val formattedUnreadCount = if (unreadCount < 100) unreadCount.toString() else "99+"
unreadCountTextView.text = formattedUnreadCount
val textSize = if (unreadCount < 100) 12.0f else 9.0f
unreadCountTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textSize)
unreadCountTextView.setTypeface(Typeface.DEFAULT, if (unreadCount < 100) Typeface.BOLD else Typeface.NORMAL)
unreadCountIndicator.isVisible = (unreadCount != 0)
2019-12-19 11:15:58 +01:00
profilePictureView.glide = glide
profilePictureView.update(thread.recipient, thread.threadId)
2021-05-12 08:27:40 +02:00
val senderDisplayName = getUserDisplayName(thread.recipient) ?: thread.recipient.address.toString()
2021-06-21 01:40:23 +02:00
conversationViewDisplayNameTextView.text = senderDisplayName
timestampTextView.text = DateUtils.getDisplayFormattedTimeSpanString(context, Locale.getDefault(), thread.date)
val recipient = thread.recipient
muteIndicatorImageView.isVisible = recipient.isMuted || recipient.notifyType != RecipientDatabase.NOTIFY_TYPE_ALL
val drawableRes = if (recipient.isMuted || recipient.notifyType == RecipientDatabase.NOTIFY_TYPE_NONE) {
R.drawable.ic_outline_notifications_off_24
} else {
R.drawable.ic_notifications_mentions
}
muteIndicatorImageView.setImageResource(drawableRes)
2019-12-17 15:15:13 +01:00
val rawSnippet = thread.getDisplayBody(context)
val snippet = highlightMentions(rawSnippet, thread.threadId, context)
2019-12-17 15:15:13 +01:00
snippetTextView.text = snippet
2021-06-24 02:18:52 +02:00
snippetTextView.typeface = if (unreadCount > 0) Typeface.DEFAULT_BOLD else Typeface.DEFAULT
2019-12-19 11:15:58 +01:00
snippetTextView.visibility = if (isTyping) View.GONE else View.VISIBLE
if (isTyping) {
typingIndicatorView.startAnimation()
} else {
typingIndicatorView.stopAnimation()
}
typingIndicatorView.visibility = if (isTyping) View.VISIBLE else View.GONE
statusIndicatorImageView.visibility = View.VISIBLE
when {
2021-05-27 07:00:16 +02:00
!thread.isOutgoing -> statusIndicatorImageView.visibility = View.GONE
thread.isFailed -> {
val drawable = ContextCompat.getDrawable(context, R.drawable.ic_error)?.mutate()
drawable?.setTint(ContextCompat.getColor(context,R.color.destructive))
statusIndicatorImageView.setImageDrawable(drawable)
}
2019-12-19 11:15:58 +01:00
thread.isPending -> statusIndicatorImageView.setImageResource(R.drawable.ic_circle_dot_dot_dot)
2021-05-31 07:53:25 +02:00
thread.isRead -> statusIndicatorImageView.setImageResource(R.drawable.ic_filled_circle_check)
2019-12-19 11:15:58 +01:00
else -> statusIndicatorImageView.setImageResource(R.drawable.ic_circle_check)
}
2019-12-17 14:27:59 +01:00
}
fun recycle() {
profilePictureView.recycle()
}
2021-05-12 08:27:40 +02:00
private fun getUserDisplayName(recipient: Recipient): String? {
2021-05-24 02:27:31 +02:00
if (recipient.isLocalNumber) {
return context.getString(R.string.note_to_self)
} else {
return recipient.name // Internally uses the Contact API
}
}
2019-12-17 14:27:59 +01:00
// endregion
}