From 8a257ab0dc80d496fb042f9c8b6f7b729a55c11d Mon Sep 17 00:00:00 2001 From: Chris Banes Date: Fri, 18 Aug 2023 12:12:48 +0100 Subject: [PATCH] Fix images not loading (#1468) Caused by us using the same disk cache location on Android as the previous Coil implementation. This results in any existing Coil-originated disk cache being incompatible with Compose Image Loader. Fixed by using a different cache folder, and deleting the remaining Coil cache folder (if exists). --- .../imageloading/AndroidImageLoaderFactory.kt | 4 ++- .../imageloading/CoilCleanupInitializer.kt | 32 +++++++++++++++++++ .../ImageLoadingPlatformComponent.kt | 8 +++++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 common/imageloading/src/androidMain/kotlin/app/tivi/common/imageloading/CoilCleanupInitializer.kt diff --git a/common/imageloading/src/androidMain/kotlin/app/tivi/common/imageloading/AndroidImageLoaderFactory.kt b/common/imageloading/src/androidMain/kotlin/app/tivi/common/imageloading/AndroidImageLoaderFactory.kt index 6b43fc82e6..2e8df8e8a2 100644 --- a/common/imageloading/src/androidMain/kotlin/app/tivi/common/imageloading/AndroidImageLoaderFactory.kt +++ b/common/imageloading/src/androidMain/kotlin/app/tivi/common/imageloading/AndroidImageLoaderFactory.kt @@ -28,7 +28,9 @@ internal class AndroidImageLoaderFactory( maxSizePercent(context.applicationContext) } diskCacheConfig { - directory(context.cacheDir.resolve("image_cache").toOkioPath()) + // We can't use `image_cache` as that is what Coil uses, and therefore users + // migrating from old versions will have a broken cache. + directory(context.cacheDir.resolve("image_loader_cache").toOkioPath()) maxSizeBytes(512L * 1024 * 1024) // 512MB } } diff --git a/common/imageloading/src/androidMain/kotlin/app/tivi/common/imageloading/CoilCleanupInitializer.kt b/common/imageloading/src/androidMain/kotlin/app/tivi/common/imageloading/CoilCleanupInitializer.kt new file mode 100644 index 0000000000..666e8b5922 --- /dev/null +++ b/common/imageloading/src/androidMain/kotlin/app/tivi/common/imageloading/CoilCleanupInitializer.kt @@ -0,0 +1,32 @@ +// Copyright 2023, Christopher Banes and the Tivi project contributors +// SPDX-License-Identifier: Apache-2.0 + +package app.tivi.common.imageloading + +import android.app.Application +import app.tivi.appinitializers.AppInitializer +import app.tivi.util.AppCoroutineDispatchers +import kotlinx.coroutines.DelicateCoroutinesApi +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext +import me.tatarka.inject.annotations.Inject + +@Inject +class CoilCleanupInitializer( + private val application: Application, + private val dispatchers: AppCoroutineDispatchers, +) : AppInitializer { + @OptIn(DelicateCoroutinesApi::class) + override fun initialize() { + GlobalScope.launch { + withContext(dispatchers.io) { + // We delete Coil's image_cache folder to claim back space for the user + val coilCache = application.cacheDir.resolve("image_cache") + if (coilCache.exists()) { + coilCache.deleteRecursively() + } + } + } + } +} diff --git a/common/imageloading/src/androidMain/kotlin/app/tivi/common/imageloading/ImageLoadingPlatformComponent.kt b/common/imageloading/src/androidMain/kotlin/app/tivi/common/imageloading/ImageLoadingPlatformComponent.kt index aee64378bd..65505e1e4a 100644 --- a/common/imageloading/src/androidMain/kotlin/app/tivi/common/imageloading/ImageLoadingPlatformComponent.kt +++ b/common/imageloading/src/androidMain/kotlin/app/tivi/common/imageloading/ImageLoadingPlatformComponent.kt @@ -4,9 +4,11 @@ package app.tivi.common.imageloading import android.app.Application +import app.tivi.appinitializers.AppInitializer import app.tivi.util.Logger import com.seiko.imageloader.ImageLoader import com.seiko.imageloader.intercept.Interceptor +import me.tatarka.inject.annotations.IntoSet import me.tatarka.inject.annotations.Provides actual interface ImageLoadingPlatformComponent { @@ -22,4 +24,10 @@ actual interface ImageLoadingPlatformComponent { addInterceptors(interceptors) } } + + @Provides + @IntoSet + fun bindCoilCleanupInitializer( + initializer: CoilCleanupInitializer, + ): AppInitializer = initializer }