Skip to content

Commit

Permalink
Merge pull request #6 from marcelpinto/multiple-permission-request
Browse files Browse the repository at this point in the history
Multiple permission request
  • Loading branch information
marcelpinto authored Mar 14, 2021
2 parents 613c15c + 4ca4f4a commit 21ad23b
Show file tree
Hide file tree
Showing 52 changed files with 1,322 additions and 112 deletions.
6 changes: 6 additions & 0 deletions .idea/copyright/Permissions_ktx_OSS.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/copyright/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 26 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
# permissions-ktx [ ![Download](https://api.bintray.com/packages/skimarxall/maven/permissions-ktx/images/download.svg?version=0.6) ](https://bintray.com/skimarxall/maven/permissions-ktx/0.6/link)
# permissions-ktx [ ![Download](https://api.bintray.com/packages/skimarxall/maven/permissions-ktx/images/download.svg?version=0.7) ](https://bintray.com/skimarxall/maven/permissions-ktx/0.7/link)

Kotlin Lightweight Android permissions library that follows the permission request principles
and it's Jetpack Compose friendly.

Learn more about best practices at
[https://developer.android.com/guide/topics/permissions/overview](https://developer.android.com/guide/topics/permissions/overview)

```
Disclaimer: this is an experimental project, the API is constantly changing, use at your own risk.
```
> **Disclaimer:** this is an experimental project, the API is constantly changing, use at your own risk.
# Overview

Expand Down Expand Up @@ -89,7 +87,7 @@ class MainFragment : Fragment() {
}
}
```
This creates a [PermissionRequest](lib/src/main/java/dev/marcelpinto/permissionktx/PermissionLauncher.kt)
This creates a [PermissionRequest](lib/src/main/java/dev/marcelpinto/permissionktx/PermissionResultLauncher.kt)
instance that can be used to launch the permission request flow.

## Launch Permission Request
Expand All @@ -102,11 +100,11 @@ This is the desired way to launch since it enforces the permission recommendatio

1. Checking if the permission was already granted --> onAlreadyGranted
2. Then if further explanation is required --> onRequireRational
3. Otherwise launching the permission request --> onRequirePermission
3. Otherwise launching the permission request --> onRequirePermissions

```kotlin
locationPermissionRequest.safeLaunch(
onRequirePermission = {
onRequirePermissions = {
// Optional:update your UI if needed and return true to launch
// the permission request
true
Expand Down Expand Up @@ -138,6 +136,27 @@ by the Jetpack Activity/Fragment library can still be used.
locationPermissionRequest.launch()
```

## Multiple permissions launch

The library support launching multiple permissions at the same time, although this is only encourage
for specific cases, for example, a videochat (CAMERA and MIC). Otherwise is always better to request
only the necessary single permission for each use case, instead of requesting all at once.

To launch multiple permissions simply pass an array following the same mechanism explained above:

```kotlin
locationPermissionsRequest = registerForMultiplePermissionResult(
arrayOf(
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION
)
) { resultMap ->
// a map of Permission and the result as boolean.
}
```

> Check the [MultipleActivity sample](app/src/main/java/dev/marcelpinto/permissionktx/multiple/MultipleActivity.kt).
## Observe Permission Status

The library adds an observability pattern to the current Android Permissions API
Expand Down
23 changes: 19 additions & 4 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
/*
* Copyright 2020 Marcel Pinto Biescas
*
* 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.
*/

import org.jetbrains.kotlin.config.KotlinCompilerVersion

plugins {
id("com.android.application")
kotlin("android")
}

val composeVersion = "1.0.0-beta01"
val composeVersion = "1.0.0-beta02"

android {
compileSdkVersion(30)
Expand Down Expand Up @@ -63,14 +79,13 @@ dependencies {
implementation(project(":lib"))

implementation("androidx.core:core-ktx:1.3.2")
implementation("androidx.appcompat:appcompat:1.2.0")
implementation("androidx.appcompat:appcompat:1.3.0-beta01")
implementation("androidx.constraintlayout:constraintlayout:2.0.4")

implementation("com.google.android.material:material:1.3.0")
implementation("androidx.compose.ui:ui:$composeVersion")
implementation("androidx.compose.material:material:$composeVersion")
implementation("androidx.compose.ui:ui-tooling:$composeVersion")
implementation("androidx.activity:activity-compose:1.3.0-alpha03")
implementation("androidx.activity:activity-compose:1.3.0-alpha04")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.3.0")
implementation("androidx.lifecycle:lifecycle-livedata-ktx:2.3.0")
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.0")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2020 Marcel Pinto Biescas
*
* 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.
*/

package dev.marcelpinto.permissionktx.compose

import android.Manifest
Expand Down Expand Up @@ -31,7 +47,7 @@ class ComposePermissionTest {
rationale = PermissionRational.OPTIONAL
)

@ExperimentalCoroutinesApi
@OptIn(ExperimentalCoroutinesApi::class)
@Before
fun setUp() {
PermissionProvider.init(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2020 Marcel Pinto Biescas
*
* 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.
*/

package dev.marcelpinto.permissionktx.simple

import android.Manifest
Expand All @@ -22,11 +38,11 @@ import org.junit.runner.RunWith
class SimpleActivityTest {

private var permissionStatus: PermissionStatus = PermissionStatus.Revoked(
type = Permission(Manifest.permission.ACCESS_FINE_LOCATION),
type = Permission(Manifest.permission.ACCESS_COARSE_LOCATION),
rationale = PermissionRational.OPTIONAL
)

@ExperimentalCoroutinesApi
@OptIn(ExperimentalCoroutinesApi::class)
@Before
fun setUp() {
// Provide a custom init that returns the values of the defined permissionStatus
Expand Down
36 changes: 32 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2020 Marcel Pinto Biescas
~
~ 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.
-->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dev.marcelpinto.permissionktx">

Expand All @@ -15,6 +31,7 @@

<activity
android:name=".MainActivity"
android:exported="true"
android:theme="@style/Theme.PermissionKtx">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand All @@ -23,10 +40,21 @@
</intent-filter>
</activity>

<activity android:name=".simple.SimpleActivity" />
<activity android:name=".advance.AdvanceActivity" />
<activity android:name=".compose.ComposeActivity" />
<activity android:name="androidx.activity.ComponentActivity" />
<activity
android:name=".simple.SimpleActivity"
android:exported="false" />
<activity
android:name=".multiple.MultipleActivity"
android:exported="false" />
<activity
android:name=".advance.AdvanceActivity"
android:exported="false" />
<activity
android:name=".compose.ComposeActivity"
android:exported="false" />
<activity
android:name="androidx.activity.ComponentActivity"
android:exported="false" />

</application>

Expand Down
22 changes: 22 additions & 0 deletions app/src/main/java/dev/marcelpinto/permissionktx/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2020 Marcel Pinto Biescas
*
* 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.
*/

package dev.marcelpinto.permissionktx

import android.content.Intent
Expand All @@ -15,6 +31,7 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import dev.marcelpinto.permissionktx.advance.AdvanceActivity
import dev.marcelpinto.permissionktx.compose.ComposeActivity
import dev.marcelpinto.permissionktx.multiple.MultipleActivity
import dev.marcelpinto.permissionktx.simple.SimpleActivity

class MainActivity : AppCompatActivity() {
Expand All @@ -34,6 +51,11 @@ class MainActivity : AppCompatActivity() {
}) {
Text("Simple Sample")
}
Button(onClick = {
start(MultipleActivity::class.java)
}) {
Text("Multiple Permissions Sample")
}
Button(onClick = {
start(AdvanceActivity::class.java)
}) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2020 Marcel Pinto Biescas
*
* 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.
*/

package dev.marcelpinto.permissionktx.advance

import android.Manifest
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2020 Marcel Pinto Biescas
*
* 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.
*/

package dev.marcelpinto.permissionktx.advance

import android.Manifest
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2020 Marcel Pinto Biescas
*
* 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.
*/

package dev.marcelpinto.permissionktx.advance

import kotlinx.coroutines.delay
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2020 Marcel Pinto Biescas
*
* 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.
*/

package dev.marcelpinto.permissionktx.compose

import android.Manifest
Expand Down
Loading

0 comments on commit 21ad23b

Please sign in to comment.