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

75 lines
3.5 KiB
Kotlin
Raw Normal View History

2019-12-17 16:24:42 +01:00
package org.thoughtcrime.securesms.loki.redesign.views
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
2019-12-19 11:15:58 +01:00
import org.thoughtcrime.securesms.database.Address
2020-01-06 00:18:43 +01:00
import org.thoughtcrime.securesms.loki.JazzIdenticonDrawable
2019-12-19 11:15:58 +01:00
import org.thoughtcrime.securesms.mms.GlideRequests
import org.thoughtcrime.securesms.recipients.Recipient
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
// 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 00:18:43 +01:00
singleModeImageViewContainer.visibility = if (additionalHexEncodedPublicKey == null && !isRSSFeed) View.VISIBLE else View.INVISIBLE
2019-12-17 16:24:42 +01:00
rssTextView.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()) {
val signalProfilePicture = Recipient.from(context, Address.fromSerialized(hexEncodedPublicKey), false).contactPhoto
if (signalProfilePicture != null) {
glide.load(signalProfilePicture).diskCacheStrategy(DiskCacheStrategy.ALL).circleCrop().into(imageView)
} else {
2020-01-06 00:18:43 +01:00
val size = resources.getDimensionPixelSize(sizeID)
val jazzIcon = JazzIdenticonDrawable(size, size, hexEncodedPublicKey)
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(singleModeImageView, hexEncodedPublicKey, R.dimen.medium_profile_picture_size)
setProfilePictureIfNeeded(doubleModeImageView1, hexEncodedPublicKey, R.dimen.small_profile_picture_size)
setProfilePictureIfNeeded(doubleModeImageView2, additionalHexEncodedPublicKey ?: "", R.dimen.small_profile_picture_size)
2019-12-17 16:24:42 +01:00
}
// endregion
}