Skip to content

Commit

Permalink
feat: Make sure Transfer author email is mandatory and correctly form…
Browse files Browse the repository at this point in the history
…atted
  • Loading branch information
KevinBoulongne committed Dec 4, 2024
1 parent 29c536c commit 817c793
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand Down Expand Up @@ -104,6 +106,7 @@ fun ImportFilesScreen(
files = { files },
filesToImportCount = { filesToImportCount },
currentSessionFilesCount = { currentSessionFilesCount },
transferAuthorEmail = importFilesViewModel.transferAuthorEmail,
transferMessage = importFilesViewModel.transferMessage,
selectedTransferType = GetSetCallbacks(
get = { selectedTransferType },
Expand Down Expand Up @@ -138,6 +141,7 @@ private fun ImportFilesScreen(
files: () -> List<FileUi>,
filesToImportCount: () -> Int,
currentSessionFilesCount: () -> Int,
transferAuthorEmail: GetSetCallbacks<String>,
transferMessage: GetSetCallbacks<String>,
selectedTransferType: GetSetCallbacks<TransferTypeUi>,
transferOptionsCallbacks: TransferOptionsCallbacks,
Expand All @@ -150,6 +154,7 @@ private fun ImportFilesScreen(
val context = LocalContext.current
var shouldShowInitialFilePick by rememberSaveable { mutableStateOf(shouldStartByPromptingUserForFiles) }
var showTransferOption by rememberSaveable { mutableStateOf<TransferOptionType?>(null) }
val shouldShowEmailAddressesFields by remember { derivedStateOf { selectedTransferType.get() == TransferTypeUi.MAIL } }

val importedFiles = files()
val humanReadableSize = remember(importedFiles) {
Expand Down Expand Up @@ -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(
Expand All @@ -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)
Expand All @@ -216,9 +229,13 @@ private fun ImportFilesScreen(
}

@Composable
private fun ColumnScope.ImportTextFields(transferMessage: GetSetCallbacks<String>, selectedTransferType: () -> TransferTypeUi) {
private fun ColumnScope.ImportTextFields(
transferAuthorEmail: GetSetCallbacks<String>,
transferMessage: GetSetCallbacks<String>,
shouldShowEmailAddressesFields: () -> Boolean,
) {

EmailAddressesTextFields(selectedTransferType)
EmailAddressesTextFields(transferAuthorEmail, shouldShowEmailAddressesFields)

SwissTransferTextField(
modifier = Modifier.fillMaxWidth(),
Expand All @@ -230,16 +247,20 @@ private fun ColumnScope.ImportTextFields(transferMessage: GetSetCallbacks<String
}

@Composable
private fun ColumnScope.EmailAddressesTextFields(selectedTransferType: () -> TransferTypeUi) {

val shouldShowEmailAddressesFields by remember { derivedStateOf { selectedTransferType() == TransferTypeUi.MAIL } }
private fun ColumnScope.EmailAddressesTextFields(
transferAuthorEmail: GetSetCallbacks<String>,
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(
Expand Down Expand Up @@ -292,11 +313,16 @@ private fun SendButton(
filesToImportCount: () -> Int,
currentSessionFilesCount: () -> Int,
importedFiles: () -> List<FileUi>,
shouldShowEmailAddressesFields: () -> Boolean,
transferAuthorEmail: GetSetCallbacks<String>,
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) }
Expand All @@ -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,
)
Expand Down Expand Up @@ -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 = {},
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<string name="firstTransferDescription">Mache deine erste Überweisung!</string>
<string name="fromHeader">Von:</string>
<string name="importFilesScreenTitle">Zu übertragende Dateien</string>
<string name="invalidAddress">Ungültige Adresse</string>
<string name="messageHeader">Nachricht:</string>
<string name="myFilesTitle">Meine Dateien</string>
<string name="networkUnavailable">Auf Netzwerk warten…</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<string name="firstTransferDescription">Realice su primera transferencia</string>
<string name="fromHeader">De:</string>
<string name="importFilesScreenTitle">Archivos para transferir</string>
<string name="invalidAddress">Dirección no válida</string>
<string name="messageHeader">Mensaje:</string>
<string name="myFilesTitle">Mis archivos</string>
<string name="networkUnavailable">Esperando la red…</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<string name="firstTransferDescription">Fais ton premier transfert !</string>
<string name="fromHeader">De :</string>
<string name="importFilesScreenTitle">Fichiers à transférer</string>
<string name="invalidAddress">Adresse invalide</string>
<string name="messageHeader">Message :</string>
<string name="myFilesTitle">Mes fichiers</string>
<string name="networkUnavailable">En attente de réseau…</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-it/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<string name="firstTransferDescription">Effettua il tuo primo trasferimento!</string>
<string name="fromHeader">Da:</string>
<string name="importFilesScreenTitle">File da trasferire</string>
<string name="invalidAddress">Indirizzo non valido</string>
<string name="messageHeader">Messaggio:</string>
<string name="myFilesTitle">I miei file</string>
<string name="networkUnavailable">In attesa della rete…</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<string name="firstTransferDescription">Make your first transfer!</string>
<string name="fromHeader">From:</string>
<string name="importFilesScreenTitle">Files to transfer</string>
<string name="invalidAddress">Invalid address</string>
<string name="messageHeader">Message:</string>
<string name="myFilesTitle">My files</string>
<string name="networkUnavailable">Waiting for network…</string>
Expand Down

0 comments on commit 817c793

Please sign in to comment.