From 62a084d4e9b2ffe15ce6d5ff7d8c7fcab3536f09 Mon Sep 17 00:00:00 2001 From: Hasib Prince Date: Mon, 6 Nov 2023 19:13:53 +0600 Subject: [PATCH] caching operation in IO dispatchers --- .../java/com/fsck/k9/ui/messagelist/EmailCache.kt | 3 +-- .../fsck/k9/ui/messagelist/MessageListLiveData.kt | 12 +++++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/ui/messagelist/EmailCache.kt b/app/ui/legacy/src/main/java/com/fsck/k9/ui/messagelist/EmailCache.kt index f1ff13198c..d4e6839294 100644 --- a/app/ui/legacy/src/main/java/com/fsck/k9/ui/messagelist/EmailCache.kt +++ b/app/ui/legacy/src/main/java/com/fsck/k9/ui/messagelist/EmailCache.kt @@ -20,7 +20,6 @@ import android.content.Context import androidx.datastore.preferences.core.edit import androidx.datastore.preferences.core.stringPreferencesKey import androidx.datastore.preferences.preferencesDataStore -import com.fsck.k9.Account import com.fsck.k9.controller.MessageReference import com.google.gson.Gson import com.google.gson.TypeAdapter @@ -55,7 +54,7 @@ class EmailCache constructor(private val context: Context, private val gson: Gso } suspend fun saveLatestMails(mailList: List) { - if (isCacheShown) return + if (mailList.size < MAX_CACHE_SIZE || isCacheShown) return isCacheShown = true val cachedMailsWithLatest = getLatestMails(mailList) diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/ui/messagelist/MessageListLiveData.kt b/app/ui/legacy/src/main/java/com/fsck/k9/ui/messagelist/MessageListLiveData.kt index 3644e3fa3c..77cb52c9c9 100644 --- a/app/ui/legacy/src/main/java/com/fsck/k9/ui/messagelist/MessageListLiveData.kt +++ b/app/ui/legacy/src/main/java/com/fsck/k9/ui/messagelist/MessageListLiveData.kt @@ -25,15 +25,21 @@ class MessageListLiveData( private fun loadMessageListAsync() { coroutineScope.launch(Dispatchers.Main) { - emailCache.getCachedMails()?.let { + val cachedMessages = withContext(Dispatchers.IO) { + emailCache.getCachedMails() + } + + cachedMessages?.let { value = MessageListInfo(it, true) } val messageList = withContext(Dispatchers.IO) { - messageListLoader.getMessageList(config) + val result = messageListLoader.getMessageList(config) + emailCache.saveLatestMails(result.messageListItems) + result } + value = messageList - emailCache.saveLatestMails(messageList.messageListItems) } } -- GitLab