WEBVTT

00:00.080 --> 00:02.320
Welcome back up in the main activity.

00:02.320 --> 00:09.240
And let's call the screen detection screen and pass the ViewModel to it.

00:09.240 --> 00:18.240
So here inside the main activity set content we need to call the detection screen object detection screen

00:18.280 --> 00:18.560
okay.

00:18.600 --> 00:26.080
Because this is the composable named as object detection screen, we need to pass uh, one optional

00:26.080 --> 00:31.080
and a second mandatory parameter which is the ViewModel.

00:31.080 --> 00:39.800
So here I need to pass one mandatory ViewModel parameter to create the ViewModel in Jetpack Compose.

00:39.840 --> 00:49.840
The best way is to specify a hilt aware method for creating the hilt ViewModel, and this is done by

00:49.840 --> 00:58.000
using the hilt ViewModel that it is from hilt dot navigation dot compose package.

00:58.240 --> 01:00.280
Let me go to the Build.gradle.

01:00.320 --> 01:01.800
This is the safest.

01:02.160 --> 01:11.840
This is the safest way to create a ViewModel in Jetpack Compose using hilt, go to Build.gradle file

01:12.020 --> 01:18.780
and here use the implementation of Android exhale dot navigation.

01:18.780 --> 01:24.740
Compose sync the project and go back to the main activity Alt+.

01:24.740 --> 01:27.860
Enter to import this health ViewModel.

01:28.020 --> 01:28.460
Again.

01:28.460 --> 01:35.460
Guys, this health ViewModel is from Compose Navigation Health Android X okay, now we need to pass

01:35.460 --> 01:36.540
this ViewModel.

01:36.540 --> 01:37.940
So pass it here.

01:38.140 --> 01:47.100
This is not the modifier, this is the view model okay, move the those outside the set content because

01:47.100 --> 01:48.580
it's a single thread.

01:48.580 --> 01:49.780
And here we go.

01:49.820 --> 01:56.340
This is how we create the ViewModel and pass it to the object detection screen.

01:56.340 --> 01:59.940
Let me run actually we have a crash here.

02:00.220 --> 02:01.180
We have an error.

02:01.180 --> 02:02.100
Scroll up.

02:02.140 --> 02:08.580
And here the exception can't create an instance of ViewModel.

02:08.580 --> 02:14.380
So uh, the main problem here is the dependency injection of the ViewModel.

02:14.380 --> 02:16.820
So we have inside the ViewModel.

02:16.820 --> 02:18.500
Let me go to the ViewModel.

02:18.500 --> 02:22.420
We have the hilt ViewModel and notation here.

02:22.580 --> 02:29.280
And here it's not favorable to use the context directly like this.

02:29.480 --> 02:35.960
We need to replace it with, uh, application or other dependencies.

02:36.040 --> 02:43.400
So this is the main problem of our of our application because we are injecting the application context

02:43.400 --> 02:44.240
in the ViewModel.

02:44.240 --> 02:48.280
And this is not allowed in Viewmodels.

02:48.280 --> 02:53.280
We use the context to get the, uh, to open the assets.

02:53.280 --> 02:57.680
So instead of it we can use the application.

02:58.000 --> 02:59.880
So we can write like this.

02:59.880 --> 03:10.960
The application import the class and use Android View model and pass the application as its parameter.

03:10.960 --> 03:13.360
So here it's working fine.

03:13.360 --> 03:22.240
And instead of using the context we're gonna use the get application and access the assets dot open

03:22.240 --> 03:22.880
folder.

03:23.120 --> 03:27.200
Let me scroll down here to the context here.

03:27.240 --> 03:30.440
Paste it and open the file here.

03:30.440 --> 03:31.760
Not open the folder.

03:31.800 --> 03:34.000
It's open file okay.

03:34.420 --> 03:38.260
So again guys we get rid of the context.

03:38.300 --> 03:44.020
We replace it with get application and get application from this side.

03:44.020 --> 03:50.620
And we replace the context in the constructor with the application.

03:50.620 --> 03:58.220
So always use this pattern when you have Android ViewModel and you want to access the application folders

03:58.380 --> 04:03.900
in in Jetpack Compose, let me run the application again and here we go.

04:03.940 --> 04:06.460
This is our lovely application.

04:06.620 --> 04:11.900
Let me grant the the camera permissions while using the app.

04:12.180 --> 04:14.500
And the camera started.

04:14.740 --> 04:16.980
And congratulations guys.

04:17.260 --> 04:21.980
This is the this is our application.

04:22.140 --> 04:25.060
We are detecting screens.

04:25.380 --> 04:27.460
We are detecting keyboard.

04:27.620 --> 04:30.020
We are detecting a mouse.

04:30.060 --> 04:31.700
Let me see or book.

04:32.020 --> 04:34.980
It's not testing it directly.

04:34.980 --> 04:35.740
Book.

04:35.780 --> 04:37.340
Let me see the pencil.

04:37.540 --> 04:38.140
You see.

04:38.180 --> 04:38.700
Oops.

04:38.740 --> 04:40.220
It's not the refrigerator.
