Skip to content

Commit 3c51c24

Browse files
committed
Avatar: Retry after some seconds.
The avatar is assigned very early in the flow while refreshing the access token did not pass yet leading to 401. The cache does not help as it insists on a refresh. This is probably a regression from switching from our own cache to coil.
1 parent f525940 commit 3c51c24

1 file changed

Lines changed: 22 additions & 6 deletions

File tree

  • opencloudApp/src/main/java/eu/opencloud/android/presentation/avatar

opencloudApp/src/main/java/eu/opencloud/android/presentation/avatar/AvatarUtils.kt

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@ import android.accounts.Account
2323
import android.view.MenuItem
2424
import android.widget.ImageView
2525
import eu.opencloud.android.R
26-
import coil.load
26+
import coil.request.ErrorResult
27+
import coil.request.ImageRequest
2728
import eu.opencloud.android.MainApp.Companion.appContext
2829
import eu.opencloud.android.presentation.thumbnails.ThumbnailsRequester
30+
import kotlinx.coroutines.delay
2931
import org.koin.core.component.KoinComponent
32+
import timber.log.Timber
3033

3134
class AvatarUtils : KoinComponent {
3235

@@ -46,19 +49,32 @@ class AvatarUtils : KoinComponent {
4649
* the server. When 'false', server is not accessed, the fallback avatar is
4750
* generated instead.
4851
*/
49-
fun loadAvatarForAccount(
52+
suspend fun loadAvatarForAccount(
5053
imageView: ImageView,
5154
account: Account,
5255
@Suppress("UnusedParameter") displayRadius: Float,
5356
imageLoader: coil.ImageLoader? = null
5457
) {
5558
val uri = ThumbnailsRequester.getAvatarUri(account)
5659
val loader = imageLoader ?: ThumbnailsRequester.getRevalidatingImageLoader(account)
57-
imageView.load(uri, loader) {
58-
placeholder(R.drawable.ic_account_circle)
59-
error(R.drawable.ic_account_circle)
60-
transformations(coil.transform.CircleCropTransformation())
60+
// No .target(imageView) here — using execute() with a ViewTarget can hang
61+
// due to Coil's internal lifecycle checks. We set the drawable manually instead.
62+
val request = ImageRequest.Builder(appContext)
63+
.data(uri)
64+
.transformations(coil.transform.CircleCropTransformation())
65+
.build()
66+
Timber.d("Avatar load for $uri")
67+
var result = loader.execute(request)
68+
if (result is ErrorResult) {
69+
// On failure, give ConnectionValidator time to refresh the token on another
70+
// thread, then retry once.
71+
Timber.d("Avatar load failed for $uri, retrying in 5s")
72+
delay(5_000L)
73+
Timber.d("Retrying avatar load for $uri")
74+
result = loader.execute(request)
6175
}
76+
(result as? coil.request.SuccessResult)?.let { imageView.setImageDrawable(it.drawable) }
77+
?: imageView.setImageResource(R.drawable.ic_account_circle)
6278
}
6379

6480
fun loadAvatarForAccount(

0 commit comments

Comments
 (0)