[ADD] Toast quand gagné, retour home quand perdu

RepositoryAndroid
Renaud BEURET 1 year ago
parent 028d60ee36
commit 11c1476334

@ -25,7 +25,7 @@ class MainActivity : ComponentActivity() {
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
NavHost()
NavHost(this)
}
}
}

@ -15,6 +15,7 @@ class PenduViewModel : ViewModel() {
if (uiState.value.motATrou.contains(lettre)) {
Log.d("PenduViewModel","L'utilisateur a fait une action invalide")
uiState.value = PenduUIState(false,
false,
uiState.value.nbViesRestantes,
uiState.value.motATrouver,
uiState.value.motATrou
@ -28,14 +29,20 @@ class PenduViewModel : ViewModel() {
nvMotATrou = nvMotATrou.replaceRange(index,index + 1, lettre.toString())
}
}
uiState.value = PenduUIState(true,
var isWon = false
if (nvMotATrou.equals(uiState.value.motATrouver)) {
isWon = true
}
uiState.value = PenduUIState(isWon,
true,
uiState.value.nbViesRestantes,
uiState.value.motATrouver,
nvMotATrou
)
} else {
Log.d("PenduViewModel","L'utilisateur s'est trompé de lettre")
uiState.value = PenduUIState(true,
uiState.value = PenduUIState(false,
true,
uiState.value.nbViesRestantes - 1,
uiState.value.motATrouver,
uiState.value.motATrou

@ -1,6 +1,7 @@
package fr.iut.sciencequest.ViewModels.UiStates
data class PenduUIState(
val isWon: Boolean = false,
val isActionGood: Boolean = false,
val nbViesRestantes: Int = 10,
val motATrouver: String = "Mot",

@ -1,5 +1,6 @@
package fr.iut.sciencequest.navigation
import android.content.Context
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
@ -18,7 +19,7 @@ import fr.iut.sciencequest.view.scientifiques.scientifiqueListeScreen
@Composable
fun NavHost() {
fun NavHost(context: Context) {
val navController = rememberNavController()
NavHost(
modifier = Modifier.fillMaxSize(),
@ -71,7 +72,8 @@ fun NavHost() {
},
goToHome = {
navController.navigate("home")
}
},
context = context
)
}

@ -1,5 +1,7 @@
package fr.iut.sciencequest.view.games
import android.content.Context
import android.widget.Toast
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
@ -19,6 +21,7 @@ import fr.iut.sciencequest.view.TopBar
@Composable
fun PenduScreen(viewModel: PenduViewModel = viewModel(),
context: Context,
goToAccount: () -> Unit,
goToHome: () -> Unit) {
val state = viewModel.uiState.collectAsState()
@ -32,6 +35,11 @@ fun PenduScreen(viewModel: PenduViewModel = viewModel(),
onValueChange = {
if (it.isNotEmpty()) {
viewModel.PlayAction(it[0])
if ((!state.value.isWon) && (state.value.nbViesRestantes == 0)) {
goToHome()
} else {
Toast.makeText(context,"Vous avez gagné !",Toast.LENGTH_LONG).show()
}
}},
modifier = Modifier.padding(20.dp))
}
@ -41,7 +49,7 @@ fun PenduScreen(viewModel: PenduViewModel = viewModel(),
@Preview
@Composable
fun PenduPreview(){
PenduScreen(goToAccount = {}) {
}
//PenduScreen(goToAccount = {}) {
//
//}
}
Loading…
Cancel
Save