google / accompanist

A collection of extension libraries for Jetpack Compose
https://google.github.io/accompanist
Apache License 2.0
7.31k stars 592 forks source link

[Permissions] `MutableMultiplePermissionsState.shouldShowRationale` returns true despite some permissions being `Denied(shouldShowRationale=false)` #1781

Open FelixZY opened 5 days ago

FelixZY commented 5 days ago

Description

The current implementation of MutableMultiplePermissionsState.shouldShowRationale does not consider cases where one or more permissions are Denied(shouldShowRationale=false). This causes issues in statements such as

val permissions = rememberMultiplePermissionsState(listOf(ACCESS_FINE_LOCATION, ACCESS_BACKGROUND_LOCATION))

when {
  // Granted
  permissions.allPermissionsGranted -> Unit,
  // Denied, but I can ask the user
  permissions.shouldShowRationale -> 
    // ERROR: Does not trigger!
    permissions.launchMultiplePermissionRequest()
  // Denied and I may not ask the user.
  else -> Unit
}

The issue is located at

https://github.com/google/accompanist/blob/57907e1c9aadef1f70e51889c04048693e983f21/permissions/src/main/java/com/google/accompanist/permissions/MutableMultiplePermissionsState.kt#L130

Suggested fix:

    override val shouldShowRationale: Boolean by derivedStateOf {
-       permissions.any { it.status.shouldShowRationale }
+       permissions.any { it.status.shouldShowRationale } && permissions.none { !it.status.isGranted && !it.status.shouldShowRationale }
    }
bentrengrove commented 1 day ago

That does make sense. PR's welcome!