Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Permissions] Preview breaks for composables using permissions: no activity context #1498

Closed
WebTiger89 opened this issue Jan 27, 2023 · 11 comments
Assignees

Comments

@WebTiger89
Copy link

WebTiger89 commented Jan 27, 2023

@Preview composables do not render composables using permissions which makes sense because they are not running in the context of an activity.

Rendering issue:

Failed to instantiate Composition Local
This preview was unable to find a CompositionLocal. You might need to define it so it can render correctly.

Stacktrace:

java.lang.IllegalStateException: Permissions should be called in the context of an Activity
at com.google.accompanist.permissions.PermissionsUtilKt.findActivity(PermissionsUtil.kt:138)
at com.google.accompanist.permissions.MutableMultiplePermissionsStateKt.rememberMutablePermissionsState(MutableMultiplePermissionsState.kt:80)
at com.google.accompanist.permissions.MutableMultiplePermissionsStateKt.rememberMutableMultiplePermissionsState(MutableMultiplePermissionsState.kt:48)
at com.google.accompanist.permissions.MultiplePermissionsStateKt.rememberMultiplePermissionsState(MultiplePermissionsState.kt:38)

Did I miss something or is this a problem with the findActivity() function?

@manuelvicnt
Copy link
Collaborator

Yeah, this is a problem with findActivity() that doesn't currently work in previews. If you split this composable as a stateful and stateless one, you should be able to preview the stateless composable function without any issues.

@WebTiger89
Copy link
Author

@manuelvicnt Why do you close the issue if it is a known one? Will it be fixed? If one wants to preview the entire screen it is not possible to split further

@manuelvicnt
Copy link
Collaborator

There's nothing we can fix on this library. Preview needs to support Lifecycle APIs like Activities or ViewModel. Without their support we cannot do anything. This would work out of the box when they fix it

@WebTiger89
Copy link
Author

Ah ok. Are you aware of an open issue where I can participate?

@manuelvicnt
Copy link
Collaborator

Tried searching in https://issuetracker.google.com/issues and asked internally but got no luck. Maybe you can file a new bug?

@WebTiger89
Copy link
Author

@manuelvicnt Thanks for investigating. I have filed an issue, see https://issuetracker.google.com/issues/267227895

Could you please re-open the issue so other users can find this discussion. The chance might be higher that we get more users to subscribe to this issue at google so google is forced to increase the priority. You are also welcome to give it a +1

@manuelvicnt
Copy link
Collaborator

Issues in Accompanist get tagged as "stale" after 30 days if no one interacts with them. Then, they are automatically closed. Given this is a common problem not only for Permissions but also for Activity and ViewModels in Compose, I'll leave this closed because there isn't much we can do in Accompanist. Thanks for filing the bug!

@eury-fsl
Copy link

@manuelvicnt I understand your reasoning behind this, but in my particular case, and I think in many cases, the workaround will go against the principle of holding the state in the smallest scope as possible. I will need to lift my state unnecessarily just to fix this.

@audkar
Copy link

audkar commented Jun 9, 2023

Maybe such wrappers could help until root cause is solved:

@OptIn(ExperimentalPermissionsApi::class)
@Composable
fun rRememberMultiplePermissionsState(
  permissions: List<String>,
  onPermissionsResult: (Map<String, Boolean>) -> Unit = {},
): MultiplePermissionsState {
  val isInspection = LocalInspectionMode.current
  return if (isInspection) {
    object : MultiplePermissionsState {
      override val allPermissionsGranted: Boolean = false
      override val permissions: List<PermissionState> = emptyList()
      override val revokedPermissions: List<PermissionState> = emptyList()
      override val shouldShowRationale: Boolean = false
      override fun launchMultiplePermissionRequest() {}
    }
  } else {
    rememberMultiplePermissionsState(permissions, onPermissionsResult)
  }
}

We have a little bit more complex wrappers and only in debug BuildType, but the idea is to check LocalInspectionMode.current

@risalfajar
Copy link

@audkar thanks that works, I can preview my code now. I think you should create a PR.

@eygraber
Copy link
Contributor

There's nothing we can fix on this library. Preview needs to support Lifecycle APIs like Activities or ViewModel. Without their support we cannot do anything. This would work out of the box when they fix it

@manuelvicnt is the above workaround not something that can be put into accompanist so that preview doesn't break for anyone using this library?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants