GithubHelp home page GithubHelp logo

trendingtechnology / powerpermission Goto Github PK

View Code? Open in Web Editor NEW

This project forked from underwindfall/powerpermission

0.0 1.0 0.0 220 KB

PowerPermission makes handling runtime permissions extremely easy.

License: Apache License 2.0

Kotlin 100.00%

powerpermission's Introduction

PowerPermission

jCenter License Android Check CI

English Documentation | 中文文档

Table of Contents

Introducation

PowerPermission is a library to simplify process of demanding RuntimePermission.

You find an example application in this Repo or downloading directly here. Here are some different points compare with other libraries:

  • support asking permissions in AppCompatActivity and Fragment(include ChildFragment)
  • support custom rational view after user refuse permission
  • support ability to choose permissions to display rational views
  • support different interface (RxJava2,RxJava3,Coroutines,LiveData)

How to Download

Basic

implementation "com.qifan.powerpermission:powerpermission:1.0.0"

Other API Support

implementation "com.qifan.powerpermission:powerpermission-rxjava2:1.0.0"
implementation "com.qifan.powerpermission:powerpermission-rxjava3:1.0.0"
implementation "com.qifan.powerpermission:powerpermission-coroutines:1.0.0"
implementation "com.qifan.powerpermission:powerpermission-livedata:1.0.0"

OverView

Package Name Role Usage
powerpermission Basic core package implementation "com.qifan.powerpermission:powerpermission:1.0.0"
powerpermission-rxjava2 Support RxJava2 implementation "com.qifan.powerpermission:powerpermission-rxjava2:1.0.0"
powerpermission-rxjava3 Support RxJava3 implementation "com.qifan.powerpermission:powerpermission-rxjava3:1.0.0"
powerpermission-coroutines Support Kotlin Coroutine implementation "com.qifan.powerpermission:powerpermission-coroutines:1.0.0"
powerpermission-livedata Support Android LiveData implementation "com.qifan.powerpermission:powerpermission-livedata:1.0.0"

How to use

Simple Usage

Singleton

  PowerPermission.init()
            .requestPermissions(
                context = this@ExampleActivity,
                permissions = *arrayOf(
                    Manifest.permission.CAMERA
                )
            ) { permissionResult ->
                when {
                    permissionResult.hasAllGranted() -> {
                        doPermissionAllGrantedWork(permissionResult.granted())
                    }
                    permissionResult.hasRational() -> {
                        doPermissionReasonWork(permissionResult.rational())
                    }
                    permissionResult.hasPermanentDenied() -> {
                        doPermissionPermanentWork(permissionResult.permanentDenied())
                    }
                }
            }

Extension

  • Require in Activity
 askPermissions(
            Manifest.permission.CAMERA,
            Manifest.permission.READ_CALENDAR
        ) { permissionResult ->
            //do whatever you want
        }
  • Require in Fragment
 askPermissions(
            Manifest.permission.CAMERA,
            Manifest.permission.READ_CALENDAR
        ) { permissionResult ->
            //do whatever you want
        }
  • Require in ChildFragment
 askPermissions(
            Manifest.permission.CAMERA,
            Manifest.permission.READ_CALENDAR
        ) { permissionResult ->
            //do whatever you want
        }

RxJava/RxKotlin

Basic Usage

 simpleRequestButton.setOnClickListener {
            askPermissionsRx(Manifest.permission.CAMERA)
}

Use with RxBinding

 rxBindingRequestButton.clicks()
            .throttleFirst(1L, TimeUnit.SECONDS)
            .flatMap {
                askPermissionsRx(Manifest.permission.CAMERA)
            }

Coroutine

    simpleRequestButton.setOnClickListener {
            launch {
                val result = awaitAskPermissions(
                    Manifest.permission.CAMERA,
                    rationaleDelegate = dialogRationaleDelegate
                )
                //do something for this permission result
               // handlePermissionRequest(result)
            }
        }

Livedata

simpleRequestButton.setOnClickListener {
            observeAskPermissions(
                Manifest.permission.CAMERA,
                rationaleDelegate = dialogRationaleDelegate
            ).observe(this, Observer{ result->
               //do something for this permission result
               // handlePermissionRequest(result)
            })
        }

Rationale Interface

Custom View

In PowerPermission it have a interface which called RationaleDelegate, it should be used as bridge to create your proper
delegation class to implement this interface. It provide two basic public declaration functions,

  • displayRationale
//aims to display a view to explain reason to user why request permissions
    fun displayRationale(
        vararg permission: Permission,
        message: String,
        actionCallback: RationaleActionCallback
    )
  • onDismissView
//aims to simply disappear view and do some clean work etc.
 fun onDismissView()

PS: PowerPermission has already provided a class called DialogRationaleDelegate.kt to display a classic dialog view you can take a look at it and find more inspiration.

Choose those permissions are rational

  • RationaleData It's a data class used as choose which permission or permissions to display rationale view. And what kind of message will be displayed in your rational view. example:
RationaleData(
            rationalPermission = Manifest.permission.CAMERA,
            message = message
        )
RationaleData(
            rationalPermissions = listOf(Manifest.permission.CAMERA),
            message = message
        )

License

Copyright (C) 2020 Qifan Yang
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

powerpermission's People

Contributors

underwindfall avatar

Watchers

 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.