Skip to content

Commit

Permalink
chore(AdvancedOptionsState): Clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianDevel committed Nov 6, 2024
1 parent b5ad405 commit 5513e9a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ import com.infomaniak.multiplatform_swisstransfer.common.models.Theme
import com.infomaniak.multiplatform_swisstransfer.common.models.ValidityPeriod
import com.infomaniak.multiplatform_swisstransfer.managers.AppSettingsManager
import com.infomaniak.swisstransfer.di.IoDispatcher
import com.infomaniak.swisstransfer.ui.screen.main.settings.DownloadLimitOption.Companion.toAdvancedOption
import com.infomaniak.swisstransfer.ui.screen.main.settings.EmailLanguageOption.Companion.toAdvancedOption
import com.infomaniak.swisstransfer.ui.screen.main.settings.ValidityPeriodOption.Companion.toAdvancedOption
import com.infomaniak.swisstransfer.ui.screen.main.settings.components.SettingOption
import com.infomaniak.swisstransfer.ui.screen.newtransfer.importfiles.PasswordTransferOption
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.CoroutineDispatcher
Expand Down Expand Up @@ -64,12 +60,5 @@ class SettingsViewModel @Inject constructor(
val downloadLimit: DownloadLimit,
val passwordOption: PasswordTransferOption,
val emailLanguage: EmailLanguage,
) {
fun toAdvancedOptionsList() = listOf<SettingOption>(
validityPeriod.toAdvancedOption(),
downloadLimit.toAdvancedOption(),
passwordOption,
emailLanguage.toAdvancedOption(),
)
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,16 @@ class NewTransferViewModel @Inject constructor(

//region Transfer Advanced Options
private val _selectedValidityPeriodOption = MutableStateFlow<ValidityPeriodOption?>(null)
val selectedValidityPeriodOption: StateFlow<ValidityPeriodOption?> = _selectedValidityPeriodOption
val selectedValidityPeriodOption: StateFlow<ValidityPeriodOption?> = _selectedValidityPeriodOption.asStateFlow()

private val _selectedDownloadLimitOption = MutableStateFlow<DownloadLimitOption?>(null)
val selectedDownloadLimitOption: StateFlow<DownloadLimitOption?> = _selectedDownloadLimitOption
val selectedDownloadLimitOption: StateFlow<DownloadLimitOption?> = _selectedDownloadLimitOption.asStateFlow()

private val _selectedPasswordOption = MutableStateFlow(PasswordTransferOption.NONE)
val selectedPasswordOption: StateFlow<PasswordTransferOption?> = _selectedPasswordOption
val selectedPasswordOption: StateFlow<PasswordTransferOption?> = _selectedPasswordOption.asStateFlow()

private val _selectedLanguageOption = MutableStateFlow<EmailLanguageOption?>(null)
val selectedLanguageOption: StateFlow<EmailLanguageOption?> = _selectedLanguageOption
val selectedLanguageOption: StateFlow<EmailLanguageOption?> = _selectedLanguageOption.asStateFlow()

fun selectTransferValidityPeriod(validityPeriodOption: ValidityPeriodOption) {
_selectedValidityPeriodOption.value = validityPeriodOption
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,29 @@ fun ImportFilesScreen(
val emailLanguageState by newTransferViewModel.selectedLanguageOption.collectAsStateWithLifecycle()
val appSettings by settingsViewModel.appSettingsFlow.collectAsStateWithLifecycle(null)

fun onAdvancedOptionsValueSelected(option: SettingOption) {
when (option) {
is ValidityPeriodOption -> newTransferViewModel.selectTransferValidityPeriod(option)
is DownloadLimitOption -> newTransferViewModel.selectTransferDownloadLimit(option)
is PasswordTransferOption -> newTransferViewModel.selectTransferPasswordOption(option)
is EmailLanguageOption -> newTransferViewModel.selectTransferLanguage(option)
}
}

appSettings?.let { safeAppSettings ->
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) },
onAdvancedOptionsValueSelected = ::onAdvancedOptionsValueSelected,
)

ImportFilesScreen(
files = { files },
filesToImportCount = { filesToImportCount },
Expand All @@ -85,25 +105,7 @@ fun ImportFilesScreen(
get = { selectedTransferType },
set = newTransferViewModel::selectTransferType,
),
advancedOptionsCallbacks = AdvancedOptionsCallbacks(
initialValues = SettingsViewModel.AppSettingsData(
validityPeriod = safeAppSettings.validityPeriod,
downloadLimit = safeAppSettings.downloadLimit,
passwordOption = PasswordTransferOption.NONE,
emailLanguage = safeAppSettings.emailLanguage,
),
advancedOptionsStates = {
listOf(validityPeriodState, downloadLimitState, passwordOptionState, emailLanguageState)
},
onAdvancedOptionsClicked = { option ->
when (option) {
is ValidityPeriodOption -> newTransferViewModel.selectTransferValidityPeriod(option)
is DownloadLimitOption -> newTransferViewModel.selectTransferDownloadLimit(option)
is PasswordTransferOption -> newTransferViewModel.selectTransferPasswordOption(option)
is EmailLanguageOption -> newTransferViewModel.selectTransferLanguage(option)
}
},
),
advancedOptionsCallbacks = advancedOptionsCallbacks,
removeFileByUid = newTransferViewModel::removeFileByUid,
addFiles = newTransferViewModel::importFiles,
closeActivity = closeActivity,
Expand Down Expand Up @@ -188,36 +190,35 @@ private fun ImportFilesScreen(
TransferTypeButtons(selectedTransferType)
ImportFilesTitle(Modifier.padding(vertical = Margin.Medium), titleRes = R.string.advancedSettingsTitle)
TransferAdvancedSettings(
modifier = Modifier,
states = advancedOptionsCallbacks.advancedOptionsStates,
onClick = ::onAdvancedOptionClicked,
)
}

ValidityPeriodBottomSheet(
isBottomSheetVisible = { showValidityPeriodBottomSheet },
onOptionClicked = { advancedOptionsCallbacks.onAdvancedOptionsClicked(it) },
onOptionClicked = { advancedOptionsCallbacks.onAdvancedOptionsValueSelected(it) },
closeBottomSheet = { showValidityPeriodBottomSheet = false },
initialPosition = advancedOptionsCallbacks.initialValues.validityPeriod.ordinal,
)

DownloadLimitBottomSheet(
isBottomSheetVisible = { showDownloadLimitBottomSheet },
onOptionClicked = { advancedOptionsCallbacks.onAdvancedOptionsClicked(it) },
onOptionClicked = { advancedOptionsCallbacks.onAdvancedOptionsValueSelected(it) },
closeBottomSheet = { showDownloadLimitBottomSheet = false },
initialPosition = advancedOptionsCallbacks.initialValues.downloadLimit.ordinal,
)

PasswordOptionBottomSheet(
isBottomSheetVisible = { showPasswordOptionBottomSheet },
onOptionClicked = { advancedOptionsCallbacks.onAdvancedOptionsClicked(it) },
onOptionClicked = { advancedOptionsCallbacks.onAdvancedOptionsValueSelected(it) },
closeBottomSheet = { showPasswordOptionBottomSheet = false },
initialPosition = advancedOptionsCallbacks.initialValues.passwordOption.ordinal,
)

EmailLanguageBottomSheet(
isBottomSheetVisible = { showEmailLanguageBottomSheet },
onOptionClicked = { advancedOptionsCallbacks.onAdvancedOptionsClicked(it) },
onOptionClicked = { advancedOptionsCallbacks.onAdvancedOptionsValueSelected(it) },
closeBottomSheet = { showEmailLanguageBottomSheet = false },
initialPosition = advancedOptionsCallbacks.initialValues.emailLanguage.ordinal,
)
Expand Down Expand Up @@ -307,7 +308,7 @@ private fun ImportFilesTitle(modifier: Modifier = Modifier, @StringRes titleRes:
data class AdvancedOptionsCallbacks(
val initialValues: SettingsViewModel.AppSettingsData,
val advancedOptionsStates: () -> List<SettingOption?>,
val onAdvancedOptionsClicked: (SettingOption) -> Unit,
val onAdvancedOptionsValueSelected: (SettingOption) -> Unit,
)

enum class PasswordTransferOption(
Expand Down Expand Up @@ -335,8 +336,15 @@ private fun ImportFilesScreenPreview(@PreviewParameter(FileUiListPreviewParamete
passwordOption = PasswordTransferOption.NONE,
emailLanguage = EmailLanguage.FRENCH,
),
advancedOptionsStates = { emptyList() },
onAdvancedOptionsClicked = {},
advancedOptionsStates = {
listOf(
ValidityPeriodOption.THIRTY,
DownloadLimitOption.TWO_HUNDRED_FIFTY,
PasswordTransferOption.NONE,
EmailLanguageOption.FRENCH,
)
},
onAdvancedOptionsValueSelected = {},
),
removeFileByUid = {},
addFiles = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import com.infomaniak.swisstransfer.ui.screen.main.settings.components.SingleSel
import com.infomaniak.swisstransfer.ui.theme.SwissTransferTheme
import com.infomaniak.swisstransfer.ui.utils.PreviewAllWindows


@Composable
private fun TransferOptionBottomSheetScaffold(
isBottomSheetVisible: () -> Boolean,
Expand Down Expand Up @@ -95,7 +94,7 @@ fun DownloadLimitBottomSheet(
initialPosition = initialPosition,
titleRes = R.string.settingsOptionDownloadLimit,
optionEntries = DownloadLimitOption.entries,
onOptionClicked = { onOptionClicked(it as DownloadLimitOption) },
onOptionClicked = { onOptionClicked(it as DownloadLimitOption) },
)
}

Expand All @@ -112,7 +111,7 @@ fun PasswordOptionBottomSheet(
initialPosition = initialPosition,
titleRes = R.string.settingsOptionPassword,
optionEntries = PasswordTransferOption.entries,
onOptionClicked = { onOptionClicked(it as PasswordTransferOption) },
onOptionClicked = { onOptionClicked(it as PasswordTransferOption) },
)
}

Expand All @@ -129,7 +128,7 @@ fun EmailLanguageBottomSheet(
initialPosition = initialPosition,
titleRes = R.string.settingsOptionEmailLanguage,
optionEntries = EmailLanguageOption.entries,
onOptionClicked = { onOptionClicked(it as EmailLanguageOption) },
onOptionClicked = { onOptionClicked(it as EmailLanguageOption) },
)
}

Expand Down

0 comments on commit 5513e9a

Please sign in to comment.