How I can trigger a piece of code only in the init...
# compose
a
How I can trigger a piece of code only in the initial composition inside a function?
d
Copy code
LaunchedEffect(key = null) {
    // First time only stuff
}
a
I need to trigger the code inside offset lambda in modifier like this:
Copy code
Modifier.offset {
//{Trigger code in the initial composition only}
IntOffset(offsetTransitionActions.roundToInt(), 0)
}
LaunchedEffect{}
need a composable scope which is not exists in offset lambda.
d
Well in that case, call me old fashioned but you could simply use an
isFirstTime
flag? Composition is single threaded so it should be safe (for now).
👍 1
f
If you want to animate the offset, there are helpers for that (animating a value from start to target on initial composition).
a
I have
offsetTransitionActions
to animate the offsite but it depends on another value that takes some mills to calculate. so I want to set a value to the offset modifier while the calculation gets ready.
z
Don't rely on composition being single-threaded, that could eventually change
I think the offset lambda is probably the wrong place for logic like this. The only reason that is a lambda is so you can do state reads without triggering recomposition.
a
Well, how to do this logic in your opinion?
z
Can you elaborate what you're actually trying to do?
a
I want to implement "Swipe to show buttons" feature to my card as the following:   -- Box       -- Card       -- Row(here I put the buttons which showing when swipe the card) Now the card and row are visible next to each other so I want to offset the row to be invisible in the normal look and return it to 0 offsets(visible) when the user swipes the card. when observing user's swipe on the card I do the following: • Change the card's offset based on the Row's width to the left via 
animateFloat
like this:
Copy code
val offsetTransitionCard by transition.animateFloat(
    targetValueByState = { if (isRevealed) -rowWidth else 0f },
)
• Change the row's offset to 0 via 
animateFloat
like this:
Copy code
val offsetTransitionRow by transitionActions.animateFloat(
    targetValueByState = { if (isRevealed) 0f else rowWidth },
)
till now all good but we have one problem, when initial composition occurred the
rowWidth
will be 0 because it's not calculated yet which means the Row offset will be 0 too which means again the Row will be showing for mills then will offset when the
rowWidth
gets ready. So I want a way to make the row's default offset to be like 500 instead of 0 on the initial compose only.
Video clips feature is more than awesome!