-
Notifications
You must be signed in to change notification settings - Fork 144
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
Why not set the userGuess to uiState? #48
Comments
I have the same question. Seems like this is against the concept of representing all the UI state in a single object. @android-dev-lxl Would there be any drawback or negative side effect of moving the userGuess into the UiState class ? |
@RicardoBelchior The reason for having 2 states is Something like this:
|
Thanks for coming back! I understand why it's hoisted in the
Can you please reference a website/documentation talking about:
? Thanks again. |
I agree. |
You cannot have MutableState as part of the screen UI state, and because that's user input, the best and easiest way to capture it is with those APIs. https://developer.android.com/jetpack/compose/state-hoisting |
It should be a part of UI state If is a part of viewmodel and will influence the UI Element.I think. It is possible that in the future I will do some other logic on this user input, so if there are two variables that determine the UI is not pure for the MVI architecture. However, if we initially design the rules to be uniform, and also conform to the MVI architecture, there is no need to do unnecessary refactoring when doing future extensions. |
@android-dev-lxl Any updates on this? I'm also wondering why it's not part of the GameUiState. There may also be a case where the user input is saved on savedStateHandle i.e class GameViewModel (savedStateHandle: SavedStateHandle) : ViewModel {
private val userGuess = savedStateHandle.getStateFlow("user_guess", "")
fun updateUserGuess(guess: String) {
savedStateHandle["user_guess"] = guess
}
private val _uiState = MutableStateFlow(GameUiState())
val uiState: StateFlow<GameUiState> =
combine(_uiState.asStateFlow(), userGuess, ::Pair)
.map { (uiState, guess) ->
uiState.copy(
userGuess = guess
)
}.stateIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(5_000),
initialValue = GameUiState()
)
// rest of code ...
} edit: add : ViewModel() |
I came here wondering about the same thing since it was not clear why the The following article delves into the issue at length. It seems that state management for
|
Did you understand, why they are doing this? |
basic-android-kotlin-compose-training-unscramble/app/src/main/java/com/example/android/unscramble/ui/GameViewModel.kt
Line 40 in 7984222
The text was updated successfully, but these errors were encountered: