If you remember, we've added Hill Dependency Injection framework to our application.
We need to use it for making dependency injection.
So here inside this package, create a new Kotlin class.
Name it as detection app.
This class extends from application and if you want to learn more about dependency injection, please
go back to the Dependency Injection section in the course.
We need to annotate it with hilt Android application and use the Oncreate function.
Set the log I for example detection application or tag and Oncreate.
This is just for logging.
Okay, but we need to create this class in order to specify it in the manifest.
So back to the manifest.
And here we don't specify the main activity or sorry the the name here, the name tag in the application
name tag and set the detection app.
Always.
When you use Health Dependency injection, you should create a class that extends from application and
annotate it with hilt Android app.
The third step is to add the name attribute inside the application and specify this class.
So after creating this class and specify it in the Android manifest, we need to make sure inside the
ViewModel we use the hilt ViewModel annotation.
So here the hilt ViewModel is set it used.
The third thing we need to use for hilt is the main activity.
So the detection app, the first part, the second part, the ViewModel, and the third part is the
main activity.
So inside this main activity.
Annotate it with Android entry point annotation.
So here we should specify it because it contains the Composables and the UI screen.
And we are using Android with hilt.
Now let me specify private val detection view model by view models and specify the detection view model.
This line declares a ViewModel property using Kotlin's property delegation.
So private detection ViewModel by Viewmodels.
The detection ViewModel.
The detection ViewModel is the name of the ViewModel property by is the Kotlin delegation keyword delegates
property access to the delegate object, and the view models that delegate that creates or manages the
ViewModel instance.
This is very important.
So this.
This is the delegate that creates or manage the ViewModel instance.
The ViewModel is created only when the first access, not when the class is initialized and the lifecycle
management.
The ViewModel survives configuration changes screen rotations.
The same instance is reused across the fragments lifecycle, and we.
We can also save the configuration changes across our compositions and make sure the that this delegation
pattern is recommended.
Way to access Viewmodels in Android development.
Providing clean syntax with proper lifecycle management.
Then inside the Oncreate remove the set content.
So here object detection theme and remove it.
Also remove the greeting composable and the preview okay.
And inside this set content camera executor.
We can create this variable here.
Or we can make it like um like a global function like this detection ViewModel.
So here private late initialize var camera executor.
It's of type executor service alt plus enter to import the class.
The executor service is a Java interface for managing thread execution.
And this is used for camera operations.
But why we use the late initialize fragments and activities are created by the system, so you can't
pass parameters to the constructor you want to initialize when the component is ready on Oncreate or
on view created, or any other composable, but here not when the class is created.
So inside the set content we need to use this camera executor.
Equals to executors dot new single thread executor.
Alt plus enter to import it.
And here we go.
We created one single thread.
Processes all camera tasks sequentially.
This is a one single thread.
Processes all camera tasks sequentially and good for simple camera operations.
If you have multiple threads for parallel execution and processing, you can use new fixed new fixed
thread pool, good for heavy image processing.
But since our application is very simple and just for detecting objects, we don't make any process,
any image process, any complex tasks.
We use new single thread executor.
So this is a pattern, a good pattern, no need for null checks everywhere.
Initialize it at the right time in the life cycle and camera operations don't block the UI thread.
This is the most important thing.
The camera operations don't block the UI thread.
Proper cleanup prevents memory leak.
This pattern is essential for camera operations in Android to ensure smooth performance and proper resource
management.
And since it's a thread, we need to ensure that when when we destroy this function, when we exit this
Oncreate function, we need to shut down this executor for this purpose.
Override Ondestroy function and set the camera executor dot shut down.