Skip to content

Commit

Permalink
refactor: Optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasBourdin88 committed Jan 28, 2025
1 parent 366a491 commit e3cf9ea
Showing 1 changed file with 30 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,48 +75,53 @@ class DownloadMessagesViewModel @Inject constructor(

fun downloadThreads(currentMailbox: Mailbox?) {
viewModelScope.launch(ioCoroutineContext) {
val downloadedThreadUris: List<Uri>? = runCatching {

val mailbox = currentMailbox ?: return@runCatching null
val listUri = mutableListOf<Uri>()
val listFileName = hashMapOf<String, Int>()
val mailbox = currentMailbox ?: return@launch
val messages = getAllMessages()

runCatching {
coroutineScope {
val deferredResponses: List<Deferred<Uri?>> = getAllMessages().map { message ->
val listFileName = HashMap<String, Int>()

val deferredResponses = getAllMessages().map { message ->
async {
val response = ApiRepository.getDownloadedMessage(
mailboxUuid = mailbox.uuid,
folderId = message.folderId,
shortUid = message.shortUid,
)

val messageSubject: String = message.subject?.removeIllegalFileNameCharacter()
?: NO_SUBJECT_FILE
val truncatedSubject = messageSubject.take(MAX_FILE_NAME_LENGTH)

val fileName = if (listFileName[truncatedSubject] == null) {
listFileName[truncatedSubject] = 0
truncatedSubject
} else {
listFileName[truncatedSubject] = listFileName[truncatedSubject]!! + 1
"$truncatedSubject (${listFileName[truncatedSubject]!! + 1})"
}

if (!response.isSuccessful || response.body == null) return@async null

saveEmlToFile(appContext, response.body!!.bytes(), fileName) ?: return@async null
val messageSubject = message.subject?.removeIllegalFileNameCharacter() ?: NO_SUBJECT_FILE
val truncatedSubject = messageSubject.take(MAX_FILE_NAME_LENGTH)
val fileName = createUniqueFileName(listFileName, truncatedSubject)

saveEmlToFile(appContext, response.body!!.bytes(), fileName)
}
}

val uris = deferredResponses.awaitAll()
listUri.addAll(uris.filterNotNull())
deferredResponses.awaitAll().filterNotNull()
}
}.onSuccess { downloadedThreadUris ->
if (downloadedThreadUris.size != messages.size) {
// TODO: Manage error
}
downloadMessagesLiveData.postValue(downloadedThreadUris)
}.onFailure { _ ->
downloadMessagesLiveData.postValue(null)
}
}
}

listUri
}.getOrNull()

downloadMessagesLiveData.postValue(downloadedThreadUris)
private fun createUniqueFileName(listFileName: HashMap<String, Int>, truncatedSubject: String): String {
val fileName = if (listFileName[truncatedSubject] == null) {
listFileName[truncatedSubject] = 0
truncatedSubject
} else {
listFileName[truncatedSubject] = listFileName[truncatedSubject]!! + 1
"$truncatedSubject (${listFileName[truncatedSubject]!! + 1})"
}
return fileName
}

private fun saveEmlToFile(context: Context, emlByteArray: ByteArray, fileName: String): Uri? {
Expand Down

0 comments on commit e3cf9ea

Please sign in to comment.