Welcome back up in the main activity.
And let's call the screen detection screen and pass the ViewModel to it.
So here inside the main activity set content we need to call the detection screen object detection screen
okay.
Because this is the composable named as object detection screen, we need to pass uh, one optional
and a second mandatory parameter which is the ViewModel.
So here I need to pass one mandatory ViewModel parameter to create the ViewModel in Jetpack Compose.
The best way is to specify a hilt aware method for creating the hilt ViewModel, and this is done by
using the hilt ViewModel that it is from hilt dot navigation dot compose package.
Let me go to the Build.gradle.
This is the safest.
This is the safest way to create a ViewModel in Jetpack Compose using hilt, go to Build.gradle file
and here use the implementation of Android exhale dot navigation.
Compose sync the project and go back to the main activity Alt+.
Enter to import this health ViewModel.
Again.
Guys, this health ViewModel is from Compose Navigation Health Android X okay, now we need to pass
this ViewModel.
So pass it here.
This is not the modifier, this is the view model okay, move the those outside the set content because
it's a single thread.
And here we go.
This is how we create the ViewModel and pass it to the object detection screen.
Let me run actually we have a crash here.
We have an error.
Scroll up.
And here the exception can't create an instance of ViewModel.
So uh, the main problem here is the dependency injection of the ViewModel.
So we have inside the ViewModel.
Let me go to the ViewModel.
We have the hilt ViewModel and notation here.
And here it's not favorable to use the context directly like this.
We need to replace it with, uh, application or other dependencies.
So this is the main problem of our of our application because we are injecting the application context
in the ViewModel.
And this is not allowed in Viewmodels.
We use the context to get the, uh, to open the assets.
So instead of it we can use the application.
So we can write like this.
The application import the class and use Android View model and pass the application as its parameter.
So here it's working fine.
And instead of using the context we're gonna use the get application and access the assets dot open
folder.
Let me scroll down here to the context here.
Paste it and open the file here.
Not open the folder.
It's open file okay.
So again guys we get rid of the context.
We replace it with get application and get application from this side.
And we replace the context in the constructor with the application.
So always use this pattern when you have Android ViewModel and you want to access the application folders
in in Jetpack Compose, let me run the application again and here we go.
This is our lovely application.
Let me grant the the camera permissions while using the app.
And the camera started.
And congratulations guys.
This is the this is our application.
We are detecting screens.
We are detecting keyboard.
We are detecting a mouse.
Let me see or book.
It's not testing it directly.
Book.
Let me see the pencil.
You see.
Oops.
It's not the refrigerator.