diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/workmanager/MessagesSenderWorker.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/workmanager/MessagesSenderWorker.kt index 49948a12d..7fad6c94e 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/workmanager/MessagesSenderWorker.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/workmanager/MessagesSenderWorker.kt @@ -19,6 +19,7 @@ import androidx.work.OneTimeWorkRequestBuilder import androidx.work.OutOfQuotaPolicy import androidx.work.WorkManager import androidx.work.WorkerParameters +import androidx.work.workDataOf import com.flowcrypt.email.Constants import com.flowcrypt.email.R import com.flowcrypt.email.api.email.EmailUtil @@ -493,7 +494,9 @@ class MessagesSenderWorker(context: Context, params: WorkerParameters) : id = msgEntity.draftId }, mediaContent - ).execute() + ).execute().apply { + setProgress(workDataOf(EXTRA_KEY_ID_OF_SENT_DRAFT to msgEntity.draftId)) + } } catch (e: GoogleJsonResponseException) { val isDraftNotFound = e.details.errors.any { it.message == "Requested entity was not found." @@ -588,9 +591,11 @@ class MessagesSenderWorker(context: Context, params: WorkerParameters) : } companion object { + const val EXTRA_KEY_ID_OF_SENT_DRAFT = "ID_OF_SENT_DRAFT" + private val TAG = MessagesSenderWorker::class.java.simpleName private val NOTIFICATION_ID = R.id.notification_id_sending_msgs_worker - val NAME = MessagesSenderWorker::class.java.simpleName + val NAME: String = MessagesSenderWorker::class.java.simpleName fun enqueue(context: Context, forceSending: Boolean = false) { val constraints = Constraints.Builder() 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 a6e79e52a..567b05f06 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 @@ -63,6 +63,7 @@ import com.flowcrypt.email.extensions.visibleOrGone import com.flowcrypt.email.jetpack.lifecycle.CustomAndroidViewModelFactory import com.flowcrypt.email.jetpack.viewmodel.RecipientsViewModel import com.flowcrypt.email.jetpack.viewmodel.ThreadDetailsViewModel +import com.flowcrypt.email.jetpack.workmanager.MessagesSenderWorker import com.flowcrypt.email.jetpack.workmanager.sync.ArchiveMsgsWorker import com.flowcrypt.email.jetpack.workmanager.sync.DeleteDraftsWorker import com.flowcrypt.email.jetpack.workmanager.sync.DeleteMessagesPermanentlyWorker @@ -870,6 +871,28 @@ class ThreadDetailsFragment : BaseFragment(), Prog } } } + + launchAndRepeatWithViewLifecycle(Lifecycle.State.CREATED) { + WorkManager.getInstance(requireContext()) + .getWorkInfosForUniqueWorkFlow(MessagesSenderWorker.NAME) + .collect { workInfoList -> + val workInfo = workInfoList.firstOrNull() ?: return@collect + val draftId = workInfo.progress.getString(MessagesSenderWorker.EXTRA_KEY_ID_OF_SENT_DRAFT) + ?: return@collect + + deleteDraftFromLocalCache(draftId) + } + } + } + + private fun deleteDraftFromLocalCache(draftId: String) { + val message = messagesInThreadListAdapter.currentList.firstOrNull { + it is MessagesInThreadListAdapter.Message && it.messageEntity.draftId == draftId + } as? MessagesInThreadListAdapter.Message + + if (message != null) { + threadDetailsViewModel.deleteMessageFromCache(message) + } } private fun replyTo(message: MessagesInThreadListAdapter.Message) {