Skip to content

Commit

Permalink
refactor(AdvancedOptionsState): Remove initialValue and directly use …
Browse files Browse the repository at this point in the history
…states instead
  • Loading branch information
FabianDevel committed Nov 6, 2024
1 parent 5513e9a commit 6a801f9
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
)

Expand Down Expand Up @@ -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,
)
}
Expand All @@ -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(
Expand Down Expand Up @@ -306,11 +316,15 @@ private fun ImportFilesTitle(modifier: Modifier = Modifier, @StringRes titleRes:
}

data class AdvancedOptionsCallbacks(
val initialValues: SettingsViewModel.AppSettingsData,
val advancedOptionsStates: () -> List<SettingOption?>,
val advancedOptionsStates: () -> List<AdvancedOptionsState>,
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,
Expand All @@ -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 = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -39,11 +39,19 @@ private fun TransferOptionBottomSheetScaffold(
isBottomSheetVisible: () -> Boolean,
onOptionClicked: (SettingOption) -> Unit,
closeBottomSheet: () -> Unit,
initialPosition: Int,
initialValue: SettingOption?,
optionEntries: List<SettingOption>,
@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(
Expand All @@ -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()
},
)
Expand All @@ -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) },
Expand All @@ -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) },
Expand All @@ -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) },
Expand All @@ -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) },
Expand All @@ -141,7 +150,7 @@ private fun ValidityPeriodOptionBottomSheetPreview() {
isBottomSheetVisible = { true },
onOptionClicked = {},
closeBottomSheet = {},
initialPosition = ValidityPeriodOption.SEVEN.ordinal,
initialValue = ValidityPeriodOption.SEVEN,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,21 @@ 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

@Composable
fun TransferAdvancedSettings(
modifier: Modifier = Modifier,
states: () -> List<SettingOption?>,
advancedSettingsItemsStates: () -> List<AdvancedOptionsState>,
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) })
}
}
}
Expand All @@ -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 = {},
)
}
}
}

0 comments on commit 6a801f9

Please sign in to comment.