diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/viewmodel/ThreadDetailsViewModel.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/viewmodel/ThreadDetailsViewModel.kt index 8570a2191..adb47909e 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/viewmodel/ThreadDetailsViewModel.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/viewmodel/ThreadDetailsViewModel.kt @@ -37,6 +37,7 @@ import com.flowcrypt.email.util.exception.ThreadNotFoundException import jakarta.mail.Message.RecipientType import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.delay import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.StateFlow @@ -203,11 +204,20 @@ class ThreadDetailsViewModel( } } - fun loadMessages(clearCache: Boolean = false, silentUpdate: Boolean = false) { + fun loadMessages( + clearCache: Boolean = false, + silentUpdate: Boolean = false, + delayInMilliseconds: Long = 0 + ) { viewModelScope.launch { if (!silentUpdate) { loadMessagesManuallyMutableStateFlow.value = Result.loading() } + + if (delayInMilliseconds > 0) { + delay(delayInMilliseconds) + } + loadMessagesManuallyMutableStateFlow.value = controlledRunnerForLoadingMessages.cancelPreviousThenRun { return@cancelPreviousThenRun loadMessagesInternal( diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/workmanager/sync/UploadDraftsWorker.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/workmanager/sync/UploadDraftsWorker.kt index 772e2b0a6..8790fad7c 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/workmanager/sync/UploadDraftsWorker.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/workmanager/sync/UploadDraftsWorker.kt @@ -113,6 +113,7 @@ class UploadDraftsWorker(context: Context, params: WorkerParameters) : val draftsDir = CacheManager.getDraftDirectory(applicationContext) val directories = draftsDir.listFiles(FileFilter { it.isDirectory }) ?: emptyArray() var attemptsCount = 0 + val setOfThreadToBeAffected = mutableSetOf() while (attemptsCount < MAX_ATTEMPTS_COUNT && FileUtils.listFiles( draftsDir, TrueFileFilter.INSTANCE, @@ -187,6 +188,8 @@ class UploadDraftsWorker(context: Context, params: WorkerParameters) : EXTRA_KEY_STATE to STATE_UPLOAD_COMPLETED ) ) + + existingDraftEntity.threadId?.let { setOfThreadToBeAffected.add(it) } } } @@ -194,13 +197,17 @@ class UploadDraftsWorker(context: Context, params: WorkerParameters) : } setProgress( - workDataOf(EXTRA_KEY_STATE to STATE_COMMON_UPLOAD_COMPLETED) + workDataOf( + EXTRA_KEY_STATE to STATE_COMMON_UPLOAD_COMPLETED, + EXTRA_KEY_THREAD_ID_LIST to setOfThreadToBeAffected.toTypedArray(), + ) ) } companion object { const val EXTRA_KEY_MESSAGE_UID = "MESSAGE_UID" const val EXTRA_KEY_THREAD_ID = "THREAD_ID" + const val EXTRA_KEY_THREAD_ID_LIST = "THREAD_ID_LIST" const val EXTRA_KEY_STATE = "STATE" const val STATE_UPLOADING = 0 diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/ThreadDetailsFragment.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/ThreadDetailsFragment.kt index 6203b1b04..f21d68b5a 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/ThreadDetailsFragment.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/ThreadDetailsFragment.kt @@ -103,6 +103,7 @@ import com.flowcrypt.email.util.exception.ThreadNotFoundException import com.google.api.client.googleapis.json.GoogleJsonResponseException import org.apache.commons.io.FilenameUtils import java.net.HttpURLConnection +import java.util.concurrent.TimeUnit /** * @author Denys Bondarenko @@ -905,21 +906,32 @@ class ThreadDetailsFragment : BaseFragment(), Prog } UploadDraftsWorker.STATE_UPLOAD_COMPLETED -> { - threadDetailsViewModel.loadMessages(silentUpdate = true) + threadDetailsViewModel.loadMessages( + silentUpdate = true, + delayInMilliseconds = TimeUnit.SECONDS.toMillis(1) + ) } } } threadId == currentThreadId && state == UploadDraftsWorker.STATE_UPLOAD_COMPLETED -> { - threadDetailsViewModel.loadMessages(silentUpdate = true) + threadDetailsViewModel.loadMessages( + silentUpdate = true, + delayInMilliseconds = TimeUnit.SECONDS.toMillis(1) + ) } state == UploadDraftsWorker.STATE_COMMON_UPLOAD_COMPLETED -> { - if (messagesInThreadListAdapter.currentList.any { - it is MessagesInThreadListAdapter.Message && it.hasActiveDraftUploadingProcess - }) { - threadDetailsViewModel.loadMessages(silentUpdate = true) + if (currentThreadId != null) { + val threadIdList = + workInfo.progress.getLongArray(UploadDraftsWorker.EXTRA_KEY_THREAD_ID_LIST) + ?: return@collect + + + if (currentThreadId in threadIdList) { + threadDetailsViewModel.loadMessages(silentUpdate = true) + } } } }