ProductPromotion
Logo

Kotlin

made by https://0x3d.site

GitHub - deva666/Peko: Android Library for requesting Permissions with Kotlin Flow
Android Library for requesting Permissions with Kotlin Flow - deva666/Peko
Visit Site

GitHub - deva666/Peko: Android Library for requesting Permissions with Kotlin Flow

GitHub - deva666/Peko: Android Library for requesting Permissions with Kotlin Flow

PEKO

PErmissions with KOtlin

Quality Gate Status Android Arsenal License

Android Permissions with Kotlin Coroutines and Flow API

No more callbacks, listeners or verbose code for requesting Android permissions.
Get Permission Request Result asynchronously with one function call.
Context or Activity not needed for requests, request permissions from your View Model or Presenter.
Built with Kotlin Coroutines and Flow .


Thanks to the supporters

Supported by JetBrains Open Source

Sponsored by CloudBit

Installation

Hosted on Maven Central

implementation 'com.markodevcic:peko:3.0.5'

Example

First initialize the PermissionRequester with Application Context. This enables all requests to be made without a Context or Activity.
If you pass an Activity as Context, IllegalStateException is raised.

PermissionRequester.initialize(applicationContext)

Get the instance of PermissionRequester interface.

val requester = PermissionRequester.instance()

Request one or more permissions. For each permission receive PermissionResult as async Flow stream of data.

launch {
    requester.request(
            Manifest.permission.CAMERA,
            Manifest.permission.READ_CONTACTS
    ).collect { p ->
        when (p) {
            is PermissionResult.Granted -> print("${p.permission} granted") // nice, proceed 
            is PermissionResult.Denied -> print("${p.permission} denied") // denied, not interested in reason
            is PermissionResult.Denied.NeedsRationale -> print("${p.permission} needs rationale") // show rationale
            is PermissionResult.Denied.DeniedPermanently -> print("${p.permission} denied for good") // no go
            is PermissionResult.Cancelled -> print("request cancelled") // op canceled, repeat the request
        }
    }
}

Need to check only if permissions are granted? Let's skip the horrible Android API. No coroutine required.

val granted: Boolean = requester.areGranted(Manifest.permission.CAMERA, Manifest.permission.READ_CONTACTS)

Or are any of the requested granted?

val anyGranted: Boolean = requester.anyGranted(Manifest.permission.CAMERA, Manifest.permission.READ_CONTACTS)

Why Flows?

Requesting multiple permissions in a single request represents a data stream of PermissionsResult objects. Flow fits here perfectly. Each permission requested is either granted or denied, with Flow we can operate on each emitted result item and inspect it individually, that is check if it is Granted, Denied or Needs Rationale. Flows are async and require a coroutine to collect so this is not a huge update from Peko version 2. Also, they are now part of Kotlin Coroutines library, so no new dependencies are added.

Don't want to use Flow? No problem, suspendable extension functions that collect for you are there.

// just check all granted
launch {
    val allGranted: Boolean = requester.request(Manifest.permission.CAMERA)
            .allGranted()
}

// give me just granted permissions
launch {
    val granted: Collection<PermissionResult> =
            requester.request(Manifest.permission.CAMERA)
                    .grantedPermissions()
}


// give me all denied permissions, whatever the reason
launch {
    val denied: Collection<PermissionResult> =
            requester.request(Manifest.permission.CAMERA)
                    .deniedPermissions()

// these can be then separated to see needs rationale or denied permanently permissions
    val needsRationale = denied.filterIsInstance<PermissionResult.Denied.NeedsRationale>()
    val deniedPermanently = denied.filterIsInstance<PermissionResult.Denied.DeniedPermanently>()
}

// give me needs rationale permissions
launch {
    val needsRationale: Collection<PermissionResult> =
            requester.request(Manifest.permission.CAMERA)
                    .needsRationalePermissions()
}

// give me needs denied permanently permissions
launch {
    val deniedPermanently: Collection<PermissionResult> =
            requester.request(Manifest.permission.CAMERA)
                    .deniedPermanently()
}

Testing

Using permission requests as part of your business logic and want to run your unit tests on JVM? Perfect, PermissionRequester is an interface which can be easily mocked in your unit tests. It does not require a Context or Activity for any methods. Only a one time registration of Application Context needs to be done during app startup with PermissionRequester.initialize method.

Screen rotations

Library supports screen rotations. The only requirement is to preserve the instance of PermissionRequester during device orientation change. How to do this is entirely up to a developer. Easiest way is to use PermissionRequester with lifecycle aware Jetpack ViewModel which does this automatically.

What is new

Peko Version 3 is now released. Peko now uses coroutine Flow instead of suspend function for returning PermissionResult. Support for LiveData is removed. Flow can easily be adapted to work with LiveData.

Breaking changes from Peko Version 2

  • PermissionResult now has a single String permission as property.
  • Peko singleton is removed. PermissionRequester interface is now its replacement.
  • Extension functions for Fragment and Activity are removed.
  • PermissionLiveData class removed

Peko Version 2.0 uses vanilla Kotlin coroutines, and is here.

License

Copyright 2024 Marko Devcic

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.

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory