Skip to content

Commit

Permalink
feat: Manage error when one message failed
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasBourdin88 committed Jan 30, 2025
1 parent 9e81557 commit 471d507
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,7 @@ abstract class TwoPaneFragment : Fragment() {
private val resultActivityResultLauncher = registerForActivityResult(StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
val fileDir: File = getEmlCacheDir(requireContext())

if (!fileDir.deleteRecursively()) {
// TODO: Manage error
}
fileDir.deleteRecursively()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import com.infomaniak.lib.core.utils.goToPlayStore
Expand All @@ -34,15 +33,15 @@ import com.infomaniak.mail.utils.SaveOnKDriveUtils.canSaveOnKDrive

class DownloadMessagesProgressDialog : DownloadProgressDialog() {
private val downloadThreadsViewModel: DownloadMessagesViewModel by viewModels()
override val dialogTitle: String? by lazy { getDialogTitleFromArgs() }
override val dialogTitle: String? by lazy { getDialogName() }

override fun onCreate(savedInstanceState: Bundle?) {
observeDownload()
super.onCreate(savedInstanceState)
}

override fun download() {
downloadThreadsViewModel.downloadThreads(mainViewModel.currentMailbox.value)
downloadThreadsViewModel.downloadMessages(mainViewModel.currentMailbox.value)
}

private fun observeDownload() {
Expand All @@ -57,11 +56,11 @@ class DownloadMessagesProgressDialog : DownloadProgressDialog() {
}
}

private fun getDialogTitleFromArgs(): String {
private fun getDialogName(): String {
val numberOfMessagesToDownload = downloadThreadsViewModel.numberOfMessagesToDownloads()

return if (numberOfMessagesToDownload == 1) {
downloadThreadsViewModel.getSubject() ?: requireContext().getString(R.string.noSubjectTitle)
downloadThreadsViewModel.getFirstMessageSubject() ?: requireContext().getString(R.string.noSubjectTitle)
} else {
requireContext().resources.getQuantityString(
R.plurals.downloadingEmailsTitle,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,33 @@ class DownloadMessagesViewModel @Inject constructor(
return messages
}

fun downloadThreads(currentMailbox: Mailbox?) {
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? {
val fileNameWithExtension = "${fileName.removeIllegalFileNameCharacter()}.eml"
val fileDir = getEmlCacheDir(context)

if (!fileDir.exists()) fileDir.mkdirs()

return runCatching {
val file = File(fileDir, fileNameWithExtension)
file.outputStream().use { it.write(emlByteArray) }
return FileProvider.getUriForFile(context, context.getString(R.string.EML_AUTHORITY), file)
}.getOrNull()
}

fun downloadMessages(currentMailbox: Mailbox?) {
viewModelScope.launch(ioCoroutineContext) {
val mailbox = currentMailbox ?: return@launch
val messages = getAllMessages()

runCatching {
coroutineScope {
Expand All @@ -103,47 +126,22 @@ class DownloadMessagesViewModel @Inject constructor(
deferredResponses.awaitAll().filterNotNull()
}
}.onSuccess { downloadedThreadUris ->
if (downloadedThreadUris.size != messages.size) {
// TODO: Manage error
}
if (downloadedThreadUris.size != numberOfMessagesToDownloads()) downloadMessagesLiveData.postValue(null)

downloadMessagesLiveData.postValue(downloadedThreadUris)
}.onFailure { _ ->
downloadMessagesLiveData.postValue(null)
}
}
}

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? {
val fileNameWithExtension = "${fileName.removeIllegalFileNameCharacter()}.eml"
val fileDir = getEmlCacheDir(context)

if (!fileDir.exists()) fileDir.mkdirs()

return runCatching {
val file = File(fileDir, fileNameWithExtension)
file.outputStream().use { it.write(emlByteArray) }
return FileProvider.getUriForFile(context, context.getString(R.string.EML_AUTHORITY), file)
}.getOrNull()
}

fun getSubject(): String? {
fun getFirstMessageSubject(): String? {
val messages = getAllMessages()
return messages.firstOrNull()?.subject
}

fun numberOfMessagesToDownloads(): Int {
return getAllMessages().size
return (messageLocalUids?.size ?: 0) + (threadLocalUids?.size ?: 0)
}

private fun String.removeIllegalFileNameCharacter(): String = this.replace(DownloadManagerUtils.regexInvalidSystemChar, "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import com.infomaniak.mail.ui.main.thread.actions.DownloadAttachmentProgressDial
import com.infomaniak.mail.utils.AccountUtils
import com.infomaniak.mail.utils.SaveOnKDriveUtils.DRIVE_PACKAGE
import com.infomaniak.mail.utils.SaveOnKDriveUtils.SAVE_EXTERNAL_ACTIVITY_CLASS
import com.infomaniak.mail.utils.SaveOnKDriveUtils.canSaveOnKDrive
import com.infomaniak.mail.utils.SentryDebug
import com.infomaniak.mail.utils.WorkerUtils.UploadMissingLocalFileException
import com.infomaniak.mail.utils.extensions.AttachmentExtensions.AttachmentIntentType.OPEN_WITH
Expand Down

0 comments on commit 471d507

Please sign in to comment.