diff --git a/app/src/main/java/com/rodalc/amarracos/mus/Mus.kt b/app/src/main/java/com/rodalc/amarracos/mus/Mus.kt index b2f651f..17d0230 100644 --- a/app/src/main/java/com/rodalc/amarracos/mus/Mus.kt +++ b/app/src/main/java/com/rodalc/amarracos/mus/Mus.kt @@ -38,5 +38,10 @@ object Mus { this.envites = envites } + fun resolverEnvite(ronda: RondasMus, pareja: Pareja) { + pareja.puntos += this.envites.getEnvite(ronda) + this.envites.setEnvite(ronda, 0) + } + } diff --git a/app/src/main/java/com/rodalc/amarracos/mus/MusViewModel.kt b/app/src/main/java/com/rodalc/amarracos/mus/MusViewModel.kt new file mode 100644 index 0000000..29f5f16 --- /dev/null +++ b/app/src/main/java/com/rodalc/amarracos/mus/MusViewModel.kt @@ -0,0 +1,46 @@ +package com.rodalc.amarracos.mus + +import androidx.lifecycle.ViewModel +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow + +class MusViewModel : ViewModel() { + private val _buenos = MutableStateFlow(Mus.getBuenos()) + val buenos: StateFlow = _buenos + + private val _malos = MutableStateFlow(Mus.getMalos()) + val malos: StateFlow = _malos + + private val _envites = MutableStateFlow(Mus.getEnvites()) + val envites: StateFlow = _envites + + private val _puntos = MutableStateFlow(Mus.getPuntos()) + val puntos: StateFlow = _puntos + + fun updateBuenos(buenos: Pareja) { + Mus.setBuenos(buenos) + _buenos.value = buenos + } + + fun updateMalos(malos: Pareja) { + Mus.setMalos(malos) + _malos.value = malos + } + + fun updateEnvites(envites: Envites) { + Mus.setEnvites(envites) + _envites.value = envites + } + + fun updatePuntos(puntos: Int) { + Mus.setPuntos(puntos) + _puntos.value = puntos + } + + fun resolverEnvite(ronda: RondasMus, pareja: Pareja) { + Mus.resolverEnvite(ronda, pareja) + _buenos.value = Mus.getBuenos() + _malos.value = Mus.getMalos() + _envites.value = Mus.getEnvites() + } +} \ No newline at end of file diff --git a/app/src/main/java/com/rodalc/amarracos/mus/PantallaMus.kt b/app/src/main/java/com/rodalc/amarracos/mus/PantallaMus.kt index f2eb1bd..a4b6ef8 100644 --- a/app/src/main/java/com/rodalc/amarracos/mus/PantallaMus.kt +++ b/app/src/main/java/com/rodalc/amarracos/mus/PantallaMus.kt @@ -14,6 +14,8 @@ import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.width import androidx.compose.foundation.text.KeyboardOptions import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.automirrored.rounded.ArrowBack +import androidx.compose.material.icons.automirrored.rounded.ArrowForward import androidx.compose.material.icons.rounded.Add import androidx.compose.material.icons.rounded.Done import androidx.compose.material.icons.rounded.Remove @@ -24,10 +26,9 @@ import androidx.compose.material3.Text import androidx.compose.material3.TextButton import androidx.compose.material3.TextField import androidx.compose.runtime.Composable +import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableIntStateOf import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment @@ -48,6 +49,7 @@ import com.rodalc.amarracos.main.ToastRateLimiter fun PantallaMus() { var showConfig by rememberSaveable { mutableStateOf(true) } val context = LocalContext.current + if (showConfig) { PantallaConfiguracion(context) { showConfig = it } } else { @@ -128,6 +130,9 @@ fun PantallaConfiguracion( @Composable fun PlantillaMus() { + val viewModel = MusViewModel() + var rondaEnvites by rememberSaveable { mutableStateOf(true) } + // TODO: If landscape ... else ... Row( horizontalArrangement = Arrangement.SpaceAround, @@ -135,18 +140,18 @@ fun PlantillaMus() { modifier = Modifier.fillMaxSize() ) { Spacer(modifier = Modifier.weight(1f)) - ColumnaParejaLandscape(pareja = Mus.getBuenos()) + ColumnaParejaLandscape(buenos = true, viewModel) { rondaEnvites = !rondaEnvites } Spacer(modifier = Modifier.weight(1f)) - ColumnaEnvites() + ColumnaEnvites(viewModel, rondaEnvites) { rondaEnvites = !rondaEnvites } Spacer(modifier = Modifier.weight(1f)) - ColumnaParejaLandscape(pareja = Mus.getMalos()) + ColumnaParejaLandscape(buenos = false, viewModel) { rondaEnvites = !rondaEnvites } Spacer(modifier = Modifier.weight(1f)) } } @Composable -fun ColumnaParejaLandscape(pareja: Pareja) { - var puntos by rememberSaveable { mutableIntStateOf(pareja.puntos) } +fun ColumnaParejaLandscape(buenos: Boolean, viewModel: MusViewModel, onOrdago: () -> Unit) { + val pareja by if (buenos) viewModel.buenos.collectAsState() else viewModel.malos.collectAsState() Column( verticalArrangement = Arrangement.Center, @@ -166,15 +171,24 @@ fun ColumnaParejaLandscape(pareja: Pareja) { fontSize = 80.sp ) Row { - TextButton(onClick = { - puntos -= 1 - pareja.puntos = puntos - }) { + TextButton( + onClick = { + if (buenos) { + viewModel.updateBuenos(pareja.copy(puntos = pareja.puntos - 1)) + } else { + viewModel.updateMalos(pareja.copy(puntos = pareja.puntos - 1)) + } + }, + enabled = pareja.puntos > 0 + ) { Icon(Icons.Rounded.Remove, contentDescription = "Remove") } TextButton(onClick = { - puntos += 1 - pareja.puntos = puntos + if (buenos) { + viewModel.updateBuenos(pareja.copy(puntos = pareja.puntos + 1)) + } else { + viewModel.updateMalos(pareja.copy(puntos = pareja.puntos + 1)) + } }) { Icon(Icons.Rounded.Add, contentDescription = "Add") } @@ -188,7 +202,11 @@ fun ColumnaParejaLandscape(pareja: Pareja) { } @Composable -fun ColumnaEnvites() { +fun ColumnaEnvites(viewModel: MusViewModel, rondaEnvites: Boolean, changeRondaEmbites: () -> Unit) { + val buenos by viewModel.buenos.collectAsState() + val malos by viewModel.malos.collectAsState() + val envites by viewModel.envites.collectAsState() + Column( verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally, @@ -196,11 +214,35 @@ fun ColumnaEnvites() { .padding(10.dp) .fillMaxWidth(.35f) ) { - FilaEnvite(envite = Mus.getEnvites().grande) { Mus.getEnvites().grande = it } - FilaEnvite(envite = Mus.getEnvites().chica) { Mus.getEnvites().chica = it } - FilaEnvite(envite = Mus.getEnvites().pares) { Mus.getEnvites().pares = it } - FilaEnvite(envite = Mus.getEnvites().juego) { Mus.getEnvites().juego = it } - Button(onClick = { /*TODO*/ }) { + FilaEnvite(rondaEnvites, envites.grande, viewModel) { + viewModel.updateEnvites( + envites.copy( + grande = it + ) + ) + } + FilaEnvite(rondaEnvites, envites.chica, viewModel) { + viewModel.updateEnvites( + envites.copy( + chica = it + ) + ) + } + FilaEnvite(rondaEnvites, envites.pares, viewModel) { + viewModel.updateEnvites( + envites.copy( + pares = it + ) + ) + } + FilaEnvite(rondaEnvites, envites.juego, viewModel) { + viewModel.updateEnvites( + envites.copy( + juego = it + ) + ) + } + Button(onClick = { changeRondaEmbites() }) { Icon(Icons.Rounded.Done, contentDescription = "Done") } @@ -208,8 +250,15 @@ fun ColumnaEnvites() { } @Composable -fun FilaEnvite(envite: Int, setValue: (Int) -> Unit) { - var temp by remember { mutableIntStateOf(envite) } +fun FilaEnvite( + rondaEnvites: Boolean, + envite: Int, + viewModel: MusViewModel, + updateEnvite: (Int) -> Unit +) { + val buenos by viewModel.buenos.collectAsState() + val malos by viewModel.malos.collectAsState() + Row( verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.SpaceBetween, @@ -219,28 +268,46 @@ fun FilaEnvite(envite: Int, setValue: (Int) -> Unit) { ) { TextButton( onClick = { - temp -= 1 - setValue(temp) + if (rondaEnvites) { + updateEnvite(envite - 1) + } else { + viewModel.updateBuenos(buenos.copy(puntos = buenos.puntos + envite)) + updateEnvite(0) + } }, - enabled = temp > 0 + enabled = !rondaEnvites || envite > 0 ) { - Icon(Icons.Rounded.Remove, contentDescription = "Remove") + if (rondaEnvites) { + Icon(Icons.Rounded.Remove, contentDescription = "Remove") + } else { + Icon(Icons.AutoMirrored.Rounded.ArrowBack, contentDescription = "Buenos") + } } Text( - text = temp.toString(), + text = envite.toString(), fontSize = 20.sp, modifier = Modifier - .clickable(onClick = { - temp += 2 - setValue(temp) - }) + .clickable( + onClick = { + updateEnvite(envite + 2) + }, + enabled = rondaEnvites + ) .padding(10.dp) ) TextButton(onClick = { - temp += 1 - setValue(temp) + if (rondaEnvites) { + updateEnvite(envite + 1) + } else { + viewModel.updateMalos(malos.copy(puntos = malos.puntos + envite)) + updateEnvite(0) + } }) { - Icon(Icons.Rounded.Add, contentDescription = "Add") + if (rondaEnvites) { + Icon(Icons.Rounded.Add, contentDescription = "Add") + } else { + Icon(Icons.AutoMirrored.Rounded.ArrowForward, contentDescription = "Add") + } } } } \ No newline at end of file diff --git a/app/src/main/java/com/rodalc/amarracos/mus/Pareja.kt b/app/src/main/java/com/rodalc/amarracos/mus/Pareja.kt index 5047e0d..bfee455 100644 --- a/app/src/main/java/com/rodalc/amarracos/mus/Pareja.kt +++ b/app/src/main/java/com/rodalc/amarracos/mus/Pareja.kt @@ -12,4 +12,29 @@ data class Envites( var chica: Int = 0, var pares: Int = 0, var juego: Int = 0, -) \ No newline at end of file +) { + fun getEnvite(ronda: RondasMus): Int { + return when (ronda) { + RondasMus.GRANDE -> this.grande + RondasMus.CHICA -> this.chica + RondasMus.PARES -> this.pares + RondasMus.JUEGO -> this.juego + } + } + + fun setEnvite(ronda: RondasMus, envite: Int) { + when (ronda) { + RondasMus.GRANDE -> this.grande = envite + RondasMus.CHICA -> this.chica = envite + RondasMus.PARES -> this.pares = envite + RondasMus.JUEGO -> this.juego = envite + } + } +} + +enum class RondasMus { + GRANDE, + CHICA, + PARES, + JUEGO +}