Skip to content

Instantly share code, notes, and snippets.

@m-maillot
Created March 25, 2021 10:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m-maillot/24ab08f338023bf306109d78355790fd to your computer and use it in GitHub Desktop.
Save m-maillot/24ab08f338023bf306109d78355790fd to your computer and use it in GitHub Desktop.
When broken the animation
data class Bubble(val id: Int, val isBig: Boolean)
class TryViewModel : ViewModel() {
private val _uiState = MutableStateFlow<List<Bubble>>((1..6).map { Bubble(it, false) })
val uiState: StateFlow<List<Bubble>> = _uiState
fun run() = viewModelScope.launch(Dispatchers.Default) {
_uiState.value.forEachIndexed { index, value ->
delay(1000)
_uiState.value = _uiState.value.updated(index, value.copy(isBig = value.isBig.not()))
}
}
private fun <E> Iterable<E>.updated(index: Int, elem: E) =
mapIndexed { i, existing -> if (i == index) elem else existing }
}
@Composable
fun TryOther(tryViewModel: TryViewModel) {
val state = tryViewModel.uiState.collectAsState()
Column(modifier = Modifier.fillMaxHeight()) {
LazyVerticalGrid(cells = GridCells.Fixed(2)) {
items(state.value) { Bubble(it) }
}
Button(onClick = { tryViewModel.run() }) {
Text(text = "Start")
}
}
}
@Composable
fun Bubble(bubble: Bubble) {
when (bubble.isBig) {
true -> Bubble(isBig = true)
false -> Bubble(isBig = false)
}
}
@Composable
fun Bubble(isBig: Boolean) {
val padding by animateDpAsState(targetValue = if (isBig) 50.dp else 10.dp)
Box(modifier = Modifier.background(Color.Yellow)) {
Text(text = "Bubble", modifier = Modifier.padding(padding).background(Color.Red))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment