From fb9568258fdefccf4199da615f9943b9cf3c94e2 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Wed, 21 Jun 2023 10:12:19 +0200 Subject: [PATCH] Clear cache: clear Coil and OkHttpClient cache. --- features/preferences/impl/build.gradle.kts | 2 ++ .../preferences/impl/tasks/ClearCacheUseCase.kt | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/features/preferences/impl/build.gradle.kts b/features/preferences/impl/build.gradle.kts index bb55094347..e8fa540f6c 100644 --- a/features/preferences/impl/build.gradle.kts +++ b/features/preferences/impl/build.gradle.kts @@ -41,6 +41,7 @@ dependencies { implementation(projects.libraries.featureflag.api) implementation(projects.libraries.featureflag.ui) implementation(projects.libraries.elementresources) + implementation(projects.libraries.network) implementation(projects.libraries.testtags) implementation(projects.libraries.uiStrings) implementation(projects.features.rageshake.api) @@ -49,6 +50,7 @@ dependencies { implementation(projects.features.logout.api) implementation(libs.datetime) implementation(libs.accompanist.placeholder) + implementation(libs.coil.compose) api(projects.features.preferences.api) ksp(libs.showkase.processor) diff --git a/features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/tasks/ClearCacheUseCase.kt b/features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/tasks/ClearCacheUseCase.kt index 1624a81d64..c351bcc127 100644 --- a/features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/tasks/ClearCacheUseCase.kt +++ b/features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/tasks/ClearCacheUseCase.kt @@ -14,9 +14,13 @@ * limitations under the License. */ +@file:OptIn(ExperimentalCoilApi::class) + package io.element.android.features.preferences.impl.tasks import android.content.Context +import coil.Coil +import coil.annotation.ExperimentalCoilApi import com.squareup.anvil.annotations.ContributesBinding import io.element.android.libraries.core.coroutine.CoroutineDispatchers import io.element.android.libraries.di.ApplicationContext @@ -24,7 +28,9 @@ import io.element.android.libraries.di.SessionScope import io.element.android.libraries.matrix.api.MatrixClient import io.element.android.libraries.matrix.api.auth.MatrixAuthenticationService import kotlinx.coroutines.withContext +import okhttp3.OkHttpClient import javax.inject.Inject +import javax.inject.Provider interface ClearCacheUseCase { suspend fun execute() @@ -36,10 +42,21 @@ class DefaultClearCacheUseCase @Inject constructor( private val matrixClient: MatrixClient, private val coroutineDispatchers: CoroutineDispatchers, private val authenticationService: MatrixAuthenticationService, + private val okHttpClient: Provider, ) : ClearCacheUseCase { override suspend fun execute() = withContext(coroutineDispatchers.io) { + // Clear Matrix cache matrixClient.clearCache() + // Clear Coil cache + Coil.imageLoader(context).let { + it.diskCache?.clear() + it.memoryCache?.clear() + } + // Clear OkHttp cache + okHttpClient.get().cache?.delete() + // Clear app cache context.cacheDir.deleteRecursively() + // Ensure the app is restarted authenticationService.incrementCacheIdx() } }