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

The viewModel() method may be missing the postInvalidate() logic. #690

Open
weijia1991 opened this issue Aug 22, 2023 · 2 comments
Open

Comments

@weijia1991
Copy link

weijia1991 commented Aug 22, 2023

In activity by viewModel() is not working properly, the invalidate() method doesn't callback when the viewLifecycleOwner is started, viewModel() is missing the postInvalidate() logic like activityViewModel(), please tell me how should I do it in activity.
Can I modify the viewModel() code like this:

inline fun <T, reified VM : MavericksViewModel<S>, reified S : MavericksState> T.viewModel(
    viewModelClass: KClass<VM> = VM::class,
    crossinline keyFactory: () -> String = { viewModelClass.java.name }
): Lazy<VM> where T : ComponentActivity, T : MavericksView = lifecycleAwareLazy(this) {
    val lifecycleOwner = this
    MavericksViewModelProvider.get(
        viewModelClass = viewModelClass.java,
        stateClass = S::class.java,
        viewModelContext = ActivityViewModelContext(this, intent.extras?.get(Mavericks.KEY_ARG)),
        key = keyFactory()
    ).apply { _internal(lifecycleOwner, action = { lifecycleOwner.postInvalidate() }) }
}
@hezd
Copy link

hezd commented Aug 25, 2023

I'm experiencing the same issue, is it because it's not recommended for use in Activity and therefore doesn't provide a solution?

@YumengNevix
Copy link

invalidate function has comment: "Override this to handle any state changes from MavericksViewModels created through Mavericks Fragment delegates." so this method is just used in Fragment class.
If you want to observe state change in Activity, you can use :

viewModel.onEach(State::property) {
   // refresh ui
}

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

3 participants