Skip to content

Commit

Permalink
fix: Apply suggestion from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasBourdin88 committed Feb 21, 2025
1 parent 83595ff commit bfd9789
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/
package com.infomaniak.mail.ui.main.folder

import android.app.Activity
import android.content.res.Configuration
import android.os.Bundle
import android.view.View
Expand Down Expand Up @@ -46,7 +45,6 @@ import com.infomaniak.mail.ui.main.thread.ThreadFragment
import com.infomaniak.mail.ui.main.thread.actions.DownloadMessagesProgressDialog
import com.infomaniak.mail.utils.LocalStorageUtils.getEmlCacheDir
import com.infomaniak.mail.utils.extensions.*
import java.io.File
import javax.inject.Inject

abstract class TwoPaneFragment : Fragment() {
Expand Down Expand Up @@ -124,10 +122,7 @@ abstract class TwoPaneFragment : Fragment() {
}

private val resultActivityResultLauncher = registerForActivityResult(StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
val fileDir: File = getEmlCacheDir(requireContext())
fileDir.deleteRecursively()
}
getEmlCacheDir(requireContext()).deleteRecursively()
}

private fun observeThreadNavigation() = with(twoPaneViewModel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import com.infomaniak.lib.core.utils.goToPlayStore
import com.infomaniak.lib.core.utils.setBackNavigationResult
import com.infomaniak.mail.R
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

class DownloadMessagesProgressDialog : DownloadProgressDialog() {
private val downloadThreadsViewModel: DownloadMessagesViewModel by viewModels()

override val dialogTitle: String? by lazy { getDialogName() }

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -56,19 +56,7 @@ class DownloadMessagesProgressDialog : DownloadProgressDialog() {
}
}

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

return if (numberOfMessagesToDownload == 1) {
downloadThreadsViewModel.getFirstMessageSubject() ?: requireContext().getString(R.string.noSubjectTitle)
} else {
requireContext().resources.getQuantityString(
R.plurals.downloadingEmailsTitle,
numberOfMessagesToDownload,
numberOfMessagesToDownload,
)
}
}
private fun getDialogName(): String = downloadThreadsViewModel.getDialogName()

