Tim Malseed
07/19/2022, 12:33 AMTim Malseed
07/19/2022, 12:34 AMIf the class’s responsibility is crucial for the whole application, you can scope an instance of that class to the(emphasis mine)class. This makes it so the instance follows the application’s lifecycle. Alternatively, if you only need to reuse the same instance in a particular flow in your app—for example, the registration or login flow—then you should scope the instance to the class that owns the lifecycle of that flow. For example, you could scope aApplication
that contains in-memory data to theRegistrationRepository
or the navigation graph of the registration flow.RegistrationActivity
Tim Malseed
07/19/2022, 12:34 AMNavGraphComponent
, and a NavGraphComponentManager
- the idea, I think, is you inject the component manager, and then you can pass in, say, a route, or graph, or something - and the component manager will decide if the dependency should be rebuilt. Then, when injecting dependencies that are provided by the component, you get an existing instance, or a new one depending on the internal logic of that component manager. So, somehow the component manager should have enough knowledge to decide if you need a new instance of your repository, based on whether the nav graph has changed.Tim Malseed
07/19/2022, 12:38 AMStylianos Gakis
07/19/2022, 12:13 PMTim Malseed
07/19/2022, 12:50 PMTim Malseed
07/19/2022, 12:51 PMTim Malseed
07/19/2022, 12:51 PMStylianos Gakis
07/19/2022, 12:52 PMStylianos Gakis
07/19/2022, 12:52 PMTim Malseed
07/19/2022, 12:52 PMTim Malseed
07/19/2022, 12:54 PMStylianos Gakis
07/19/2022, 1:00 PMViewModelComponent
which I suppose if you provide something using that component and you get it into your NavGraph scoped ViewModel, the Repository should also be scoped appropriately no?
As far as the scoping to the NavGraph it’s a matter of providing a different owner to the by viewModel(owner = navController.getOdysseyScopedNavBackStackEntry())
call with
private fun NavController.getOdysseyScopedNavBackStackEntry(): NavBackStackEntry {
return getBackStackEntry(MainScreen.Odyssey.route)
}
And MainScreen.Odyssey.route is the string of the parent route of the destinations.
Here’s the source code. I was linking into.