Jetpack Compose Permissions Made Easy!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so Michael France welcome back to the channel always Eugene shafferi in this video we are going to see how you can request permissions in Android application when you are using jetpack compose let's get started so here as always I have simple jetpack compose application here we used previously so what we are going to do we are going to show two ways on how to request permissions one is the normal way with just simple contracts and the other one with a company's Library let's get started so first of all let's have this on click here let's change that and that's for example requests the camera very much camera information and first of course you add it to the Manifest so here I have manifest one and I will add permission for that so it will be permission camera I think no to operation it's uses permission this one name which is camera for example that's the first thing we need to do of course and then you can request it now the first way as I told you we used launchers so the way to use it in compose is just to remember activity lash so the way to do it is simply to remember remember launcher for activity result this one and here we give it this contract the contract will be request permission right this request permission come from activity result contract which means if you do a activity result contract and here you will see all of them you will see request permission there is multiple permissions if you have many in one flow or you can do that and he of course we did an object like that and here we have the result here it will be simple Boolean right so here it will be like I don't know you can just swap some text in here or something first of all let's do that let's do it following way for is camera permission granted it will be by remembering some mutable States remember so we will import that remembering function and the Getters and Setters that's one thing and here let's yeah let's add this one it is true or false is granted like that and when we get the resulting here if it is it so here with this granted we will simply do or yeah maybe we can assign it directly well I want to log something here is granted and here if it is the case what we will simply do we will change this camera to this grant or to which will be true but here for the first time it should be always false we need to check that we need to check if the permission is provided and based on that you will set the initial value well first of all we will do that if there is camera permission if it is not right if it is not granted what we will do we will use what here I should store it is some variable launcher like that and use that launcher to launch the input string will be just the camera string what we can do here simply is just use the following we use manifest and use this Android dot permission DOT camera okay it should be just simple string and too much so if it is not granted we will lash it and if it is the case we'll update that this will be like a compose thing it will observe it so it will change it will change basically this one well you can use it to change this one or to completely Swap this screen to another screen that would work fine yeah what is happening here because conditions yeah this I want to change this into something else but first let's run the application so here is our app which camera permission Falls if we click on it it will show us a lot of room to record Big Challenge video I will say while using this app it will become true okay now if I launched again here's a problem it will be forced the first time and if we click on it well guess what it will give me that dialogue again but we already requested that permission and we have grabbed it so we need to change this initial value to something else so for that you are going to create check camera permission or check permission based on some permission based on some stuff like our function here that will tell us a bully check permission for yeah just create this function this will return Boolean right so here it is a function from the context compact I think yeah which called check separation you give it this all text and permission we want if the permission should pass it from here or let's write it here okay it will be permission and we should import that or create parameter for this function it will be strength refactor it and this this function will give us an it so this end is like for internal purposes not internal purposes you can check it you can't return if this one from the package manager right we can check for permission exactly right it s value is as you can see it will be just a simple end if value is granted it will be true otherwise to be false we can refactor that to use single expression body like that you move that this is our function you can go furthermore and Report static this one and you have this function here so we need to pass this operation would be this one so we need to refactor it to the camera permission and we can use it up in here that would work fine and then we can pass this one here since you are passing it in two places I know I'm just passing one value so let's be so here in the next time if I run the app again now the camera is already there so here as you can see I did nothing it is just basically true here we are just decomposing so you can go to another screen do something else display another composable any anything you want so this is perfectly fine if you want like if you want to revoke the permission I suggest using I use where is it ADB idea so I want to revoke permission icon rework permission I distort I will do that and the app here will be false as you can see as you can see I did nothing I just used that plugin it is great plugin you can type the command yourself but this is fine so here I will request it again I will be this one it will be true I can go outside the app and start it again and you will see it will be simply true so this is the first way you can use this activity result contract with simple activities of contract with remembering the launcher and you can drag the machine you can use this one or you can use multiple permission and it will work in the same way now here I don't think you need on this one the result here yeah let me just get back here the result here is a map between the string and booleans so the map will be let's rename it to a map Boolean map anyways it's just a simple name come on so here if you want to check if all the permissions like all the permissions are granted here for the map you can Loop over the map and check if all the booleans are true you can do that with well here the values are simply booleans I can reduce them to something like accumulator and B okay which is the bully I'm sure this collection wouldn't be empty since I'm passing because if it's empty it will throw an exceptional I think yeah to throw an exception yeah and support the exception right so that way you can use default but I'm pretty sure that I'm I will be checking what permission or two and this value will be is Grant you can use this type of function and it will work fine okay so yeah you can use that and here simply for the launcher I think yeah exactly you need to pass list off I think it's a list of let me check okay so it will be an array of like that and you can pass the different permissions and it would work just fine that's the first thing you are going to use now the accompanies library in order to check like there is an experimental feature on requesting permissions let's check it first of course we have to add a company's Library thanks for the permissions you go to dot Creator file here so here's a dependency we need for the a company's library for the permissions it is syncing let's wait for it and let's see how we can use it so the basically the way to use it well here as you can see we are remembering some State for the permission this is done out of the box by the a company's Library what you need to do is to Simply remember permission State and we need to pass permission state with all we need to pass permission and it will be this one and here yeah exactly it will be like that and a company see exactly it's an experimental API so we will do that here yeah okay let me just align that or let's remove that so here it will be let's say camera permission State well a lot of stuff won't work here so here for the camera permission State what we need to do we need to know if it is granted or not from directly from here you can check it well here you can delete that it's not needed anymore this can be yeah it could be a bar like that and then what you can do is simply check if the camera permission State guess what you can check the status and here is Grant now I want to do the negation here and if it is not I can use that camera State here I'm going just to launch the permission request as you can see here if you do the control Q check documentation request permission user you should always be triggered from non-composable from like side effect or a non-composable callback here this is a non-composable callback because you are on the on the click I can remove that basically here I can do dot here and status in order to check if it is granted or not and it will be exactly the same now I need just to restart the application with this one so I'll go to ADB and I should revoke and permission I restart the application yeah let me just combine it again so here is our application now and let's do camera permission allow it it will be true if you exit the app and then run it again it will be simply true here because we already like that so this will simplify the code much much better just remember that this one need to be need to be run from non-composable scope like here this thing if you do it here that's just request in the beginning and let's run it it will run and then it will crash if you go in here you will see the following here let me just wrap that I have many many stuff in here so here if you get if you check the error you will see the following error okay illegal activity activity results cannot be known like this illegal safe accession they were talking about so here if you want to use it from a composable part so what you need to do is following use launch effect like that yeah with the unit key for example and you simply close that block and you streaming love that like the following and move it here that would work fine let me just run the application again and like revoke permission to study app so it will be false at the beginning as you can see we did nothing we didn't click anything because that would run at the beginning so it will be true at the beginning so this is the way on how to use a company's library to like get permissions remember them in a composable fashion and launch them as I said if you are from callback here you can use it easily it's not a composable callback otherwise use them with some side effect manner like judge backup post provide a lot of those side effects so that's it for this video thanks a lot for watching this video to the end don't forget to subscribe to the channel and see in the next videos
Info
Channel: Charfaoui Younes
Views: 3,294
Rating: undefined out of 5
Keywords: mobile development for beginners, mobile development roadmap, mobile development vs web development, Jetpack Compose, Android development, Permissions, App development, Tutorial., UI design, Android Studio, Kotlin, Mobile development, Best practices
Id: KTDvsoP6g8Q
Channel Id: undefined
Length: 12min 12sec (732 seconds)
Published: Tue May 09 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.