Composable state value not updating inside Coroutine

22 Views Asked by At

I am new at Jetpack Compose. I am trying to create Slot-Spinner game. Everything is working fine. Here is my below code.

Start Spinning using Compose Button.

Button(
                    onClick = {
                        if (cash >= bet) {
                            isSpinEnable = !isSpinEnable.also { spinSpeed = slowSpeed }
                            CoroutineScope(Dispatchers.Default).launch {
                                launch {
                                    isSpinEnable1 = isSpinEnable
                                }
                                launch {
                                    delay(100)
                                    isSpinEnable2 = isSpinEnable
                                }
                                launch {
                                    delay(200)
                                    isSpinEnable3 = isSpinEnable
                                }
                                launch {
                                    delay(300)
                                    isSpinEnable4 = isSpinEnable
                                }
                                launch {
                                    delay(400)
                                    isSpinEnable5 = isSpinEnable
                                }
                            }
                        } else {
                            isSpinEnable = false
                            Toast.makeText(
                                context,
                                context.getString(R.string.error_cash),
                                Toast.LENGTH_SHORT
                            ).show()
                            return@Button
                        }
                        if (!isSpinEnable) {
                            spinnerItems.clear()
                            spinnerItems.addAll(generateRandomNumbers())
                            var _cash = mutableStateOf(cash)
                            getResult(scaleSlotList, spinnerItems, _cash, win)
                            cash = _cash.value
                            spinSpeed = slowSpeed
                            if (isForeground) {
                                sndpool!!.play(sndStop, 0.5f, 0.5f, 0, 0, 1f)
                            }
                            GlobalScope.launch {
                                delay(3000)
                                scaleSlotList.forEachIndexed { index, value ->
                                    scaleSlotList[index] = false
                                }
                            }
                        } else {
                            cash -= bet
                            if (isForeground) {
                                sndpool!!.play(sndStart, 0.5f, 0.5f, 0, 0, 1f)
                            }
                            buttonVisibility = false
                        }
                        CoroutineScope(Dispatchers.Default).launch {
                            val spin1 = launch {
                                val index = 0
                                Log.e("svvdfvdfv", "Spin$index: $isSpinEnable1")
                                while (isSpinEnable1) {
                                    delay(spinSpeed + (index * 10))
                                    spinSpeed = if (spinSpeed == slowSpeed) fastSpeed else spinSpeed
                                    if (count[index] == 6) {
                                        count[index] = 0
                                    } else {
                                        count[index]++
                                    }
                                }
                            }
                            val spin2 = launch {
                                val index = 1
                                Log.e("svvdfvdfv", "Spin$index: $isSpinEnable2")
                                while (isSpinEnable2) {
                                    delay(spinSpeed + (index * 10))
                                    spinSpeed = if (spinSpeed == slowSpeed) fastSpeed else spinSpeed
                                    if (count[index] == 6) {
                                        count[index] = 0
                                    } else {
                                        count[index]++
                                    }
                                }
                            }
                            val spin3 = launch {
                                val index = 2
                                Log.e("svvdfvdfv", "Spin$index: $isSpinEnable3")
                                while (isSpinEnable3) {
                                    delay(spinSpeed + (index * 10))
                                    spinSpeed = if (spinSpeed == slowSpeed) fastSpeed else spinSpeed
                                    if (count[index] == 6) {
                                        count[index] = 0
                                    } else {
                                        count[index]++
                                    }
                                }
                            }
                            val spin4 = launch {
                                val index = 3
                                while (isSpinEnable4) {
                                    delay(spinSpeed + (index * 10))
                                    spinSpeed = if (spinSpeed == slowSpeed) fastSpeed else spinSpeed
                                    if (count[index] == 6) {
                                        count[index] = 0
                                    } else {
                                        count[index]++
                                    }
                                }
                            }
                            val spin5 = launch {
                                val index = 4
                                while (isSpinEnable5) {
                                    delay(spinSpeed + (index * 10))
                                    spinSpeed = if (spinSpeed == slowSpeed) fastSpeed else spinSpeed
                                    if (count[index] == 6) {
                                        count[index] = 0
                                    } else {
                                        count[index]++
                                    }
                                }
                            }
//                            sequenceOf(spin1, spin2, spin3, spin4, spin5)
                            spin1.join()
                            spin2.join()
                            spin3.join()
                            spin4.join()
                            spin5.join()
                            spin1.join()
                        }
//                        GlobalScope.launch {
//                            withContext(Dispatchers.Default) {
//                                while (isSpinEnable) {
//                                    delay(spinSpeed + (0 * 10))
//                                    spinSpeed = if (spinSpeed == slowSpeed) fastSpeed else spinSpeed
//                                    if (count[0] == 6) {
//                                        count[0] = 0
//                                    } else {
//                                        count[0]++
//                                    }
//                                }
//                            }
//                        }
                    },
                    enabled = buttonVisibility,
                    modifier = Modifier
                        .padding(top = 20.dp)
                        .wrapContentSize()
                        .scale(scaleButtonSize)
                        .alpha(if (buttonVisibility) 1f else 0f),
                    shape = RoundedCornerShape(10.dp),
                    border = BorderStroke(3.dp, Color.White),
                    colors = ButtonDefaults.buttonColors(
                        containerColor = Color.Red
                    )

                )

