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

Cleanup Hilt ViewModel extensions #288

Closed
wants to merge 1 commit into from

Conversation

ghostbear
Copy link
Contributor

  • Share code between getViewModel extension functions
  • Add tag and viewModelStoreOwner as parameters to allow sharing ViewModels between Screens

Sharing ViewModels between screen example:

class AScreen : AndroidScreen() {

    @Composable
    override fun Content() {
        val navigator = LocalNavigator.currentOrThrow
        val viewModelStoreOwner = LocalContext.current as ViewModelStoreOwner
        val viewModel = getViewModel<AViewModel, AViewModel.Factory>(viewModelStoreOwner = viewModelStoreOwner) { factory ->
            factory.create("World")
        }
        Column {
            Button(onClick = { navigator.push(OtherScreen(viewModelStoreOwner)) }) {
                Text(text = "Other")
            }
        }
        // More Code
    }
}

class OtherScreen : AndroidScreen() {

    @Composable
    override fun Content() {
        val viewModelStoreOwner = LocalContext.current as ViewModelStoreOwner
        val viewModel = getViewModel<AViewModel>(viewModelStoreOwner = viewModelStoreOwner)
        // More Code
    }

}

- Share code between getViewModel extension functions
- Add tag and viewModelStoreOwner as parameters to allow sharing ViewModels between Screens
@programadorthi
Copy link
Collaborator

programadorthi commented Jan 9, 2024

Have you seen #217 ?
Also AndroidX provides a LocalViewModelStoreOwner.current that help to avoid casting your local context.

@ghostbear
Copy link
Contributor Author

Have you seen #217 ?

I haven't but it looks like a cleaner version.

@ghostbear ghostbear closed this Jan 10, 2024
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.

2 participants