I’m have a composable that creates a scoped dagger component and fetches several dependencies from it.
In order for my code to work correctly, this scope would need to survive configuration changes.
As a solution I’ve hacked my dagger component to be retained by the ViewModelStore.
Does this make sense or is there a better way to retain objects at this point?
Copy code
class HoldingViewModel : ViewModel() {
val values = mutableMapOf<Class<*>, Any>()
}
@Composable
inline fun <reified T : Any> viewModelStoreRemembered(
crossinline create: () -> T
): T {
val map = viewModel(
modelClass = HoldingViewModel::class.java,
).values
return map.getOrPut(T::class.java) {
create()
} as T
}