Skip to content

Commit

Permalink
Fixed updating drafts on the thread details screen. Step 2.| #74
Browse files Browse the repository at this point in the history
  • Loading branch information
DenBond7 committed Nov 21, 2024
1 parent a4c588e commit 6e82361
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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."
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -870,6 +871,28 @@ class ThreadDetailsFragment : BaseFragment<FragmentThreadDetailsBinding>(), 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) {
Expand Down

0 comments on commit 6e82361

Please sign in to comment.