Skip to content

Commit

Permalink
Make permissions work with Android Studio Preview
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedRejeb committed Nov 10, 2024
1 parent 7453f96 commit 5a1dfc4
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -19,7 +21,24 @@ fun rememberMultiplePermissionsState(
permissions: List<Permission>,
onPermissionsResult: (Map<Permission, Boolean>) -> 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<PermissionState> = emptyList()
override val revokedPermissions: List<PermissionState> = emptyList()
override val allPermissionsGranted: Boolean = true
override val shouldShowRationale: Boolean = false
override fun launchMultiplePermissionRequest() {}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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() {}
}
}

/**
Expand Down
9 changes: 5 additions & 4 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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" }

Expand Down
2 changes: 2 additions & 0 deletions sample/android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -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 = {}
)
}

0 comments on commit 5a1dfc4

Please sign in to comment.