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

Publicly accessible state through intent extensions. #122

Closed
JakubMosakowski opened this issue Mar 18, 2022 · 1 comment
Closed

Publicly accessible state through intent extensions. #122

JakubMosakowski opened this issue Mar 18, 2022 · 1 comment

Comments

@JakubMosakowski
Copy link

JakubMosakowski commented Mar 18, 2022

I'm a bit worried about the idea of a public mutable state. I would assume that the state of view would be only editable inside the view model. Currently, it is open for everybody (assuming that a view model is a ContainerHost which is recommended) through the intent extension (more precisely by the orbit method).

I would expect that I won't be able to do smth like that: viewModel.intent { reduce { state.copy(isLoading = true) } } but it is allowed.

Is it implemented that way on purpose? Is it by the limitation of the interface?

@GuilhE
Copy link
Contributor

GuilhE commented Apr 11, 2022

@JakubMosakowski you can solve it by:

data class MyState(val message: String = "")

class MyViewModel : ViewModel() {
    private val host: ContainerHost<MyState, Nothing> = object : ContainerHost<MyState, Nothing>{
        override val container = viewModelScope.container<MyState, Nothing>(MyState())
    }

    init {
        host.intent { reduce { MyState(message = "Hello world!")} }
    }

    val stateFlow = host.container.stateFlow
}

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

No branches or pull requests

2 participants