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

ritesh

03/27/2022, 8:47 AM
I have a logic to show animated composable sliding in from top. I was wondering if there's a better way to achieve what i have, the
Side-effect
part? More in 🧵
Copy code
@Composable
fun MyComposable(somCondn:Boolean){
 var show by remember { mutableStateOf(false) }
 ComposableWhichAnimatesFromTop(show = show) // slides in from top 
 if(somCondn){
       LaunchedEffect(Unit) { // is there a better of doing this, Considering MyComposable can be called on every button click
        try {
          if (!show) {
            show = true
            delay(3000L)
          }
        } catch (ex: Exception) {
          show = false
        } finally {
          show = false
        }
      } 
 } else {
    // Show Loading
 }
}
c

Chris Sinco [G]

03/27/2022, 9:52 AM
Do you want the slide in every time this Composable enters composition? Would also be helpful to see a video of the desired effect
r

ritesh

03/27/2022, 9:55 AM
It's working the way i want, no i do-not want to slide in every-time. Considering a scenario where, on every button click
somCondn
is false to
showLoading
(the else block) and then it's
true
to show network-error.
Sharing the video in a while
It's working the way i want
Copy code
if(somCondn){
       LaunchedEffect(Unit) { // is there a better of doing this, Considering MyComposable can be called on every button click
        try {
          if (!show) {
            show = true
            delay(3000L)
          }
        } catch (ex: Exception) {
          show = false
        } finally {
          show = false
        }
      } 
 }
every time
LaunchedEffect
enter composition, it checks for the
show
flag, so there will be no multiple slide-ins. every time
LaunchedEffect
leaves or finishes it will make it false.
I was wondering, if there's a better way of doing it in effect-handler world, if i could avoid
try catch and finally
c

Chris Sinco [G]

03/27/2022, 7:59 PM
Have you also looked at AnimatedVisibility? Also curious if the try/catch is for Network exceptions?
I would actually pull the try/catch Network exception out of the Composable so it's not tightly coupled with this notification component. Then use a Boolean and/or Exception state to track whether to continue showing the notification or not (reduces need to recompose and show, and the animation is more likely to run once).
👀 1
r

ritesh

03/27/2022, 11:57 PM
I had added
try/catch/finally
block for cases when my
LaunchedEffect
gets cancelled in between (
catch
block) or completed successfully (
finally
block). It can just be
try {} finally {}
block and drop
catch {}
as in the end i am just making the
show=false
to slide it up.
c

Chris Sinco [G]

03/28/2022, 12:38 AM
I see. You could try the
AnimatedVisibility
API then which you can pass
show
as the
targetState
then define different animations when entering versus exiting e.g. sliding in versus sliding out
r

ritesh

03/28/2022, 12:49 AM
yeah,
MyComposable
will be always part of view-tree and
ComposableWhichAnimatesFromTop(show = show)
does takes the
show
as param and it internally uses
AnimatedVisibility
Sorry, if i was not clear before and thanks for bearing with me! Thanks Chris.
c

Chris Sinco [G]

03/28/2022, 12:54 AM
I see - I'm still not sure why the try/catch is in your LaunchedEffect if it's only controlling animation, but you may know better. What you could also do is control how you respond on the button click to filter out clicks / not respond to them if you know things are in a certain state.
👍 1
2 Views