diff --git a/app/src/main/java/es/genol/tictactoe/ui/elements/AppContent.kt b/app/src/main/java/es/genol/tictactoe/ui/elements/AppContent.kt index c045266..e2d287d 100644 --- a/app/src/main/java/es/genol/tictactoe/ui/elements/AppContent.kt +++ b/app/src/main/java/es/genol/tictactoe/ui/elements/AppContent.kt @@ -8,10 +8,15 @@ import androidx.compose.material3.Button import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Scaffold +import androidx.compose.material3.SnackbarDuration +import androidx.compose.material3.SnackbarHost +import androidx.compose.material3.SnackbarHostState import androidx.compose.material3.Surface import androidx.compose.material3.Text import androidx.compose.material3.TopAppBar import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.remember import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalConfiguration import androidx.lifecycle.viewmodel.compose.viewModel @@ -25,6 +30,7 @@ fun AppContent() { val configuration = LocalConfiguration.current val orientationHeight: Float val orientationWidth: Float + val snackBarState = remember { SnackbarHostState() } when (configuration.orientation) { Configuration.ORIENTATION_PORTRAIT -> { @@ -38,6 +44,16 @@ fun AppContent() { } } + if (viewModel.isWinner) { + LaunchedEffect(key1 = viewModel.isWinner) { + viewModel.resetFromSnackBar(snackBarState.showSnackbar( + message = "Ganador", + actionLabel = "Reiniciar", + duration = SnackbarDuration.Indefinite + )) + } + } + TicTacToeTheme { Surface( modifier = Modifier.fillMaxSize(), @@ -51,7 +67,7 @@ fun AppContent() { Text(text = "REINICIAR") } }) - }) { + }, snackbarHost = { SnackbarHost(hostState = snackBarState) }) { Column(Modifier.padding(it)) { GameBoard( boardSize = 3, @@ -59,7 +75,8 @@ fun AppContent() { orientationWidth = orientationWidth, playerValue = { row, col -> viewModel.getPlayer(row, col) - } + }, + buttonEnabled = !viewModel.isWinner ) { row, col -> viewModel.printPosition(row, col) } diff --git a/app/src/main/java/es/genol/tictactoe/ui/elements/GameBoard.kt b/app/src/main/java/es/genol/tictactoe/ui/elements/GameBoard.kt index 996bc3f..43bfe8c 100644 --- a/app/src/main/java/es/genol/tictactoe/ui/elements/GameBoard.kt +++ b/app/src/main/java/es/genol/tictactoe/ui/elements/GameBoard.kt @@ -20,6 +20,7 @@ fun GameBoard( orientationHeight: Float, orientationWidth: Float, playerValue: (Int, Int) -> Boolean?, + buttonEnabled: Boolean, onButtonClick: (Int, Int) -> Unit ) { Column( @@ -41,12 +42,13 @@ fun GameBoard( Button( onClick = { onButtonClick(row, col) }, modifier = Modifier.size(75.dp), + enabled = buttonEnabled, shape = CircleShape ) { - playerValue(row, col).let { player -> - if (player == true){ + playerValue(row, col)?.let { player -> + if (player){ CircleIcon() - }else if(player == false){ + }else if(!player){ CrossIcon() } } diff --git a/app/src/main/java/es/genol/tictactoe/ui/state/TicTacToeViewModel.kt b/app/src/main/java/es/genol/tictactoe/ui/state/TicTacToeViewModel.kt index 7bfe580..73dfec3 100644 --- a/app/src/main/java/es/genol/tictactoe/ui/state/TicTacToeViewModel.kt +++ b/app/src/main/java/es/genol/tictactoe/ui/state/TicTacToeViewModel.kt @@ -1,6 +1,10 @@ package es.genol.tictactoe.ui.state +import androidx.compose.material3.SnackbarResult +import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateListOf +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.setValue import androidx.lifecycle.ViewModel import es.genol.tictactoe.data.model.Ficha import kotlin.random.Random @@ -9,6 +13,10 @@ class TicTacToeViewModel : ViewModel() { private var buttonStateList = mutableStateListOf() private var playerChange = ramdomPlayer() + var isWinner by mutableStateOf(false) + private set + + init { fillBoardGame() } @@ -17,7 +25,7 @@ class TicTacToeViewModel : ViewModel() { val index = buttonStateList.indexOf(buttonStateList.find { (it.row == row && it.col == col) }) if (buttonStateList[index].player == null) { buttonStateList[index] = buttonStateList[index].copy(player = playerChange) - horizontalCheck(player = playerChange) + isWinner = horizontalCheck(player = playerChange) playerChange = !playerChange } } @@ -29,6 +37,7 @@ class TicTacToeViewModel : ViewModel() { fun boardReboot() { buttonStateList.clear() playerChange = ramdomPlayer() + isWinner = false fillBoardGame() } @@ -57,5 +66,12 @@ class TicTacToeViewModel : ViewModel() { } return false } + + fun resetFromSnackBar(result: SnackbarResult){ + when (result) { + SnackbarResult.Dismissed -> {} + SnackbarResult.ActionPerformed -> { boardReboot() } + } + } }