Skip to content

Commit

Permalink
feat(TransferPasswordAlertDialog): Get AppSettings from viewModel ins…
Browse files Browse the repository at this point in the history
…tead of collecting flow
  • Loading branch information
FabianDevel committed Nov 6, 2024
1 parent 8c8d9e7 commit f7caa47
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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<AdvancedOptionsState>): 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 },
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -57,7 +56,6 @@ private const val TOTAL_FILE_SIZE: Long = 50_000_000_000L
@Composable
fun ImportFilesScreen(
newTransferViewModel: NewTransferViewModel = hiltViewModel<NewTransferViewModel>(),
settingsViewModel: SettingsViewModel = hiltViewModel<SettingsViewModel>(),
closeActivity: () -> Unit,
navigateToUploadProgress: () -> Unit,
) {
Expand All @@ -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 },
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit f7caa47

Please sign in to comment.