I have a code style question: I'm thinking about a...
# compose
v
I have a code style question: I'm thinking about a pattern where a modifier has/uses some kind of internal state, like for example
scrollable
. 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? 🧵
here's some hypothetical pseudocode
Copy code
class 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 */ }
}
is there a way to prevent other places than
Modifier.longClick
from accessing isDepressed as a MutableState? i.e. preventing this whether it's accidental or intentional:
Copy code
val s = remember { LongClickState().apply { isDepressed.value = true }
Box(Modifier.longClick(s) {}.longClickVisualization(s))
I thought one way of approaching could be defining the modifier inside the class:
Copy code
class 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:
Copy code
val s = remember { LongClickState() }
val longClick = with (s) { Modifier.longClick() {} }
Box(Modifier.[...].then(longClick).longClickVisualization(s))
does this make any sense? 🤔
I think in the compose libraries this is mostly achieved with using
internal
, but that's not an option (I think?) inside a single module?
s
Can you create a new module for this, make everything public except for the getter for that state which can be internal, so that the modifier, which will also be inside this module can access it?
🤔 1