session-android/src/org/thoughtcrime/securesms/loki/views/UserView.kt

102 lines
4.2 KiB
Kotlin
Raw Normal View History

2020-05-11 08:19:26 +02:00
package org.thoughtcrime.securesms.loki.views
2020-01-31 03:57:24 +01:00
import android.content.Context
import android.text.TextUtils
2020-01-31 03:57:24 +01:00
import android.util.AttributeSet
import android.view.LayoutInflater
2020-04-15 08:37:41 +02:00
import android.view.View
2020-01-31 03:57:24 +01:00
import android.widget.LinearLayout
import kotlinx.android.synthetic.main.view_conversation.view.profilePictureView
import kotlinx.android.synthetic.main.view_user.view.*
import network.loki.messenger.R
import org.thoughtcrime.securesms.database.DatabaseFactory
2020-04-15 05:06:10 +02:00
import org.thoughtcrime.securesms.groups.GroupManager
2020-01-31 03:57:24 +01:00
import org.thoughtcrime.securesms.mms.GlideRequests
import org.thoughtcrime.securesms.recipients.Recipient
2020-05-07 09:59:41 +02:00
import org.whispersystems.signalservice.loki.protocol.mentions.MentionsManager
2020-01-31 03:57:24 +01:00
class UserView : LinearLayout {
2020-08-18 00:55:17 +02:00
enum class ActionIndicator {
None,
Menu,
Tick
}
2020-01-31 03:57:24 +01: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()
}
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) {
setUpViewHierarchy()
}
private fun setUpViewHierarchy() {
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
2020-01-31 03:57:24 +01:00
val contentView = inflater.inflate(R.layout.view_user, null)
addView(contentView)
}
// endregion
// region Updating
fun bind(user: Recipient, glide: GlideRequests, actionIndicator: ActionIndicator, isSelected: Boolean = false) {
2020-04-15 05:06:10 +02:00
val address = user.address.serialize()
if (user.isGroupRecipient) {
2020-05-06 03:43:04 +02:00
if ("Session Public Chat" == user.name || user.address.isRSSFeed) {
2020-07-15 06:26:20 +02:00
profilePictureView.publicKey = ""
profilePictureView.displayName = null
2020-07-15 06:26:20 +02:00
profilePictureView.additionalPublicKey = null
2020-04-15 05:06:10 +02:00
profilePictureView.isRSSFeed = true
} else {
2020-05-12 08:33:04 +02:00
val threadID = GroupManager.getThreadIDFromGroupID(address, context)
val userKeys = MentionsManager.shared.userPublicKeyCache[threadID]?.toList() ?: listOf()
val sortedUserKeys = userKeys.sorted() // Sort to provide a level of stability
val userKey0 = sortedUserKeys.getOrNull(0) ?: ""
val userKey1 = sortedUserKeys.getOrNull(1) ?: ""
profilePictureView.publicKey = userKey0
profilePictureView.displayName = getUserDisplayName(userKey0)
profilePictureView.additionalPublicKey = userKey1
profilePictureView.additionalDisplayName = getUserDisplayName(userKey1)
2020-04-15 05:06:10 +02:00
profilePictureView.isRSSFeed = false
}
} else {
2020-07-15 06:26:20 +02:00
profilePictureView.publicKey = address
profilePictureView.displayName = getUserDisplayName(address)
2020-07-15 06:26:20 +02:00
profilePictureView.additionalPublicKey = null
2020-04-15 05:06:10 +02:00
profilePictureView.isRSSFeed = false
}
actionIndicatorImageView.setImageResource(R.drawable.ic_baseline_edit_24)
2020-01-31 03:57:24 +01:00
profilePictureView.glide = glide
profilePictureView.update()
2020-04-15 05:06:10 +02:00
nameTextView.text = user.name ?: "Unknown Contact"
when (actionIndicator) {
2020-08-18 00:55:17 +02:00
ActionIndicator.None -> {
actionIndicatorImageView.visibility = View.GONE
}
2020-08-18 00:55:17 +02:00
ActionIndicator.Menu -> {
actionIndicatorImageView.visibility = View.VISIBLE
actionIndicatorImageView.setImageResource(R.drawable.ic_more_horiz_white)
}
2020-08-18 00:55:17 +02:00
ActionIndicator.Tick -> {
actionIndicatorImageView.visibility = View.VISIBLE
actionIndicatorImageView.setImageResource(if (isSelected) R.drawable.ic_circle_check else R.drawable.ic_circle)
}
}
2020-01-31 03:57:24 +01:00
}
private fun getUserDisplayName(publicKey: String?): String? {
if (TextUtils.isEmpty(publicKey)) return null
return DatabaseFactory.getLokiUserDatabase(context).getDisplayName(publicKey!!)
}
2020-01-31 03:57:24 +01:00
// endregion
}