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

feat: added support to have navigator scoped ViewModel #217

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

programadorthi
Copy link
Collaborator

@programadorthi programadorthi commented Sep 23, 2023

ISSUES SOLVED: #155 #168 and #210

Navigator scoped screen model was added on the #233

AndroidX ViewModel has support to scoped ViewModel here

So, we are supporting scoped ViewModel in Voyager too. How it works?
Androidx has a function viewModel {} that use the local ViewModelStoreOwner so scoped ViewModel is supported by default.
By default each Screen has your own ViewModelStoreOwner that doesn't support scoped and each screen will have a unique instance.

// ScreenA
val viewModel1 = viewModel { MyViewModel() }

// ScreenB
val viewModel2 = viewModel { MyViewModel() }

To have ViewModel scoped to a Navigator and share the same instance between screens do:

// ScreenA
val viewModel = rememberNavigatorViewModel<MyViewModel>()

// ScreenB
val viewModel = rememberNavigatorViewModel<MyViewModel>()

If you have a ViewModel scoped to Activity or Fragment do:

// ScreenA
val viewModel = viewModel(viewModelStoreOwner = activityOrFragmentViewModelStoreOwner) { SharedViewModel() }

@HiltViewModel has support now to use scoped ViewModel. To have or to get scoped ViewModel just provide to hilt function a ViewModelStoreOwner

@osrl
Copy link
Contributor

osrl commented Sep 27, 2023

Any update on this PR?

@slickorange
Copy link

@programadorthi Is is possible for you to release this on your own fork? This is really a big challenge for me..

@programadorthi
Copy link
Collaborator Author

Sorry guys. I can't. You will have to wait for owners review to check and avoid breaking changes.

@renaudmathieu
Copy link

I'm really looking forward to this feature

feat: navigator scoped viewmodel
@programadorthi programadorthi force-pushed the feat/navigator-scoped-screen-model branch from a6690ff to e6fc71f Compare January 9, 2024 05:23
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should move this Android ViewModel specific API to it own module. Maybe restaure previous know voyager-androidx module

Comment on lines 71 to 80
// Public: used in Navigator Scoped ScreenModels
@InternalVoyagerApi
public inline fun <reified T : ScreenModel> getOrNull(
holderKey: String,
tag: String?
): T? {
val key = getKey<T>(holderKey, tag)
return screenModels[key] as T?
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not required if you remove the navigator sopced screen models

@DevSrSouza DevSrSouza changed the title feat: added support to have navigator scoped screen model feat: added support to have navigator scoped ViewModel Apr 8, 2024
@osrl
Copy link
Contributor

osrl commented May 20, 2024

hi, are there any plans to merge this?

@DevSrSouza
Copy link
Collaborator

@osrl we have a experimental support for it on the new Lifecycle KMP API.

@osrl
Copy link
Contributor

osrl commented Aug 19, 2024

any idea how to inject a viewmodel using koin into this navigatorViewModel

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

Successfully merging this pull request may close these issues.

6 participants