From f8b8d2c043dc5aa5672e7d2b0514842138de68b6 Mon Sep 17 00:00:00 2001 From: Gibran Chevalley Date: Thu, 12 Dec 2024 15:39:17 +0100 Subject: [PATCH] feat: Add confirmation when trying to leave the new transfer flow Don't let them escape! --- .../ui/components/SwissTransferAlertDialog.kt | 59 +++++++++++-------- .../screen/newtransfer/NewTransferScreen.kt | 42 ++++++++++++- .../components/PasswordOptionAlertDialog.kt | 10 +++- app/src/main/res/values-de/strings.xml | 4 ++ app/src/main/res/values-es/strings.xml | 4 ++ app/src/main/res/values-fr/strings.xml | 4 ++ app/src/main/res/values-it/strings.xml | 4 ++ app/src/main/res/values/strings.xml | 4 ++ 8 files changed, 103 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/components/SwissTransferAlertDialog.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/components/SwissTransferAlertDialog.kt index 93a2c6437..b1a86427b 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/components/SwissTransferAlertDialog.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/components/SwissTransferAlertDialog.kt @@ -31,15 +31,35 @@ import com.infomaniak.swisstransfer.ui.theme.Margin import com.infomaniak.swisstransfer.ui.theme.SwissTransferTheme import com.infomaniak.core2.R as RCore2 +object SwissTransferAlertDialogDefaults { + @Composable + fun confirmButton(isEnabled: () -> Boolean = { true }, onClick: () -> Unit) { + SmallButton( + title = stringResource(R.string.buttonConfirm), + enabled = isEnabled, + onClick = onClick, + ) + } + + @Composable + fun cancelButton(onClick: () -> Unit) { + SmallButton( + style = ButtonType.TERTIARY, + title = stringResource(RCore2.string.buttonCancel), + onClick = onClick, + ) + } +} + @OptIn(ExperimentalMaterial3Api::class) @Composable fun SwissTransferAlertDialog( modifier: Modifier = Modifier, @StringRes titleRes: Int, @StringRes descriptionRes: Int, + positiveButton: @Composable () -> Unit, + negativeButton: @Composable () -> Unit, onDismiss: () -> Unit, - onConfirmation: () -> Unit, - isConfirmButtonEnabled: () -> Boolean = { true }, content: @Composable (ColumnScope.() -> Unit)? = null, ) { BasicAlertDialog( @@ -52,11 +72,10 @@ fun SwissTransferAlertDialog( BasicAlertDialogContent( modifier = modifier, titleRes = titleRes, + positiveButton = positiveButton, + negativeButton = negativeButton, descriptionRes = descriptionRes, additionalContent = content, - onDismiss = onDismiss, - onConfirmation = onConfirmation, - isConfirmButtonEnabled = isConfirmButtonEnabled, ) } } @@ -67,10 +86,9 @@ private fun BasicAlertDialogContent( modifier: Modifier, @StringRes titleRes: Int, @StringRes descriptionRes: Int, + positiveButton: @Composable () -> Unit, + negativeButton: @Composable () -> Unit, additionalContent: @Composable (ColumnScope.() -> Unit)? = null, - onDismiss: () -> Unit, - onConfirmation: () -> Unit, - isConfirmButtonEnabled: () -> Boolean = { true }, ) { Column(modifier.padding(Margin.Large)) { TitleAndDescription(titleRes, descriptionRes) @@ -79,7 +97,7 @@ private fun BasicAlertDialogContent( it() Spacer(Modifier.height(Margin.Mini)) } - ActionButtons(onDismiss, onConfirmation, isConfirmButtonEnabled) + ActionButtons(positiveButton, negativeButton) } } @@ -99,23 +117,17 @@ private fun TitleAndDescription(titleRes: Int, descriptionRes: Int) { } @Composable -private fun ActionButtons(onDismissRequest: () -> Unit, onConfirmation: () -> Unit, isEnabled: () -> Boolean) { +private fun ActionButtons( + positiveButton: @Composable () -> Unit, + negativeButton: @Composable () -> Unit, +) { Row( modifier = Modifier.fillMaxWidth(), - horizontalArrangement = Arrangement.End, + horizontalArrangement = Arrangement.spacedBy(Margin.Micro, Alignment.End), verticalAlignment = Alignment.CenterVertically, ) { - SmallButton( - style = ButtonType.TERTIARY, - title = stringResource(RCore2.string.buttonCancel), - onClick = onDismissRequest, - ) - Spacer(Modifier.width(Margin.Micro)) - SmallButton( - title = stringResource(R.string.buttonConfirm), - onClick = onConfirmation, - enabled = isEnabled - ) + negativeButton() + positiveButton() } } @@ -127,8 +139,9 @@ private fun PreviewAlertDialog() { SwissTransferAlertDialog( titleRes = R.string.settingsOptionPassword, descriptionRes = R.string.settingsPasswordDescription, + positiveButton = { SwissTransferAlertDialogDefaults.confirmButton { } }, + negativeButton = { SwissTransferAlertDialogDefaults.cancelButton { } }, onDismiss = {}, - onConfirmation = {}, ) } } diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/NewTransferScreen.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/NewTransferScreen.kt index e12920249..fddeca0e0 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/NewTransferScreen.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/NewTransferScreen.kt @@ -18,20 +18,56 @@ package com.infomaniak.swisstransfer.ui.screen.newtransfer import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.saveable.rememberSaveable +import androidx.compose.runtime.setValue +import androidx.compose.ui.res.stringResource import androidx.navigation.compose.rememberNavController +import com.infomaniak.swisstransfer.R +import com.infomaniak.swisstransfer.ui.components.ButtonType +import com.infomaniak.swisstransfer.ui.components.SmallButton +import com.infomaniak.swisstransfer.ui.components.SwissTransferAlertDialog import com.infomaniak.swisstransfer.ui.theme.SwissTransferTheme import com.infomaniak.swisstransfer.ui.utils.PreviewAllWindows @Composable fun NewTransferScreen(closeActivity: () -> Unit) { val navController = rememberNavController() - NewTransferNavHost(navController, closeActivity) + var displayConfirmationDialog by rememberSaveable { mutableStateOf(false) } + + NewTransferNavHost(navController, closeActivity = { displayConfirmationDialog = true }) + + if (displayConfirmationDialog) ConfirmLeavingDialog(onLeave = closeActivity, onCancel = { displayConfirmationDialog = false }) +} + +@Composable +fun ConfirmLeavingDialog(onLeave: () -> Unit, onCancel: () -> Unit) { + SwissTransferAlertDialog( + titleRes = R.string.newTransferConfirmLeavingDialogTitle, + descriptionRes = R.string.newTransferLeavingDialogDescription, + positiveButton = { + SmallButton( + title = stringResource(R.string.newTransferLeavingDialogPositiveButton), + style = ButtonType.ERROR, + onClick = onLeave, + ) + }, + negativeButton = { + SmallButton( + title = stringResource(R.string.newTransferLeavingDialogNegativeButton), + style = ButtonType.TERTIARY, + onClick = onCancel, + ) + }, + onDismiss = onCancel, + ) } @PreviewAllWindows @Composable -private fun NewTransferPreview() { +private fun Preview() { SwissTransferTheme { - NewTransferScreen {} + ConfirmLeavingDialog({}, {}) } } diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/importfiles/components/PasswordOptionAlertDialog.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/importfiles/components/PasswordOptionAlertDialog.kt index 007742331..d8a8f33bd 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/importfiles/components/PasswordOptionAlertDialog.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/importfiles/components/PasswordOptionAlertDialog.kt @@ -33,6 +33,7 @@ import androidx.compose.ui.tooling.preview.Preview import com.infomaniak.swisstransfer.R import com.infomaniak.swisstransfer.ui.MatomoSwissTransfer.toFloat import com.infomaniak.swisstransfer.ui.components.SwissTransferAlertDialog +import com.infomaniak.swisstransfer.ui.components.SwissTransferAlertDialogDefaults import com.infomaniak.swisstransfer.ui.components.SwissTransferTextField import com.infomaniak.swisstransfer.ui.screen.newtransfer.importfiles.PasswordTransferOption import com.infomaniak.swisstransfer.ui.screen.newtransfer.upload.components.WeightOneSpacer @@ -72,9 +73,14 @@ fun PasswordOptionAlertDialog( SwissTransferAlertDialog( titleRes = R.string.settingsOptionPassword, descriptionRes = R.string.settingsPasswordDescription, + positiveButton = { + SwissTransferAlertDialogDefaults.confirmButton( + isEnabled = { !isPasswordActivated || isPasswordValid() }, + onClick = ::onConfirmButtonClicked, + ) + }, + negativeButton = { SwissTransferAlertDialogDefaults.cancelButton(onClick = ::onDismiss) }, onDismiss = ::onDismiss, - onConfirmation = ::onConfirmButtonClicked, - isConfirmButtonEnabled = { !isPasswordActivated || isPasswordValid() }, ) { ActivatePasswordSwitch(isChecked = isPasswordActivated, onCheckedChange = { isPasswordActivated = it }) Spacer(Modifier.height(Margin.Medium)) diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index 24c4e7c98..5a382c78f 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -46,6 +46,10 @@ Nachricht: Meine Dateien Auf Netzwerk warten… + Ups, willst du wirklich abbrechen? + Dein Transfer ist fast fertig. Bist du sicher, dass du alles rückgängig machen willst? + Nein, weiter + Ja, abbrechen Fügt bis zu 50 GB an Dateien hinzu Keine Datei, keine Übertragung! Wählt einen Parameter zur Anzeige aus diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index 029a1aa48..b726d2a5a 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -46,6 +46,10 @@ Mensaje: Mis archivos Esperando la red… + Ups, ¿realmente quieres cancelar? + Tu traslado está casi listo. ¿Estás seguro de que quieres cancelarlo? + No, continúe + Sí, cancelar Añade hasta 50 GB de archivos Si no hay archivo, no hay transferencia. Selecciona un parámetro para su visualización diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index 9b08f9bf7..508a8de0d 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -47,6 +47,10 @@ Message : Mes fichiers En attente de réseau… + Oups, tu veux vraiment annuler ? + Ton transfert est presque prêt. Es-tu sûr de vouloir tout annuler ? + Non, continuer + Oui, annuler Ajoute jusqu’à 50 Go de fichiers Pas de fichier, pas de transfert ! Sélectionne un paramètre pour l’afficher diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml index cb5b20a57..07ae7ba70 100644 --- a/app/src/main/res/values-it/strings.xml +++ b/app/src/main/res/values-it/strings.xml @@ -46,6 +46,10 @@ Messaggio: I miei file In attesa della rete… + Ops, vuoi davvero cancellarti? + Il trasferimento è quasi pronto. Sei sicuro di volerlo annullare? + No, continua + Sì, annulla Aggiungere fino a 50 GB di file Nessun file, nessun trasferimento! Seleziona un parametro da visualizzare diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1d8aa9e51..4b13e1b27 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -50,6 +50,10 @@ Message: My files Waiting for network… + Oops, do you really want to cancel? + Your transfer is almost ready. Are you sure you want to cancel it? + No, continue + Yes, cancel Add up to 50 GB of files No file, no transfer! Select a parameter to display it