vide
08/15/2023, 10:55 AMscrollable
. In my case I have a modifier that calls a callback when long-clicking, and I would like to visualize the delay progress with another modifier. I extracted the state into a state object, let's call it LongClickState
, which contains a single mutable state, which the longClick
modifier updates.
I would like to somehow prevent "outsiders" from updating the state as the modifier should be the sole owner and source of truth of the state. Is this possible or am I just fundamentally approaching the problem incorrectly? 🧵vide
08/15/2023, 10:55 AMclass LongClickState {
val isDepressed = mutableStateOf(false)
}
fun Modifier.longClick(state: LongClickState, onLongClick: () -> Unit) {
/* Update isDepressed.value = true when pressed */
}
fun Modifier.longClickVisualization(state: LongClickState) = composed {
val animatedProgress = animateFloatAsState(if (state.isDepressed.value) 1f else 0f)
drawBehind { /* draw something with animatedProgess.value */ }
}
vide
08/15/2023, 10:57 AMModifier.longClick
from accessing isDepressed as a MutableState? i.e. preventing this whether it's accidental or intentional:
val s = remember { LongClickState().apply { isDepressed.value = true }
Box(Modifier.longClick(s) {}.longClickVisualization(s))
vide
08/15/2023, 11:01 AMclass LongClickState {
private val _isDepressed = mutableStateOf(false)
val isDepressed: State<Boolean> = _isDepressed
fun Modifier.longClick(onLongClick: () -> Unit) {
/* Update isDepressed.value = true when pressed */
}
}
and then use the modifier like this:
val s = remember { LongClickState() }
val longClick = with (s) { Modifier.longClick() {} }
Box(Modifier.[...].then(longClick).longClickVisualization(s))
does this make any sense? 🤔vide
08/15/2023, 11:04 AMinternal
, but that's not an option (I think?) inside a single module?Stylianos Gakis
08/15/2023, 11:17 AM