From 16cca2d3cc961644a743de1428fc0ed5bb913b6f Mon Sep 17 00:00:00 2001 From: ThomasSession Date: Wed, 23 Oct 2024 13:48:52 +1100 Subject: [PATCH] Getting the recipient from the VM (#1694) --- .../components/ProfilePictureView.kt | 11 +++++++- .../securesms/preferences/SettingsActivity.kt | 17 ++++++++---- .../preferences/SettingsViewModel.kt | 26 ++++++++++++++++++- 3 files changed, 47 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/components/ProfilePictureView.kt b/app/src/main/java/org/thoughtcrime/securesms/components/ProfilePictureView.kt index 9511bddb6ab..d390d82ec3c 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/components/ProfilePictureView.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/components/ProfilePictureView.kt @@ -36,6 +36,7 @@ class ProfilePictureView @JvmOverloads constructor( var displayName: String? = null var additionalPublicKey: String? = null var additionalDisplayName: String? = null + var recipient: Recipient? = null private val profilePicturesCache = mutableMapOf() private val resourcePadding by lazy { @@ -51,6 +52,7 @@ class ProfilePictureView @JvmOverloads constructor( } fun update(recipient: Recipient) { + this.recipient = recipient recipient.run { update(address, isClosedGroupRecipient, isOpenGroupInboxRecipient) } } @@ -121,7 +123,14 @@ class ProfilePictureView @JvmOverloads constructor( private fun setProfilePictureIfNeeded(imageView: ImageView, publicKey: String, displayName: String?) { if (publicKey.isNotEmpty()) { - val recipient = Recipient.from(context, Address.fromSerialized(publicKey), false) + // if we already have a recipient that matches the current key, reuse it + val recipient = if(this.recipient != null && this.recipient?.address?.serialize() == publicKey){ + this.recipient!! + } + else { + this.recipient = Recipient.from(context, Address.fromSerialized(publicKey), false) + this.recipient!! + } if (profilePicturesCache[imageView] == recipient) return profilePicturesCache[imageView] = recipient val signalProfilePicture = recipient.contactPhoto diff --git a/app/src/main/java/org/thoughtcrime/securesms/preferences/SettingsActivity.kt b/app/src/main/java/org/thoughtcrime/securesms/preferences/SettingsActivity.kt index eaebcb57f6c..c3ecc3cdac1 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/preferences/SettingsActivity.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/preferences/SettingsActivity.kt @@ -173,11 +173,6 @@ class SettingsActivity : PassphraseRequiredActionBarActivity() { } binding.run { - profilePictureView.apply { - publicKey = viewModel.hexEncodedPublicKey - displayName = viewModel.getDisplayName() - update() - } profilePictureView.setOnClickListener { binding.avatarDialog.isVisible = true showAvatarDialog = true @@ -209,6 +204,18 @@ class SettingsActivity : PassphraseRequiredActionBarActivity() { binding.profilePictureView.update() } } + + lifecycleScope.launch { + viewModel.avatarData.collect { + if(it == null) return@collect + + binding.profilePictureView.apply { + publicKey = it.publicKey + displayName = it.displayName + update(it.recipient) + } + } + } } override fun onStart() { diff --git a/app/src/main/java/org/thoughtcrime/securesms/preferences/SettingsViewModel.kt b/app/src/main/java/org/thoughtcrime/securesms/preferences/SettingsViewModel.kt index 5b6fa78d44b..07240e24043 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/preferences/SettingsViewModel.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/preferences/SettingsViewModel.kt @@ -26,6 +26,7 @@ import org.session.libsession.utilities.Address import org.session.libsession.utilities.ProfileKeyUtil import org.session.libsession.utilities.ProfilePictureUtilities import org.session.libsession.utilities.TextSecurePreferences +import org.session.libsession.utilities.recipients.Recipient import org.session.libsession.utilities.truncateIdForDisplay import org.session.libsignal.utilities.ExternalStorageUtil.getImageDir import org.session.libsignal.utilities.Log @@ -52,7 +53,7 @@ class SettingsViewModel @Inject constructor( private var tempFile: File? = null - val hexEncodedPublicKey: String get() = prefs.getLocalNumber() ?: "" + val hexEncodedPublicKey: String = prefs.getLocalNumber() ?: "" private val userAddress = Address.fromSerialized(hexEncodedPublicKey) @@ -70,6 +71,10 @@ class SettingsViewModel @Inject constructor( val recoveryHidden: StateFlow get() = _recoveryHidden + private val _avatarData: MutableStateFlow = MutableStateFlow(null) + val avatarData: StateFlow + get() = _avatarData + /** * Refreshes the avatar on the main settings page */ @@ -77,6 +82,19 @@ class SettingsViewModel @Inject constructor( val refreshAvatar: SharedFlow get() = _refreshAvatar.asSharedFlow() + init { + viewModelScope.launch(Dispatchers.Default) { + val recipient = Recipient.from(context, Address.fromSerialized(hexEncodedPublicKey), false) + _avatarData.update { + AvatarData( + publicKey = hexEncodedPublicKey, + displayName = getDisplayName(), + recipient = recipient + ) + } + } + } + fun getDisplayName(): String = prefs.getProfileName() ?: truncateIdForDisplay(hexEncodedPublicKey) @@ -249,4 +267,10 @@ class SettingsViewModel @Inject constructor( val hasAvatar: Boolean // true if the user has an avatar set already but is in this temp state because they are trying out a new avatar ) : AvatarDialogState() } + + data class AvatarData( + val publicKey: String, + val displayName: String, + val recipient: Recipient + ) } \ No newline at end of file