Skip to content

Commit

Permalink
refactor(TransferPasswordAlertDialog): Move all advancedOptions view …
Browse files Browse the repository at this point in the history
…in a single composable
  • Loading branch information
FabianDevel authored and KevinBoulongne committed Nov 11, 2024
1 parent 3626c0c commit fea38ff
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,7 @@ class ImportFilesViewModel @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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import com.infomaniak.swisstransfer.ui.utils.PreviewAllWindows

@Composable
private fun TransferOptionBottomSheetScaffold(
isBottomSheetVisible: () -> Boolean,
onOptionClicked: (SettingOption) -> Unit,
closeBottomSheet: () -> Unit,
initialValue: SettingOption?,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -130,7 +121,6 @@ private fun ValidityPeriodOptionBottomSheetPreview() {
SwissTransferTheme {
Surface {
ValidityPeriodBottomSheet(
isBottomSheetVisible = { true },
onOptionClicked = {},
closeBottomSheet = {},
initialValue = ValidityPeriodOption.SEVEN,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ import com.infomaniak.swisstransfer.ui.utils.GetSetCallbacks
@Composable
fun PasswordOptionAlertDialog(
password: GetSetCallbacks<String>,
showPasswordOptionAlert: () -> Boolean,
closeAlertDialog: () -> Unit,
onConfirmation: (PasswordTransferOption) -> Unit,
isPasswordValid: () -> Boolean,
Expand All @@ -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)
}
}

Expand Down Expand Up @@ -124,7 +121,6 @@ fun Preview() {
Surface {
PasswordOptionAlertDialog(
password = GetSetCallbacks(get = { "pass" }, set = {}),
showPasswordOptionAlert = { true },
closeAlertDialog = {},
onConfirmation = {},
isPasswordValid = { false },
Expand Down

0 comments on commit fea38ff

Please sign in to comment.