google-developer-training / basic-android-kotlin-compose-training-lemonade

Apache License 2.0
53 stars 27 forks source link

🚑 Lemon Click event issue #40

Open hyeonhh opened 1 year ago

hyeonhh commented 1 year ago

` @Composable fun makingLemonade( contentText: String, modifier: Modifier=Modifier.padding(16.dp)) {

var isClicked by remember { mutableStateOf(1) }
var randomLemon=(2..4).random()
var squeezedLemon by remember { mutableStateOf(randomLemon)
}
var tabCountNow by remember {
    mutableStateOf(0)
}

val imageResource = when (isClicked) {
    1-> R.drawable.lemon_tree
    2->R.drawable.lemon_squeeze
    3->R.drawable.lemon_drink
    else->R.drawable.lemon_restart
}

val textResource = when (isClicked) {
    1 -> R.string.step1_body
    2 ->R.string.step2_body
    3 -> R.string.step3_body
    else -> R.string.step4_body
}

Column(
    modifier = modifier,
    verticalArrangement = Arrangement.Center,
    horizontalAlignment = Alignment.CenterHorizontally

) {
    Image(
        painter = painterResource(id = imageResource),
        contentDescription = contentText,
        contentScale= ContentScale.Crop,
        modifier= modifier
            .clip(RoundedCornerShape(20.dp))
            .background(Color(0xFF92B8B1))
            .clickable {
                if (isClicked == 2) {
                    //todo : tabcount만큼 클릭이 되면
                    if (squeezedLemon == tabCountNow) isClicked = 3
                    else tabCountNow++

                }
                if (isClicked == 4) {
                    isClicked = 1
                } else {
                    isClicked++
                }

            }

    )

}

This is how I implemented the code. First of all, we looked at each process of making lemonade as the number 1,2,3,4 in the isClick variable. RandomLemon variable randomly generates the number of times a lemon needs to be pressed This was saved in the squaredzedLemon variable.

The tabCountNow variable has been remembered to save the number of times the current compression has been made.

And on Modifier.Clickable I implemented the following logic

But just one tap of the execution results and it goes to the next image. What's the problem?

`

RakhmetKotlin commented 8 months ago

Hi. Its because both of your if conditionals are true. isClicked == 2 true and then else branch in second if conditional also executes (isClicked++). Try this code. See if it works for you

                    if(isClicked != 2) {
                        isClicked++
                        if(isClicked > 4) {
                            isClicked = 1
                            squeezedLemon = (2..4).random()
                        }
                    } else {
                        squeezedLemon--
                        if (squeezedLemon == 0) {
                            isClicked = 3
                        }
                    }