I'm transitioning from iOS SWIFT to Android Kotlin & Jetpack, and struggling a little with @composables, I'm not using activity/fragments. I'm creating separate ViewModel classes to hoist state & business logic but I'm struggling with context.
Within the ViewModel's there's a number of functions that require context, example registerReceiver, I'm also using a number of libraries such as MQTT client. From my research I shouldn't be using context in a ViewModel:
How to get Context in Android MVVM ViewModel
So my question is how do create ViewModel that requires context for dependent functions, or is my understanding of context or ViewModel wrong?
I know it's not recommended but for a few features I've created functions in the ViewModel which the composable calls and pass context = LocalContext.current
As an example within the ViewModel I've created the following:
fun registerScannerReceiver(context: Context) {
val filter = IntentFilter(context.getString(R.string.datawedge_intent_filter_action))
filter.addCategory(Intent.CATEGORY_DEFAULT)
context.registerReceiver(reciver, filter)
}
This works but it feels wrong any suggestions, recommendations or example code would be greatly appreciated.