Advertisement
EnGold

Untitled

Sep 3rd, 2023
1,268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.88 KB | None | 0 0
  1. //SCREEN
  2. @Composable
  3. fun MainScreen(bGAViewModel: BackgroundAnimationViewModel = viewModel()) {
  4.     val list by bGAViewModel.list.collectAsState()
  5.     Box(Modifier.fillMaxSize()) {
  6.         //adding new item in viewmodel every second
  7.         LaunchedEffect(key1 = true) {
  8.             while (true) {
  9.                 delay(1000)
  10.                 bGAViewModel.addNewItem()
  11.             }
  12.         }
  13.         //container of items
  14.         Box(modifier = Modifier.fillMaxSize()) {
  15.             list.forEach { item ->
  16.                 BackgroundDropItem(
  17.                     bGAViewModel = bGAViewModel, backDropItem = item
  18.                 )
  19.             }
  20.         }
  21.         //When you tap to button screen will refreshed because of count variable, but viewmodel wont update screen
  22.         Row {
  23.             var count by remember {
  24.                 mutableStateOf(0)
  25.             }
  26.             Button(onClick = {
  27.                 count++
  28.                 Log.d("LOGG", list.toString())
  29.             }) {
  30.                 Text(text = "tap")
  31.             }
  32.             Text(text = count.toString())
  33.         }
  34.     }
  35. }
  36. //item that's dropping
  37. @Composable
  38. fun BackgroundDropItem(
  39.     bGAViewModel: BackgroundAnimationViewModel, backDropItem: BackDropData
  40. ) {
  41.     Box {
  42.         // animteAs*State animatiob
  43.         val i by animateDpAsState(targetValue = if (backDropItem.state == DropState.Start) 0.dp else 600.dp,
  44.             animationSpec = tween(
  45.                 durationMillis = (750..1500).random(), easing = FastOutLinearInEasing
  46.             ),
  47.             label = "",
  48.             finishedListener = {
  49.                 bGAViewModel.deleteItem(backDropItem)
  50.             })
  51.         Image(
  52.             painter = painterResource(id = R.drawable.circle),
  53.             contentDescription = "icon",
  54.             modifier = Modifier
  55.                 .offset(backDropItem.axisX.dp, i.value.dp)
  56.                 .size(50.dp)
  57.         )
  58.         //starting animation
  59.         bGAViewModel.changeState(backDropItem)
  60.     }
  61. }
  62.  
  63.  
  64. //ViewModel
  65. class BackgroundAnimationViewModel: ViewModel() {
  66.     private var item: Int = 1
  67.     private var _list = MutableStateFlow(listOf(BackDropData(0)).toMutableList())
  68.     var list: StateFlow<List<BackDropData>> = _list.asStateFlow()
  69.    
  70.     //adding new BackDropData item in _list
  71.     fun addNewItem() {
  72.         _list.value.add(BackDropData(number = item++, axisX = (0..350).random()))
  73.     }
  74.     //deleting item from _list
  75.     fun deleteItem(item: BackDropData) {
  76.         _list.value.remove(item)
  77.     }
  78.     //change state for animation
  79.     fun changeState(item: BackDropData){
  80.         item.state = DropState.Finish
  81.     }
  82. }
  83.  
  84. //Data
  85. //State for animation
  86. enum class DropState {
  87.     Start, Finish
  88. }
  89.  
  90. data class BackDropData(
  91.     val number: Int,
  92.     var state: DropState = DropState.Start,
  93.     //X axis for calculating random X position on screen
  94.     val axisX: Int = 0
  95. )
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement