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

fix: Wrong avatar after user name change #2190

Merged
merged 7 commits into from
Feb 26, 2025
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Infomaniak Mail - Android
* Copyright (C) 2023-2024 Infomaniak Network SA
* Copyright (C) 2023-2025 Infomaniak Network SA
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -78,7 +78,7 @@ class AvatarNameEmailView @JvmOverloads constructor(
}

fun setMergedContact(mergedContact: MergedContact) = with(binding) {
userAvatar.loadAvatar(mergedContact)
userAvatar.loadRawMergedContactAvatar(mergedContact)
setNameAndEmail(mergedContact)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ class ThreadListFragment : TwoPaneFragment() {
}

private fun setupUserAvatar() {
AccountUtils.currentUser?.let(binding.userAvatar::loadAvatar)
AccountUtils.currentUser?.let(binding.userAvatar::loadUserAvatar)
}

private fun setupUnreadCountChip() = with(binding) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ class ThreadAdapter(
}

if (message.isDraft) {
userAvatar.loadAvatar(AccountUtils.currentUser!!)
userAvatar.loadUserAvatar(AccountUtils.currentUser!!)
expeditorName.apply {
text = context.getString(R.string.messageIsDraftOption)
setTextAppearance(R.style.BodyMedium_Error)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Infomaniak Mail - Android
* Copyright (C) 2022-2024 Infomaniak Network SA
* Copyright (C) 2022-2025 Infomaniak Network SA
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -52,7 +52,7 @@ class SwitchUserAdapter(
override fun onBindViewHolder(holder: SwitchUserAccountViewHolder, position: Int) = with(holder.binding) {
val account = accounts[position]

userAvatar.loadAvatar(account)
userAvatar.loadUserAvatar(account)
userName.text = account.displayName
userMailAddress.text = account.email
updateSelectedUi(position)
Expand Down
43 changes: 26 additions & 17 deletions app/src/main/java/com/infomaniak/mail/views/AvatarView.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Infomaniak Mail - Android
* Copyright (C) 2022-2024 Infomaniak Network SA
* Copyright (C) 2022-2025 Infomaniak Network SA
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -147,7 +147,7 @@ class AvatarView @JvmOverloads constructor(
binding.root.isFocusable = focusable
}

fun loadAvatar(user: User) = with(binding.avatarImage) {
fun loadUserAvatar(user: User) = with(binding.avatarImage) {
contentDescription = user.email
loadAvatar(
backgroundColor = context.getBackgroundColorBasedOnId(user.id, R.array.AvatarColors),
Expand All @@ -164,16 +164,25 @@ class AvatarView @JvmOverloads constructor(
loadAvatarByDisplayType(avatarDisplayType, correspondent, bimi, contactsFromViewModel)
}

fun loadAvatar(mergedContact: MergedContact) {
binding.avatarImage.baseLoadAvatar(mergedContact)
private fun updateStateAndLoadUserAvatar(user: User, correspondent: Correspondent, bimi: Bimi) {
state.update(correspondent, bimi)
loadUserAvatar(user)
}

fun loadRawMergedContactAvatar(mergedContact: MergedContact) {
if (mergedContact.shouldDisplayUserAvatar()) {
loadUserAvatar(AccountUtils.currentUser!!)
} else {
binding.avatarImage.baseLoadAvatar(mergedContact)
}
}

fun loadUnknownUserAvatar() {
state.update(correspondent = null, bimi = null)
binding.avatarImage.load(R.drawable.ic_unknown_user_avatar)
}

private fun loadBimiAvatar(bimi: Bimi, correspondent: Correspondent) = with(binding.avatarImage) {
private fun loadBimiAvatar(correspondent: Correspondent, bimi: Bimi) = with(binding.avatarImage) {
state.update(correspondent, bimi)
contentDescription = correspondent.email
loadAvatar(
Expand All @@ -196,15 +205,18 @@ class AvatarView @JvmOverloads constructor(
) {
when (avatarDisplayType) {
AvatarDisplayType.UNKNOWN_CORRESPONDENT -> loadUnknownUserAvatar()
AvatarDisplayType.USER_AVATAR ->
AccountUtils.currentUser?.let { updateStateAndLoadUserAvatar(user = it, correspondent!!, bimi!!) }
AvatarDisplayType.CUSTOM_AVATAR,
AvatarDisplayType.INITIALS -> loadAvatarUsingDictionary(correspondent!!, contacts, bimi)
AvatarDisplayType.BIMI -> loadBimiAvatar(bimi!!, correspondent!!)
AvatarDisplayType.BIMI -> loadBimiAvatar(correspondent!!, bimi!!)
}
}

private fun getAvatarDisplayType(correspondent: Correspondent?, bimi: Bimi?, isBimiEnabled: Boolean): AvatarDisplayType {
return when {
correspondent == null -> AvatarDisplayType.UNKNOWN_CORRESPONDENT
correspondent.shouldDisplayUserAvatar() -> AvatarDisplayType.USER_AVATAR
correspondent.hasMergedContactAvatar(contactsFromViewModel) -> AvatarDisplayType.CUSTOM_AVATAR
bimi?.isDisplayable(isBimiEnabled) == true -> AvatarDisplayType.BIMI
else -> AvatarDisplayType.INITIALS
Expand All @@ -229,17 +241,13 @@ class AvatarView @JvmOverloads constructor(
}

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

private data class State(
Expand All @@ -255,6 +263,7 @@ class AvatarView @JvmOverloads constructor(
enum class AvatarDisplayType {
UNKNOWN_CORRESPONDENT,
CUSTOM_AVATAR,
USER_AVATAR,
BIMI,
INITIALS,
}
Expand Down