From 6a801f911a753f6ca456903f44b71140d2707ec0 Mon Sep 17 00:00:00 2001 From: Fabian Devel Date: Wed, 6 Nov 2024 11:38:15 +0100 Subject: [PATCH] refactor(AdvancedOptionsState): Remove initialValue and directly use states instead --- .../importfiles/ImportFilesScreen.kt | 74 ++++++++++++------- .../TransferOptionsBottomSheets.kt | 39 ++++++---- .../components/TransferAdvancedSettings.kt | 36 ++++++--- 3 files changed, 97 insertions(+), 52 deletions(-) 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 fc70d33a8..b076be1b3 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 @@ -36,9 +36,6 @@ import androidx.compose.ui.tooling.preview.PreviewParameter import androidx.hilt.navigation.compose.hiltViewModel import androidx.lifecycle.compose.collectAsStateWithLifecycle import com.infomaniak.multiplatform_swisstransfer.common.interfaces.ui.FileUi -import com.infomaniak.multiplatform_swisstransfer.common.models.DownloadLimit -import com.infomaniak.multiplatform_swisstransfer.common.models.EmailLanguage -import com.infomaniak.multiplatform_swisstransfer.common.models.ValidityPeriod import com.infomaniak.swisstransfer.R import com.infomaniak.swisstransfer.ui.components.* import com.infomaniak.swisstransfer.ui.previewparameter.FileUiListPreviewParameter @@ -87,13 +84,26 @@ fun ImportFilesScreen( newTransferViewModel.initTransferAdvancedOptionsValues(safeAppSettings) val advancedOptionsCallbacks = AdvancedOptionsCallbacks( - initialValues = SettingsViewModel.AppSettingsData( - validityPeriod = safeAppSettings.validityPeriod, - downloadLimit = safeAppSettings.downloadLimit, - passwordOption = PasswordTransferOption.NONE, - emailLanguage = safeAppSettings.emailLanguage, - ), - advancedOptionsStates = { listOf(validityPeriodState, downloadLimitState, passwordOptionState, emailLanguageState) }, + advancedOptionsStates = { + listOf( + AdvancedOptionsState( + advancedSettingType = TransferAdvancedSettingType.VALIDITY_DURATION, + settingState = { validityPeriodState }, + ), + AdvancedOptionsState( + advancedSettingType = TransferAdvancedSettingType.DOWNLOAD_NUMBER_LIMIT, + settingState = { downloadLimitState }, + ), + AdvancedOptionsState( + advancedSettingType = TransferAdvancedSettingType.PASSWORD, + settingState = { passwordOptionState }, + ), + AdvancedOptionsState( + advancedSettingType = TransferAdvancedSettingType.LANGUAGE, + settingState = { emailLanguageState }, + ), + ) + }, onAdvancedOptionsValueSelected = ::onAdvancedOptionsValueSelected, ) @@ -190,7 +200,7 @@ private fun ImportFilesScreen( TransferTypeButtons(selectedTransferType) ImportFilesTitle(Modifier.padding(vertical = Margin.Medium), titleRes = R.string.advancedSettingsTitle) TransferAdvancedSettings( - states = advancedOptionsCallbacks.advancedOptionsStates, + advancedSettingsItemsStates = advancedOptionsCallbacks.advancedOptionsStates, onClick = ::onAdvancedOptionClicked, ) } @@ -199,28 +209,28 @@ private fun ImportFilesScreen( isBottomSheetVisible = { showValidityPeriodBottomSheet }, onOptionClicked = { advancedOptionsCallbacks.onAdvancedOptionsValueSelected(it) }, closeBottomSheet = { showValidityPeriodBottomSheet = false }, - initialPosition = advancedOptionsCallbacks.initialValues.validityPeriod.ordinal, + initialValue = advancedOptionsCallbacks.advancedOptionsStates()[0].settingState(), ) DownloadLimitBottomSheet( isBottomSheetVisible = { showDownloadLimitBottomSheet }, onOptionClicked = { advancedOptionsCallbacks.onAdvancedOptionsValueSelected(it) }, closeBottomSheet = { showDownloadLimitBottomSheet = false }, - initialPosition = advancedOptionsCallbacks.initialValues.downloadLimit.ordinal, + initialValue = advancedOptionsCallbacks.advancedOptionsStates()[1].settingState(), ) PasswordOptionBottomSheet( isBottomSheetVisible = { showPasswordOptionBottomSheet }, onOptionClicked = { advancedOptionsCallbacks.onAdvancedOptionsValueSelected(it) }, closeBottomSheet = { showPasswordOptionBottomSheet = false }, - initialPosition = advancedOptionsCallbacks.initialValues.passwordOption.ordinal, + initialValue = advancedOptionsCallbacks.advancedOptionsStates()[2].settingState(), ) EmailLanguageBottomSheet( isBottomSheetVisible = { showEmailLanguageBottomSheet }, onOptionClicked = { advancedOptionsCallbacks.onAdvancedOptionsValueSelected(it) }, closeBottomSheet = { showEmailLanguageBottomSheet = false }, - initialPosition = advancedOptionsCallbacks.initialValues.emailLanguage.ordinal, + initialValue = advancedOptionsCallbacks.advancedOptionsStates()[3].settingState(), ) UploadSourceChoiceBottomSheet( @@ -306,11 +316,15 @@ private fun ImportFilesTitle(modifier: Modifier = Modifier, @StringRes titleRes: } data class AdvancedOptionsCallbacks( - val initialValues: SettingsViewModel.AppSettingsData, - val advancedOptionsStates: () -> List, + val advancedOptionsStates: () -> List, val onAdvancedOptionsValueSelected: (SettingOption) -> Unit, ) +data class AdvancedOptionsState( + val advancedSettingType: TransferAdvancedSettingType, + val settingState: () -> SettingOption?, +) + enum class PasswordTransferOption( override val title: @Composable () -> String, override val imageVector: ImageVector? = null, @@ -330,18 +344,24 @@ private fun ImportFilesScreenPreview(@PreviewParameter(FileUiListPreviewParamete currentSessionFilesCount = { 0 }, selectedTransferType = GetSetCallbacks(get = { TransferType.QR_CODE }, set = {}), advancedOptionsCallbacks = AdvancedOptionsCallbacks( - initialValues = SettingsViewModel.AppSettingsData( - validityPeriod = ValidityPeriod.THIRTY, - downloadLimit = DownloadLimit.ONE, - passwordOption = PasswordTransferOption.NONE, - emailLanguage = EmailLanguage.FRENCH, - ), advancedOptionsStates = { listOf( - ValidityPeriodOption.THIRTY, - DownloadLimitOption.TWO_HUNDRED_FIFTY, - PasswordTransferOption.NONE, - EmailLanguageOption.FRENCH, + AdvancedOptionsState( + advancedSettingType = TransferAdvancedSettingType.VALIDITY_DURATION, + settingState = { ValidityPeriodOption.THIRTY }, + ), + AdvancedOptionsState( + advancedSettingType = TransferAdvancedSettingType.DOWNLOAD_NUMBER_LIMIT, + settingState = { DownloadLimitOption.TWO_HUNDRED_FIFTY }, + ), + AdvancedOptionsState( + advancedSettingType = TransferAdvancedSettingType.PASSWORD, + settingState = { PasswordTransferOption.NONE }, + ), + AdvancedOptionsState( + advancedSettingType = TransferAdvancedSettingType.LANGUAGE, + settingState = { EmailLanguageOption.FRENCH }, + ), ) }, onAdvancedOptionsValueSelected = {}, 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 c51d90019..c87c85628 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 @@ -21,7 +21,7 @@ import androidx.annotation.StringRes import androidx.compose.material3.Surface import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableIntStateOf +import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue import com.infomaniak.swisstransfer.R @@ -39,11 +39,19 @@ private fun TransferOptionBottomSheetScaffold( isBottomSheetVisible: () -> Boolean, onOptionClicked: (SettingOption) -> Unit, closeBottomSheet: () -> Unit, - initialPosition: Int, + initialValue: SettingOption?, optionEntries: List, @StringRes titleRes: Int, ) { - var selectedItem by rememberSaveable { mutableIntStateOf(initialPosition) } + var selectedItem by rememberSaveable { mutableStateOf(initialValue) } + + val selectedPosition = when (initialValue) { + is ValidityPeriodOption -> initialValue.ordinal + is DownloadLimitOption -> initialValue.ordinal + is PasswordTransferOption -> initialValue.ordinal + is EmailLanguageOption -> initialValue.ordinal + else -> 0 + } if (isBottomSheetVisible()) { SwissTransferBottomSheet( @@ -52,10 +60,11 @@ private fun TransferOptionBottomSheetScaffold( content = { SingleSelectOptions( items = optionEntries, - selectedItem = { selectedItem }, + selectedItem = { selectedPosition }, setSelectedItem = { position -> - selectedItem = position - onOptionClicked(optionEntries[position]) + val selectedValue = optionEntries[position] + selectedItem = selectedValue + onOptionClicked(selectedValue) closeBottomSheet() }, ) @@ -69,12 +78,12 @@ fun ValidityPeriodBottomSheet( isBottomSheetVisible: () -> Boolean, onOptionClicked: (ValidityPeriodOption) -> Unit, closeBottomSheet: () -> Unit, - initialPosition: Int, + initialValue: SettingOption?, ) { TransferOptionBottomSheetScaffold( isBottomSheetVisible = isBottomSheetVisible, closeBottomSheet = closeBottomSheet, - initialPosition = initialPosition, + initialValue = initialValue, titleRes = R.string.settingsOptionValidityPeriod, optionEntries = ValidityPeriodOption.entries, onOptionClicked = { onOptionClicked(it as ValidityPeriodOption) }, @@ -86,12 +95,12 @@ fun DownloadLimitBottomSheet( isBottomSheetVisible: () -> Boolean, onOptionClicked: (DownloadLimitOption) -> Unit, closeBottomSheet: () -> Unit, - initialPosition: Int, + initialValue: SettingOption?, ) { TransferOptionBottomSheetScaffold( isBottomSheetVisible = isBottomSheetVisible, closeBottomSheet = closeBottomSheet, - initialPosition = initialPosition, + initialValue = initialValue, titleRes = R.string.settingsOptionDownloadLimit, optionEntries = DownloadLimitOption.entries, onOptionClicked = { onOptionClicked(it as DownloadLimitOption) }, @@ -103,12 +112,12 @@ fun PasswordOptionBottomSheet( isBottomSheetVisible: () -> Boolean, onOptionClicked: (PasswordTransferOption) -> Unit, closeBottomSheet: () -> Unit, - initialPosition: Int, + initialValue: SettingOption?, ) { TransferOptionBottomSheetScaffold( isBottomSheetVisible = isBottomSheetVisible, closeBottomSheet = closeBottomSheet, - initialPosition = initialPosition, + initialValue = initialValue, titleRes = R.string.settingsOptionPassword, optionEntries = PasswordTransferOption.entries, onOptionClicked = { onOptionClicked(it as PasswordTransferOption) }, @@ -120,12 +129,12 @@ fun EmailLanguageBottomSheet( isBottomSheetVisible: () -> Boolean, onOptionClicked: (EmailLanguageOption) -> Unit, closeBottomSheet: () -> Unit, - initialPosition: Int, + initialValue: SettingOption?, ) { TransferOptionBottomSheetScaffold( isBottomSheetVisible = isBottomSheetVisible, closeBottomSheet = closeBottomSheet, - initialPosition = initialPosition, + initialValue = initialValue, titleRes = R.string.settingsOptionEmailLanguage, optionEntries = EmailLanguageOption.entries, onOptionClicked = { onOptionClicked(it as EmailLanguageOption) }, @@ -141,7 +150,7 @@ private fun ValidityPeriodOptionBottomSheetPreview() { isBottomSheetVisible = { true }, onOptionClicked = {}, closeBottomSheet = {}, - initialPosition = ValidityPeriodOption.SEVEN.ordinal, + initialValue = ValidityPeriodOption.SEVEN, ) } } diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/importfiles/components/TransferAdvancedSettings.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/importfiles/components/TransferAdvancedSettings.kt index 71313349e..6a0d4df0a 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/importfiles/components/TransferAdvancedSettings.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/importfiles/components/TransferAdvancedSettings.kt @@ -38,7 +38,7 @@ import com.infomaniak.swisstransfer.ui.images.icons.SpeechBubble import com.infomaniak.swisstransfer.ui.screen.main.settings.DownloadLimitOption import com.infomaniak.swisstransfer.ui.screen.main.settings.EmailLanguageOption import com.infomaniak.swisstransfer.ui.screen.main.settings.ValidityPeriodOption -import com.infomaniak.swisstransfer.ui.screen.main.settings.components.SettingOption +import com.infomaniak.swisstransfer.ui.screen.newtransfer.importfiles.AdvancedOptionsState import com.infomaniak.swisstransfer.ui.screen.newtransfer.importfiles.PasswordTransferOption import com.infomaniak.swisstransfer.ui.theme.Margin import com.infomaniak.swisstransfer.ui.theme.SwissTransferTheme @@ -46,13 +46,13 @@ import com.infomaniak.swisstransfer.ui.theme.SwissTransferTheme @Composable fun TransferAdvancedSettings( modifier: Modifier = Modifier, - states: () -> List, + advancedSettingsItemsStates: () -> List, onClick: (TransferAdvancedSettingType) -> Unit, ) { SwissTransferCard(modifier = modifier) { - TransferAdvancedSettingType.entries.forEach { settingType -> - val title by remember { derivedStateOf { states()[settingType.ordinal] } } - TransferAdvancedSetting(settingType, { title }, { onClick(settingType) }) + advancedSettingsItemsStates().forEach { + val title by remember { derivedStateOf { it.settingState } } + TransferAdvancedSetting(it.advancedSettingType, title, onClick = { onClick(it.advancedSettingType) }) } } } @@ -71,13 +71,29 @@ private fun TransferTypeButtonsPreview() { SwissTransferTheme { Surface { val selectedOptionValues = listOf( - ValidityPeriodOption.THIRTY, - DownloadLimitOption.TWO_HUNDRED_FIFTY, - PasswordTransferOption.NONE, - EmailLanguageOption.FRENCH, + AdvancedOptionsState( + advancedSettingType = TransferAdvancedSettingType.VALIDITY_DURATION, + settingState = { ValidityPeriodOption.THIRTY }, + ), + AdvancedOptionsState( + advancedSettingType = TransferAdvancedSettingType.DOWNLOAD_NUMBER_LIMIT, + settingState = { DownloadLimitOption.TWO_HUNDRED_FIFTY }, + ), + AdvancedOptionsState( + advancedSettingType = TransferAdvancedSettingType.PASSWORD, + settingState = { PasswordTransferOption.NONE }, + ), + AdvancedOptionsState( + advancedSettingType = TransferAdvancedSettingType.LANGUAGE, + settingState = { EmailLanguageOption.FRENCH }, + ), ) - TransferAdvancedSettings(modifier = Modifier.padding(Margin.Medium), states = { selectedOptionValues }, onClick = {}) + TransferAdvancedSettings( + modifier = Modifier.padding(Margin.Medium), + advancedSettingsItemsStates = { selectedOptionValues }, + onClick = {}, + ) } } }