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.TextView;
import android.widget.Toast;
import androidx.annotation.DimenRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.annimon.stream.Stream;
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.DatabaseAttachment;
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.utilities.GroupUtil;
import org.session.libsession.utilities.Stub;
import org.session.libsession.utilities.TextSecurePreferences;
import org.session.libsession.utilities.ThemeUtil;
import org.session.libsession.utilities.Util;
import org.session.libsession.utilities.ViewUtil;
import org.session.libsession.utilities.Stub;
import org.session.libsignal.utilities.guava.Optional;
import org.session.libsession.utilities.recipients.Recipient;
import org.session.libsession.utilities.recipients.RecipientModifiedListener;
import org.session.libsignal.utilities.Log;
import org.session.libsignal.utilities.guava.Optional;
import org.thoughtcrime.securesms.BindableConversationItem;
import org.thoughtcrime.securesms.MediaPreviewActivity;
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.linkpreview.LinkPreviewUtil;
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.OpenGroupInvitationView;
import org.thoughtcrime.securesms.loki.views.ProfilePictureView;
@ -759,7 +759,6 @@ public class ConversationItem extends LinearLayout
String displayName = recipient.getName();
profilePictureView.setDisplayName(displayName);
profilePictureView.setAdditionalPublicKey(null);
profilePictureView.setRSSFeed(false);
profilePictureView.setGlide(glideRequests);
profilePictureView.update();
}

View File

