https://kotlinlang.org logo
#compose
Title
# compose
l

Lilly

05/27/2020, 4:44 PM
Are there any best practices for requesting permissions in compose. While the request has to remain in activity the dialog will be displayed in composable functions...feels a bit fragmented. Maybe there is solution I'm not aware of!?
a

Adam Powell

05/27/2020, 11:50 PM
The new activity result and permissions libraries were designed with this in mind 🙂
l

Lilly

05/28/2020, 4:06 PM
@Adam Powell Do you have an example or a link to this permission library
and you can write up a bridge to have compose register/unregister the ActivityResultLauncher for you https://developer.android.com/reference/kotlin/androidx/activity/result/ActivityResultLauncher
👍🏻 1
c

codeslubber

05/29/2020, 11:55 PM
Just did a bunch of permissions stuff.. thank you!
👍 1
a

Adrian Devezin

06/18/2020, 12:44 AM
Does anyone have a code sample @codeslubber @Adam Powell?
a

Archie

10/15/2020, 2:14 PM
Hi @Adam Powell, I had this in mind,
Copy code
class PermissionHandler(
    private val activity: AppCompatActivity
) {

    fun requestPermission(
        permission: String,
        onResultReceived: (PermissionResult) -> Unit,
    ) {
        val requestPermissionLauncher = activity.registerForActivityResult(
            ActivityResultContracts.RequestPermission(),
        ) { granted ->
            onResultReceived(
                if (granted)
                    PermissionResult.Granted
                else
                    PermissionResult.Denied
            )
        }

        when {
            ContextCompat.checkSelfPermission(
                activity,
                permission
            ) == PackageManager.PERMISSION_GRANTED -> {
                onResultReceived(PermissionResult.Granted)
            }
            ActivityCompat.shouldShowRequestPermissionRationale(
                activity,
                permission
            ) -> {
                onResultReceived(PermissionResult.ShowRationale)
            }
            else -> requestPermissionLauncher.launch(permission)
        }
    }

}

@Composable
fun requestPermission(
    permission: String,
): State<PermissionResult> {
    val permissionHandler = AmbientPermissionHandler.current

    val permissionResult = remember(permission) {
        mutableStateOf<PermissionResult>(PermissionResult.Requesting)
    }

    remember(permission) {
        permissionHandler.requestPermission(permission) {
            permissionResult.value = it
        }
    }

    return permissionResult
}

sealed class PermissionResult {
    object Requesting : PermissionResult()

    object ShowRationale : PermissionResult()

    object Denied : PermissionResult()

    object Granted : PermissionResult()
}

val AmbientPermissionHandler: ProvidableAmbient<PermissionHandler> =
    staticAmbientOf { throw IllegalStateException("permission handler is not initialized") }
and then provide
PermissionHandler
as an ambient like so:
Copy code
Providers(AmbientPermissionHandler provides permissionHandler) {
    ....
}
so then I could use it like so:
Copy code
val permissionResult by requestPermission(permission = Manifest.permission.CAMERA)
when (missionResult) {
     PermissionResult.Requesting -> Text("Requesting")
     PermissionResult.ShowRationale -> Text("Show Rationale")
     PermissionResult.Denied -> Text("Denied")
     PermissionResult.Granted -> Text("Granted")
}
The problem is when I get to the
Composable
where the code above is located, I get the error:
Copy code
LifecycleOwner ... is attempting to register while current state is RESUMED. LifecycleOwners must call register before they are STARTED.
        at androidx.activity.result.ActivityResultRegistry.register 
        ...
I not really sure if I made everything correctly but based on the error logs, it seems that it isn't possible to create
registerForActivityResult(...)
inside
@Composable
as at the point the app is already at
LifecycleState.RESUMED
? Is there any advise which you could give?
@Vinay Gaba just linked me to: https://gist.github.com/objcode/775fe45127fd40f17932f672ee203f72#file-permissions-kt-L78 Thank you very much. Might be useful for someone else looking into this topic.
👍 1
🙏🏼 1
2 Views