masteramyx
11/07/2023, 5:25 AMviewModel(named("CartViewModel")) { CartViewModel(get(), get()) }
My activity and fragment return same instance by using viewmodel delegates
But I have a Presentation
class which represents a secondary display, I want to observe some VM state changes on this screen. Since it is a Presentation
which basically just extends Dialog
I have no lifecycle components to leverage and can not use the typical injection patterns that are so easy with Activity/Fragments.
Presentation Class
companion object {
var VM_STORE: ViewModelStore? = null
}
override val viewModelStore: ViewModelStore
get() {
if (VM_STORE == null) {
VM_STORE = ViewModelStore()
}
return VM_STORE!!
}
// TODO - This is not injecting the single instance of CartViewModel...
private val cartViewModel = getViewModel(
owner = this,
clazz = CartViewModel::class.java,
qualifier = named("CartViewModel")
)
Any thoughts on how I can retrieve the single VM instance which my activity/fragment are already using?arnaud.giuliani
11/08/2023, 9:09 AMmasteramyx
11/08/2023, 2:08 PMCartViewModel
exposes a flow that is observed by the primary screen(activity/fragment). The data exposed in that flow also needs to be displayed on the secondary display.
I tried injecting a factory instance of CartViewModel
in the Presentation class but a new flow is created since its a new VM instance and the flow from the VM instance which the fragment is using is never observed.
I have an application scoped VM that handles changing the secondary display, it seems more appropriate to pass this job onto that VM.Ankit Kumar
11/09/2023, 11:19 AMAnkit Kumar
11/09/2023, 11:20 AMarnaud.giuliani
11/09/2023, 2:36 PM