Created
March 25, 2021 10:14
-
-
Save m-maillot/24ab08f338023bf306109d78355790fd to your computer and use it in GitHub Desktop.
When broken the animation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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