Clean up ProfilePictureView

This commit is contained in:
nielsandriesse 2021-05-28 16:09:15 +10:00
parent de5d8506cf
commit 1262f80a1f
10 changed files with 44 additions and 140 deletions

View File

@ -45,9 +45,11 @@ import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.DimenRes; import androidx.annotation.DimenRes;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.annimon.stream.Stream; import com.annimon.stream.Stream;
import org.session.libsession.messaging.contacts.Contact; import org.session.libsession.messaging.contacts.Contact;
@ -58,17 +60,16 @@ import org.session.libsession.messaging.open_groups.OpenGroupV2;
import org.session.libsession.messaging.sending_receiving.attachments.AttachmentTransferProgress; import org.session.libsession.messaging.sending_receiving.attachments.AttachmentTransferProgress;
import org.session.libsession.messaging.sending_receiving.attachments.DatabaseAttachment; import org.session.libsession.messaging.sending_receiving.attachments.DatabaseAttachment;
import org.session.libsession.messaging.sending_receiving.link_preview.LinkPreview; import org.session.libsession.messaging.sending_receiving.link_preview.LinkPreview;
import org.session.libsession.utilities.recipients.Recipient;
import org.session.libsession.utilities.recipients.RecipientModifiedListener;
import org.session.libsession.messaging.utilities.UpdateMessageData; import org.session.libsession.messaging.utilities.UpdateMessageData;
import org.session.libsession.utilities.GroupUtil; import org.session.libsession.utilities.Stub;
import org.session.libsession.utilities.TextSecurePreferences; import org.session.libsession.utilities.TextSecurePreferences;
import org.session.libsession.utilities.ThemeUtil; import org.session.libsession.utilities.ThemeUtil;
import org.session.libsession.utilities.Util; import org.session.libsession.utilities.Util;
import org.session.libsession.utilities.ViewUtil; import org.session.libsession.utilities.ViewUtil;
import org.session.libsession.utilities.Stub; import org.session.libsession.utilities.recipients.Recipient;
import org.session.libsignal.utilities.guava.Optional; import org.session.libsession.utilities.recipients.RecipientModifiedListener;
import org.session.libsignal.utilities.Log; import org.session.libsignal.utilities.Log;
import org.session.libsignal.utilities.guava.Optional;
import org.thoughtcrime.securesms.BindableConversationItem; import org.thoughtcrime.securesms.BindableConversationItem;
import org.thoughtcrime.securesms.MediaPreviewActivity; import org.thoughtcrime.securesms.MediaPreviewActivity;
import org.thoughtcrime.securesms.MessageDetailsActivity; import org.thoughtcrime.securesms.MessageDetailsActivity;
@ -88,7 +89,6 @@ import org.thoughtcrime.securesms.database.model.MmsMessageRecord;
import org.thoughtcrime.securesms.database.model.Quote; import org.thoughtcrime.securesms.database.model.Quote;
import org.thoughtcrime.securesms.linkpreview.LinkPreviewUtil; import org.thoughtcrime.securesms.linkpreview.LinkPreviewUtil;
import org.thoughtcrime.securesms.loki.utilities.MentionUtilities; import org.thoughtcrime.securesms.loki.utilities.MentionUtilities;
import org.thoughtcrime.securesms.loki.utilities.OpenGroupUtilities;
import org.thoughtcrime.securesms.loki.views.MessageAudioView; import org.thoughtcrime.securesms.loki.views.MessageAudioView;
import org.thoughtcrime.securesms.loki.views.OpenGroupInvitationView; import org.thoughtcrime.securesms.loki.views.OpenGroupInvitationView;
import org.thoughtcrime.securesms.loki.views.ProfilePictureView; import org.thoughtcrime.securesms.loki.views.ProfilePictureView;
@ -759,7 +759,6 @@ public class ConversationItem extends LinearLayout
String displayName = recipient.getName(); String displayName = recipient.getName();
profilePictureView.setDisplayName(displayName); profilePictureView.setDisplayName(displayName);
profilePictureView.setAdditionalPublicKey(null); profilePictureView.setAdditionalPublicKey(null);
profilePictureView.setRSSFeed(false);
profilePictureView.setGlide(glideRequests); profilePictureView.setGlide(glideRequests);
profilePictureView.update(); profilePictureView.update();
} }

View File

