From 5a1dfc490d811a15b1d617adb83651d52702e0d0 Mon Sep 17 00:00:00 2001 From: MohamedRejeb Date: Sun, 10 Nov 2024 10:56:00 +0100 Subject: [PATCH] Make permissions work with Android Studio Preview --- .../permissions/MultiplePermissionsState.kt | 21 +++++++++++++++++- .../permissions/PermissionState.kt | 22 ++++++++++++++++++- gradle/libs.versions.toml | 9 ++++---- sample/android/build.gradle.kts | 2 ++ .../calf/sample/PermissionScreenPreview.kt | 13 +++++++++++ 5 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 sample/android/src/main/java/com/mohamedrejeb/calf/sample/PermissionScreenPreview.kt diff --git a/calf-permissions/src/commonMain/kotlin/com.mohamedrejeb.calf/permissions/MultiplePermissionsState.kt b/calf-permissions/src/commonMain/kotlin/com.mohamedrejeb.calf/permissions/MultiplePermissionsState.kt index 1efc353..60421ce 100644 --- a/calf-permissions/src/commonMain/kotlin/com.mohamedrejeb.calf/permissions/MultiplePermissionsState.kt +++ b/calf-permissions/src/commonMain/kotlin/com.mohamedrejeb.calf/permissions/MultiplePermissionsState.kt @@ -2,6 +2,8 @@ package com.mohamedrejeb.calf.permissions import androidx.compose.runtime.Composable import androidx.compose.runtime.Stable +import androidx.compose.runtime.remember +import androidx.compose.ui.platform.LocalInspectionMode /** * Creates a [MultiplePermissionsState] that is remembered across compositions. @@ -19,7 +21,24 @@ fun rememberMultiplePermissionsState( permissions: List, onPermissionsResult: (Map) -> Unit = {} ): MultiplePermissionsState { - return rememberMutableMultiplePermissionsState(permissions, onPermissionsResult) + val isInspection = LocalInspectionMode.current + + return if (isInspection) + rememberPreviewMultiplePermissionState() + else + rememberMutableMultiplePermissionsState(permissions, onPermissionsResult) +} + +@OptIn(ExperimentalPermissionsApi::class) +@Composable +private fun rememberPreviewMultiplePermissionState(): MultiplePermissionsState = remember { + object : MultiplePermissionsState { + override val permissions: List = emptyList() + override val revokedPermissions: List = emptyList() + override val allPermissionsGranted: Boolean = true + override val shouldShowRationale: Boolean = false + override fun launchMultiplePermissionRequest() {} + } } /** diff --git a/calf-permissions/src/commonMain/kotlin/com.mohamedrejeb.calf/permissions/PermissionState.kt b/calf-permissions/src/commonMain/kotlin/com.mohamedrejeb.calf/permissions/PermissionState.kt index 42bbf6e..d241286 100644 --- a/calf-permissions/src/commonMain/kotlin/com.mohamedrejeb.calf/permissions/PermissionState.kt +++ b/calf-permissions/src/commonMain/kotlin/com.mohamedrejeb.calf/permissions/PermissionState.kt @@ -2,6 +2,8 @@ package com.mohamedrejeb.calf.permissions import androidx.compose.runtime.Composable import androidx.compose.runtime.Stable +import androidx.compose.runtime.remember +import androidx.compose.ui.platform.LocalInspectionMode /** * Creates a [PermissionState] that is remembered across compositions. @@ -19,7 +21,25 @@ fun rememberPermissionState( permission: Permission, onPermissionResult: (Boolean) -> Unit = {}, ): PermissionState { - return rememberMutablePermissionState(permission, onPermissionResult) + val isInspection = LocalInspectionMode.current + + return if (isInspection) + rememberPreviewPermissionState(permission) + else + rememberMutablePermissionState(permission, onPermissionResult) +} + +@OptIn(ExperimentalPermissionsApi::class) +@Composable +private fun rememberPreviewPermissionState( + permission: Permission +): PermissionState = remember { + object : PermissionState { + override val permission: Permission = permission + override var status: PermissionStatus = PermissionStatus.Granted + override fun launchPermissionRequest() {} + override fun openAppSettings() {} + } } /** diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index e9ff41f..2b8cf6f 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -2,7 +2,6 @@ agp = "8.5.2" kotlin = "2.0.21" compose = "1.7.0" -kotlinx-serialization-json = "1.7.1" kotlinx-datetime = "0.6.1" nexus-publish = "2.0.0" documentfile = "1.0.1" @@ -12,12 +11,13 @@ coil = "3.0.1" calf = "0.5.5" # For sample -activity-compose = "1.9.1" -navigation-compose = "2.7.7" +activity-compose = "1.9.3" +navigation-compose = "2.8.3" android-minSdk = "21" android-compileSdk = "34" kotlinx-coroutines = "1.9.0" appcompat = "1.7.0" +compose-ui = "1.7.1" android-lifecycle = "2.2.0" play-services-location = "21.3.0" compose-navigation = "2.7.0-alpha07" @@ -32,13 +32,14 @@ gradlePlugin-composeCompiler = { module = "org.jetbrains.kotlin:compose-compiler kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" } activity-compose = { module = "androidx.activity:activity-compose", version.ref = "activity-compose" } appcompat = { module = "androidx.appcompat:appcompat", version.ref = "appcompat" } +compose-tooling-preview = { module = "androidx.compose.ui:ui-tooling-preview", version.ref = "compose-ui" } +compose-tooling = { module = "androidx.compose.ui:ui-tooling", version.ref = "compose-ui" } lifecycle-extensions = { module = "androidx.lifecycle:lifecycle-extensions", version.ref = "android-lifecycle" } play-services-location = { module = "com.google.android.gms:play-services-location", version.ref = "play-services-location" } kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kotlinx-coroutines" } kotlinx-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "kotlinx-coroutines" } kotlinx-coroutines-swing = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-swing", version.ref = "kotlinx-coroutines" } kotlinx-coroutines-javafx = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-javafx", version.ref = "kotlinx-coroutines" } -kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx-serialization-json" } kotlinx-datetime = { module = "org.jetbrains.kotlinx:kotlinx-datetime", version.ref = "kotlinx-datetime" } nexus-publish = { module = "io.github.gradle-nexus.publish-plugin:io.github.gradle-nexus.publish-plugin.gradle.plugin", version.ref = "nexus-publish" } diff --git a/sample/android/build.gradle.kts b/sample/android/build.gradle.kts index 3c968fd..6d3a7b9 100644 --- a/sample/android/build.gradle.kts +++ b/sample/android/build.gradle.kts @@ -37,6 +37,8 @@ android { implementation(projects.sample.common) implementation(libs.activity.compose) + implementation(libs.compose.tooling.preview) implementation(libs.appcompat) + debugImplementation(libs.compose.tooling) } } \ No newline at end of file diff --git a/sample/android/src/main/java/com/mohamedrejeb/calf/sample/PermissionScreenPreview.kt b/sample/android/src/main/java/com/mohamedrejeb/calf/sample/PermissionScreenPreview.kt new file mode 100644 index 0000000..8875263 --- /dev/null +++ b/sample/android/src/main/java/com/mohamedrejeb/calf/sample/PermissionScreenPreview.kt @@ -0,0 +1,13 @@ +package com.mohamedrejeb.calf.sample + +import androidx.compose.runtime.Composable +import androidx.compose.ui.tooling.preview.Preview +import com.mohamedrejeb.calf.sample.screens.PermissionScreen + +@Preview +@Composable +fun PermissionScreenPreview() { + PermissionScreen( + navigateBack = {} + ) +} \ No newline at end of file