I have a preview that references accompanist permi...
# compose-android
t
I have a preview that references accompanist permissions:
Copy code
private fun BLEBrowserPreview() {
    TwigmcTheme {
       val context = LocalContext.current
       BLEBrowser(scanList = remember { NearbyMCList(context) },
          onConnect = { conduit -> conduit.logged("BLE CONNECT!") },
          permissions = rememberMultiplePermissionsState(
             listOf(
                android.Manifest.permission.BLUETOOTH_SCAN,
                android.Manifest.permission.BLUETOOTH_CONNECT
             )
          ),
          modifier = Modifier.fillMaxSize()
       )
    }
}
Unfortunately, it won't render. Digging into the render issues, I get to some stack that looks like:
Copy code
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)
	at com.nelsonirrigation.twigmc.ui.browser.ComposableSingletons$BLEBrowserKt$lambda-5$1.invoke(BLEBrowser.kt:275)
	at com.nelsonirrigation.twigmc.ui.browser.ComposableSingletons$BLEBrowserKt$lambda-5$1.invoke(BLEBrowser.kt:271)
I already had to wrap my reference to BLEManager in a java.lang.AssertionError. Is this just one of those areas I shouldn't be writing a preview? The code works fine/well on a real device. Even on the simulator whose BLE appears to not really do anything.
s
Hoist your permission related stuff to a higher up composable and preview the composable which doesn't know about it, but instead takes a lambda to trigger the request and a boolean to know if it has the permission or not. Or something like that.
1
t
thanks, figured it would be something like that. appreciate the confirmation.
s
I had to do exactly that over here the other day, not with permissions but with
rememberLauncherForActivityResult
calls, which once again is something that makes assumptions for things that come from CompositionLocals which are just not there for a preview