@ -34,7 +34,6 @@ class MentionCandidateView(context: Context, attrs: AttributeSet?, defStyleAttr:
profilePictureView.publicKey = mentionCandidate.publicKey profilePictureView.publicKey = mentionCandidate.publicKey
profilePictureView.displayName = mentionCandidate.displayName profilePictureView.displayName = mentionCandidate.displayName
profilePictureView.additionalPublicKey = null profilePictureView.additionalPublicKey = null
profilePictureView.isRSSFeed = false
profilePictureView.glide = glide!! profilePictureView.glide = glide!!
profilePictureView.update() profilePictureView.update()
if (openGroupServer != null && openGroupRoom != null) { if (openGroupServer != null && openGroupRoom != null) {

View File

@ -20,17 +20,15 @@ import org.thoughtcrime.securesms.database.DatabaseFactory
import org.thoughtcrime.securesms.loki.utilities.AvatarPlaceholderGenerator import org.thoughtcrime.securesms.loki.utilities.AvatarPlaceholderGenerator
import org.thoughtcrime.securesms.mms.GlideRequests import org.thoughtcrime.securesms.mms.GlideRequests
// TODO: Look into a better way of handling different sizes. Maybe an enum (with associated values) encapsulating the different modes?
class ProfilePictureView : RelativeLayout { class ProfilePictureView : RelativeLayout {
lateinit var glide: GlideRequests lateinit var glide: GlideRequests
var publicKey: String? = null var publicKey: String? = null
var displayName: String? = null var displayName: String? = null
var additionalPublicKey: String? = null var additionalPublicKey: String? = null
var additionalDisplayName: String? = null var additionalDisplayName: String? = null
var isRSSFeed = false
var isLarge = false var isLarge = false
private val profilePicturesCached = mutableMapOf<String,String?>()
private val profilePicturesCache = mutableMapOf<String, String?>()
// region Lifecycle // region Lifecycle
constructor(context: Context) : super(context) { constructor(context: Context) : super(context) {
@ -79,15 +77,11 @@ class ProfilePictureView : RelativeLayout {
val apk = randomUsers.getOrNull(1) ?: "" val apk = randomUsers.getOrNull(1) ?: ""
additionalPublicKey = apk additionalPublicKey = apk
additionalDisplayName = getUserDisplayName(apk) additionalDisplayName = getUserDisplayName(apk)
isRSSFeed = recipient.name == "Loki News" ||
recipient.name == "Session Updates" ||
recipient.name == "Session Public Chat"
} else { } else {
val publicKey = recipient.address.toString() val publicKey = recipient.address.toString()
this.publicKey = publicKey this.publicKey = publicKey
displayName = getUserDisplayName(publicKey) displayName = getUserDisplayName(publicKey)
additionalPublicKey = null additionalPublicKey = null
isRSSFeed = false
} }
update() update()
} }
@ -95,68 +89,47 @@ class ProfilePictureView : RelativeLayout {
fun update() { fun update() {
val publicKey = publicKey ?: return val publicKey = publicKey ?: return
val additionalPublicKey = additionalPublicKey val additionalPublicKey = additionalPublicKey
doubleModeImageViewContainer.visibility = if (additionalPublicKey != null && !isRSSFeed) { if (additionalPublicKey != null) {
setProfilePictureIfNeeded( setProfilePictureIfNeeded(doubleModeImageView1, publicKey, displayName, R.dimen.small_profile_picture_size)
doubleModeImageView1, setProfilePictureIfNeeded(doubleModeImageView2, additionalPublicKey, additionalDisplayName, R.dimen.small_profile_picture_size)
publicKey, doubleModeImageViewContainer.visibility = View.VISIBLE
displayName,
R.dimen.small_profile_picture_size)
setProfilePictureIfNeeded(
doubleModeImageView2,
additionalPublicKey,
additionalDisplayName,
R.dimen.small_profile_picture_size)
View.VISIBLE
} else { } else {
glide.clear(doubleModeImageView1) glide.clear(doubleModeImageView1)
glide.clear(doubleModeImageView2) glide.clear(doubleModeImageView2)
View.INVISIBLE doubleModeImageViewContainer.visibility = View.INVISIBLE
} }
singleModeImageViewContainer.visibility = if (additionalPublicKey == null && !isRSSFeed && !isLarge) { if (additionalPublicKey == null && !isLarge) {
setProfilePictureIfNeeded( setProfilePictureIfNeeded(singleModeImageView, publicKey, displayName, R.dimen.medium_profile_picture_size)
singleModeImageView, singleModeImageView.visibility = View.VISIBLE
publicKey,
displayName,
R.dimen.medium_profile_picture_size)
View.VISIBLE
} else { } else {
glide.clear(singleModeImageView) glide.clear(singleModeImageView)
View.INVISIBLE singleModeImageView.visibility = View.INVISIBLE
} }
largeSingleModeImageViewContainer.visibility = if (additionalPublicKey == null && !isRSSFeed && isLarge) { if (additionalPublicKey == null && isLarge) {
setProfilePictureIfNeeded( setProfilePictureIfNeeded(largeSingleModeImageView, publicKey, displayName, R.dimen.large_profile_picture_size)
largeSingleModeImageView, largeSingleModeImageView.visibility = View.VISIBLE
publicKey,
displayName,
R.dimen.large_profile_picture_size)
View.VISIBLE
} else { } else {
glide.clear(largeSingleModeImageView) glide.clear(largeSingleModeImageView)
View.INVISIBLE largeSingleModeImageView.visibility = View.INVISIBLE
} }
rssImageView.visibility = if (isRSSFeed) View.VISIBLE else View.INVISIBLE
} }
private fun setProfilePictureIfNeeded(imageView: ImageView, publicKey: String, displayName: String?, @DimenRes sizeResId: Int) { private fun setProfilePictureIfNeeded(imageView: ImageView, publicKey: String, displayName: String?, @DimenRes sizeResId: Int) {
if (publicKey.isNotEmpty()) { if (publicKey.isNotEmpty()) {
val recipient = Recipient.from(context, Address.fromSerialized(publicKey), false) val recipient = Recipient.from(context, Address.fromSerialized(publicKey), false)
if (profilePicturesCached.containsKey(publicKey) && profilePicturesCached[publicKey] == recipient.profileAvatar) return if (profilePicturesCache.containsKey(publicKey) && profilePicturesCache[publicKey] == recipient.profileAvatar) return
val signalProfilePicture = recipient.contactPhoto val signalProfilePicture = recipient.contactPhoto
val avatar = (signalProfilePicture as? ProfileContactPhoto)?.avatarObject val avatar = (signalProfilePicture as? ProfileContactPhoto)?.avatarObject
if (signalProfilePicture != null && avatar != "0" && avatar != "") { if (signalProfilePicture != null && avatar != "0" && avatar != "") {
glide.clear(imageView) glide.clear(imageView)
glide.load(signalProfilePicture).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).circleCrop().into(imageView) glide.load(signalProfilePicture).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).circleCrop().into(imageView)
profilePicturesCached[publicKey] = recipient.profileAvatar profilePicturesCache[publicKey] = recipient.profileAvatar
} else { } else {
val sizeInPX = resources.getDimensionPixelSize(sizeResId) val sizeInPX = resources.getDimensionPixelSize(sizeResId)
glide.clear(imageView) glide.clear(imageView)
glide.load(AvatarPlaceholderGenerator.generate( glide.load(AvatarPlaceholderGenerator.generate(context, sizeInPX, publicKey, displayName))
context, .diskCacheStrategy(DiskCacheStrategy.ALL).circleCrop().into(imageView)
sizeInPX, profilePicturesCache[publicKey] = recipient.profileAvatar
publicKey,
displayName
)).diskCacheStrategy(DiskCacheStrategy.ALL).circleCrop().into(imageView)
profilePicturesCached[publicKey] = recipient.profileAvatar
} }
} else { } else {
imageView.setImageDrawable(null) imageView.setImageDrawable(null)
@ -164,7 +137,7 @@ class ProfilePictureView : RelativeLayout {
} }
fun recycle() { fun recycle() {
profilePicturesCached.clear() profilePicturesCache.clear()
} }
// endregion // endregion
} }

View File

@ -6,6 +6,4 @@
<solid android:color="@color/profile_picture_background" /> <solid android:color="@color/profile_picture_background" />
<corners android:radius="38dp" /> <corners android:radius="38dp" />
<stroke android:width="@dimen/profile_picture_border_thickness" android:color="@color/border" />
</shape> </shape>

View File

@ -6,6 +6,4 @@
<solid android:color="@color/transparent" /> <solid android:color="@color/transparent" />
<corners android:radius="38dp" /> <corners android:radius="38dp" />
<stroke android:width="@dimen/profile_picture_border_thickness" android:color="@color/profile_picture_border" />
</shape> </shape>

View File

@ -6,6 +6,4 @@
<solid android:color="@color/profile_picture_background" /> <solid android:color="@color/profile_picture_background" />
<corners android:radius="23dp" /> <corners android:radius="23dp" />
<stroke android:width="@dimen/profile_picture_border_thickness" android:color="@color/profile_picture_border" />
</shape> </shape>

View File

@ -6,6 +6,4 @@
<solid android:color="@color/transparent" /> <solid android:color="@color/transparent" />
<corners android:radius="23dp" /> <corners android:radius="23dp" />
<stroke android:width="@dimen/profile_picture_border_thickness" android:color="@color/profile_picture_border" />
</shape> </shape>

View File

@ -6,6 +6,4 @@
<solid android:color="@color/profile_picture_background" /> <solid android:color="@color/profile_picture_background" />
<corners android:radius="23dp" /> <corners android:radius="23dp" />
<stroke android:width="@dimen/profile_picture_border_thickness" android:color="@color/profile_picture_border" />
</shape> </shape>

View File

@ -6,6 +6,4 @@
<solid android:color="@color/profile_picture_background" /> <solid android:color="@color/profile_picture_background" />
<corners android:radius="18dp" /> <corners android:radius="18dp" />
<stroke android:width="@dimen/profile_picture_border_thickness" android:color="@color/profile_picture_border" />
</shape> </shape>

View File

@ -1,97 +1,42 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" <RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content">
xmlns:app="http://schemas.android.com/apk/res-auto">
<RelativeLayout <RelativeLayout
android:id="@+id/doubleModeImageViewContainer" android:id="@+id/doubleModeImageViewContainer"
android:layout_width="@dimen/medium_profile_picture_size" android:layout_width="@dimen/medium_profile_picture_size"
android:layout_height="@dimen/medium_profile_picture_size"> android:layout_height="@dimen/medium_profile_picture_size">
<RelativeLayout <ImageView
android:id="@+id/doubleModeImageView1"
android:layout_width="@dimen/small_profile_picture_size" android:layout_width="@dimen/small_profile_picture_size"
android:layout_height="@dimen/small_profile_picture_size" android:layout_height="@dimen/small_profile_picture_size"
android:layout_alignParentLeft="true" android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"> android:layout_alignParentTop="true"
android:background="@drawable/profile_picture_view_small_background" />
<ImageView <ImageView
android:id="@+id/doubleModeImageView1" android:id="@+id/doubleModeImageView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/profile_picture_view_small_background" />
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/profile_picture_view_small_foreground" />
</RelativeLayout>
<RelativeLayout
android:layout_width="@dimen/small_profile_picture_size" android:layout_width="@dimen/small_profile_picture_size"
android:layout_height="@dimen/small_profile_picture_size" android:layout_height="@dimen/small_profile_picture_size"
android:layout_alignParentRight="true" android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"> android:layout_alignParentBottom="true"
android:background="@drawable/profile_picture_view_small_background" />
<ImageView
android:id="@+id/doubleModeImageView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/profile_picture_view_small_background" />
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/profile_picture_view_small_foreground" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/singleModeImageViewContainer"
android:layout_width="@dimen/medium_profile_picture_size"
android:layout_height="@dimen/medium_profile_picture_size">
<ImageView
android:id="@+id/singleModeImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/profile_picture_view_medium_background" />
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/profile_picture_view_medium_foreground" />
</RelativeLayout> </RelativeLayout>
<ImageView <ImageView
android:id="@+id/rssImageView" android:id="@+id/singleModeImageView"
android:layout_width="@dimen/medium_profile_picture_size" android:layout_width="@dimen/medium_profile_picture_size"
android:layout_height="@dimen/medium_profile_picture_size" android:layout_height="@dimen/medium_profile_picture_size"
android:gravity="center" android:background="@drawable/profile_picture_view_medium_background" />
android:src="@drawable/session_logo_white"
android:padding="12dp"
android:background="@drawable/profile_picture_view_rss_medium_background" />
<RelativeLayout <ImageView
android:id="@+id/largeSingleModeImageViewContainer" android:id="@+id/largeSingleModeImageView"
android:layout_width="@dimen/large_profile_picture_size" android:layout_width="@dimen/large_profile_picture_size"
android:layout_height="@dimen/large_profile_picture_size"> android:layout_height="@dimen/large_profile_picture_size"
android:background="@drawable/profile_picture_view_large_background" />
<ImageView
android:id="@+id/largeSingleModeImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/profile_picture_view_large_background" />
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/profile_picture_view_large_foreground" />
</RelativeLayout>
</RelativeLayout> </RelativeLayout>