A fully functional Android app built entirely with Kotlin and Jetpack Compose
Improve lazy loading for Coil + OkHttp
Tomáš Mlynarič5f0612102d0f
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.ktnew file mode 100644index 000000000..540082e50--- /dev/null+++ b/app/src/main/java/com/google/samples/apps/nowinandroid/util/ImageLoaderAsyncFactory.kt@@ -0,0 +1,59 @@+/*+ * 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.di.ApplicationScope+import kotlinx.coroutines.CoroutineScope+import kotlinx.coroutines.Deferred+import kotlinx.coroutines.async+import kotlinx.coroutines.launch+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<ImageLoader>,+) : ImageLoaderFactory {+ private lateinit var asyncNewImageLoader: Deferred<ImageLoader>++ init {+ appScope.launch {+ // Initializing asynchronously, but start immediately+ asyncNewImageLoader = 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") { 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.ktindex 9f0bb2ef7..40fffc4d9 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,21 +17,20 @@package com.google.samples.apps.nowinandroidimport android.app.Application-import coil.ImageLoader-import coil.ImageLoaderFactory+import coil.Coilimport com.google.samples.apps.nowinandroid.sync.initializers.Sync+import com.google.samples.apps.nowinandroid.util.ImageLoaderAsyncFactoryimport com.google.samples.apps.nowinandroid.util.ProfileVerifierLoggerimport dagger.hilt.android.HiltAndroidAppimport javax.inject.Inject-import javax.inject.Provider/*** [Application] class for NiA*/@HiltAndroidApp-class NiaApplication : Application(), ImageLoaderFactory {+class NiaApplication : Application() {@Inject- lateinit var imageLoader: Provider<ImageLoader>+ lateinit var imageLoaderAsyncFactory: ImageLoaderAsyncFactory@Injectlateinit var profileVerifierLogger: ProfileVerifierLogger@@ -41,7 +40,7 @@ class NiaApplication : Application(), ImageLoaderFactory {// 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()}diff --git a/core/network/src/main/kotlin/com/google/samples/apps/nowinandroid/core/network/di/NetworkModule.kt b/core/network/src/main/kotlin/com/google/samples/apps/nowinandroid/core/network/di/NetworkModule.ktindex 21d93c0e4..a68683c7c 100644--- a/core/network/src/main/kotlin/com/google/samples/apps/nowinandroid/core/network/di/NetworkModule.kt+++ b/core/network/src/main/kotlin/com/google/samples/apps/nowinandroid/core/network/di/NetworkModule.kt@@ -17,6 +17,7 @@package com.google.samples.apps.nowinandroid.core.network.diimport android.content.Context+import androidx.tracing.traceimport coil.ImageLoaderimport coil.decode.SvgDecoderimport coil.util.DebugLogger@@ -51,16 +52,18 @@ internal object NetworkModule {@Provides@Singleton- fun okHttpCallFactory(): Call.Factory = OkHttpClient.Builder()- .addInterceptor(- HttpLoggingInterceptor()- .apply {- if (BuildConfig.DEBUG) {- setLevel(HttpLoggingInterceptor.Level.BODY)- }- },- )- .build()+ fun okHttpCallFactory(): Call.Factory = trace("NiaOkHttpClient") {+ OkHttpClient.Builder()+ .addInterceptor(+ HttpLoggingInterceptor()+ .apply {+ if (BuildConfig.DEBUG) {+ setLevel(HttpLoggingInterceptor.Level.BODY)+ }+ },+ )+ .build()+ }/*** Since we're displaying SVGs in the app, Coil needs an ImageLoader which supports this@@ -72,20 +75,21 @@ internal object NetworkModule {@Provides@Singletonfun imageLoader(- okHttpCallFactory: Call.Factory,+ // We specifically request dagger.Lazy here, so that it's not instantiated from Dagger.+ okHttpCallFactory: dagger.Lazy<Call.Factory>,@ApplicationContext application: Context,- ): ImageLoader = ImageLoader.Builder(application)- .callFactory(okHttpCallFactory)- .components {- add(SvgDecoder.Factory())- }- // Assume most content images are versioned urls- // but some problematic images are fetching each time- .respectCacheHeaders(false)- .apply {- if (BuildConfig.DEBUG) {- logger(DebugLogger())+ ): ImageLoader = trace("NiaImageLoader") {+ ImageLoader.Builder(application)+ .callFactory { okHttpCallFactory.get() }+ .components { add(SvgDecoder.Factory()) }+ // Assume most content images are versioned urls+ // but some problematic images are fetching each time+ .respectCacheHeaders(false)+ .apply {+ if (BuildConfig.DEBUG) {+ logger(DebugLogger())+ }}- }- .build()+ .build()+ }}