From 74175c1d1cf8a50c04ec8154a024898333f5e915 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Mlynari=C4=8D?= Date: Thu, 8 Feb 2024 15:05:12 +0100 Subject: [PATCH] Revert fast initialization of Coil As discussed in https://github.com/coil-kt/coil/issues/2097 the problem is caused by regitering system services, which will be fixed in 2.6.0 Change-Id: I9085309780508137f10b25ff82deed3c62e5d159 --- .../util/ImageLoaderAsyncFactory.kt | 63 ------------------- .../apps/nowinandroid/NiaApplication.kt | 12 ++-- 2 files changed, 6 insertions(+), 69 deletions(-) delete mode 100644 app/src/main/java/com/google/samples/apps/nowinandroid/util/ImageLoaderAsyncFactory.kt diff --git a/app/src/main/java/com/google/samples/apps/nowinandroid/util/ImageLoaderAsyncFactory.kt b/app/src/main/java/com/google/samples/apps/nowinandroid/util/ImageLoaderAsyncFactory.kt deleted file mode 100644 index d8f928de5..000000000 --- a/app/src/main/java/com/google/samples/apps/nowinandroid/util/ImageLoaderAsyncFactory.kt +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright 2024 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.samples.apps.nowinandroid.util - -import androidx.tracing.trace -import coil.ImageLoader -import coil.ImageLoaderFactory -import com.google.samples.apps.nowinandroid.core.network.NiaDispatchers.Default -import com.google.samples.apps.nowinandroid.core.network.di.ApplicationScope -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Deferred -import kotlinx.coroutines.async -import kotlinx.coroutines.runBlocking -import javax.inject.Inject - -/** - * This class asynchronously loads the Coil's image loader on a [ApplicationScope], which uses Default dispatcher. - * Reason for this is to prevent initializing Coil (and thus OkHttp internally) with the first image loading - * to prevent skipping frames and performance issues. - * - * Usage: - * - Init creates an async initialization of the image loader. - * - delegate to [newImageLoader] so that Coil can automatically reach for its loader. - */ -class ImageLoaderAsyncFactory @Inject constructor( - @ApplicationScope - appScope: CoroutineScope, - private val imageLoader: dagger.Lazy, -) : ImageLoaderFactory { - - /** - * Initialize immediately, but need a Deferred for callers - * [ApplicationScope] already uses [Default] dispatcher, so we don't have to switch it here. - */ - private val asyncNewImageLoader: Deferred = appScope.async { imageLoader.get() } - - /** - * This runBlocking here is on purpose to prevent any unfinished Coil initialization. - * Most likely this will be already initialized by the time we want to show an image on the screen. - */ - override fun newImageLoader() = - trace("NiaImageLoader.runBlocking") { - if (asyncNewImageLoader.isCompleted) { - asyncNewImageLoader.getCompleted() - } else { - runBlocking { asyncNewImageLoader.await() } - } - } -} diff --git a/app/src/main/kotlin/com/google/samples/apps/nowinandroid/NiaApplication.kt b/app/src/main/kotlin/com/google/samples/apps/nowinandroid/NiaApplication.kt index 40fffc4d9..8e3ad814a 100644 --- a/app/src/main/kotlin/com/google/samples/apps/nowinandroid/NiaApplication.kt +++ b/app/src/main/kotlin/com/google/samples/apps/nowinandroid/NiaApplication.kt @@ -17,9 +17,9 @@ package com.google.samples.apps.nowinandroid import android.app.Application -import coil.Coil +import coil.ImageLoader +import coil.ImageLoaderFactory import com.google.samples.apps.nowinandroid.sync.initializers.Sync -import com.google.samples.apps.nowinandroid.util.ImageLoaderAsyncFactory import com.google.samples.apps.nowinandroid.util.ProfileVerifierLogger import dagger.hilt.android.HiltAndroidApp import javax.inject.Inject @@ -28,9 +28,9 @@ import javax.inject.Inject * [Application] class for NiA */ @HiltAndroidApp -class NiaApplication : Application() { +class NiaApplication : Application(), ImageLoaderFactory { @Inject - lateinit var imageLoaderAsyncFactory: ImageLoaderAsyncFactory + lateinit var imageLoader: dagger.Lazy @Inject lateinit var profileVerifierLogger: ProfileVerifierLogger @@ -40,7 +40,7 @@ class NiaApplication : Application() { // Initialize Sync; the system responsible for keeping data in the app up to date. Sync.initialize(context = this) profileVerifierLogger() - // We set immediately Coil's image loader factory to prevent initialization with the first image. - Coil.setImageLoader(imageLoaderAsyncFactory) } + + override fun newImageLoader(): ImageLoader = imageLoader.get() }