From 817c793f6c8ed92e6d9036c45b9513cc8ae46490 Mon Sep 17 00:00:00 2001 From: Kevin Boulongne Date: Wed, 4 Dec 2024 11:01:54 +0100 Subject: [PATCH] feat: Make sure Transfer author email is mandatory and correctly formatted --- .../newtransfer/ImportFilesViewModel.kt | 7 ++- .../importfiles/ImportFilesScreen.kt | 49 ++++++++++++++----- app/src/main/res/values-de/strings.xml | 1 + app/src/main/res/values-es/strings.xml | 1 + app/src/main/res/values-fr/strings.xml | 1 + app/src/main/res/values-it/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 7 files changed, 49 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/ImportFilesViewModel.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/ImportFilesViewModel.kt index 258ff5b31..c9ca720f2 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/ImportFilesViewModel.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/ImportFilesViewModel.kt @@ -137,7 +137,7 @@ class ImportFilesViewModel @Inject constructor( private fun generateNewUploadSession(): NewUploadSession { return NewUploadSession( duration = selectedValidityPeriodOption.value.apiValue, - authorEmail = "", + authorEmail = if (selectedTransferType.value == TransferTypeUi.MAIL) _transferAuthorEmail else "", password = if (selectedPasswordOption.value == PasswordTransferOption.ACTIVATED) transferPassword else NO_PASSWORD, message = _transferMessage, numberOfDownload = selectedDownloadLimitOption.value.apiValue, @@ -240,6 +240,11 @@ class ImportFilesViewModel @Inject constructor( } //endregion + //region Transfer Author Email + private var _transferAuthorEmail by mutableStateOf("") + val transferAuthorEmail = GetSetCallbacks(get = { _transferAuthorEmail }, set = { _transferAuthorEmail = it }) + //endregion + //region Transfer Message private var _transferMessage by mutableStateOf("") val transferMessage = GetSetCallbacks(get = { _transferMessage }, set = { _transferMessage = it }) diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/importfiles/ImportFilesScreen.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/importfiles/ImportFilesScreen.kt index a540d5e18..1cbd55486 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/importfiles/ImportFilesScreen.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/importfiles/ImportFilesScreen.kt @@ -32,9 +32,11 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.vector.ImageVector import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.input.KeyboardType import androidx.compose.ui.tooling.preview.PreviewParameter import androidx.hilt.navigation.compose.hiltViewModel import androidx.lifecycle.compose.collectAsStateWithLifecycle +import com.infomaniak.core2.isEmail import com.infomaniak.multiplatform_swisstransfer.common.interfaces.ui.FileUi import com.infomaniak.swisstransfer.R import com.infomaniak.swisstransfer.ui.components.* @@ -104,6 +106,7 @@ fun ImportFilesScreen( files = { files }, filesToImportCount = { filesToImportCount }, currentSessionFilesCount = { currentSessionFilesCount }, + transferAuthorEmail = importFilesViewModel.transferAuthorEmail, transferMessage = importFilesViewModel.transferMessage, selectedTransferType = GetSetCallbacks( get = { selectedTransferType }, @@ -138,6 +141,7 @@ private fun ImportFilesScreen( files: () -> List, filesToImportCount: () -> Int, currentSessionFilesCount: () -> Int, + transferAuthorEmail: GetSetCallbacks, transferMessage: GetSetCallbacks, selectedTransferType: GetSetCallbacks, transferOptionsCallbacks: TransferOptionsCallbacks, @@ -150,6 +154,7 @@ private fun ImportFilesScreen( val context = LocalContext.current var shouldShowInitialFilePick by rememberSaveable { mutableStateOf(shouldStartByPromptingUserForFiles) } var showTransferOption by rememberSaveable { mutableStateOf(null) } + val shouldShowEmailAddressesFields by remember { derivedStateOf { selectedTransferType.get() == TransferTypeUi.MAIL } } val importedFiles = files() val humanReadableSize = remember(importedFiles) { @@ -184,7 +189,15 @@ private fun ImportFilesScreen( ) }, topButton = { modifier -> - SendButton(filesToImportCount, currentSessionFilesCount, files, modifier, sendTransfer) + SendButton( + filesToImportCount = filesToImportCount, + currentSessionFilesCount = currentSessionFilesCount, + importedFiles = files, + shouldShowEmailAddressesFields = { shouldShowEmailAddressesFields }, + transferAuthorEmail = transferAuthorEmail, + modifier = modifier, + navigateToUploadProgress = sendTransfer, + ) }, content = { Column( @@ -200,7 +213,7 @@ private fun ImportFilesScreen( pickFiles = ::pickFiles, removeFileByUid = removeFileByUid, ) - ImportTextFields(transferMessage, selectedTransferType.get) + ImportTextFields(transferAuthorEmail, transferMessage, { shouldShowEmailAddressesFields }) ImportFilesTitle(Modifier.padding(vertical = Margin.Medium), titleRes = R.string.transferTypeTitle) TransferTypeButtons(selectedTransferType) ImportFilesTitle(Modifier.padding(vertical = Margin.Medium), titleRes = R.string.advancedSettingsTitle) @@ -216,9 +229,13 @@ private fun ImportFilesScreen( } @Composable -private fun ColumnScope.ImportTextFields(transferMessage: GetSetCallbacks, selectedTransferType: () -> TransferTypeUi) { +private fun ColumnScope.ImportTextFields( + transferAuthorEmail: GetSetCallbacks, + transferMessage: GetSetCallbacks, + shouldShowEmailAddressesFields: () -> Boolean, +) { - EmailAddressesTextFields(selectedTransferType) + EmailAddressesTextFields(transferAuthorEmail, shouldShowEmailAddressesFields) SwissTransferTextField( modifier = Modifier.fillMaxWidth(), @@ -230,16 +247,20 @@ private fun ColumnScope.ImportTextFields(transferMessage: GetSetCallbacks TransferTypeUi) { - - val shouldShowEmailAddressesFields by remember { derivedStateOf { selectedTransferType() == TransferTypeUi.MAIL } } +private fun ColumnScope.EmailAddressesTextFields( + transferAuthorEmail: GetSetCallbacks, + shouldShowEmailAddressesFields: () -> Boolean, +) { - AnimatedVisibility(visible = shouldShowEmailAddressesFields) { + AnimatedVisibility(visible = shouldShowEmailAddressesFields()) { Column { SwissTransferTextField( modifier = Modifier.fillMaxWidth(), label = stringResource(R.string.transferSenderAddressPlaceholder), - onValueChange = { /* TODO */ }, + initialValue = transferAuthorEmail.get(), + keyboardType = KeyboardType.Email, + errorMessage = { if (transferAuthorEmail.get().isEmail()) null else stringResource(R.string.invalidAddress) }, + onValueChange = transferAuthorEmail.set, ) Spacer(Modifier.height(Margin.Medium)) SwissTransferTextField( @@ -292,11 +313,16 @@ private fun SendButton( filesToImportCount: () -> Int, currentSessionFilesCount: () -> Int, importedFiles: () -> List, + shouldShowEmailAddressesFields: () -> Boolean, + transferAuthorEmail: GetSetCallbacks, modifier: Modifier, navigateToUploadProgress: () -> Unit, ) { val remainingFilesCount = filesToImportCount() val isImporting by remember(remainingFilesCount) { derivedStateOf { remainingFilesCount > 0 } } + val isSenderEmailCorrect by remember { + derivedStateOf { if (shouldShowEmailAddressesFields()) transferAuthorEmail.get().isEmail() else true } + } val total = currentSessionFilesCount() val importProgress = remember(remainingFilesCount, total) { 1 - (remainingFilesCount.toFloat() / total) } @@ -311,7 +337,7 @@ private fun SendButton( modifier = modifier, titleRes = R.string.transferSendButton, style = ButtonType.PRIMARY, - enabled = { importedFiles().isNotEmpty() && !isImporting }, + enabled = { importedFiles().isNotEmpty() && !isImporting && isSenderEmailCorrect }, progress = progress, onClick = navigateToUploadProgress, ) @@ -381,8 +407,9 @@ private fun Preview(@PreviewParameter(FileUiListPreviewParameter::class) files: files = { files }, filesToImportCount = { 0 }, currentSessionFilesCount = { 0 }, + transferAuthorEmail = GetSetCallbacks(get = { "" }, set = {}), transferMessage = GetSetCallbacks(get = { "" }, set = {}), - selectedTransferType = GetSetCallbacks(get = { TransferTypeUi.QR_CODE }, set = {}), + selectedTransferType = GetSetCallbacks(get = { TransferTypeUi.MAIL }, set = {}), transferOptionsCallbacks = transferOptionsCallbacks, removeFileByUid = {}, addFiles = {}, diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index 6c21bf7b7..2865bd15d 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -41,6 +41,7 @@ Mache deine erste Überweisung! Von: Zu übertragende Dateien + Ungültige Adresse Nachricht: Meine Dateien Auf Netzwerk warten… diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index b9e68d41f..279b0461c 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -41,6 +41,7 @@ Realice su primera transferencia De: Archivos para transferir + Dirección no válida Mensaje: Mis archivos Esperando la red… diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index 68ca6c07a..e20913da4 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -42,6 +42,7 @@ Fais ton premier transfert ! De : Fichiers à transférer + Adresse invalide Message : Mes fichiers En attente de réseau… diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml index f748d7def..ae8c95c47 100644 --- a/app/src/main/res/values-it/strings.xml +++ b/app/src/main/res/values-it/strings.xml @@ -41,6 +41,7 @@ Effettua il tuo primo trasferimento! Da: File da trasferire + Indirizzo non valido Messaggio: I miei file In attesa della rete… diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 17301b2f9..8acc4afdf 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -45,6 +45,7 @@ Make your first transfer! From: Files to transfer + Invalid address Message: My files Waiting for network…