https://kotlinlang.org logo
#compose
Title
# compose
r

rsktash

02/26/2021, 7:33 PM
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

Adam Powell

02/26/2021, 7:44 PM
please file as a bug
j

jim

02/26/2021, 7:50 PM
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

rsktash

02/26/2021, 7:50 PM
I see there another param
onValueChangeFinished
Then, should we store two values first is main value second is awaiting user interaction?
Like that ??
j

jim

02/26/2021, 7:54 PM
Why do you have a userPosition if you are not passing it to the slider?
r

rsktash

02/26/2021, 7:55 PM
There is a media player listener which updates position value
j

jim

02/26/2021, 7:56 PM
Oh,
onValueChangeFinished
doesn't have a value parameter, eww
Yeah, something like what you did seems very reasonable
r

rsktash

02/26/2021, 7:59 PM
Yes a bit strange ))
Ok. I removed second variable. Now I must ignore player position updates while user interaction
r

Rick Regan

02/26/2021, 10:41 PM
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

Thomas

02/27/2021, 10:01 AM
Seems you are experiencing this issue: https://issuetracker.google.com/u/0/issues/175943373
r

Rick Regan

02/27/2021, 1:44 PM
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

Thomas

02/27/2021, 8:40 PM
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

Rick Regan

02/27/2021, 9:30 PM
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

rsktash

02/27/2021, 10:41 PM
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

Thomas

02/27/2021, 11:37 PM
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

Rick Regan

02/27/2021, 11:50 PM
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

rsktash

02/28/2021, 4:37 PM
@Rick Regan Yes exactly . But then I removed userPosition
r

Rick Regan

02/28/2021, 5:16 PM
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

rsktash

02/28/2021, 5:30 PM
Yes. The same happened with my code. When I declared two variable states
r

Rick Regan

02/28/2021, 6:39 PM
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.
37 Views