Comments (7)
Hi @5AbhishekSaxena ! 👋
So you have some options, I'll try to go over them.
- You can just use a default value for the ViewModel:
@Composable
@Destination(start = true)
fun SampleScreen(
viewModel: SampleViewModel = hiltViewModel(),
navigator: DestinationsNavigator,
) {
// components
}
- You can manually call that screen:
@Composable
fun NavigationComponent(
modifier: Modifier = Modifier
) {
DestinationsNavHost(
navGraph = NavGraphs.root,
modifier = modifier
) {
composable(SampleScreenDestination) {
SampleScreen(
viewModel = hiltViewModel(),
navigator = destinationsNavigator
)
}
}
}
Btw number 2 can be used to pass anything that you may want to pass from the NavHost down (like lets say the ScaffoldState
.
AAAND you still don't need to call all your screens manually, you can opt to just manually call the screens you need to pass something explicitly. More info here.
BUT I usually go with 1 for the ViewModel, coz it can be built anywhere, it doesn't need to be constructed at the NavHost level.
Glad you opened this because it reminds me.. I should add a compile time error if you define some parameter that the lib cannot provide 🤔
I'll add that to my todo list.
Let me know if the solutions seem good to you!
from compose-destinations.
Ahh the compile-time error won't work because people can just be manually calling them. I can improve the error message though :)
from compose-destinations.
Not at all! I'd be thrilled if you could! 🙂
I honestly haven't had the time to refine the documentation to what it should be 😔
Btw if people are not using Hilt, your solution might not be enough if you plan to access the navigation arguments from the SavedStateHandle. For that you need something like this:
class Factory(
navBackStackEntry: NavBackStackEntry
) : AbstractSavedStateViewModelFactory(
owner = navBackStackEntry,
defaultArgs = navBackStackEntry.arguments
) {
@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel?> create(
key: String,
modelClass: Class<T>,
handle: SavedStateHandle
): T {
return ProfileViewModel(handle) as T
}
}
// And to get the VM:
//...
val vm = viewModel<ProfileViewModel>(
factory = ProfileViewModel.Factory(navBackStackEntry)
)
from compose-destinations.
Cool, I'll create a PR with the suggestions for the documentation.
Btw if people are not using Hilt, your solution might not be enough if you plan to access the navigation arguments from the SavedStateHandle.
A really good solution, thanks. I'll update the same in my project.
from compose-destinations.
I'll close the issue then, feel free to reopen it or add more comments if you want to :)
from compose-destinations.
Thanks for the quick reply,
I went with method #2 to avoid the default value thingy.
If somebody isn't using hilt, they can refer to this
@Composable
@Suppress("UNCHECKED_CAST")
private fun getSampleScreenViewModel(): SamplelScreenViewModel {
return viewModel(factory = object : ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
return SampleScreenViewModel() as T
}
})
}
and used it like this
@Composable
fun SampleNavigationComponent(
modifier: Modifier = Modifier
) {
DestinationsNavHost(
navGraph = NavGraphs.root,
modifier = modifier
) {
composable(SampleScreenDestination) {
SampleScreen(
viewModel = getSampleScreenViewModel(),
navigator = destinationsNavigator
)
}
}
}
Also, I found the documentation a bit confusing or missing details, would you mind if I suggest the changes?
from compose-destinations.
Ahh the compile-time error won't work because people can just be manually calling them. I can improve the error message though :)
An IDE warning should work just fine.
from compose-destinations.
Related Issues (20)
- [ksp] NullPointerException | UtilsKt.toImportable HOT 7
- [KSP] NullPointerException HOT 23
- What does `navigators` argument in `engine.rememberNavController()` do? HOT 1
- KSP option for single destination invoke() function HOT 2
- Bottom Sheet navigation problem when onlyIfResumed = true HOT 1
- How to make bottom bar navigation work with a nested graph using v2 HOT 11
- How to use bottom sheets with material 3 HOT 5
- Support for KMP HOT 1
- Expected NavGraphSpec HOT 2
- Any plans to support kotlin 1.9.23? HOT 5
- Crash in multi module setup HOT 3
- Possible dependencies' versions mismatch (v2) HOT 4
- NestedGraph destinations not in parent NavGraphSpec.allDestinations HOT 2
- Looking for advice: Would like to know to to set nested graph with it's own Scaffold + shared viewmodel HOT 1
- Mandatory navigation argument for start destination HOT 1
- Support for material3 bottom sheets HOT 4
- Providing title to Destinations on v2 HOT 1
- Provide a default Destination AnimatedContent Scope to support the Shared Element Transition API HOT 7
- IDE plugin for visualizing the navigation graphs HOT 1
- Multi-module setup throws compilation error that start route does not exist. HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from compose-destinations.