Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for restoring state from SavedStateHandle in Vector state factory #17

Open
haroldadmin opened this issue Oct 2, 2019 · 1 comment
Labels
enhancement New feature or request

Comments

@haroldadmin
Copy link
Owner

Is your feature request related to a problem? Please describe.
VectorStateFactory tries two approaches to create an initial state: Using the VectorViewModelFactory and using the constructor.

For applications using the SavedStateVectorViewModel, it becomes necessary to implement VectorViewModelFactory's initialState() method to restore this state from the SavedStateHandle, even though the most basic implementation here simply retrieves the state from the handle and returns it.

Describe the solution you'd like
An automated restoration of state from the SavedStateHandle in the case where the ViewModel does not implement the factory interface's initialState() method.

@haroldadmin haroldadmin added the enhancement New feature or request label Oct 2, 2019
@haroldadmin
Copy link
Owner Author

haroldadmin commented Oct 3, 2019

Implementation for this would be go somewhat like this:

// VectorStateFactory

override fun <S : VectorState> createInitialState(
  vmClass: KClass<*>,
  stateClass: KClass<out S>,
  handle: SavedStateHandle,
  owner: ViewModelOwner
): S {
  return getStateFromVectorVMFactory(vmClass, handle, owner)
    ?: getPersistedState(handle)
    ?: getDefaultStateFromConstructor(stateClass)
    ?: throw UnInstantiableStateClassException(stateClass.java.simpleName)
}

fun <S: VectorState> getPersistedState(handle: SavedStateHandle): S? {
  return handle[SavedStateVectorViewModel.KEY_SAVED_STATE]
}

This will work correctly only when the state has been saved using KEY_SAVED_STATE. If the user uses a different implementation of the persistState() method then the retrieved persisted state might be incomplete, or null.

Looks like this is infeasible using this solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant