Getting the recipient from the VM (#1694)

This commit is contained in:
ThomasSession 2024-10-23 13:48:52 +11:00 committed by GitHub
parent 3e17ab2b06
commit 16cca2d3cc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 47 additions and 7 deletions

View File

@ -36,6 +36,7 @@ class ProfilePictureView @JvmOverloads constructor(
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 recipient: Recipient? = null
private val profilePicturesCache = mutableMapOf<View, Recipient>() private val profilePicturesCache = mutableMapOf<View, Recipient>()
private val resourcePadding by lazy { private val resourcePadding by lazy {
@ -51,6 +52,7 @@ class ProfilePictureView @JvmOverloads constructor(
} }
fun update(recipient: Recipient) { fun update(recipient: Recipient) {
this.recipient = recipient
recipient.run { update(address, isClosedGroupRecipient, isOpenGroupInboxRecipient) } recipient.run { update(address, isClosedGroupRecipient, isOpenGroupInboxRecipient) }
} }
@ -121,7 +123,14 @@ class ProfilePictureView @JvmOverloads constructor(
private fun setProfilePictureIfNeeded(imageView: ImageView, publicKey: String, displayName: String?) { private fun setProfilePictureIfNeeded(imageView: ImageView, publicKey: String, displayName: String?) {
if (publicKey.isNotEmpty()) { 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 if (profilePicturesCache[imageView] == recipient) return
profilePicturesCache[imageView] = recipient profilePicturesCache[imageView] = recipient
val signalProfilePicture = recipient.contactPhoto val signalProfilePicture = recipient.contactPhoto

View File

@ -173,11 +173,6 @@ class SettingsActivity : PassphraseRequiredActionBarActivity() {
} }
binding.run { binding.run {
profilePictureView.apply {
publicKey = viewModel.hexEncodedPublicKey
displayName = viewModel.getDisplayName()
update()
}
profilePictureView.setOnClickListener { profilePictureView.setOnClickListener {
binding.avatarDialog.isVisible = true binding.avatarDialog.isVisible = true
showAvatarDialog = true showAvatarDialog = true
@ -209,6 +204,18 @@ class SettingsActivity : PassphraseRequiredActionBarActivity() {
binding.profilePictureView.update() 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() { override fun onStart() {

View File

@ -26,6 +26,7 @@ import org.session.libsession.utilities.Address
import org.session.libsession.utilities.ProfileKeyUtil import org.session.libsession.utilities.ProfileKeyUtil
import org.session.libsession.utilities.ProfilePictureUtilities import org.session.libsession.utilities.ProfilePictureUtilities
import org.session.libsession.utilities.TextSecurePreferences import org.session.libsession.utilities.TextSecurePreferences
import org.session.libsession.utilities.recipients.Recipient
import org.session.libsession.utilities.truncateIdForDisplay import org.session.libsession.utilities.truncateIdForDisplay
import org.session.libsignal.utilities.ExternalStorageUtil.getImageDir import org.session.libsignal.utilities.ExternalStorageUtil.getImageDir
import org.session.libsignal.utilities.Log import org.session.libsignal.utilities.Log
@ -52,7 +53,7 @@ class SettingsViewModel @Inject constructor(
private var tempFile: File? = null private var tempFile: File? = null
val hexEncodedPublicKey: String get() = prefs.getLocalNumber() ?: "" val hexEncodedPublicKey: String = prefs.getLocalNumber() ?: ""
private val userAddress = Address.fromSerialized(hexEncodedPublicKey) private val userAddress = Address.fromSerialized(hexEncodedPublicKey)
@ -70,6 +71,10 @@ class SettingsViewModel @Inject constructor(
val recoveryHidden: StateFlow<Boolean> val recoveryHidden: StateFlow<Boolean>
get() = _recoveryHidden get() = _recoveryHidden
private val _avatarData: MutableStateFlow<AvatarData?> = MutableStateFlow(null)
val avatarData: StateFlow<AvatarData?>
get() = _avatarData
/** /**
* Refreshes the avatar on the main settings page * Refreshes the avatar on the main settings page
*/ */
@ -77,6 +82,19 @@ class SettingsViewModel @Inject constructor(
val refreshAvatar: SharedFlow<Unit> val refreshAvatar: SharedFlow<Unit>
get() = _refreshAvatar.asSharedFlow() 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 = fun getDisplayName(): String =
prefs.getProfileName() ?: truncateIdForDisplay(hexEncodedPublicKey) 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 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() ) : AvatarDialogState()
} }
data class AvatarData(
val publicKey: String,
val displayName: String,
val recipient: Recipient
)
} }