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

85 lines
4.5 KiB
Kotlin
Raw Normal View History

2020-05-11 08:19:26 +02:00
package org.thoughtcrime.securesms.loki.views
2019-12-17 16:24:42 +01:00
import android.content.Context
2020-01-06 00:18:43 +01:00
import android.support.annotation.DimenRes
2019-12-17 16:24:42 +01:00
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
2019-12-19 11:15:58 +01:00
import android.widget.ImageView
2019-12-17 16:24:42 +01:00
import android.widget.RelativeLayout
2019-12-19 11:15:58 +01:00
import com.bumptech.glide.load.engine.DiskCacheStrategy
import kotlinx.android.synthetic.main.view_profile_picture.view.*
2019-12-17 16:24:42 +01:00
import network.loki.messenger.R
2020-01-07 04:51:11 +01:00
import org.thoughtcrime.securesms.contacts.avatars.ProfileContactPhoto
2019-12-19 11:15:58 +01:00
import org.thoughtcrime.securesms.database.Address
2020-05-12 03:46:11 +02:00
import org.thoughtcrime.securesms.loki.todo.JazzIdenticonDrawable
2019-12-19 11:15:58 +01:00
import org.thoughtcrime.securesms.mms.GlideRequests
import org.thoughtcrime.securesms.recipients.Recipient
2020-04-01 05:16:31 +02:00
import org.thoughtcrime.securesms.util.TextSecurePreferences
2019-12-17 16:24:42 +01:00
2020-01-06 06:05:57 +01:00
// TODO: Look into a better way of handling different sizes. Maybe an enum (with associated values) encapsulating the different modes?
2020-01-06 04:26:52 +01:00
2019-12-17 16:24:42 +01:00
class ProfilePictureView : RelativeLayout {
2019-12-19 11:15:58 +01:00
lateinit var glide: GlideRequests
2019-12-17 16:24:42 +01:00
var hexEncodedPublicKey: String? = null
var additionalHexEncodedPublicKey: String? = null
var isRSSFeed = false
2020-01-06 04:26:52 +01:00
var isLarge = false
2019-12-17 16:24:42 +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.applicationContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val contentView = inflater.inflate(R.layout.view_profile_picture, null)
2019-12-17 16:24:42 +01:00
addView(contentView)
}
// endregion
2019-12-17 16:24:42 +01:00
// region Updating
2019-12-17 16:24:42 +01:00
fun update() {
val hexEncodedPublicKey = hexEncodedPublicKey ?: return
val additionalHexEncodedPublicKey = additionalHexEncodedPublicKey
doubleModeImageViewContainer.visibility = if (additionalHexEncodedPublicKey != null && !isRSSFeed) View.VISIBLE else View.INVISIBLE
2020-01-06 04:26:52 +01:00
singleModeImageViewContainer.visibility = if (additionalHexEncodedPublicKey == null && !isRSSFeed && !isLarge) View.VISIBLE else View.INVISIBLE
largeSingleModeImageViewContainer.visibility = if (additionalHexEncodedPublicKey == null && !isRSSFeed && isLarge) View.VISIBLE else View.INVISIBLE
2020-01-23 00:25:22 +01:00
rssImageView.visibility = if (isRSSFeed) View.VISIBLE else View.INVISIBLE
2020-01-06 00:18:43 +01:00
fun setProfilePictureIfNeeded(imageView: ImageView, hexEncodedPublicKey: String, @DimenRes sizeID: Int) {
2019-12-19 11:15:58 +01:00
glide.clear(imageView)
if (hexEncodedPublicKey.isNotEmpty()) {
2020-04-01 05:16:31 +02:00
val recipient = Recipient.from(context, Address.fromSerialized(hexEncodedPublicKey), false);
val signalProfilePicture = recipient.contactPhoto
2020-01-31 22:59:53 +01:00
if (signalProfilePicture != null && (signalProfilePicture as? ProfileContactPhoto)?.avatarObject != "0" && (signalProfilePicture as? ProfileContactPhoto)?.avatarObject != "") {
2019-12-19 11:15:58 +01:00
glide.load(signalProfilePicture).diskCacheStrategy(DiskCacheStrategy.ALL).circleCrop().into(imageView)
} else {
2020-01-06 00:18:43 +01:00
val size = resources.getDimensionPixelSize(sizeID)
2020-04-06 06:34:19 +02:00
val masterHexEncodedPublicKey = TextSecurePreferences.getMasterHexEncodedPublicKey(context)
val hepk = if (recipient.isLocalNumber && masterHexEncodedPublicKey != null) masterHexEncodedPublicKey else hexEncodedPublicKey
val jazzIcon = JazzIdenticonDrawable(size, size, hepk)
2020-01-06 00:18:43 +01:00
glide.load(jazzIcon).diskCacheStrategy(DiskCacheStrategy.ALL).circleCrop().into(imageView)
2019-12-19 11:15:58 +01:00
}
} else {
imageView.setImageDrawable(null)
}
}
2020-01-06 00:18:43 +01:00
setProfilePictureIfNeeded(doubleModeImageView1, hexEncodedPublicKey, R.dimen.small_profile_picture_size)
setProfilePictureIfNeeded(doubleModeImageView2, additionalHexEncodedPublicKey ?: "", R.dimen.small_profile_picture_size)
2020-01-06 04:26:52 +01:00
setProfilePictureIfNeeded(singleModeImageView, hexEncodedPublicKey, R.dimen.medium_profile_picture_size)
setProfilePictureIfNeeded(largeSingleModeImageView, hexEncodedPublicKey, R.dimen.large_profile_picture_size)
2019-12-17 16:24:42 +01:00
}
// endregion
}