Hi why Slider onValueChanged is being triggered on...
# compose
r
Hi why Slider onValueChanged is being triggered on every state change? I think onValueChange must be called when user interacts with the slider isn’t it?
a
please file as a bug
j
I suppose it is changing when the position parameter changes, or when a user is dragging? The former would be a bug, the later would be expected behavior.
r
I see there another param
onValueChangeFinished
Then, should we store two values first is main value second is awaiting user interaction?
Like that ??
j
Why do you have a userPosition if you are not passing it to the slider?
r
There is a media player listener which updates position value
j
Oh,
onValueChangeFinished
doesn't have a value parameter, eww
Yeah, something like what you did seems very reasonable
r
Yes a bit strange ))
Ok. I removed second variable. Now I must ignore player position updates while user interaction
r
I am not following what the problem is. You are getting callbacks without moving the slider? Other than this bug I haven't seen anything like that in my slider.
t
Seems you are experiencing this issue: https://issuetracker.google.com/u/0/issues/175943373
r
That is the same issue I cited (which happens to be the one you alerted me to in January :) ) but I don't think it applies here (though I can't tell without more information). The slider will always tell you where it is so that you can update its movement, regardless of whether you want the "intermediate" values or not. You don't want the thumb to move until you stop dragging?
t
I think the linked issue is exactly what is happening here. The onValueChange lambda is getting called without touching the slider. The issue is currently preventing me from using the Compose slider, unfortunately.
In this sample, the lambda is not supposed to be called when
position
changes without touching the slider. That is what the linked issue is about as well.
r
For me, the linked issue only happens when I give the slider its initial value (and I was able to work around it). RUSTAM, can you post a minimal repro in text (in particular, how are
position
and
userPosition
declared)?
r
Hi @Rick Regan
I think 
onValueChange
 fires on any value change by user or by state. But 
onValueChangeFinished
 fires only at the end of user interaction. So if I want to bind the value with player and at the same time accept user interaction I must ignore player position updates from listener while user is interacting with the slider and start accepting player position when user stops playing the slider bar
t
I think onValueChange fires on any value change by user or by state.
At the moment it is called by user and by state changes, but the latter is a bug which was confirmed in the linked issue.
r
I constructed this code from your screenshots. First a question: does this capture your code, or am I missing an important element? If the latter, please post something that matches your problem.
Copy code
@Composable
fun SliderTest() {
    println("SliderTest: Composed")
    var position by remember { mutableStateOf(0f) }
    var userPosition by remember { mutableStateOf(0f) }
    Slider(
        value = position,
        onValueChange = {
            userPosition = it
            println("SliderTest: Position changed: userPosition = $userPosition (position = $position)")
        },
        onValueChangeFinished = {
            position = userPosition
            println("SliderTest: User selected position: (position = $position)")
        }
    )
}
r
@Rick Regan Yes exactly . But then I removed userPosition
r
I had assumed something was different with my code because when I run it (beta01) the slider can be moved right, but not left! Is that what you saw too? Here's a look at example output of the code I posted. To make the messages easier to decipher (i.e., fewer of them) I just clicked the mouse at one point on the slider. First note that upon initialization, bug 75943373 hits:
Copy code
...I/System.out: SliderTest: Composed
...I/System.out: SliderTest: Position changed: userPosition = 0.0 (position = 0.0)
Clicking to the right (the thumb jumps as expected) gives:
Copy code
...I/System.out: SliderTest: Position changed: userPosition = 0.8638611 (position = 0.0)
...I/System.out: SliderTest: User selected position: (position = 0.8638611)
...I/System.out: SliderTest: Composed
But then clicking to the left (the thumb doesn't move) gives:
Copy code
...I/System.out: SliderTest: Position changed: userPosition = 0.087005615 (position = 0.8638611)
...I/System.out: SliderTest: Position changed: userPosition = 0.8638611 (position = 0.8638611)
...I/System.out: SliderTest: User selected position: (position = 0.8638611)
Thomas, that second callback is likely bug 75943373 as well, as you suspected. It overrides the position with the old position. I don't know why it only happens in that direction though. This has not been a problem in my app (except at initialization), maybe because I don't use
onValueChangeFinished.
r
Yes. The same happened with my code. When I declared two variable states
r
I'm hoping now that beta's here that some of the
Slider
issues will be addressed.
RUSTAM https://issuetracker.google.com/u/3/issues/175943373 has only 3 stars so if you care to star it.. Thanks.
I put this example in the bug comments.
154 Views