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 7f3da4ff75..d43f7bcd72 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 @@ -21,6 +21,7 @@ import android.net.Uri import androidx.lifecycle.SavedStateHandle import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope +import com.infomaniak.multiplatform_swisstransfer.common.interfaces.appSettings.AppSettings import com.infomaniak.multiplatform_swisstransfer.common.interfaces.upload.RemoteUploadFile import com.infomaniak.multiplatform_swisstransfer.common.interfaces.upload.UploadFileSession import com.infomaniak.multiplatform_swisstransfer.common.models.EmailLanguage @@ -29,7 +30,13 @@ import com.infomaniak.multiplatform_swisstransfer.data.NewUploadSession import com.infomaniak.multiplatform_swisstransfer.managers.UploadManager import com.infomaniak.sentry.SentryLog import com.infomaniak.swisstransfer.di.IoDispatcher -import com.infomaniak.swisstransfer.ui.screen.main.settings.components.SettingOption +import com.infomaniak.swisstransfer.ui.screen.main.settings.DownloadLimitOption +import com.infomaniak.swisstransfer.ui.screen.main.settings.DownloadLimitOption.Companion.toAdvancedOption +import com.infomaniak.swisstransfer.ui.screen.main.settings.EmailLanguageOption +import com.infomaniak.swisstransfer.ui.screen.main.settings.EmailLanguageOption.Companion.toAdvancedOption +import com.infomaniak.swisstransfer.ui.screen.main.settings.ValidityPeriodOption +import com.infomaniak.swisstransfer.ui.screen.main.settings.ValidityPeriodOption.Companion.toAdvancedOption +import com.infomaniak.swisstransfer.ui.screen.newtransfer.importfiles.PasswordTransferOption import com.infomaniak.swisstransfer.ui.screen.newtransfer.importfiles.components.TransferType import com.infomaniak.swisstransfer.workers.UploadWorker import dagger.hilt.android.lifecycle.HiltViewModel @@ -67,13 +74,6 @@ class NewTransferViewModel @Inject constructor( savedStateHandle[IS_VIEW_MODEL_RESTORED_KEY] = value } - private val _selectedTransferType = MutableStateFlow(TransferType.LINK) - val selectedTransferType: StateFlow = _selectedTransferType.asStateFlow() - - private val _selectedAdvancedOptions = MutableStateFlow SettingOption>>(emptyList()) - val selectedAdvancedOptions: StateFlow SettingOption>> = _selectedAdvancedOptions - - init { viewModelScope.launch(ioDispatcher) { if (isFirstViewModelCreation) { @@ -134,12 +134,53 @@ class NewTransferViewModel @Inject constructor( ) } - companion object { - private val TAG = NewTransferViewModel::class.java.simpleName - private const val IS_VIEW_MODEL_RESTORED_KEY = "IS_VIEW_MODEL_RESTORED_KEY" - } + //region Transfer Type + private val _selectedTransferType = MutableStateFlow(TransferType.LINK) + val selectedTransferType: StateFlow = _selectedTransferType fun selectTransferType(type: TransferType) { _selectedTransferType.value = type } + //endregion + + //region Transfer Advanced Options + private val _selectedValidityPeriodOption = MutableStateFlow(null) + val selectedValidityPeriodOption: StateFlow = _selectedValidityPeriodOption + + private val _selectedDownloadLimitOption = MutableStateFlow(null) + val selectedDownloadLimitOption: StateFlow = _selectedDownloadLimitOption + + private val _selectedPasswordOption = MutableStateFlow(PasswordTransferOption.NONE) + val selectedPasswordOption: StateFlow = _selectedPasswordOption + + private val _selectedLanguageOption = MutableStateFlow(null) + val selectedLanguageOption: StateFlow = _selectedLanguageOption + + fun selectTransferValidityPeriod(validityPeriodOption: ValidityPeriodOption) { + _selectedValidityPeriodOption.value = validityPeriodOption + } + + fun selectTransferDownloadLimit(downloadLimit: DownloadLimitOption) { + _selectedDownloadLimitOption.value = downloadLimit + } + + fun selectTransferPasswordOption(passwordOption: PasswordTransferOption) { + _selectedPasswordOption.value = passwordOption + } + + fun selectTransferLanguage(language: EmailLanguageOption) { + _selectedLanguageOption.value = language + } + + fun initTransferAdvancedOptionsValues(safeAppSettings: AppSettings) { + selectTransferValidityPeriod(safeAppSettings.validityPeriod.toAdvancedOption()) + selectTransferDownloadLimit(safeAppSettings.downloadLimit.toAdvancedOption()) + selectTransferLanguage(safeAppSettings.emailLanguage.toAdvancedOption()) + } + //endregion + + companion object { + private val TAG = NewTransferViewModel::class.java.simpleName + private const val IS_VIEW_MODEL_RESTORED_KEY = "IS_VIEW_MODEL_RESTORED_KEY" + } } 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 9a31c7e801..517ec69a8f 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 @@ -42,7 +42,10 @@ 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 +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.SettingsViewModel +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.NewTransferViewModel import com.infomaniak.swisstransfer.ui.screen.newtransfer.importfiles.components.* @@ -65,10 +68,14 @@ fun ImportFilesScreen( val filesToImportCount by newTransferViewModel.filesToImportCount.collectAsStateWithLifecycle() val currentSessionFilesCount by newTransferViewModel.currentSessionFilesCount.collectAsStateWithLifecycle() val selectedTransferType by newTransferViewModel.selectedTransferType.collectAsStateWithLifecycle() - val advancedOptionsStates by newTransferViewModel.selectedAdvancedOptions.collectAsStateWithLifecycle() + val validityPeriodState by newTransferViewModel.selectedValidityPeriodOption.collectAsStateWithLifecycle() + val downloadLimitState by newTransferViewModel.selectedDownloadLimitOption.collectAsStateWithLifecycle() + val passwordOptionState by newTransferViewModel.selectedPasswordOption.collectAsStateWithLifecycle() + val emailLanguageState by newTransferViewModel.selectedLanguageOption.collectAsStateWithLifecycle() val appSettings by settingsViewModel.appSettingsFlow.collectAsStateWithLifecycle(null) appSettings?.let { safeAppSettings -> + newTransferViewModel.initTransferAdvancedOptionsValues(safeAppSettings) ImportFilesScreen( files = { files }, @@ -85,9 +92,16 @@ fun ImportFilesScreen( passwordOption = PasswordTransferOption.NONE, emailLanguage = safeAppSettings.emailLanguage, ), - advancedOptionsStates = advancedOptionsStates, + advancedOptionsStates = { + listOf(validityPeriodState, downloadLimitState, passwordOptionState, emailLanguageState) + }, onAdvancedOptionsClicked = { option -> - // TODO: Update viewModel + when (option) { + is ValidityPeriodOption -> newTransferViewModel.selectTransferValidityPeriod(option) + is DownloadLimitOption -> newTransferViewModel.selectTransferDownloadLimit(option) + is PasswordTransferOption -> newTransferViewModel.selectTransferPasswordOption(option) + is EmailLanguageOption -> newTransferViewModel.selectTransferLanguage(option) + } }, ), removeFileByUid = newTransferViewModel::removeFileByUid, @@ -175,7 +189,7 @@ private fun ImportFilesScreen( ImportFilesTitle(Modifier.padding(vertical = Margin.Medium), titleRes = R.string.advancedSettingsTitle) TransferAdvancedSettings( modifier = Modifier, - states = { advancedOptionsCallbacks.initialValues.toAdvancedOptionsList() }, + states = advancedOptionsCallbacks.advancedOptionsStates, onClick = ::onAdvancedOptionClicked, ) } @@ -292,7 +306,7 @@ private fun ImportFilesTitle(modifier: Modifier = Modifier, @StringRes titleRes: data class AdvancedOptionsCallbacks( val initialValues: SettingsViewModel.AppSettingsData, - val advancedOptionsStates: List<() -> SettingOption>, + val advancedOptionsStates: () -> List, val onAdvancedOptionsClicked: (SettingOption) -> Unit, ) @@ -321,7 +335,7 @@ private fun ImportFilesScreenPreview(@PreviewParameter(FileUiListPreviewParamete passwordOption = PasswordTransferOption.NONE, emailLanguage = EmailLanguage.FRENCH, ), - advancedOptionsStates = emptyList(), + advancedOptionsStates = { emptyList() }, onAdvancedOptionsClicked = {}, ), removeFileByUid = {}, diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/importfiles/components/TransferAdvancedSetting.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/importfiles/components/TransferAdvancedSetting.kt index ea377c301c..db3e10c5d0 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/importfiles/components/TransferAdvancedSetting.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/importfiles/components/TransferAdvancedSetting.kt @@ -41,7 +41,7 @@ import com.infomaniak.swisstransfer.ui.theme.SwissTransferTheme @Composable fun TransferAdvancedSetting( settingType: TransferAdvancedSettingType, - selectedSetting: () -> SettingOption, + selectedSetting: () -> SettingOption?, onClick: () -> Unit, ) { SharpRippleButton(onClick = onClick, contentPadding = PaddingValues(horizontal = Margin.Large, vertical = Margin.Medium)) { @@ -70,9 +70,9 @@ fun TransferAdvancedSetting( } @Composable -private fun SettingValue(selectedSetting: () -> SettingOption) { +private fun SettingValue(selectedSetting: () -> SettingOption?) { Text( - text = selectedSetting().title(), + text = selectedSetting()?.title?.invoke() ?: "", style = SwissTransferTheme.typography.bodySmallRegular, color = SwissTransferTheme.colors.secondaryTextColor, ) 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 fb9e65e290..71313349e8 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 @@ -46,7 +46,7 @@ import com.infomaniak.swisstransfer.ui.theme.SwissTransferTheme @Composable fun TransferAdvancedSettings( modifier: Modifier = Modifier, - states: () -> List, + states: () -> List, onClick: (TransferAdvancedSettingType) -> Unit, ) { SwissTransferCard(modifier = modifier) {