Giter VIP home page Giter VIP logo

request-permission-compose's Introduction

Request-Permission-Compose

Table of contents

Introduction

This is an Android Library that's implemented in JetpackCompoe to help you to request a single permission or multiple permissions on runtime.

Screen Recording 2023-03-21 at 4 22 36 PM

Setup

Step1: settings.gradle

   repositories {
        ...
        maven { url 'https://jitpack.io' }
    }

Step2: add the dependency

 dependencies {
     ...
     implementation 'com.github.MshariAlsayari:Request-Permission-Compose:<last-version>'
 }

Full Example

@Composable
fun HomeScreen() {

    val context = LocalContext.current
    var openGallery by remember { mutableStateOf(false) }
    var openCameraAndMic by remember { mutableStateOf(false) }

    val galleryLauncher = rememberLauncherForActivityResult(
        contract = ActivityResultContracts.GetContent(),
        onResult = { uri: Uri? ->
        }
    )


   // Single Permission
    if (openGallery){
     RequestPermissions(
            permissions = listOf(Manifest.permission.READ_EXTERNAL_STORAGE),
            rationalDialogParams = DialogParams(
                title = R.string.galleryRequestPermissionDialogTitle,
                message = R.string.galleryRequestPermissionDialogBody,
                icon = R.drawable.baseline_info_24
            ),
            deniedDialogParams = DialogParams(
                title = R.string.galleryDeniedPermissionDialogTitle,
                message = R.string.galleryDeniedPermissionDialogBody,
                positiveButtonText = R.string.openSettings
            ),
            isGranted = {
                galleryLauncher.launch("image/*")
            },
            onDone = {
                openGallery = false

            }
        )
    }
    
    
 // Multiple Permissions
    if(openCameraAndMic){
       RequestPermissions(
            permissions = listOf(Manifest.permission.CAMERA,Manifest.permission.RECORD_AUDIO),
            rationalDialogParams = DialogParams(
                title = R.string.micAndCameraPermissionDialogTitle,
                message = R.string.micAndCameraPermissionDialogBody,
                icon = R.drawable.baseline_info_24
            ),
            deniedDialogParams = DialogParams(
                title = R.string.micAndCameraDeniedPermissionDialogTitle,
                message = R.string.micAndCameraDeniedPermissionDialogBody,
                positiveButtonText = R.string.openSettings
            ),
            isGranted = {
                Toast.makeText(context, R.string.micAndCameraGranted,Toast.LENGTH_LONG).show()
            },
            onDone = {
                openCameraAndMic = false

            }
        )
    }


    Column(
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center
    ) {

        GalleryButton(onClick = {
            openGallery = true
        })



        CameraAndMic(onClick = {
            openCameraAndMic = true
        })
    }
}

@Composable
fun GalleryButton(onClick:()->Unit = {}) {
    MyButton(text = "Storage", onClick = onClick)
}


@Composable
fun CameraAndMic(onClick:()->Unit = {}) {
    MyButton(text = "Camera and Mic", onClick = onClick)
}


@Composable
fun MyButton(text: String, onClick:()->Unit = {}) {
    Button(onClick = { onClick() }) {
        Text(text = text)
    }
}

request-permission-compose's People

Contributors

msharialsayari avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.