private fun List<Uri>.openKDriveOrPlayStore(context: Context): Intent? {
return if (canSaveOnKDrive(context)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,46 +102,51 @@ class DownloadMessagesViewModel @Inject constructor(
val mailbox = currentMailbox ?: return@launch

runCatching {
coroutineScope {
val listFileName = HashMap<String, Int>()
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 deferredResponses = getAllMessages().map { message ->
async {
val response = ApiRepository.getDownloadedMessage(
mailboxUuid = mailbox.uuid,
folderId = message.folderId,
shortUid = message.shortUid,
)

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

val messageSubject = message.subject?.removeIllegalFileNameCharacter() ?: NO_SUBJECT_FILE
val truncatedSubject = messageSubject.take(MAX_FILE_NAME_LENGTH)
val fileName = createUniqueFileName(listFileName, truncatedSubject)
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)
}
saveEmlToFile(appContext, response.body!!.bytes(), fileName)
}

deferredResponses.awaitAll().filterNotNull()
}

deferredResponses.awaitAll().filterNotNull()
}.onSuccess { downloadedThreadUris ->
if (downloadedThreadUris.size != numberOfMessagesToDownloads()) downloadMessagesLiveData.postValue(null)

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

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

private fun numberOfMessagesToDownloads(): Int = (messageLocalUids?.size ?: 0) + (threadLocalUids?.size ?: 0)

fun numberOfMessagesToDownloads(): Int {
return (messageLocalUids?.size ?: 0) + (threadLocalUids?.size ?: 0)
fun getDialogName(): String {
val numberOfMessagesToDownload = numberOfMessagesToDownloads()

return if (numberOfMessagesToDownload == 1) {
getFirstMessageSubject() ?: appContext.getString(R.string.noSubjectTitle)
} else {
appContext.resources.getQuantityString(
R.plurals.downloadingEmailsTitle,
numberOfMessagesToDownload,
numberOfMessagesToDownload,
)
}
}

private fun String.removeIllegalFileNameCharacter(): String = this.replace(DownloadManagerUtils.regexInvalidSystemChar, "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ object LocalStorageUtils {

private const val ATTACHMENTS_CACHE_DIR = "attachments_cache"
private const val ATTACHMENTS_UPLOAD_DIR = "attachments_upload"
private const val EML_CACHE_DIR = "eml_export"
private const val HIDDEN_FILE_NAME = "HIDDEN_FILE_NAME"
private const val NAME_TOO_LONG_EXCEPTION = "ENAMETOOLONG"
private const val EML_CACHE_DIR = "eml_export"

private inline val Context.attachmentsCacheRootDir get() = File(cacheDir, ATTACHMENTS_CACHE_DIR)
private inline val Context.attachmentsUploadRootDir get() = File(filesDir, ATTACHMENTS_UPLOAD_DIR)
Expand Down
5 changes: 2 additions & 3 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
~ You should have received a copy of the GNU General Public License
~ along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<resources>
<resources xmlns:tools="http://schemas.android.com/tools">
<string name="accentColorBlueTitle">Blau</string>
<string name="accentColorPinkTitle">Rosa</string>
<string name="accentColorSystemTitle">Farbe des Systems</string>
Expand Down Expand Up @@ -246,8 +246,7 @@
<string name="disabledFeatureFlagTitle">Benötigen Sie mehr Speicherplatz und Funktionen?</string>
<string name="displayMailIssueDescription">Um Anzeigeprobleme zu beheben, aktualisieren Sie bitte die Anwendung Android System Webview.</string>
<string name="displayMailIssueTitle">Problem mit der Anzeige Ihrer E-Mails</string>
<plurals name="downloadingEmailsTitle">
<item quantity="one">Herunterladen von %d-E-Mails</item>
<plurals name="downloadingEmailsTitle" tools:ignore="MissingQuantity">
<item quantity="other">Herunterladen von %d-E-Mails</item>
</plurals>
<string name="draftFolder">Entwürfe</string>
Expand Down
5 changes: 2 additions & 3 deletions app/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
~ You should have received a copy of the GNU General Public License
~ along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<resources>
<resources xmlns:tools="http://schemas.android.com/tools">
<string name="accentColorBlueTitle">Azul</string>
<string name="accentColorPinkTitle">Rosa</string>
<string name="accentColorSystemTitle">Color del sistema</string>
Expand Down Expand Up @@ -246,8 +246,7 @@
<string name="disabledFeatureFlagTitle">¿Necesita más almacenamiento y funciones?</string>
<string name="displayMailIssueDescription">Para solucionar los problemas de visualización, actualice la aplicación Android System Webview.</string>
<string name="displayMailIssueTitle">Problema de visualización de sus emails</string>
<plurals name="downloadingEmailsTitle">
<item quantity="one">Descargando %d correos electrónicos</item>
<plurals name="downloadingEmailsTitle" tools:ignore="MissingQuantity">
<item quantity="other">Descargando %d correos electrónicos</item>
</plurals>
<string name="draftFolder">Borradores</string>
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,7 @@
<string name="disabledFeatureFlagTitle">Besoin de plus de stockage et de fonctionnalités ?</string>
<string name="displayMailIssueDescription">Pour corriger les problèmes d’affichage, veuillez mettre à jour l’application Android System Webview.</string>
<string name="displayMailIssueTitle">Problème d’affichage de vos e-mails</string>
<plurals name="downloadingEmailsTitle">
<item quantity="one">Téléchargement de %d e-mails</item>
<plurals name="downloadingEmailsTitle" tools:ignore="MissingQuantity">
<item quantity="other">Téléchargement de %d e-mails</item>
<item quantity="many">Téléchargement de %d d’e-mails</item>
</plurals>
Expand Down
5 changes: 2 additions & 3 deletions app/src/main/res/values-it/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
~ You should have received a copy of the GNU General Public License
~ along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<resources>
<resources xmlns:tools="http://schemas.android.com/tools">
<string name="accentColorBlueTitle">Blu</string>
<string name="accentColorPinkTitle">Rosa</string>
<string name="accentColorSystemTitle">Colore del sistema</string>
Expand Down Expand Up @@ -246,8 +246,7 @@
<string name="disabledFeatureFlagTitle">Hai bisogno di più spazio di archiviazione e funzionalità?</string>
<string name="displayMailIssueDescription">Per risolvere i problemi di visualizzazione, aggiornare l’applicazione Android System Webview.</string>
<string name="displayMailIssueTitle">Problema di visualizzazione delle email</string>
<plurals name="downloadingEmailsTitle">
<item quantity="one">Download di %d e-mail</item>
<plurals name="downloadingEmailsTitle" tools:ignore="MissingQuantity">
<item quantity="other">Download di %d email</item>
</plurals>
<string name="draftFolder">Bozze</string>
Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,6 @@
<string name="displayMailIssueDescription">To fix display problems, please update the Android System Webview application.</string>
<string name="displayMailIssueTitle">Problem displaying your emails</string>
<plurals name="downloadingEmailsTitle">
<item quantity="one">Downloading %d emails</item>
<item quantity="other">Downloading %d emails</item>
</plurals>
<string name="draftFolder">Drafts</string>
Expand Down

0 comments on commit bfd9789

Please sign in to comment.