abbic
07/21/2023, 11:23 AMval coroutineScope = rememberCoroutineScope()
var counter by remember { mutableStateOf(0)}
val currentText by remember {
mutableStateOf(messages[counter])
}
var showText by remember { mutableStateOf(false)}
val textAlpha: Float by animateFloatAsState(
targetValue = if (showText) 1f else 0f,
finishedListener = { finalValue ->
if (finalValue == 0f) {
showText = true
counter = (counter + 1) % (messages.size)
} else {
coroutineScope.launch {
delay(20.seconds)
showText = false
}
}
}
)
Stylianos Gakis
07/21/2023, 11:36 AMStylianos Gakis
07/21/2023, 11:37 AMCrossFade
or just AnimatedContent
or something like that.abbic
07/21/2023, 11:37 AMStylianos Gakis
07/21/2023, 11:38 AMCrossFade
composable? See if it’s giving you more or less exactly what you want just by changing the text that you pass into it?abbic
07/21/2023, 11:38 AMStylianos Gakis
07/21/2023, 11:45 AMvar text by remember { mutableStateOf(messages.first()) }
LaunchedEffect(Unit) {
var counter = 1
while (isActive) {
text = messages[counter % messages.size]
counter = counter + 1
delay(20.seconds)
}
}
AnimatedContent(
targetState = text,
transitionSpec = {
val enterTransition = fadeIn(tween(durationMillis = 220, delayMillis = 220))
val exitTransition = fadeOut(tween(durationMillis = 220, delayMillis = 220))
enterTransition with exitTransition
},
contentAlignment = Alignment.Center,
) { resultingText ->
Text(resultingText)
}
would make it so that they don’t overlap. And you can play with the transitions as you wishAlbert Chang
07/21/2023, 1:46 PMvar counter by remember { mutableStateOf(0) }
val alpha = remember { Animatable(0f) }
LaunchedEffect(messages) {
while (true) {
alpha.animateTo(1f)
delay(20.seconds)
alpha.animateTo(0f)
counter = (counter + 1) % (messages.size)
}
}
Albert Chang
07/21/2023, 1:46 PMcurrentText
is for but it looks suspicious.abbic
07/21/2023, 1:48 PMStylianos Gakis
07/21/2023, 1:49 PMabbic
07/21/2023, 1:49 PMabbic
07/21/2023, 1:51 PMAlbert Chang
07/21/2023, 1:52 PMmessages
can change, you should do. Or at least LaunchedEffect(messages.size)
.abbic
07/21/2023, 1:52 PMabbic
07/21/2023, 1:53 PMStylianos Gakis
07/21/2023, 1:54 PMLaunchedEffect(Unit)
, if messages changes, your LaunchedEffect won’t be restarted and it will keep a reference to the old instance of messages
that you had passed into your composable.Stylianos Gakis
07/21/2023, 1:55 PMLaunchedEffect(messages) {
to avoid thisabbic
07/21/2023, 1:55 PMabbic
07/21/2023, 1:56 PMStylianos Gakis
07/21/2023, 1:56 PMLaunchedEffect
will get cancelled and everything will leave the memory completely. If you then call it again it will be a fresh instance yeahabbic
07/21/2023, 1:56 PMStylianos Gakis
07/21/2023, 1:57 PMStylianos Gakis
07/21/2023, 1:59 PMfun Composable(input: String) {
val updatedInput by rememberUpdatedState(input)
LaunchedEffect(Unit) {
while(something) {
doSomethingWith(updatedInput)
}
}
}
This now would mean that if your LaunchedEffect tries to use the updatedInput again, it will now get the latest value that was passed into your composable, whatever that is.
This will not make it re-trigger in case input changes though, so it really depends what you’re going for, and you decide according to your needs what approach you prefer.abbic
07/21/2023, 1:59 PMabbic
07/21/2023, 2:00 PM