From fdc9261df1b9317e252f53aed59d9a0980ffe496 Mon Sep 17 00:00:00 2001 From: Niko Diamadis Date: Tue, 16 Apr 2024 18:39:45 +0200 Subject: [PATCH] Clear and refresh image caches on swipe down --- .../kotlin/com/github/gotify/CoilHandler.kt | 8 ++++++-- .../github/gotify/messages/MessagesActivity.kt | 17 +++++++++-------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/app/src/main/kotlin/com/github/gotify/CoilHandler.kt b/app/src/main/kotlin/com/github/gotify/CoilHandler.kt index 7541fc5..26ff360 100644 --- a/app/src/main/kotlin/com/github/gotify/CoilHandler.kt +++ b/app/src/main/kotlin/com/github/gotify/CoilHandler.kt @@ -56,8 +56,12 @@ internal class CoilHandler(private val context: Context, private val settings: S fun get() = imageLoader @OptIn(ExperimentalCoilApi::class) - @Throws(IOException::class) fun evict() { - imageLoader.diskCache?.directory?.toFile()?.deleteRecursively() + try { + imageLoader.diskCache?.clear() + imageLoader.memoryCache?.clear() + } catch (e: IOException) { + Logger.error(e, "Problem evicting Coil cache") + } } } diff --git a/app/src/main/kotlin/com/github/gotify/messages/MessagesActivity.kt b/app/src/main/kotlin/com/github/gotify/messages/MessagesActivity.kt index 5dfb757..006a981 100644 --- a/app/src/main/kotlin/com/github/gotify/messages/MessagesActivity.kt +++ b/app/src/main/kotlin/com/github/gotify/messages/MessagesActivity.kt @@ -58,7 +58,6 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.navigation.NavigationView import com.google.android.material.snackbar.BaseTransientBottomBar.BaseCallback import com.google.android.material.snackbar.Snackbar -import java.io.IOException import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import org.tinylog.kotlin.Logger @@ -170,19 +169,20 @@ internal class MessagesActivity : } private fun refreshAll() { - try { - viewModel.coilHandler.evict() - } catch (e: IOException) { - Logger.error(e, "Problem evicting Coil cache") - } + viewModel.coilHandler.evict() startActivity(Intent(this, InitializationActivity::class.java)) finish() } private fun onRefresh() { + viewModel.coilHandler.evict() viewModel.messages.clear() launchCoroutine { - loadMore(viewModel.appId) + loadMore(viewModel.appId).forEachIndexed { index, message -> + if (message.image != null) { + listMessageAdapter.notifyItemChanged(index) + } + } } } @@ -556,11 +556,12 @@ internal class MessagesActivity : ) } - private suspend fun loadMore(appId: Long) { + private suspend fun loadMore(appId: Long): List { val messagesWithImages = viewModel.messages.loadMore(appId) withContext(Dispatchers.Main) { updateMessagesAndStopLoading(messagesWithImages) } + return messagesWithImages } private suspend fun updateMessagesForApplication(withLoadingSpinner: Boolean, appId: Long) {