Skip to content

Commit

Permalink
mus con Model View ViewModel
Browse files Browse the repository at this point in the history
  • Loading branch information
RodAlc24 committed Jun 2, 2024
1 parent b62db5a commit 4be51e2
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 34 deletions.
5 changes: 5 additions & 0 deletions app/src/main/java/com/rodalc/amarracos/mus/Mus.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}


}
46 changes: 46 additions & 0 deletions app/src/main/java/com/rodalc/amarracos/mus/MusViewModel.kt
Original file line number Diff line number Diff line change
@@ -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<Pareja> = _buenos

private val _malos = MutableStateFlow(Mus.getMalos())
val malos: StateFlow<Pareja> = _malos

private val _envites = MutableStateFlow(Mus.getEnvites())
val envites: StateFlow<Envites> = _envites

private val _puntos = MutableStateFlow(Mus.getPuntos())
val puntos: StateFlow<Int> = _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()
}
}
133 changes: 100 additions & 33 deletions app/src/main/java/com/rodalc/amarracos/mus/PantallaMus.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -128,25 +130,28 @@ fun PantallaConfiguracion(

@Composable
fun PlantillaMus() {
val viewModel = MusViewModel()
var rondaEnvites by rememberSaveable { mutableStateOf(true) }

// TODO: If landscape ... else ...
Row(
horizontalArrangement = Arrangement.SpaceAround,
verticalAlignment = Alignment.CenterVertically,
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,
Expand All @@ -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")
}
Expand All @@ -188,28 +202,63 @@ 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,
modifier = Modifier
.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")
}

}
}

@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,
Expand All @@ -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")
}
}
}
}
27 changes: 26 additions & 1 deletion app/src/main/java/com/rodalc/amarracos/mus/Pareja.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,29 @@ data class Envites(
var chica: Int = 0,
var pares: Int = 0,
var juego: Int = 0,
)
) {
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
}

0 comments on commit 4be51e2

Please sign in to comment.