Stop persisting add-edit state to SavedStateHandle (#375)

This commit is contained in:
David Perez 2023-12-12 11:11:23 -06:00 committed by Álison Fernandes
parent f4db50b700
commit df71aa573a

View file

@ -35,16 +35,17 @@ private const val KEY_STATE = "state"
* *
* This ViewModel processes UI actions, manages the state of the generator screen, * This ViewModel processes UI actions, manages the state of the generator screen,
* and provides data for the UI to render. It extends a `BaseViewModel` and works * and provides data for the UI to render. It extends a `BaseViewModel` and works
* with a `SavedStateHandle` for state restoration. * with a `SavedStateHandle` for retrieving navigation arguments.
* *
* @property savedStateHandle Handles the saved state of this ViewModel. * @param savedStateHandle Handles the navigation arguments of this ViewModel.
*/ */
@HiltViewModel @HiltViewModel
@Suppress("TooManyFunctions") @Suppress("TooManyFunctions")
class VaultAddItemViewModel @Inject constructor( class VaultAddItemViewModel @Inject constructor(
private val savedStateHandle: SavedStateHandle, savedStateHandle: SavedStateHandle,
private val vaultRepository: VaultRepository, private val vaultRepository: VaultRepository,
) : BaseViewModel<VaultAddItemState, VaultAddItemEvent, VaultAddItemAction>( ) : BaseViewModel<VaultAddItemState, VaultAddItemEvent, VaultAddItemAction>(
// We load the state from the savedStateHandle for testing purposes.
initialState = savedStateHandle[KEY_STATE] initialState = savedStateHandle[KEY_STATE]
?: run { ?: run {
val vaultAddEditType = VaultAddEditItemArgs(savedStateHandle).vaultAddEditType val vaultAddEditType = VaultAddEditItemArgs(savedStateHandle).vaultAddEditType
@ -62,8 +63,6 @@ class VaultAddItemViewModel @Inject constructor(
//region Initialization and Overrides //region Initialization and Overrides
init { init {
stateFlow.onEach { savedStateHandle[KEY_STATE] = it }.launchIn(viewModelScope)
when (val vaultAddEditType = state.vaultAddEditType) { when (val vaultAddEditType = state.vaultAddEditType) {
VaultAddEditType.AddItem -> Unit VaultAddEditType.AddItem -> Unit
is VaultAddEditType.EditItem -> { is VaultAddEditType.EditItem -> {