Skip to content

Commit

Permalink
Getting the recipient from the VM (#1694)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasSession authored Oct 23, 2024
1 parent 3e17ab2 commit 16cca2d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<View, Recipient>()
private val resourcePadding by lazy {
Expand All @@ -51,6 +52,7 @@ class ProfilePictureView @JvmOverloads constructor(
}

fun update(recipient: Recipient) {
this.recipient = recipient
recipient.run { update(address, isClosedGroupRecipient, isOpenGroupInboxRecipient) }
}

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand All @@ -70,13 +71,30 @@ class SettingsViewModel @Inject constructor(
val recoveryHidden: StateFlow<Boolean>
get() = _recoveryHidden

private val _avatarData: MutableStateFlow<AvatarData?> = MutableStateFlow(null)
val avatarData: StateFlow<AvatarData?>
get() = _avatarData

/**
* Refreshes the avatar on the main settings page
*/
private val _refreshAvatar: MutableSharedFlow<Unit> = MutableSharedFlow()
val refreshAvatar: SharedFlow<Unit>
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)

Expand Down Expand Up @@ -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
)
}

0 comments on commit 16cca2d

Please sign in to comment.