Giter Site home page Giter Site logo

lorenzofelletti / permissions Goto Github PK

View Code? Open in Web Editor NEW
15.0 1.0 1.0 185 KB

Easy declarative permission request management library for Android

License: GNU General Public License v3.0

Kotlin 100.00%
android android-library kotlin kotlin-android kotlin-dsl permission-android permission-manager

permissions's Introduction

Permissions

Easy Permissions Management Library for Android

An easy to use permissions management library written in Kotlin.

Import the Library

The library is deployed on JitPack here. To add it to your project, add to settings.gradle:

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

And to your module level build.gradle:

dependencies {
  ...
  implementation 'com.github.lorenzofelletti:permissions:0.4.2'
}

Usage

To use the library in your project, just:

  • Declare the permissions your app will use in your application's Manifest
    • Example: add to the app AndroidManifest.xml
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  • Create a PermissionManager in your Activity
    • Example: in your MainActivity
      private val permissionManager = PermissionManager(this)
  • Decide a request code for each set of permissions that you will require simultaneously (each request code is a positive integer of your choice, but do not use the same code for different set of permissions)
    • Example: add to your MainActivity
    companion object {
          private const val POSITION_REQUEST_CODE = 1
          private val POSITION_REQUIRED_PERMISSIONS = arrayOf(
              android.Manifest.permission.ACCESS_FINE_LOCATION,
              android.Manifest.permission.ACCESS_COARSE_LOCATION
          )
      }
  • Build the RequestResultsDispatcher
    • Example: in the MainActivity's onCreate function add
    permissionManager.buildRequestResultsDispatcher {
        withRequestCode(POSITION_REQUEST_CODE) {
            checkPermissions(POSITION_REQUIRED_PERMISSIONS)
            doOnGranted {
                Log.d(TAG, "Location permission granted") 
            }
            doOnDenied {
                Log.d(TAG, "Location permission denied")
            }
        }
    }
  • Call permissionManager.checkRequestAndDispatch where you want to check for a set of permissions (and ask them if not granted)
    • Example: in your Activity, where you want to check (and request) permissions add
    permissionManager checkRequestAndDispatch POSITION_REQUEST_CODE
  • Override onRequestPermissionsResult and call PermissionsUtilities.dispatchOnRequestPermissionsResult in it
    • Example: inside your MainActivity
    override fun onRequestPermissionsResult(
          requestCode: Int,
          permissions: Array<out String>,
          grantResults: IntArray
      ) {
          super.onRequestPermissionsResult(requestCode, permissions, grantResults)
          permissionManager.dispatchOnRequestPermissionsResult(requestCode, grantResults)
      }

Contributing

All kinds of contributions are welcome (bug reports, feature requests, pull requests, etc.). Suggestions and improvements on documentation, tests, code quality, translations, etc. are also welcome.

permissions's People

Contributors

lorenzofelletti avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

virendersran01

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.