Skip to content

Commit 4bfc208

Browse files
committed
Put ArmedNotches back in the wheel module
Trimming the dialog helper out of the copied module took the stray-notch guard with it, because the two sit next to each other in the file the other apps carry, and the module stopped compiling. It is the part that makes the first notch after a pause do nothing, so the wheel cannot be nudged by a thumb resting on it.
1 parent 2fbfda3 commit 4bfc208

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

  • examples/LightNYCSubway/src/main/kotlin/com/thelightphone/subway/hw

‎examples/LightNYCSubway/src/main/kotlin/com/thelightphone/subway/hw/Wheel.kt‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,46 @@ fun WheelScroll(state: ScrollableState, active: Boolean = true) {
140140
}
141141
}
142142

143+
/**
144+
* Notches, minus the stray ones. See [ARM_NOTCHES].
145+
*
146+
* Armed state lives in the effect rather than in composition state: it is a property of the
147+
* turn in progress, and a recomposition mid-turn should not disarm the wheel.
148+
*/
149+
@Composable
150+
private fun ArmedNotches(active: Boolean, onNotch: (Int) -> Unit) {
151+
val handler by rememberUpdatedState(onNotch)
152+
val bus = LocalWheelBus.current ?: return
153+
LaunchedEffect(bus, active) {
154+
if (!active) return@LaunchedEffect
155+
var armed = false
156+
var held = 0
157+
var count = 0
158+
var last = 0L
159+
bus.notches.collect { notches ->
160+
val now = System.nanoTime() / 1_000_000
161+
if (now - last > IDLE_MS) {
162+
armed = false
163+
held = 0
164+
count = 0
165+
}
166+
last = now
167+
if (armed) {
168+
handler(notches)
169+
return@collect
170+
}
171+
held += notches
172+
count++
173+
if (count >= ARM_NOTCHES) {
174+
armed = true
175+
// Release what the guard was holding, so nothing deliberate is lost.
176+
if (held != 0) handler(held) else handler(notches.sign)
177+
held = 0
178+
}
179+
}
180+
}
181+
}
182+
143183
/**
144184
* Distance still owed to the scroller.
145185
*

0 commit comments

Comments
 (0)