https://kotlinlang.org logo
Title
t

Tgo1014

03/10/2023, 11:22 AM
Is there a way to combine
WindowInsets
with
PaddingValues
? I need both for
contentPadding
in a
LazyColumn
s

Stylianos Gakis

03/10/2023, 11:56 AM
What do you mean by
combine
? If you explain a bit better I think I might be able to help.
t

Tgo1014

03/10/2023, 11:58 AM
WindowInsets + PaddingValues
I've came up with this actually, but wanted to know if there's something official:
@Composable
operator fun PaddingValues.plus(paddingValues: PaddingValues): PaddingValues {
    val layoutDirection = LocalLayoutDirection.current
    return PaddingValues(
        top = this.calculateTopPadding() + paddingValues.calculateTopPadding(),
        bottom = this.calculateBottomPadding() + paddingValues.calculateBottomPadding(),
        start = this.calculateStartPadding(layoutDirection) + paddingValues.calculateStartPadding(layoutDirection),
        end = this.calculateEndPadding(layoutDirection) + paddingValues.calculateEndPadding(layoutDirection)
    )
}
And use it like this:
WindowInsets.navigationBars.asPaddingValues() + PaddingValues(16.dp)
s

Stylianos Gakis

03/10/2023, 12:06 PM
Okay now I understood your question 😄 I don’t think I’ve seen any other official API for this either. This looks just fine, I’d do the same.
e

ephemient

03/10/2023, 2:49 PM
why not
operator fun PaddingValues.plus(that: PaddingValues): PaddingValues = object : PaddingValues {
    override fun calculateBottomPadding(): Dp =
        this@plus.calculateBottomPadding() + that.calculateBottomPadding()

    override fun calculateLeftPadding(layoutDirection: LayoutDirection): Dp =
        this@plus.calculateLeftPadding(layoutDirection) + that.calculateLeftPadding(layoutDirection)

    override fun calculateRightPadding(layoutDirection: LayoutDirection): Dp =
        this@plus.calculateRightPadding(layoutDirection) + that.calculateRightPadding(layoutDirection)

    override fun calculateTopPadding(): Dp =
        this@plus.calculateTopPadding() + that.calculateTopPadding()
}
which avoids the need to make it
@Composable
?
t

Tgo1014

03/10/2023, 3:01 PM
@ephemient that's really cool, thanks for the code! 😄
a

Alex Vanyo

03/10/2023, 4:40 PM
Not only does that avoid the need to make it
@Composable
, it also delays the phase from composition to layout, which can be good for performance for animations and some inset quirks
m

mgrazianodecastro

03/10/2023, 7:14 PM
I learn so much with this community lol
s

Stylianos Gakis

03/10/2023, 7:23 PM
I don't think I would know even half of what I know now regarding Kotlin, compose and android in general if I never joined this slack channel. It's been a vital point in my progression, it's honestly one of the best things I did back in the day 😅
e

ephemient

03/11/2023, 1:37 AM
one thing I did a lot of as an early user of Stack Overflow, and which I think applies here too: if you see a question you don't know the answer to, try to do some research yourself. you don't have to share if you didn't find an answer or don't have confidence in it. but doing that first, and then comparing to other answers, is a great way to learn. at least in my experience