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

vide

04/04/2023, 1:51 AM
Question about compose.foundation: why is
DefaultFlingBehavior
private? To extend it I need to copy/paste code to my own codebase which is suboptimal 😕
c

chanjungskim

04/04/2023, 2:15 AM
How do you customize it?
v

vide

04/04/2023, 2:26 AM
I'm adding some normalization to the fling speed. It would be easy to call
super.performFling()
with a modified initial velocity
a

Albert Chang

04/04/2023, 3:11 AM
You don't need to. You can easily do something like this:
Copy code
@Composable
fun rememberMyFlingBehavior(): FlingBehavior {
    val defaultFlingBehavior = ScrollableDefaults.flingBehavior()
    return remember(defaultFlingBehavior) {
        object : FlingBehavior {
            override suspend fun ScrollScope.performFling(initialVelocity: Float): Float =
                with(defaultFlingBehavior) { performFling(adjustedVelocity) }
        }
    }
}
"Composition over inheritance" is a principle used everywhere in compose.
v

vide

04/04/2023, 3:13 AM
now I feel really dumb 😅
thanks for reminding me of this style!
c

chanjungskim

04/04/2023, 3:18 AM
"Composition over inheritance" seems difficult to come up in my head.
v

vide

04/04/2023, 10:18 AM
I was implementing this but actually not so sure about the simplicity anymore. It seems like I don't really understand the kotlin syntax here... why is the
with
function necessary?
a

Albert Chang

04/04/2023, 10:33 AM
Because
performFling()
function has a receiver type and that's how you call a member function with receiver.
v

vide

04/04/2023, 10:33 AM
this is relevant reading for anyone else wondering about this: I didn't really know you could even do this! https://kotlinlang.org/docs/extensions.html#declaring-extensions-as-members
Is there even any other way to do this without using
with
? Calling a member function with receiver?
a

Albert Chang

04/04/2023, 10:36 AM
You can also use
defaultFlingBehavior.run { performFling(velocity) }
.
c

chanjungskim

04/04/2023, 11:27 AM
72 Views