miggs597
01/10/2024, 6:22 PMviewModel { }
? Currently I have to manually create my BarcodeDataReceiver
object manually because I need to capture two of my viewModels in a lambda that's used for updating the UI.
private val storeItemViewModel: StoreItemViewModel by viewModel()
private val scanningViewModel: ScanningViewModel by viewModel()
private val bScanner: BarcodeScanner by inject()
private val barcodeDataReceiver =
BarcodeDataReceiver {
storeItemViewModel.addItem(
it,
LoadingResource(scanningViewModel::toggle)
)
}
Ideally I would like to simply inject my BarcodeDataReceiver
, but with viewModel {}
being a factory I can't get the single instance that I need.curioustechizen
01/11/2024, 6:01 AMviewModel
is by definition a factory
(but tied to a specific scope). If you call by viewModel()
multiple times in the same scope, you'll get the same instance back.
(Here "scope" depends on how you're using ViewModels, it differs for example if you're injecting it into a Fragment versus if you're injecting it into an AndroidX navigation composable destination).curioustechizen
01/11/2024, 6:02 AMcurioustechizen
01/11/2024, 6:04 AMPedro Alberto
01/12/2024, 11:54 AMarnaud.giuliani
01/12/2024, 5:58 PMmiggs597
01/22/2024, 9:42 PMbarcodeDataReceiver
is created in my android application's main activity. But I was hoping to create it in my koin app module.