Skip to content

Commit

Permalink
Reduce Bimi priority (it should only be displayed if we couldn't find…
Browse files Browse the repository at this point in the history
… an avatar)
  • Loading branch information
KevinBoulongne committed Jun 26, 2024
1 parent f4938dc commit f211e16
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 42 deletions.
2 changes: 2 additions & 0 deletions app/src/main/java/com/infomaniak/mail/data/models/Bimi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class Bimi() : EmbeddedRealmObject, Parcelable {
this.isCertified = isCertified
}

fun isDisplayable(): Boolean = isCertified && svgContentUrl?.isNotEmpty() == true

companion object : Parceler<Bimi> {

override fun create(parcel: Parcel): Bimi = with(parcel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ import com.infomaniak.mail.R
import com.infomaniak.mail.data.LocalSettings
import com.infomaniak.mail.data.LocalSettings.SwipeAction
import com.infomaniak.mail.data.LocalSettings.ThreadDensity
import com.infomaniak.mail.data.api.ApiRoutes
import com.infomaniak.mail.data.models.Folder.FolderRole
import com.infomaniak.mail.data.models.correspondent.Recipient
import com.infomaniak.mail.data.models.thread.Thread
Expand Down Expand Up @@ -402,11 +401,7 @@ class ThreadListAdapter @Inject constructor(

private fun CardviewThreadItemBinding.displayAvatar(thread: Thread) {
val (recipient, bimi) = thread.computeAvatarRecipient()
if (bimi?.isCertified == true) {
expeditorAvatar.loadBimiAvatar(ApiRoutes.bimi(bimi.svgContentUrl.toString()), recipient)
} else {
expeditorAvatar.loadAvatar(recipient)
}
expeditorAvatar.loadAvatar(recipient, bimi)
}

private fun CardviewThreadItemBinding.formatRecipientNames(recipients: List<Recipient>): String {
Expand Down
79 changes: 43 additions & 36 deletions app/src/main/java/com/infomaniak/mail/views/AvatarView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ class AvatarView @JvmOverloads constructor(
private val binding by lazy { ViewAvatarBinding.inflate(LayoutInflater.from(context), this, true) }

private var currentCorrespondent: Correspondent? = null
private var isBimiShown: Boolean = false
private var _bimi: Bimi? = null

private val mergedContactObserver = Observer<MergedContactDictionary> { contacts ->
currentCorrespondent?.let { correspondent ->
if (!isBimiShown) loadAvatarUsingDictionary(correspondent, contacts)
loadAvatarUsingDictionary(correspondent, contacts, _bimi)
}
}

Expand Down Expand Up @@ -129,14 +129,23 @@ class AvatarView @JvmOverloads constructor(
)
}

fun loadAvatar(correspondent: Correspondent?) {
if (correspondent == null) {
loadUnknownUserAvatar()
} else {
// Avoid lateinit property has not been initialized in preview
val contactsFromViewModel = if (isInEditMode) emptyMap() else avatarMergedContactData.mergedContactLiveData.value
loadAvatarUsingDictionary(correspondent, contacts = contactsFromViewModel ?: emptyMap())
currentCorrespondent = correspondent
fun loadAvatar(correspondent: Correspondent?, bimi: Bimi? = null) {

_bimi = bimi

when {
correspondent != null -> {
// Avoid lateinit property has not been initialized in preview
val contactsFromViewModel = if (isInEditMode) emptyMap() else avatarMergedContactData.mergedContactLiveData.value
loadAvatarUsingDictionary(correspondent, contacts = contactsFromViewModel ?: emptyMap(), bimi)
currentCorrespondent = correspondent
}
bimi?.isDisplayable() == true -> {
loadBimiAvatar(ApiRoutes.bimi(bimi.svgContentUrl!!))
}
else -> {
loadUnknownUserAvatar()
}
}
}

Expand All @@ -149,9 +158,8 @@ class AvatarView @JvmOverloads constructor(
binding.avatarImage.load(R.drawable.ic_unknown_user_avatar)
}

fun loadBimiAvatar(bimiUrl: String, correspondent: Correspondent?) = with(binding.avatarImage) {
private fun loadBimiAvatar(bimiUrl: String, correspondent: Correspondent? = null) = with(binding.avatarImage) {
contentDescription = correspondent?.email.orEmpty()
isBimiShown = bimiUrl.isNotEmpty()
loadAvatar(
backgroundColor = context.getBackgroundColorBasedOnId(
correspondent?.email.orEmpty().hashCode(),
Expand All @@ -171,33 +179,32 @@ class AvatarView @JvmOverloads constructor(
return recipientsForEmail?.getOrElse(correspondent.name) { recipientsForEmail.entries.elementAt(0).value }
}

private fun loadAvatarUsingDictionary(correspondent: Correspondent, contacts: MergedContactDictionary) {
private fun loadAvatarUsingDictionary(correspondent: Correspondent, contacts: MergedContactDictionary, bimi: Bimi?) {
val mergedContact = searchInMergedContact(correspondent, contacts)
binding.avatarImage.baseLoadAvatar(correspondent = mergedContact ?: correspondent)
binding.avatarImage.baseLoadAvatar(correspondent = mergedContact ?: correspondent, bimi)
}

private fun ImageView.baseLoadAvatar(correspondent: Correspondent) {
if (correspondent.shouldDisplayUserAvatar()) {
this@AvatarView.loadAvatar(AccountUtils.currentUser!!)
} else {
val avatar = (correspondent as? MergedContact)?.avatar
val color = context.getColor(R.color.onColorfulBackground)
loadAvatar(
backgroundColor = context.getBackgroundColorBasedOnId(correspondent.email.hashCode(), R.array.AvatarColors),
avatarUrl = avatar,
initials = correspondent.initials,
imageLoader = context.imageLoader,
initialsColor = color,
)
}
}

fun loadAvatar(correspondent: Correspondent?, bimi: Bimi?) {
val svgContentUrl = bimi?.svgContentUrl
if (bimi == null || !bimi.isCertified || svgContentUrl.isNullOrEmpty()) {
loadAvatar(correspondent)
} else {
loadBimiAvatar(ApiRoutes.bimi(svgContentUrl), correspondent)
private fun ImageView.baseLoadAvatar(correspondent: Correspondent, bimi: Bimi? = null) {

val avatar = (correspondent as? MergedContact)?.avatar

when {
correspondent.shouldDisplayUserAvatar() -> {
this@AvatarView.loadAvatar(AccountUtils.currentUser!!)
}
avatar == null && bimi?.isDisplayable() == true -> {
loadBimiAvatar(ApiRoutes.bimi(bimi.svgContentUrl!!), correspondent)
}
else -> {
val color = context.getColor(R.color.onColorfulBackground)
loadAvatar(
backgroundColor = context.getBackgroundColorBasedOnId(correspondent.email.hashCode(), R.array.AvatarColors),
avatarUrl = avatar,
initials = correspondent.initials,
imageLoader = context.imageLoader,
initialsColor = color,
)
}
}
}
}

0 comments on commit f211e16

Please sign in to comment.