Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
DenBond7 committed Nov 26, 2024
1 parent cd774f9 commit dc9e417
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Long>()
while (attemptsCount < MAX_ATTEMPTS_COUNT && FileUtils.listFiles(
draftsDir,
TrueFileFilter.INSTANCE,
Expand Down Expand Up @@ -187,20 +188,26 @@ class UploadDraftsWorker(context: Context, params: WorkerParameters) :
EXTRA_KEY_STATE to STATE_UPLOAD_COMPLETED
)
)

existingDraftEntity.threadId?.let { setOfThreadToBeAffected.add(it) }
}
}

attemptsCount++
}

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -905,21 +906,32 @@ class ThreadDetailsFragment : BaseFragment<FragmentThreadDetailsBinding>(), 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)
}
}
}
}
Expand Down

0 comments on commit dc9e417

Please sign in to comment.