@ -34,7 +34,6 @@ class MentionCandidateView(context: Context, attrs: AttributeSet?, defStyleAttr:
profilePictureView.publicKey = mentionCandidate.publicKey
profilePictureView.displayName = mentionCandidate.displayName
profilePictureView.additionalPublicKey = null
profilePictureView.isRSSFeed = false
profilePictureView.glide = glide!!
profilePictureView.update()
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.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 {
lateinit var glide: GlideRequests
var publicKey: String? = null
var displayName: String? = null
var additionalPublicKey: String? = null
var additionalDisplayName: String? = null
var isRSSFeed = false
var isLarge = false
private val profilePicturesCached = mutableMapOf<String,String?>()
private val profilePicturesCache = mutableMapOf<String, String?>()
// region Lifecycle
constructor(context: Context) : super(context) {
@ -79,15 +77,11 @@ class ProfilePictureView : RelativeLayout {
val apk = randomUsers.getOrNull(1) ?: ""
additionalPublicKey = apk
additionalDisplayName = getUserDisplayName(apk)
isRSSFeed = recipient.name == "Loki News" ||
recipient.name == "Session Updates" ||
recipient.name == "Session Public Chat"
} else {
val publicKey = recipient.address.toString()
this.publicKey = publicKey
displayName = getUserDisplayName(publicKey)
additionalPublicKey = null
isRSSFeed = false
}
update()
}
@ -95,68 +89,47 @@ class ProfilePictureView : RelativeLayout {
fun update() {
val publicKey = publicKey ?: return
val additionalPublicKey = additionalPublicKey
doubleModeImageViewContainer.visibility = if (additionalPublicKey != null && !isRSSFeed) {
setProfilePictureIfNeeded(
doubleModeImageView1,
publicKey,
displayName,
R.dimen.small_profile_picture_size)
setProfilePictureIfNeeded(
doubleModeImageView2,
additionalPublicKey,
additionalDisplayName,
R.dimen.small_profile_picture_size)
View.VISIBLE
if (additionalPublicKey != null) {
setProfilePictureIfNeeded(doubleModeImageView1, publicKey, displayName, R.dimen.small_profile_picture_size)
setProfilePictureIfNeeded(doubleModeImageView2, additionalPublicKey, additionalDisplayName, R.dimen.small_profile_picture_size)
doubleModeImageViewContainer.visibility = View.VISIBLE
} else {
glide.clear(doubleModeImageView1)
glide.clear(doubleModeImageView2)
View.INVISIBLE
doubleModeImageViewContainer.visibility = View.INVISIBLE
}
singleModeImageViewContainer.visibility = if (additionalPublicKey == null && !isRSSFeed && !isLarge) {
setProfilePictureIfNeeded(
singleModeImageView,
publicKey,
displayName,
R.dimen.medium_profile_picture_size)
View.VISIBLE
if (additionalPublicKey == null && !isLarge) {
setProfilePictureIfNeeded(singleModeImageView, publicKey, displayName, R.dimen.medium_profile_picture_size)
singleModeImageView.visibility = View.VISIBLE
} else {
glide.clear(singleModeImageView)
View.INVISIBLE
singleModeImageView.visibility = View.INVISIBLE
}
largeSingleModeImageViewContainer.visibility = if (additionalPublicKey == null && !isRSSFeed && isLarge) {
setProfilePictureIfNeeded(
largeSingleModeImageView,
publicKey,
displayName,
R.dimen.large_profile_picture_size)
View.VISIBLE
if (additionalPublicKey == null && isLarge) {
setProfilePictureIfNeeded(largeSingleModeImageView, publicKey, displayName, R.dimen.large_profile_picture_size)
largeSingleModeImageView.visibility = View.VISIBLE
} else {
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) {
if (publicKey.isNotEmpty()) {
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 avatar = (signalProfilePicture as? ProfileContactPhoto)?.avatarObject
if (signalProfilePicture != null && avatar != "0" && avatar != "") {
glide.clear(imageView)
glide.load(signalProfilePicture).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).circleCrop().into(imageView)
profilePicturesCached[publicKey] = recipient.profileAvatar
profilePicturesCache[publicKey] = recipient.profileAvatar
} else {
val sizeInPX = resources.getDimensionPixelSize(sizeResId)
glide.clear(imageView)
glide.load(AvatarPlaceholderGenerator.generate(
context,
sizeInPX,
publicKey,
displayName
)).diskCacheStrategy(DiskCacheStrategy.ALL).circleCrop().into(imageView)
profilePicturesCached[publicKey] = recipient.profileAvatar
glide.load(AvatarPlaceholderGenerator.generate(context, sizeInPX, publicKey, displayName))
.diskCacheStrategy(DiskCacheStrategy.ALL).circleCrop().into(imageView)
profilePicturesCache[publicKey] = recipient.profileAvatar
}
} else {
imageView.setImageDrawable(null)
@ -164,7 +137,7 @@ class ProfilePictureView : RelativeLayout {
}
fun recycle() {
profilePicturesCached.clear()
profilePicturesCache.clear()
}
// endregion
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,97 +1,42 @@
<?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_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/doubleModeImageViewContainer"
android:layout_width="@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_height="@dimen/small_profile_picture_size"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true">
android:layout_alignParentTop="true"
android:background="@drawable/profile_picture_view_small_background" />
<ImageView
android:id="@+id/doubleModeImageView1"
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
<ImageView
android:id="@+id/doubleModeImageView2"
android:layout_width="@dimen/small_profile_picture_size"
android:layout_height="@dimen/small_profile_picture_size"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true">
<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" />
android:layout_alignParentBottom="true"
android:background="@drawable/profile_picture_view_small_background" />
</RelativeLayout>
<ImageView
android:id="@+id/rssImageView"
android:id="@+id/singleModeImageView"
android:layout_width="@dimen/medium_profile_picture_size"
android:layout_height="@dimen/medium_profile_picture_size"
android:gravity="center"
android:src="@drawable/session_logo_white"
android:padding="12dp"
android:background="@drawable/profile_picture_view_rss_medium_background" />
android:background="@drawable/profile_picture_view_medium_background" />
<RelativeLayout
android:id="@+id/largeSingleModeImageViewContainer"
<ImageView
android:id="@+id/largeSingleModeImageView"
android:layout_width="@dimen/large_profile_picture_size"
android:layout_height="@dimen/large_profile_picture_size">
<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>
android:layout_height="@dimen/large_profile_picture_size"
android:background="@drawable/profile_picture_view_large_background" />
</RelativeLayout>