Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting the recipient from the VM #1694

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
)
}