From 673fd8ea5dc6a508924a8ecacd5f854195c22b5f Mon Sep 17 00:00:00 2001 From: beaulaton Date: Fri, 21 Mar 2025 11:22:29 +0100 Subject: [PATCH] viewModel pour username authentification marche --- What_The_Fantasy/app/build.gradle.kts | 5 +++- .../what_the_fantasy/ui/screens/LoginPage.kt | 28 ++++++++++++------- .../ui/states/AuthUserState.kt | 13 +++++++++ .../ui/viewModels/AuthUserViewModel.kt | 20 +++++++++++++ What_The_Fantasy/gradle/libs.versions.toml | 3 ++ 5 files changed, 58 insertions(+), 11 deletions(-) create mode 100644 What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/states/AuthUserState.kt create mode 100644 What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/viewModels/AuthUserViewModel.kt diff --git a/What_The_Fantasy/app/build.gradle.kts b/What_The_Fantasy/app/build.gradle.kts index 090bb7f..3b20b0e 100644 --- a/What_The_Fantasy/app/build.gradle.kts +++ b/What_The_Fantasy/app/build.gradle.kts @@ -71,7 +71,10 @@ dependencies { androidTestImplementation(libs.androidx.ui.test.junit4) debugImplementation(libs.androidx.ui.tooling) debugImplementation(libs.androidx.ui.test.manifest) - implementation(libs.coil.compose) //gére les url des image + implementation(libs.coil.compose) //gére les url des images implementation(kotlin("script-runtime")) + //ViewModel + implementation(libs.android.lifecycle.viewmodel) + implementation(libs.android.lifecycle.viewmodel.runtime.ktx) } \ No newline at end of file diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/LoginPage.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/LoginPage.kt index b08a21d..a8d0ba0 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/LoginPage.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/LoginPage.kt @@ -16,6 +16,7 @@ import androidx.compose.material3.MaterialTheme import androidx.compose.material3.OutlinedTextField import androidx.compose.material3.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember @@ -30,6 +31,7 @@ import androidx.compose.ui.text.input.PasswordVisualTransformation import androidx.compose.ui.text.input.VisualTransformation import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp +import androidx.lifecycle.viewmodel.compose.viewModel import com.example.what_the_fantasy.Logs.LogsUsers import com.example.what_the_fantasy.R import com.example.what_the_fantasy.data.model.User @@ -40,10 +42,13 @@ import com.example.what_the_fantasy.ui.components.TitlePageComponent import com.example.what_the_fantasy.ui.components.hashPassword import com.example.what_the_fantasy.ui.theme.colorBackground import com.example.what_the_fantasy.ui.theme.gradienBox +import com.example.what_the_fantasy.ui.viewModels.AuthUserViewModel @Composable fun LoginPage(navControllerSignUp: () -> Unit, navControllerProfil: (Int) -> Unit, services : IServices) { val users = services.getAllUsers() + val authUserVM : AuthUserViewModel = viewModel() + val authState by authUserVM.userState.collectAsState() Box( modifier = Modifier @@ -63,7 +68,9 @@ fun LoginPage(navControllerSignUp: () -> Unit, navControllerProfil: (Int) -> Uni TitlePageComponent(R.string.titleLogin, MaterialTheme.colorScheme.primary) SpaceHeightComponent(20) - ConnexionButtonLogin(users,IdentifiantTextField(R.string.IdentifiantLogin), PassWdTextField(R.string.PasswdLogin), R.string.ButtonLogin,18, Color.White, Color.Black,navControllerProfil) + ConnexionButtonLogin(users,IdentifiantTextField(R.string.IdentifiantLogin, authState.username){ + authUserVM.setUsername(it) + }, PassWdTextField(R.string.PasswdLogin), R.string.ButtonLogin,18,navControllerProfil) SpaceHeightComponent(16) CreateAccountButton(R.string.ButtonCreateLogin,12, MaterialTheme.colorScheme.primary, navControllerSignUp) } @@ -74,13 +81,14 @@ fun LoginPage(navControllerSignUp: () -> Unit, navControllerProfil: (Int) -> Uni @Composable -fun IdentifiantTextField(textIdentifiantResId : Int) : String{ +fun IdentifiantTextField(textIdentifiantResId : Int, username : String, onValueChange: (String) -> Unit ) : String{ val textIdentifiant = stringResource(id = textIdentifiantResId) - var identifiant by remember { mutableStateOf("") } + //var identifiant by remember { mutableStateOf("") } + Column(modifier = Modifier.padding(top = 16.dp)) { OutlinedTextField( - value = identifiant, - onValueChange = { identifiant = it }, + value = username, + onValueChange = onValueChange, label = { Text(textIdentifiant) }, modifier = Modifier .fillMaxWidth() @@ -89,7 +97,7 @@ fun IdentifiantTextField(textIdentifiantResId : Int) : String{ shape = RoundedCornerShape(16.dp) ) } - return identifiant; + return username; } @Composable @@ -119,11 +127,11 @@ fun PassWdTextField(textpasswdResId : Int) : String{ @Composable -fun ConnexionButtonLogin(userStub : List, id : String, passwd : String, titleResId : Int, size : Int, colorButton : Color, colorText : Color, navController: (Int) -> Unit){ +fun ConnexionButtonLogin(users : List, username : String, passwd : String, titleResId : Int, size : Int, navController: (Int) -> Unit){ val title = stringResource(id = titleResId) var showError by remember { mutableStateOf(false) } Button( - onClick = { showError = !validLogin(id, passwd, userStub, navController) + onClick = { showError = !validLogin(username, passwd, users, navController) }, colors = ButtonDefaults.buttonColors(containerColor = MaterialTheme.colorScheme.background), modifier = Modifier @@ -138,12 +146,12 @@ fun ConnexionButtonLogin(userStub : List, id : String, passwd : String, ti } -fun validLogin(identifiant : String, passwd : String, users : List, navController: (Int) -> Unit): Boolean { +fun validLogin(username : String, passwd : String, users : List, navController: (Int) -> Unit): Boolean { val logsUser = LogsUsers() //gestion des logs pour les utilisateurs users.forEachIndexed { index, user -> val hashPassWd = hashPassword(passwd) - if (user.username == identifiant && user.password == hashPassWd) { + if (user.username == username && user.password == hashPassWd) { navController(index) logsUser.logInformationUserConnect(user, "UserConnect") return true diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/states/AuthUserState.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/states/AuthUserState.kt new file mode 100644 index 0000000..cf6c8c5 --- /dev/null +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/states/AuthUserState.kt @@ -0,0 +1,13 @@ +package com.example.what_the_fantasy.ui.states + +import com.example.what_the_fantasy.data.model.SrcLanguage +import com.example.what_the_fantasy.data.model.User + +data class AuthUserState ( + val username : String = "", + val password: String = "", + //val email:String = "", + //val date:String = "", + //val imgUrl: String = "", + //val langage : SrcLanguage = SrcLanguage.vo +) \ No newline at end of file diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/viewModels/AuthUserViewModel.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/viewModels/AuthUserViewModel.kt new file mode 100644 index 0000000..1321f12 --- /dev/null +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/viewModels/AuthUserViewModel.kt @@ -0,0 +1,20 @@ +package com.example.what_the_fantasy.ui.viewModels + +import androidx.lifecycle.ViewModel +import com.example.what_the_fantasy.data.services.ServicesStub +import com.example.what_the_fantasy.ui.states.AuthUserState +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.asStateFlow +import kotlinx.coroutines.flow.update + +class AuthUserViewModel : ViewModel(){ + private val services = ServicesStub() // faire repository qui gère les services Stub et API + private val _userState = MutableStateFlow(AuthUserState()) + val userState : StateFlow = _userState.asStateFlow() + + + fun setUsername(username : String){ + _userState.update { it.copy(username=username) } + } +} \ No newline at end of file diff --git a/What_The_Fantasy/gradle/libs.versions.toml b/What_The_Fantasy/gradle/libs.versions.toml index 26f2026..a855f46 100644 --- a/What_The_Fantasy/gradle/libs.versions.toml +++ b/What_The_Fantasy/gradle/libs.versions.toml @@ -14,6 +14,7 @@ composeBom = "2024.04.01" navigationCompose = "2.8.6" navigationCommonAndroid = "2.9.0-alpha05" engageCore = "1.5.6" +lifecycle = "2.8.0" [libraries] androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" } @@ -37,6 +38,8 @@ androidx-navigation-compose = { group = "androidx.navigation", name = "navigatio androidx-navigation-common-android = { group = "androidx.navigation", name = "navigation-common-android", version.ref = "navigationCommonAndroid" } engage-core = { group = "com.google.android.engage", name = "engage-core", version.ref = "engageCore" } +android-lifecycle-viewmodel ={ group = "androidx.lifecycle", name ="lifecycle-viewmodel-compose", version.ref ="lifecycle"} +android-lifecycle-viewmodel-runtime-ktx ={ group = "androidx.lifecycle", name ="lifecycle-viewmodel-ktx", version.ref ="lifecycle"} [plugins] android-application = { id = "com.android.application", version.ref = "agp" } kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }