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 f541705bf..682a70b1e 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 @@ -25,12 +25,12 @@ import androidx.compose.runtime.setValue 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 import com.infomaniak.multiplatform_swisstransfer.common.utils.mapToList import com.infomaniak.multiplatform_swisstransfer.data.NewUploadSession +import com.infomaniak.multiplatform_swisstransfer.managers.AppSettingsManager import com.infomaniak.multiplatform_swisstransfer.managers.UploadManager import com.infomaniak.sentry.SentryLog import com.infomaniak.swisstransfer.di.IoDispatcher @@ -44,7 +44,6 @@ import com.infomaniak.swisstransfer.ui.screen.main.settings.components.SettingOp import com.infomaniak.swisstransfer.ui.screen.newtransfer.importfiles.AdvancedOptionsCallbacks import com.infomaniak.swisstransfer.ui.screen.newtransfer.importfiles.AdvancedOptionsState import com.infomaniak.swisstransfer.ui.screen.newtransfer.importfiles.PasswordTransferOption -import com.infomaniak.swisstransfer.ui.screen.newtransfer.importfiles.components.TransferAdvancedSettingType import com.infomaniak.swisstransfer.ui.screen.newtransfer.importfiles.components.TransferType import com.infomaniak.swisstransfer.ui.utils.GetSetCallbacks import com.infomaniak.swisstransfer.workers.UploadWorker @@ -57,6 +56,7 @@ import javax.inject.Inject @HiltViewModel class NewTransferViewModel @Inject constructor( + private val appSettingsManager: AppSettingsManager, private val savedStateHandle: SavedStateHandle, private val importationFilesManager: ImportationFilesManager, private val uploadManager: UploadManager, @@ -193,44 +193,24 @@ class NewTransferViewModel @Inject constructor( savedStateHandle[SELECTED_LANGUAGE_KEY] = language } - fun initTransferAdvancedOptionsValues(safeAppSettings: AppSettings) { - selectTransferValidityPeriod(safeAppSettings.validityPeriod.toAdvancedOption()) - selectTransferDownloadLimit(safeAppSettings.downloadLimit.toAdvancedOption()) - selectTransferLanguage(safeAppSettings.emailLanguage.toAdvancedOption()) + fun initTransferAdvancedOptionsValues() { + viewModelScope.launch(ioDispatcher) { + appSettingsManager.getAppSettings()?.let { + selectTransferValidityPeriod(it.validityPeriod.toAdvancedOption()) + selectTransferDownloadLimit(it.downloadLimit.toAdvancedOption()) + selectTransferLanguage(it.emailLanguage.toAdvancedOption()) + } ?: run { + SentryLog.e(TAG, "AppSettings is null in ImportFilesScreen, it should not happened") + selectTransferValidityPeriod(ValidityPeriodOption.THIRTY) + selectTransferDownloadLimit(DownloadLimitOption.TWO_HUNDRED_FIFTY) + selectTransferLanguage(EmailLanguageOption.FRENCH) + } + } } - fun getTransferAdvancedOptionCallbacks( - appSettings: AppSettings?, - validityPeriodState: () -> ValidityPeriodOption?, - downloadLimitState: () -> DownloadLimitOption?, - passwordOptionState: () -> PasswordTransferOption?, - emailLanguageState: () -> EmailLanguageOption?, - ): AdvancedOptionsCallbacks? { - - if (appSettings == null) return null - - initTransferAdvancedOptionsValues(appSettings) + fun getTransferAdvancedOptionsCallbacks(advancedOptionsStates: () -> List): AdvancedOptionsCallbacks { return AdvancedOptionsCallbacks( - 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, - ), - ) - }, + advancedOptionsStates = advancedOptionsStates, onAdvancedOptionsValueSelected = ::onAdvancedOptionsValueSelected, password = GetSetCallbacks( get = { transferPassword }, @@ -251,9 +231,9 @@ class NewTransferViewModel @Inject constructor( //endregion //region Password - var transferPassword by mutableStateOf("") + private var transferPassword by mutableStateOf("") - val isPasswordValid by derivedStateOf { + private val isPasswordValid by derivedStateOf { val trimmedPassword = transferPassword.trim() trimmedPassword.length in 6..25 } 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 4270694f6..34f3565b1 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 @@ -41,7 +41,6 @@ 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 @@ -57,7 +56,6 @@ private const val TOTAL_FILE_SIZE: Long = 50_000_000_000L @Composable fun ImportFilesScreen( newTransferViewModel: NewTransferViewModel = hiltViewModel(), - settingsViewModel: SettingsViewModel = hiltViewModel(), closeActivity: () -> Unit, navigateToUploadProgress: () -> Unit, ) { @@ -72,14 +70,31 @@ fun ImportFilesScreen( val passwordOptionState by newTransferViewModel.selectedPasswordOption.collectAsStateWithLifecycle() val emailLanguageState by newTransferViewModel.selectedLanguageOption.collectAsStateWithLifecycle() - val appSettings by settingsViewModel.appSettingsFlow.collectAsStateWithLifecycle(null) + LaunchedEffect(Unit) { newTransferViewModel.initTransferAdvancedOptionsValues() } - val advancedOptionsCallbacks = newTransferViewModel.getTransferAdvancedOptionCallbacks( - appSettings, { validityPeriodState }, { downloadLimitState }, { passwordOptionState }, { emailLanguageState }, + val advancedOptionsCallbacks = newTransferViewModel.getTransferAdvancedOptionsCallbacks( + 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 }, + ), + ) + }, ) - if (advancedOptionsCallbacks == null) return - ImportFilesScreen( files = { files }, filesToImportCount = { filesToImportCount }, diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 5ac4e109d..2e95b54ec 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -20,7 +20,7 @@ qrose = "1.0.1" recaptcha = "18.6.1" sentry = "4.12.0" serialization = "1.7.1" -swisstransfer = "0.7.0" +swisstransfer = "0.7.1" workmanager = "2.9.1" [libraries]