Variables

var isSpinEnable by remember {
        mutableStateOf(false)
    }
    LaunchedEffect(Unit) {
        isSpinEnable = false
    }
var isSpinEnable1 by remember {
        mutableStateOf(false)
    }
    var isSpinEnable2 by remember {
        mutableStateOf(false)
    }
    var isSpinEnable3 by remember {
        mutableStateOf(false)
    }
    var isSpinEnable4 by remember {
        mutableStateOf(false)
    }
    var isSpinEnable5 by remember {
        mutableStateOf(false)
    }

    //-------------------------------------

//    val isSpinEnable1State = isSpinEnable1.observeAsState()

    //------------------------------------
    LaunchedEffect(Unit) {
        isSpinEnable1 = false
    }
    LaunchedEffect(Unit) {
        isSpinEnable2 = false
    }
    LaunchedEffect(Unit) {
        isSpinEnable3 = false
    }
    LaunchedEffect(Unit) {
        isSpinEnable4 = false
    }
    LaunchedEffect(Unit) {
        isSpinEnable5 = false
    }
LaunchedEffect(Unit) {
        isSpinEnable1 = false
    }
    LaunchedEffect(Unit) {
        isSpinEnable2 = false
    }
    LaunchedEffect(Unit) {
        isSpinEnable3 = false
    }
    LaunchedEffect(Unit) {
        isSpinEnable4 = false
    }
    LaunchedEffect(Unit) {
        isSpinEnable5 = false
    }

Here i am trying to achieve a spinning effect should start one by one.

CoroutineScope(Dispatchers.Default).launch {
                                launch {
                                    isSpinEnable1 = isSpinEnable
                                }
                                launch {
                                    delay(100)
                                    isSpinEnable2 = isSpinEnable
                                }
                                launch {
                                    delay(200)
                                    isSpinEnable3 = isSpinEnable
                                }
                                launch {
                                    delay(300)
                                    isSpinEnable4 = isSpinEnable
                                }
                                launch {
                                    delay(400)
                                    isSpinEnable5 = isSpinEnable
                                }
                            }

But the issue is below code is not working beacuse isSpinEnable1 value is not updating in below code.

CoroutineScope(Dispatchers.Default).launch {
                            val spin1 = launch {
                                val index = 0
                                while (isSpinEnable1) {
                                    delay(spinSpeed + (index * 10))
                                    spinSpeed = if (spinSpeed == slowSpeed) fastSpeed else spinSpeed
                                    if (count[index] == 6) {
                                        count[index] = 0
                                    } else {
                                        count[index]++
                                    }
                                }
                            }
                            val spin2 = launch {
                                val index = 1
                                Log.e("svvdfvdfv", "Spin$index: $isSpinEnable2")
                                while (isSpinEnable2) {
                                    delay(spinSpeed + (index * 10))
                                    spinSpeed = if (spinSpeed == slowSpeed) fastSpeed else spinSpeed
                                    if (count[index] == 6) {
                                        count[index] = 0
                                    } else {
                                        count[index]++
                                    }
                                }
                            }
                            val spin3 = launch {
                                val index = 2
                                Log.e("svvdfvdfv", "Spin$index: $isSpinEnable3")
                                while (isSpinEnable3) {
                                    delay(spinSpeed + (index * 10))
                                    spinSpeed = if (spinSpeed == slowSpeed) fastSpeed else spinSpeed
                                    if (count[index] == 6) {
                                        count[index] = 0
                                    } else {
                                        count[index]++
                                    }
                                }
                            }
                            val spin4 = launch {
                                val index = 3
                                while (isSpinEnable4) {
                                    delay(spinSpeed + (index * 10))
                                    spinSpeed = if (spinSpeed == slowSpeed) fastSpeed else spinSpeed
                                    if (count[index] == 6) {
                                        count[index] = 0
                                    } else {
                                        count[index]++
                                    }
                                }
                            }
                            val spin5 = launch {
                                val index = 4
                                while (isSpinEnable5) {
                                    delay(spinSpeed + (index * 10))
                                    spinSpeed = if (spinSpeed == slowSpeed) fastSpeed else spinSpeed
                                    if (count[index] == 6) {
                                        count[index] = 0
                                    } else {
                                        count[index]++
                                    }
                                }
                            }
                            spin1.join()
                            spin2.join()
                            spin3.join()
                            spin4.join()
                            spin5.join()
                            spin1.join()
                        }

Value is updating but not reflecting in those spin section.

I am trying to create a slot spinner. It's working, but the problem is that it starts and stops each reel at the same time. I am trying to achieve that it should start spinning one by one, but my logic is not working.

0

There are 0 best solutions below