diff --git a/README.md b/README.md
index 5146b39..83c1192 100644
--- a/README.md
+++ b/README.md
@@ -153,6 +153,12 @@ sense.
|-------|-------|
|[How can I navigate to different screen in Jetpack Compose?](https://github.com/vinaygaba/Learn-Jetpack-Compose-By-Example/blob/master/app/src/main/java/com/example/jetpackcompose/navigation/ComposeNavigationActivity.kt) |
|
+
+### Permissions
+| Example |Preview|
+|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------|
+| [How can I request for permissions in Jetpack Compose?](https://github.com/vinaygaba/Learn-Jetpack-Compose-By-Example/blob/master/app/src/main/java/com/example/jetpackcompose/permissions/PermissionActivity.kt) |
|
+
### Testing
|Example|Preview|
|-------|-------|
diff --git a/app/build.gradle b/app/build.gradle
index ae84e66..e206e74 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -26,6 +26,9 @@ android {
}
kotlinOptions {
jvmTarget = '11'
+ freeCompilerArgs += [
+ '-Xopt-in=com.google.accompanist.permissions.ExperimentalPermissionsApi'
+ ]
}
buildFeatures {
compose true
@@ -65,6 +68,9 @@ dependencies {
implementation deps.support.ktxLiveData
implementation deps.support.lifecycleExtensions
+ // Accompanist
+ implementation deps.accompanist.permissions
+
// CardView
implementation deps.cardView
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 6dc0e12..b2ed5ed 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -3,6 +3,7 @@
package="com.example.jetpackcompose">
+
+
diff --git a/app/src/main/java/com/example/jetpackcompose/core/MainActivity.kt b/app/src/main/java/com/example/jetpackcompose/core/MainActivity.kt
index 1c7ff84..cd09cec 100644
--- a/app/src/main/java/com/example/jetpackcompose/core/MainActivity.kt
+++ b/app/src/main/java/com/example/jetpackcompose/core/MainActivity.kt
@@ -29,6 +29,7 @@ import com.example.jetpackcompose.material.FlowRowActivity
import com.example.jetpackcompose.material.MaterialActivity
import com.example.jetpackcompose.material.ShadowActivity
import com.example.jetpackcompose.navigation.ComposeNavigationActivity
+import com.example.jetpackcompose.permissions.PermissionActivity
import com.example.jetpackcompose.scrollers.HorizontalScrollableActivity
import com.example.jetpackcompose.scrollers.VerticalScrollableActivity
import com.example.jetpackcompose.stack.StackActivity
@@ -190,4 +191,8 @@ class MainActivity : AppCompatActivity() {
fun startComposeNavigationExample(view: View) {
startActivity(Intent(this, ComposeNavigationActivity::class.java))
}
+
+ fun startPermissionExample(view: View) {
+ startActivity(Intent(this, PermissionActivity::class.java))
+ }
}
diff --git a/app/src/main/java/com/example/jetpackcompose/permissions/PermissionActivity.kt b/app/src/main/java/com/example/jetpackcompose/permissions/PermissionActivity.kt
new file mode 100644
index 0000000..e5d5c8e
--- /dev/null
+++ b/app/src/main/java/com/example/jetpackcompose/permissions/PermissionActivity.kt
@@ -0,0 +1,75 @@
+package com.example.jetpackcompose.permissions
+
+import android.Manifest
+import android.os.Bundle
+import androidx.activity.ComponentActivity
+import androidx.activity.compose.setContent
+import androidx.compose.foundation.layout.Column
+import androidx.compose.foundation.layout.fillMaxSize
+import androidx.compose.foundation.layout.padding
+import androidx.compose.material.Button
+import androidx.compose.material.Text
+import androidx.compose.runtime.Composable
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.unit.dp
+import com.google.accompanist.permissions.isGranted
+import com.google.accompanist.permissions.rememberPermissionState
+import com.google.accompanist.permissions.shouldShowRationale
+
+class PermissionActivity : ComponentActivity() {
+
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+
+ setContent {
+ App()
+ }
+ }
+}
+
+/**
+ * In this example, we are going to use Google's Accompanist Library - Jetpack Compose Permissions,
+ * to request for the permissions from the user.
+ * Reference: https://google.github.io/accompanist/permissions/
+ *
+ * Please Note: The version of the library depends on the version of the Jetpack Compose version
+ * that you're using in the application. So, please make sure that the Accompanist library version
+ * matches the Jetpack Compose version.
+ * Version Reference: https://github.com/google/accompanist#compose-versions
+ */
+@Composable
+private fun App() {
+ Column(
+ modifier = Modifier
+ .fillMaxSize()
+ .padding(16.dp),
+ ) {
+ val contactsPermissionState = rememberPermissionState(
+ permission = Manifest.permission.READ_CONTACTS
+ )
+
+ if (contactsPermissionState.status.isGranted) {
+ Text("Contacts permission is granted")
+ } else {
+ Column {
+ val text = if (contactsPermissionState.status.shouldShowRationale) {
+ // Shown if the user has denied the requested permissions but
+ // the rationale can be shown, informing the user why the permission
+ // is needed.
+ "Contacts permission is required to display list of contact."
+ } else {
+ // Shown if it is the first time the user reached this page, and
+ // the app is requesting the permission for the first time.
+ "Please grant the Contacts permission to display list of contacts."
+ }
+
+ Text(text)
+ Button(
+ onClick = { contactsPermissionState.launchPermissionRequest() },
+ ) {
+ Text("Request Permission")
+ }
+ }
+ }
+ }
+}
diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml
index c7ff7c7..f63dafb 100644
--- a/app/src/main/res/layout/activity_main.xml
+++ b/app/src/main/res/layout/activity_main.xml
@@ -677,5 +677,24 @@
android:layout_gravity="center"
android:fontFamily="monospace" />
+
+
+
+
+
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index b8f3e3e..a7f36d6 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -70,4 +70,6 @@
Back Press Component
Zoomable Component
Compose Navigation
+ Permission Example
+ Jetpack Compose Permissions
diff --git a/build.gradle b/build.gradle
index 1c85aa3..b1b8bde 100644
--- a/build.gradle
+++ b/build.gradle
@@ -2,6 +2,7 @@
buildscript {
ext.versions = [
+ 'accompanistPermissions': '0.25.1',
'cardView': '1.0.0',
'compose': '1.2.0-rc01',
'composeActivity': '1.4.0',
@@ -24,6 +25,9 @@ buildscript {
'testRunner': '1.2.0',
]
ext.deps = [
+ 'accompanist': [
+ 'permissions': "com.google.accompanist:accompanist-permissions:$versions.accompanistPermissions"
+ ],
'cardView': "androidx.cardview:cardview:${versions.cardView}",
'compose': [
'activityCompose': "androidx.activity:activity-compose:${versions.composeActivity}",
diff --git a/screenshots/compose_permission_example.gif b/screenshots/compose_permission_example.gif
new file mode 100644
index 0000000..56e762d
Binary files /dev/null and b/screenshots/compose_permission_example.gif differ