Skip to content

Commit

Permalink
feat(AdvancedOptionsState): Update viewModel when selecting an option
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianDevel committed Nov 6, 2024
1 parent c1e4f13 commit b5ad405
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -67,13 +74,6 @@ class NewTransferViewModel @Inject constructor(
savedStateHandle[IS_VIEW_MODEL_RESTORED_KEY] = value
}

private val _selectedTransferType = MutableStateFlow(TransferType.LINK)
val selectedTransferType: StateFlow<TransferType> = _selectedTransferType.asStateFlow()

private val _selectedAdvancedOptions = MutableStateFlow<List<() -> SettingOption>>(emptyList())
val selectedAdvancedOptions: StateFlow<List<() -> SettingOption>> = _selectedAdvancedOptions


init {
viewModelScope.launch(ioDispatcher) {
if (isFirstViewModelCreation) {
Expand Down Expand Up @@ -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<TransferType> = _selectedTransferType

fun selectTransferType(type: TransferType) {
_selectedTransferType.value = type
}
//endregion

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

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

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

private val _selectedLanguageOption = MutableStateFlow<EmailLanguageOption?>(null)
val selectedLanguageOption: StateFlow<EmailLanguageOption?> = _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"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand All @@ -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 },
Expand All @@ -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,
Expand Down Expand Up @@ -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,
)
}
Expand Down Expand Up @@ -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<SettingOption?>,
val onAdvancedOptionsClicked: (SettingOption) -> Unit,
)

Expand Down Expand Up @@ -321,7 +335,7 @@ private fun ImportFilesScreenPreview(@PreviewParameter(FileUiListPreviewParamete
passwordOption = PasswordTransferOption.NONE,
emailLanguage = EmailLanguage.FRENCH,
),
advancedOptionsStates = emptyList(),
advancedOptionsStates = { emptyList() },
onAdvancedOptionsClicked = {},
),
removeFileByUid = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import com.infomaniak.swisstransfer.ui.theme.SwissTransferTheme
@Composable
fun TransferAdvancedSettings(
modifier: Modifier = Modifier,
states: () -> List<SettingOption>,
states: () -> List<SettingOption?>,
onClick: (TransferAdvancedSettingType) -> Unit,
) {
SwissTransferCard(modifier = modifier) {
Expand Down

0 comments on commit b5ad405

Please sign in to comment.