abbic
02/17/2023, 11:27 AM@ExperimentalPermissionsApi
@Composable
fun rememberPermissionStateWithCount(
permission: String,
onPermissionResult: (Boolean) -> Unit = {}
): SPPermissionState {
var count by remember {
mutableStateOf(0)
}
val permissionState = rememberPermissionState(
permission = permission,
onPermissionResult = {
count += 1
onPermissionResult(it)
}
)
return remember {
SPPermissionState(
permissionState = permissionState,
dontLaunchPermissionRequest = count >= 2
)
}
}
So there are bound to be errors i think, but debugging through trial and error would mean a lot of work.
I am effectively trying to wrap accompanist's PermissionState
inside one of my own creation with a little added state.
Is what i'm trying to achieve clear enough? does this approach work or is there an api i'm missing? for example, i may not need the remember in front of the returnStylianos Gakis
02/17/2023, 11:37 AMpermissionState
to be and never update it? Same for evaluating if count >= 2
only the first time?SPPermissionState
and if those are simple variables or MutableState
objects. Cause it may behave differently thenabbic
02/17/2023, 11:38 AMStylianos Gakis
02/17/2023, 11:43 AMderivedStateOf
and specifically reading data from data stored inside MutableState
, as for example this shows (from this article).
@Composable
fun ScrollToTopButton(lazyListState: LazyListState, threshold: Int) {
val isEnabled by remember(threshold) {
derivedStateOf { lazyListState.firstVisibleItemIndex > threshold }
}
Button(onClick = { }, enabled = isEnabled) {
Text("Scroll to top")
}
}
Since firstVisibleItemToIndex
is reading from a MutableState
, from here and internally, at the index which is backed by MutableStateremember
block, you need to make sure you’re either keying it, (or using derivedStateOf + what you are reading is compose state object) otherwise you are getting whatever is read on the first pass, and it won’t keep up with further updates after that.
So yeah in this case do try to key that last remember on permissionState
and count
.abbic
02/17/2023, 11:45 AMStylianos Gakis
02/17/2023, 11:49 AM