Welcome back.
In this application we're going to use a simple user interface because the main purpose is to use the
pre-trained model to detect objects for this purpose, create a new package, create call it screens,
and inside this screens create a new Kotlin file.
Name it as camera permission wrapper.
First of all, let's start with the main screen that requests from the user the permission of camera.
And for this, let me add in the manifest the camera permissions.
So here uses permission camera.
It's very simple.
And there is a note that we need to add uses feature as we did in the previous videos.
So here uses feature camera required and set it to false always.
When you use camera in your app, add the uses feature and the user's permission back to the camera
permission wrapper file.
Let me create a new composable and name it the same as the file.
This function takes two parameters the modifier modifier and on granted, which is a composable lambda
expression.
Alt+ enter to import the modifier.
And here let's start building this composable function.
Start with val context.
Get the context from the local context and var has permission by.
Remember start as mutable state of context compat Alt+.
Enter to import it and mutable state of the by operator and get value local context the manifest and
package.
Here I imported the wrong manifest, So make sure you import the manifest of Android okay.
This code creates a reactive permission state in jetpack.
Compose has permission a boolean state variable that tracks camera permission status if the user granted
or doesn't grant it.
Remember, preserve the state across Recompositions and the mutable state of makes the value observable
for compose UI updates.
Context compact self permission checks if the camera permission is granted.
So here let me write this note down and comparing it with the package manager.
Package manager permission granted to get a boolean result.
Okay, so here we are getting this and compare it with the package manager.
Permission granted boolean.
If it is the same so it is granted.
If not, the That permission is not granted initially sets the has permission based on current permission
status.
When permission changes, the UI automatically Recomposes.
Used to conditionally show and hide UI elements based on UI and the permission state.
Then you create another variable called launcher equals to remember launcher for activity result.
Then add the contract.
Pass the contract parameter as activity result context and the second parameter is on result.
So we need to pass two parameters the contract and on result.
The on result is passed like this.
Okay so let me remove this and pass it like this.
It's better.
Close.
This code launches the permission request.
The remember launcher for activity result remembers a launcher across Recompositions and activity result
contracts that request permissions contract for requesting a single permission for.
So this is requesting a single permission.
The second parameter is on results callback that updates the state when permission result is received.
What we need to do is receiving a boolean value and updating the has permission variable that we just
created before.
Okay.
So on uh requesting the single permission and on result, what we need to do go and update the has permission
and set it to granted.
So this is a callback that updates the state when permission result is received okay.
So this is very important.
In this way we handle the permission and we ask the user for the permission.
In the next video we'll continue with the UI.
We're going to create the box and the button.