WEBVTT

00:00.080 --> 00:01.080
Welcome back.

00:01.120 --> 00:08.080
In this application we're going to use a simple user interface because the main purpose is to use the

00:08.120 --> 00:16.360
pre-trained model to detect objects for this purpose, create a new package, create call it screens,

00:16.360 --> 00:20.120
and inside this screens create a new Kotlin file.

00:20.320 --> 00:24.480
Name it as camera permission wrapper.

00:24.560 --> 00:34.320
First of all, let's start with the main screen that requests from the user the permission of camera.

00:34.320 --> 00:41.000
And for this, let me add in the manifest the camera permissions.

00:41.160 --> 00:45.000
So here uses permission camera.

00:45.040 --> 00:46.320
It's very simple.

00:46.320 --> 00:54.880
And there is a note that we need to add uses feature as we did in the previous videos.

00:54.880 --> 01:02.560
So here uses feature camera required and set it to false always.

01:02.560 --> 01:09.730
When you use camera in your app, add the uses feature and the user's permission back to the camera

01:09.730 --> 01:12.130
permission wrapper file.

01:12.250 --> 01:19.370
Let me create a new composable and name it the same as the file.

01:19.410 --> 01:30.850
This function takes two parameters the modifier modifier and on granted, which is a composable lambda

01:31.050 --> 01:32.010
expression.

01:32.050 --> 01:34.450
Alt+ enter to import the modifier.

01:34.610 --> 01:39.250
And here let's start building this composable function.

01:39.290 --> 01:41.850
Start with val context.

01:41.890 --> 01:50.650
Get the context from the local context and var has permission by.

01:50.690 --> 01:56.090
Remember start as mutable state of context compat Alt+.

01:56.090 --> 02:06.850
Enter to import it and mutable state of the by operator and get value local context the manifest and

02:06.890 --> 02:07.890
package.

02:07.930 --> 02:16.380
Here I imported the wrong manifest, So make sure you import the manifest of Android okay.

02:16.540 --> 02:21.100
This code creates a reactive permission state in jetpack.

02:21.100 --> 02:30.780
Compose has permission a boolean state variable that tracks camera permission status if the user granted

02:30.780 --> 02:32.940
or doesn't grant it.

02:32.980 --> 02:41.380
Remember, preserve the state across Recompositions and the mutable state of makes the value observable

02:41.380 --> 02:43.660
for compose UI updates.

02:43.820 --> 02:50.660
Context compact self permission checks if the camera permission is granted.

02:50.660 --> 02:57.700
So here let me write this note down and comparing it with the package manager.

02:57.700 --> 03:02.340
Package manager permission granted to get a boolean result.

03:02.340 --> 03:08.540
Okay, so here we are getting this and compare it with the package manager.

03:08.580 --> 03:10.820
Permission granted boolean.

03:10.820 --> 03:14.660
If it is the same so it is granted.

03:14.820 --> 03:22.820
If not, the That permission is not granted initially sets the has permission based on current permission

03:22.820 --> 03:23.500
status.

03:23.540 --> 03:27.580
When permission changes, the UI automatically Recomposes.

03:27.740 --> 03:35.260
Used to conditionally show and hide UI elements based on UI and the permission state.

03:35.300 --> 03:44.940
Then you create another variable called launcher equals to remember launcher for activity result.

03:44.980 --> 03:46.860
Then add the contract.

03:46.860 --> 03:55.220
Pass the contract parameter as activity result context and the second parameter is on result.

03:55.220 --> 03:58.740
So we need to pass two parameters the contract and on result.

03:59.020 --> 04:02.300
The on result is passed like this.

04:02.500 --> 04:07.740
Okay so let me remove this and pass it like this.

04:07.780 --> 04:08.620
It's better.

04:08.820 --> 04:09.660
Close.

04:09.660 --> 04:13.380
This code launches the permission request.

04:13.700 --> 04:21.830
The remember launcher for activity result remembers a launcher across Recompositions and activity result

04:21.830 --> 04:28.310
contracts that request permissions contract for requesting a single permission for.

04:28.550 --> 04:32.590
So this is requesting a single permission.

04:32.750 --> 04:40.670
The second parameter is on results callback that updates the state when permission result is received.

04:40.670 --> 04:49.230
What we need to do is receiving a boolean value and updating the has permission variable that we just

04:49.230 --> 04:50.350
created before.

04:50.550 --> 04:51.030
Okay.

04:51.150 --> 05:00.710
So on uh requesting the single permission and on result, what we need to do go and update the has permission

05:00.710 --> 05:02.790
and set it to granted.

05:02.790 --> 05:08.550
So this is a callback that updates the state when permission result is received okay.

05:08.790 --> 05:10.710
So this is very important.

05:10.750 --> 05:16.350
In this way we handle the permission and we ask the user for the permission.

05:16.350 --> 05:18.430
In the next video we'll continue with the UI.

05:18.670 --> 05:22.110
We're going to create the box and the button.
