From 022c4e35d16ccf5e12046ef815d44f61246dba05 Mon Sep 17 00:00:00 2001 From: Fabian Devel Date: Thu, 7 Nov 2024 09:50:02 +0100 Subject: [PATCH] refactor(TransferPasswordAlertDialog): Move all advancedOptions view in a single composable --- .../newtransfer/NewTransferViewModel.kt | 5 +- .../importfiles/ImportFilesScreen.kt | 67 ++++++++++--------- .../TransferOptionsBottomSheets.kt | 42 +++++------- .../components/PasswordOptionAlertDialog.kt | 22 +++--- 4 files changed, 62 insertions(+), 74 deletions(-) diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/NewTransferViewModel.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/NewTransferViewModel.kt index 9fde30c2d..28772f586 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/NewTransferViewModel.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/NewTransferViewModel.kt @@ -240,10 +240,7 @@ class NewTransferViewModel @Inject constructor( //region Password private var transferPassword by mutableStateOf("") - private val isPasswordValid by derivedStateOf { - val trimmedPassword = transferPassword.trim() - trimmedPassword.length in PASSWORD_MIN_LENGTH..PASSWORD_MAX_LENGTH - } + private val isPasswordValid by derivedStateOf { transferPassword.length in PASSWORD_MIN_LENGTH..PASSWORD_MAX_LENGTH } //endregion sealed class SendActionResult { 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 8847bf349..368af69b6 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 @@ -201,37 +201,7 @@ private fun ImportFilesScreen( ) } - ValidityPeriodBottomSheet( - isBottomSheetVisible = { showAdvancedOption == TransferAdvancedSettingType.VALIDITY_DURATION }, - onOptionClicked = { advancedOptionsCallbacks.onAdvancedOptionsValueSelected(it) }, - closeBottomSheet = ::closeAdvancedOption, - initialValue = advancedOptionsCallbacks.advancedOptionsStates()[0].settingState(), - ) - - DownloadLimitBottomSheet( - isBottomSheetVisible = { showAdvancedOption == TransferAdvancedSettingType.DOWNLOAD_NUMBER_LIMIT }, - onOptionClicked = { advancedOptionsCallbacks.onAdvancedOptionsValueSelected(it) }, - closeBottomSheet = ::closeAdvancedOption, - initialValue = advancedOptionsCallbacks.advancedOptionsStates()[1].settingState(), - ) - - PasswordOptionAlertDialog( - password = advancedOptionsCallbacks.password, - showPasswordOptionAlert = { showAdvancedOption == TransferAdvancedSettingType.PASSWORD }, - onConfirmation = { passwordOption -> - advancedOptionsCallbacks.onAdvancedOptionsValueSelected(passwordOption) - closeAdvancedOption() - }, - closeAlertDialog = ::closeAdvancedOption, - isPasswordValid = advancedOptionsCallbacks.isPasswordValid, - ) - - EmailLanguageBottomSheet( - isBottomSheetVisible = { showAdvancedOption == TransferAdvancedSettingType.LANGUAGE }, - onOptionClicked = { advancedOptionsCallbacks.onAdvancedOptionsValueSelected(it) }, - closeBottomSheet = ::closeAdvancedOption, - initialValue = advancedOptionsCallbacks.advancedOptionsStates()[3].settingState(), - ) + AdvancedOptions({ showAdvancedOption }, advancedOptionsCallbacks, ::closeAdvancedOption) UploadSourceChoiceBottomSheet( isVisible = { showUploadSourceChoiceBottomSheet }, @@ -276,6 +246,41 @@ private fun ColumnScope.EmailAddressesTextFields(selectedTransferType: () -> Tra } } +@Composable +private fun AdvancedOptions( + selectedTransferType: () -> TransferAdvancedSettingType?, + advancedOptionsCallbacks: AdvancedOptionsCallbacks, + closeAdvancedOption: () -> Unit, +) { + when (selectedTransferType()) { + TransferAdvancedSettingType.VALIDITY_DURATION -> ValidityPeriodBottomSheet( + onOptionClicked = { advancedOptionsCallbacks.onAdvancedOptionsValueSelected(it) }, + closeBottomSheet = closeAdvancedOption, + initialValue = advancedOptionsCallbacks.advancedOptionsStates()[0].settingState(), + ) + TransferAdvancedSettingType.DOWNLOAD_NUMBER_LIMIT -> DownloadLimitBottomSheet( + onOptionClicked = { advancedOptionsCallbacks.onAdvancedOptionsValueSelected(it) }, + closeBottomSheet = closeAdvancedOption, + initialValue = advancedOptionsCallbacks.advancedOptionsStates()[1].settingState(), + ) + TransferAdvancedSettingType.PASSWORD -> PasswordOptionAlertDialog( + password = advancedOptionsCallbacks.password, + onConfirmation = { passwordOption -> + advancedOptionsCallbacks.onAdvancedOptionsValueSelected(passwordOption) + closeAdvancedOption() + }, + closeAlertDialog = closeAdvancedOption, + isPasswordValid = advancedOptionsCallbacks.isPasswordValid, + ) + TransferAdvancedSettingType.LANGUAGE -> EmailLanguageBottomSheet( + onOptionClicked = { advancedOptionsCallbacks.onAdvancedOptionsValueSelected(it) }, + closeBottomSheet = closeAdvancedOption, + initialValue = advancedOptionsCallbacks.advancedOptionsStates()[3].settingState(), + ) + null -> Unit + } +} + @Composable private fun SendButton( filesToImportCount: () -> Int, diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/importfiles/TransferOptionsBottomSheets.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/importfiles/TransferOptionsBottomSheets.kt index 5eb7a3762..158cd60c6 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/importfiles/TransferOptionsBottomSheets.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/importfiles/TransferOptionsBottomSheets.kt @@ -36,7 +36,6 @@ import com.infomaniak.swisstransfer.ui.utils.PreviewAllWindows @Composable private fun TransferOptionBottomSheetScaffold( - isBottomSheetVisible: () -> Boolean, onOptionClicked: (SettingOption) -> Unit, closeBottomSheet: () -> Unit, initialValue: SettingOption?, @@ -53,35 +52,31 @@ private fun TransferOptionBottomSheetScaffold( else -> 0 } - if (isBottomSheetVisible()) { - SwissTransferBottomSheet( - onDismissRequest = closeBottomSheet, - titleRes = titleRes, - content = { - SingleSelectOptions( - items = optionEntries, - selectedItem = { selectedPosition }, - setSelectedItem = { position -> - val selectedValue = optionEntries[position] - selectedItem = selectedValue - onOptionClicked(selectedValue) - closeBottomSheet() - }, - ) - }, - ) - } + SwissTransferBottomSheet( + onDismissRequest = closeBottomSheet, + titleRes = titleRes, + content = { + SingleSelectOptions( + items = optionEntries, + selectedItem = { selectedPosition }, + setSelectedItem = { position -> + val selectedValue = optionEntries[position] + selectedItem = selectedValue + onOptionClicked(selectedValue) + closeBottomSheet() + }, + ) + }, + ) } @Composable fun ValidityPeriodBottomSheet( - isBottomSheetVisible: () -> Boolean, onOptionClicked: (ValidityPeriodOption) -> Unit, closeBottomSheet: () -> Unit, initialValue: SettingOption?, ) { TransferOptionBottomSheetScaffold( - isBottomSheetVisible = isBottomSheetVisible, closeBottomSheet = closeBottomSheet, initialValue = initialValue, titleRes = R.string.settingsOptionValidityPeriod, @@ -92,13 +87,11 @@ fun ValidityPeriodBottomSheet( @Composable fun DownloadLimitBottomSheet( - isBottomSheetVisible: () -> Boolean, onOptionClicked: (DownloadLimitOption) -> Unit, closeBottomSheet: () -> Unit, initialValue: SettingOption?, ) { TransferOptionBottomSheetScaffold( - isBottomSheetVisible = isBottomSheetVisible, closeBottomSheet = closeBottomSheet, initialValue = initialValue, titleRes = R.string.settingsOptionDownloadLimit, @@ -109,13 +102,11 @@ fun DownloadLimitBottomSheet( @Composable fun EmailLanguageBottomSheet( - isBottomSheetVisible: () -> Boolean, onOptionClicked: (EmailLanguageOption) -> Unit, closeBottomSheet: () -> Unit, initialValue: SettingOption?, ) { TransferOptionBottomSheetScaffold( - isBottomSheetVisible = isBottomSheetVisible, closeBottomSheet = closeBottomSheet, initialValue = initialValue, titleRes = R.string.settingsOptionEmailLanguage, @@ -130,7 +121,6 @@ private fun ValidityPeriodOptionBottomSheetPreview() { SwissTransferTheme { Surface { ValidityPeriodBottomSheet( - isBottomSheetVisible = { true }, onOptionClicked = {}, closeBottomSheet = {}, initialValue = ValidityPeriodOption.SEVEN, 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 942038d2e..798f47356 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 @@ -44,7 +44,6 @@ import com.infomaniak.swisstransfer.ui.utils.GetSetCallbacks @Composable fun PasswordOptionAlertDialog( password: GetSetCallbacks, - showPasswordOptionAlert: () -> Boolean, closeAlertDialog: () -> Unit, onConfirmation: (PasswordTransferOption) -> Unit, isPasswordValid: () -> Boolean, @@ -71,17 +70,15 @@ fun PasswordOptionAlertDialog( onConfirmation(passwordOption) } - if (showPasswordOptionAlert()) { - SwissTransferAlertDialog( - titleRes = R.string.settingsOptionPassword, - descriptionRes = R.string.settingsPasswordDescription, - onDismiss = ::onDismiss, - onConfirmation = ::onConfirmButtonClicked, - shouldEnableConfirmButton = { if (isPasswordActivated) isPasswordValid() else true }, - ) { - ActivatePasswordSwitch(isChecked = isPasswordActivated, onCheckedChange = { isPasswordActivated = it }) - AnimatedPasswordInput(isPasswordActivated, password, isPasswordValid) - } + SwissTransferAlertDialog( + titleRes = R.string.settingsOptionPassword, + descriptionRes = R.string.settingsPasswordDescription, + onDismiss = ::onDismiss, + onConfirmation = ::onConfirmButtonClicked, + shouldEnableConfirmButton = { if (isPasswordActivated) isPasswordValid() else true }, + ) { + ActivatePasswordSwitch(isChecked = isPasswordActivated, onCheckedChange = { isPasswordActivated = it }) + AnimatedPasswordInput(isPasswordActivated, password, isPasswordValid) } } @@ -124,7 +121,6 @@ fun Preview() { Surface { PasswordOptionAlertDialog( password = GetSetCallbacks(get = { "pass" }, set = {}), - showPasswordOptionAlert = { true }, closeAlertDialog = {}, onConfirmation = {}, isPasswordValid = { false },