Skip to content

Commit

Permalink
Merge pull request #1803 from eygraber/permission-in-previews
Browse files Browse the repository at this point in the history
[Permissions] Support calling rememberPermissionState in a Compose preview
  • Loading branch information
bentrengrove authored Dec 9, 2024
2 parents a58dd5d + 2d05d95 commit bb1d0e7
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 2 deletions.
2 changes: 2 additions & 0 deletions permissions/api/current.api
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.google.accompanist.permissions {

public final class MultiplePermissionsStateKt {
method @androidx.compose.runtime.Composable @com.google.accompanist.permissions.ExperimentalPermissionsApi public static com.google.accompanist.permissions.MultiplePermissionsState rememberMultiplePermissionsState(java.util.List<java.lang.String> permissions, optional kotlin.jvm.functions.Function1<? super java.util.Map<java.lang.String,java.lang.Boolean>,kotlin.Unit> onPermissionsResult);
method @androidx.compose.runtime.Composable @com.google.accompanist.permissions.ExperimentalPermissionsApi public static com.google.accompanist.permissions.MultiplePermissionsState rememberMultiplePermissionsState(java.util.List<java.lang.String> permissions, optional kotlin.jvm.functions.Function1<? super java.util.Map<java.lang.String,java.lang.Boolean>,kotlin.Unit> onPermissionsResult, optional java.util.Map<java.lang.String,? extends com.google.accompanist.permissions.PermissionStatus> previewPermissionStatuses);
}

public final class MutableMultiplePermissionsStateKt {
Expand All @@ -36,6 +37,7 @@ package com.google.accompanist.permissions {

public final class PermissionStateKt {
method @androidx.compose.runtime.Composable @com.google.accompanist.permissions.ExperimentalPermissionsApi public static com.google.accompanist.permissions.PermissionState rememberPermissionState(String permission, optional kotlin.jvm.functions.Function1<? super java.lang.Boolean,kotlin.Unit> onPermissionResult);
method @androidx.compose.runtime.Composable @com.google.accompanist.permissions.ExperimentalPermissionsApi public static com.google.accompanist.permissions.PermissionState rememberPermissionState(String permission, optional kotlin.jvm.functions.Function1<? super java.lang.Boolean,kotlin.Unit> onPermissionResult, optional com.google.accompanist.permissions.PermissionStatus previewPermissionStatus);
}

@androidx.compose.runtime.Stable @com.google.accompanist.permissions.ExperimentalPermissionsApi public sealed interface PermissionStatus {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
package com.google.accompanist.permissions

import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.Stable
import androidx.compose.ui.platform.LocalInspectionMode
import androidx.compose.ui.util.fastMap

/**
* Creates a [MultiplePermissionsState] that is remembered across compositions.
Expand All @@ -35,7 +38,33 @@ public fun rememberMultiplePermissionsState(
permissions: List<String>,
onPermissionsResult: (Map<String, Boolean>) -> Unit = {}
): MultiplePermissionsState {
return rememberMutableMultiplePermissionsState(permissions, onPermissionsResult)
return rememberMultiplePermissionsState(permissions, onPermissionsResult, emptyMap())
}

/**
* Creates a [MultiplePermissionsState] that is remembered across compositions.
*
* It's recommended that apps exercise the permissions workflow as described in the
* [documentation](https://developer.android.com/training/permissions/requesting#workflow_for_requesting_permissions).
*
* @param permissions the permissions to control and observe.
* @param onPermissionsResult will be called with whether or not the user granted the permissions
* after [MultiplePermissionsState.launchMultiplePermissionRequest] is called.
* @param previewPermissionStatuses provides a [PermissionStatus] for a given permission when running
* in a preview.
*/
@ExperimentalPermissionsApi
@Composable
public fun rememberMultiplePermissionsState(
permissions: List<String>,
onPermissionsResult: (Map<String, Boolean>) -> Unit = {},
previewPermissionStatuses: Map<String, PermissionStatus> = emptyMap()
): MultiplePermissionsState {
return when {
LocalInspectionMode.current ->
PreviewMultiplePermissionsState(permissions, previewPermissionStatuses)
else -> rememberMutableMultiplePermissionsState(permissions, onPermissionsResult)
}
}

/**
Expand Down Expand Up @@ -83,3 +112,23 @@ public interface MultiplePermissionsState {
*/
public fun launchMultiplePermissionRequest(): Unit
}

@OptIn(ExperimentalPermissionsApi::class)
@Immutable
private class PreviewMultiplePermissionsState(
permissions: List<String>,
permissionStatuses: Map<String, PermissionStatus>
) : MultiplePermissionsState {
override val permissions: List<PermissionState> = permissions.fastMap { permission ->
PreviewPermissionState(
permission = permission,
status = permissionStatuses[permission] ?: PermissionStatus.Granted,
)
}

override val revokedPermissions: List<PermissionState> = emptyList()
override val allPermissionsGranted: Boolean = false
override val shouldShowRationale: Boolean = false

override fun launchMultiplePermissionRequest() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package com.google.accompanist.permissions

import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.Stable
import androidx.compose.ui.platform.LocalInspectionMode

/**
* Creates a [PermissionState] that is remembered across compositions.
Expand All @@ -35,7 +37,31 @@ public fun rememberPermissionState(
permission: String,
onPermissionResult: (Boolean) -> Unit = {}
): PermissionState {
return rememberMutablePermissionState(permission, onPermissionResult)
return rememberPermissionState(permission, onPermissionResult, PermissionStatus.Granted)
}

/**
* Creates a [PermissionState] that is remembered across compositions.
*
* It's recommended that apps exercise the permissions workflow as described in the
* [documentation](https://developer.android.com/training/permissions/requesting#workflow_for_requesting_permissions).
*
* @param permission the permission to control and observe.
* @param onPermissionResult will be called with whether or not the user granted the permission
* after [PermissionState.launchPermissionRequest] is called.
* @param previewPermissionStatus provides a [PermissionStatus] when running in a preview.
*/
@ExperimentalPermissionsApi
@Composable
public fun rememberPermissionState(
permission: String,
onPermissionResult: (Boolean) -> Unit = {},
previewPermissionStatus: PermissionStatus = PermissionStatus.Granted
): PermissionState {
return when {
LocalInspectionMode.current -> PreviewPermissionState(permission, previewPermissionStatus)
else -> rememberMutablePermissionState(permission, onPermissionResult)
}
}

/**
Expand Down Expand Up @@ -73,3 +99,12 @@ public interface PermissionState {
*/
public fun launchPermissionRequest(): Unit
}

@OptIn(ExperimentalPermissionsApi::class)
@Immutable
internal class PreviewPermissionState(
override val permission: String,
override val status: PermissionStatus
) : PermissionState {
override fun launchPermissionRequest() {}
}

0 comments on commit bb1d0e7

Please sign in to comment.