WEBVTT

00:00.120 --> 00:06.960
If you remember, we've added Hill Dependency Injection framework to our application.

00:07.000 --> 00:11.080
We need to use it for making dependency injection.

00:11.280 --> 00:15.080
So here inside this package, create a new Kotlin class.

00:15.080 --> 00:18.080
Name it as detection app.

00:18.120 --> 00:25.880
This class extends from application and if you want to learn more about dependency injection, please

00:25.880 --> 00:30.560
go back to the Dependency Injection section in the course.

00:30.600 --> 00:37.760
We need to annotate it with hilt Android application and use the Oncreate function.

00:37.880 --> 00:45.360
Set the log I for example detection application or tag and Oncreate.

00:45.400 --> 00:47.120
This is just for logging.

00:47.440 --> 00:53.080
Okay, but we need to create this class in order to specify it in the manifest.

00:53.080 --> 00:54.760
So back to the manifest.

00:54.760 --> 01:03.380
And here we don't specify the main activity or sorry the the name here, the name tag in the application

01:03.380 --> 01:06.940
name tag and set the detection app.

01:06.980 --> 01:07.620
Always.

01:07.620 --> 01:14.700
When you use Health Dependency injection, you should create a class that extends from application and

01:14.740 --> 01:17.140
annotate it with hilt Android app.

01:17.180 --> 01:26.540
The third step is to add the name attribute inside the application and specify this class.

01:26.540 --> 01:34.700
So after creating this class and specify it in the Android manifest, we need to make sure inside the

01:34.940 --> 01:39.780
ViewModel we use the hilt ViewModel annotation.

01:39.780 --> 01:44.420
So here the hilt ViewModel is set it used.

01:44.460 --> 01:49.820
The third thing we need to use for hilt is the main activity.

01:49.820 --> 01:57.580
So the detection app, the first part, the second part, the ViewModel, and the third part is the

01:57.580 --> 01:59.260
main activity.

01:59.420 --> 02:01.730
So inside this main activity.

02:01.730 --> 02:06.290
Annotate it with Android entry point annotation.

02:06.410 --> 02:12.490
So here we should specify it because it contains the Composables and the UI screen.

02:12.490 --> 02:15.650
And we are using Android with hilt.

02:15.690 --> 02:27.570
Now let me specify private val detection view model by view models and specify the detection view model.

02:27.730 --> 02:34.810
This line declares a ViewModel property using Kotlin's property delegation.

02:34.810 --> 02:40.810
So private detection ViewModel by Viewmodels.

02:40.850 --> 02:42.530
The detection ViewModel.

02:42.650 --> 02:51.250
The detection ViewModel is the name of the ViewModel property by is the Kotlin delegation keyword delegates

02:51.250 --> 03:03.030
property access to the delegate object, and the view models that delegate that creates or manages the

03:03.030 --> 03:04.750
ViewModel instance.

03:05.030 --> 03:06.750
This is very important.

03:06.750 --> 03:08.190
So this.

03:08.230 --> 03:13.230
This is the delegate that creates or manage the ViewModel instance.

03:13.550 --> 03:20.630
The ViewModel is created only when the first access, not when the class is initialized and the lifecycle

03:20.630 --> 03:21.150
management.

03:21.150 --> 03:25.910
The ViewModel survives configuration changes screen rotations.

03:25.950 --> 03:31.070
The same instance is reused across the fragments lifecycle, and we.

03:31.350 --> 03:40.830
We can also save the configuration changes across our compositions and make sure the that this delegation

03:40.830 --> 03:43.270
pattern is recommended.

03:43.270 --> 03:47.270
Way to access Viewmodels in Android development.

03:47.270 --> 03:52.590
Providing clean syntax with proper lifecycle management.

03:52.710 --> 03:57.910
Then inside the Oncreate remove the set content.

03:57.910 --> 04:02.060
So here object detection theme and remove it.

04:02.060 --> 04:06.340
Also remove the greeting composable and the preview okay.

04:06.540 --> 04:10.460
And inside this set content camera executor.

04:10.500 --> 04:12.780
We can create this variable here.

04:12.780 --> 04:19.540
Or we can make it like um like a global function like this detection ViewModel.

04:19.540 --> 04:25.820
So here private late initialize var camera executor.

04:25.940 --> 04:30.620
It's of type executor service alt plus enter to import the class.

04:30.660 --> 04:36.700
The executor service is a Java interface for managing thread execution.

04:36.700 --> 04:39.740
And this is used for camera operations.

04:39.740 --> 04:48.260
But why we use the late initialize fragments and activities are created by the system, so you can't

04:48.260 --> 04:56.020
pass parameters to the constructor you want to initialize when the component is ready on Oncreate or

04:56.020 --> 05:03.360
on view created, or any other composable, but here not when the class is created.

05:03.360 --> 05:07.600
So inside the set content we need to use this camera executor.

05:07.720 --> 05:13.760
Equals to executors dot new single thread executor.

05:13.800 --> 05:15.760
Alt plus enter to import it.

05:15.760 --> 05:17.280
And here we go.

05:17.320 --> 05:21.240
We created one single thread.

05:21.240 --> 05:25.440
Processes all camera tasks sequentially.

05:25.600 --> 05:27.600
This is a one single thread.

05:27.600 --> 05:34.240
Processes all camera tasks sequentially and good for simple camera operations.

05:34.240 --> 05:41.680
If you have multiple threads for parallel execution and processing, you can use new fixed new fixed

05:41.720 --> 05:44.760
thread pool, good for heavy image processing.

05:44.760 --> 05:52.080
But since our application is very simple and just for detecting objects, we don't make any process,

05:52.080 --> 05:55.240
any image process, any complex tasks.

05:55.280 --> 05:58.520
We use new single thread executor.

05:58.520 --> 06:03.910
So this is a pattern, a good pattern, no need for null checks everywhere.

06:04.150 --> 06:11.070
Initialize it at the right time in the life cycle and camera operations don't block the UI thread.

06:11.110 --> 06:13.150
This is the most important thing.

06:13.310 --> 06:16.870
The camera operations don't block the UI thread.

06:17.030 --> 06:19.790
Proper cleanup prevents memory leak.

06:20.030 --> 06:28.510
This pattern is essential for camera operations in Android to ensure smooth performance and proper resource

06:28.510 --> 06:29.510
management.

06:29.510 --> 06:38.150
And since it's a thread, we need to ensure that when when we destroy this function, when we exit this

06:38.190 --> 06:45.550
Oncreate function, we need to shut down this executor for this purpose.

06:45.790 --> 06:53.030
Override Ondestroy function and set the camera executor dot shut down.
