From 673fd8ea5dc6a508924a8ecacd5f854195c22b5f Mon Sep 17 00:00:00 2001 From: beaulaton Date: Fri, 21 Mar 2025 11:22:29 +0100 Subject: [PATCH 01/15] 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" } From 819ef8d73960b2229125b0ee73853705dd85b61c Mon Sep 17 00:00:00 2001 From: beaulaton Date: Fri, 21 Mar 2025 11:29:36 +0100 Subject: [PATCH 02/15] viewModel password fonctionne aussi --- .../what_the_fantasy/ui/screens/LoginPage.kt | 15 ++++++++------- .../ui/viewModels/AuthUserViewModel.kt | 4 ++++ 2 files changed, 12 insertions(+), 7 deletions(-) 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 a8d0ba0..243e9dd 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 @@ -70,7 +70,9 @@ fun LoginPage(navControllerSignUp: () -> Unit, navControllerProfil: (Int) -> Uni SpaceHeightComponent(20) ConnexionButtonLogin(users,IdentifiantTextField(R.string.IdentifiantLogin, authState.username){ authUserVM.setUsername(it) - }, PassWdTextField(R.string.PasswdLogin), R.string.ButtonLogin,18,navControllerProfil) + }, PassWdTextField(R.string.PasswdLogin, authState.password){ + authUserVM.setPassword(it) + }, R.string.ButtonLogin,18,navControllerProfil) SpaceHeightComponent(16) CreateAccountButton(R.string.ButtonCreateLogin,12, MaterialTheme.colorScheme.primary, navControllerSignUp) } @@ -83,7 +85,6 @@ fun LoginPage(navControllerSignUp: () -> Unit, navControllerProfil: (Int) -> Uni @Composable fun IdentifiantTextField(textIdentifiantResId : Int, username : String, onValueChange: (String) -> Unit ) : String{ val textIdentifiant = stringResource(id = textIdentifiantResId) - //var identifiant by remember { mutableStateOf("") } Column(modifier = Modifier.padding(top = 16.dp)) { OutlinedTextField( @@ -101,14 +102,14 @@ fun IdentifiantTextField(textIdentifiantResId : Int, username : String, onValueC } @Composable -fun PassWdTextField(textpasswdResId : Int) : String{ +fun PassWdTextField(textpasswdResId : Int, password : String, onValueChange: (String) -> Unit) : String{ val textpasswd = stringResource(id = textpasswdResId) - var passwd by remember { mutableStateOf("") } var passwordVisible by remember { mutableStateOf(false) } + Column(modifier = Modifier.padding(top = 10.dp, bottom = 30.dp)) { OutlinedTextField( - value = passwd, - onValueChange = { passwd = it }, + value = password, + onValueChange = onValueChange, label = { Text(textpasswd) }, modifier = Modifier .fillMaxWidth() @@ -122,7 +123,7 @@ fun PassWdTextField(textpasswdResId : Int) : String{ shape = RoundedCornerShape(16.dp) ) } - return passwd; + return password; } 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 index 1321f12..59f1ef2 100644 --- 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 @@ -17,4 +17,8 @@ class AuthUserViewModel : ViewModel(){ fun setUsername(username : String){ _userState.update { it.copy(username=username) } } + + fun setPassword(password : String){ + _userState.update { it.copy(password=password) } + } } \ No newline at end of file From 619526be172d9f30abce9d9fa46d8d9c0b318344 Mon Sep 17 00:00:00 2001 From: beaulaton Date: Fri, 21 Mar 2025 11:42:00 +0100 Subject: [PATCH 03/15] =?UTF-8?q?D=C3=A9placement=20de=20la=20fonction=20l?= =?UTF-8?q?ogin=20dans=20service?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../what_the_fantasy/data/services/IServices.kt | 2 ++ .../data/services/ServicesStub.kt | 15 +++++++++++++++ .../what_the_fantasy/ui/screens/LoginPage.kt | 6 +++--- .../ui/viewModels/AuthUserViewModel.kt | 4 ++++ 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/IServices.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/IServices.kt index afa5cd6..af40ed1 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/IServices.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/IServices.kt @@ -5,6 +5,8 @@ import com.example.what_the_fantasy.data.model.Quote import com.example.what_the_fantasy.data.model.User interface IServices { + + fun validLogin(username : String, passwd : String, navController: (Int) -> Unit): Boolean fun EditUsername(username : String, index : Int) : Boolean fun EditEmail(email : String, index : Int) : Boolean fun EditPasswd(passwd : String, index : Int) diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesStub.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesStub.kt index 7e8eab0..4dcb5f9 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesStub.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesStub.kt @@ -15,6 +15,21 @@ import java.time.LocalDate class ServicesStub : IServices { val logsUser = LogsUsers() //gestion des logs pour les utilisateurs + + override fun validLogin(username : String, passwd : String, navController: (Int) -> Unit): Boolean{ + + users.forEachIndexed { index, user -> + val hashPassWd = hashPassword(passwd) + if (user.username == username && user.password == hashPassWd) { + navController(index) + logsUser.logInformationUserConnect(user, "UserConnect") + return true + } + } + return false + } + + override fun EditUsername(username: String, index : Int) : Boolean{ val user = getUserById(index) 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 243e9dd..58d610f 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 @@ -68,7 +68,7 @@ fun LoginPage(navControllerSignUp: () -> Unit, navControllerProfil: (Int) -> Uni TitlePageComponent(R.string.titleLogin, MaterialTheme.colorScheme.primary) SpaceHeightComponent(20) - ConnexionButtonLogin(users,IdentifiantTextField(R.string.IdentifiantLogin, authState.username){ + ConnexionButtonLogin(authUserVM,IdentifiantTextField(R.string.IdentifiantLogin, authState.username){ authUserVM.setUsername(it) }, PassWdTextField(R.string.PasswdLogin, authState.password){ authUserVM.setPassword(it) @@ -128,11 +128,11 @@ fun PassWdTextField(textpasswdResId : Int, password : String, onValueChange: (St @Composable -fun ConnexionButtonLogin(users : List, username : String, passwd : String, titleResId : Int, size : Int, navController: (Int) -> Unit){ +fun ConnexionButtonLogin(authUserVM : AuthUserViewModel, 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(username, passwd, users, navController) + onClick = { showError = !authUserVM.validLogin(username, passwd, navController) }, colors = ButtonDefaults.buttonColors(containerColor = MaterialTheme.colorScheme.background), modifier = Modifier 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 index 59f1ef2..9fc6bfb 100644 --- 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 @@ -21,4 +21,8 @@ class AuthUserViewModel : ViewModel(){ fun setPassword(password : String){ _userState.update { it.copy(password=password) } } + + fun validLogin(username : String, passwd : String, navController: (Int) -> Unit) : Boolean{ + return services.validLogin(username,passwd,navController) + } } \ No newline at end of file From 197ea96cd20791d119b288d1a1d997fecdde8e6e Mon Sep 17 00:00:00 2001 From: beaulaton Date: Fri, 21 Mar 2025 11:45:41 +0100 Subject: [PATCH 04/15] =?UTF-8?q?suppression=20des=20services=20pass=C3=A9?= =?UTF-8?q?s=20en=20param=C3=A8tre=20=C3=A0=20login?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/what_the_fantasy/ui/navigations/AppNavigator.kt | 2 -- .../java/com/example/what_the_fantasy/ui/screens/LoginPage.kt | 3 +-- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/navigations/AppNavigator.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/navigations/AppNavigator.kt index 27546de..fd2daed 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/navigations/AppNavigator.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/navigations/AppNavigator.kt @@ -13,7 +13,6 @@ import androidx.navigation.compose.rememberNavController import androidx.navigation.toRoute import com.example.what_the_fantasy.data.services.ServicesStub import com.example.what_the_fantasy.ui.screens.* -import com.example.what_the_fantasy.ui.theme.colorNavBar import kotlinx.serialization.Serializable @Serializable @@ -78,7 +77,6 @@ fun AppNavigator() { popUpTo(Login) { inclusive = true } } }, - services = services ) } composable { 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 58d610f..7a14869 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 @@ -45,8 +45,7 @@ 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() +fun LoginPage(navControllerSignUp: () -> Unit, navControllerProfil: (Int) -> Unit) { val authUserVM : AuthUserViewModel = viewModel() val authState by authUserVM.userState.collectAsState() From 4489ca930e15967e03b867ee246fabdb858eed83 Mon Sep 17 00:00:00 2001 From: beaulaton Date: Fri, 21 Mar 2025 12:19:21 +0100 Subject: [PATCH 05/15] Archi pour viewmodel de profil --- .../repository/AuthRepository.kt | 6 ++++++ .../ui/states/InfoUserState.kt | 12 +++++++++++ .../ui/viewModels/InfoUserViewModel.kt | 20 +++++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/repository/AuthRepository.kt create mode 100644 What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/states/InfoUserState.kt create mode 100644 What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/viewModels/InfoUserViewModel.kt diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/repository/AuthRepository.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/repository/AuthRepository.kt new file mode 100644 index 0000000..cab157c --- /dev/null +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/repository/AuthRepository.kt @@ -0,0 +1,6 @@ +package com.example.what_the_fantasy.repository + +import com.example.what_the_fantasy.data.services.IServices +import okhttp3.Request + +class AuthRepository {} diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/states/InfoUserState.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/states/InfoUserState.kt new file mode 100644 index 0000000..e3fe6f7 --- /dev/null +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/states/InfoUserState.kt @@ -0,0 +1,12 @@ +package com.example.what_the_fantasy.ui.states + +import com.example.what_the_fantasy.data.model.SrcLanguage + +data class InfoUserState ( + val imagePath : String ="", + val username :String="", + val email : String="", + val password : String="", + val confirmPassword : String="", + val langue : 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/InfoUserViewModel.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/viewModels/InfoUserViewModel.kt new file mode 100644 index 0000000..3f5fc1a --- /dev/null +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/viewModels/InfoUserViewModel.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.InfoUserState +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.asStateFlow +import kotlinx.coroutines.flow.update + +class InfoUserViewModel : ViewModel(){ + private val services = ServicesStub() // faire repository qui gère les services Stub et API + private val _userInfoState = MutableStateFlow(InfoUserState()) + val InfoUserState : StateFlow = _userInfoState.asStateFlow() + + fun setUsername(username : String){ + _userInfoState.update { it.copy(username=username) } + + } +} \ No newline at end of file From 311d876e1cd684907a0866f96df6d7da7b59f9ad Mon Sep 17 00:00:00 2001 From: beaulaton Date: Wed, 26 Mar 2025 11:27:18 +0100 Subject: [PATCH 06/15] ViewModel Inscription --- .../data/services/IServices.kt | 2 +- .../data/services/ServicesStub.kt | 3 +- .../ui/navigations/AppNavigator.kt | 1 - .../what_the_fantasy/ui/screens/SignUpPage.kt | 58 +++++++++++++------ .../ui/states/AuthUserState.kt | 4 -- .../ui/states/SignInUserState.kt | 8 +++ .../ui/viewModels/SignInUserViewModel.kt | 35 +++++++++++ 7 files changed, 87 insertions(+), 24 deletions(-) create mode 100644 What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/states/SignInUserState.kt create mode 100644 What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/viewModels/SignInUserViewModel.kt diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/IServices.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/IServices.kt index af40ed1..5d753d5 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/IServices.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/IServices.kt @@ -13,7 +13,7 @@ interface IServices { fun EditImage(imageURL : String, index : Int) fun ChangeLangage(user: User) - fun CreateUser(username : String, email : String, passwd : String, services : IServices) : Boolean + fun CreateUser(username : String, email : String, passwd : String) : Boolean fun getFavorite(user: User): List fun getAllUsers(): List diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesStub.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesStub.kt index 4dcb5f9..6bec6e7 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesStub.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesStub.kt @@ -78,9 +78,10 @@ class ServicesStub : IServices { logsUser.logDebugUserLangage(user, "ChangeLangue") } - override fun CreateUser(username: String, email: String, passwd: String, services : IServices) : Boolean { + override fun CreateUser(username: String, email: String, passwd: String) : Boolean { val date =dateDuJour() val passwordhash = hashPassword(passwd) + val services = ServicesStub() val userStub = services.getAllUsers() val nbUser = userStub.size diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/navigations/AppNavigator.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/navigations/AppNavigator.kt index fd2daed..f0c6864 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/navigations/AppNavigator.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/navigations/AppNavigator.kt @@ -151,7 +151,6 @@ fun AppNavigator() { popUpTo(Login) { inclusive = true } } }, - services = services ) } composable { diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/SignUpPage.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/SignUpPage.kt index cce938e..314e7b7 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/SignUpPage.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/SignUpPage.kt @@ -1,5 +1,6 @@ package com.example.what_the_fantasy.ui.screens +import android.util.Log import androidx.compose.foundation.background import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Box @@ -20,6 +21,7 @@ import androidx.compose.material3.IconButton 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 @@ -34,21 +36,25 @@ 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.R -import com.example.what_the_fantasy.data.services.IServices import com.example.what_the_fantasy.ui.components.SpaceHeightComponent import com.example.what_the_fantasy.ui.components.TitlePageComponent 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.components.ErrorMessageProfileComponent +import com.example.what_the_fantasy.ui.states.SignInUserState +import com.example.what_the_fantasy.ui.viewModels.SignInUserViewModel @Composable -fun SignUpPage(navControllerLogin: () -> Unit, services : IServices) { +fun SignUpPage(navControllerLogin: () -> Unit) { var username by remember { mutableStateOf("") } var email by remember { mutableStateOf("") } var password by remember { mutableStateOf("") } var confirmPassword by remember { mutableStateOf("") } - var passwordVisible by remember { mutableStateOf(false) } + + val signInUserVM : SignInUserViewModel = viewModel() + val signInState by signInUserVM.userState.collectAsState() Box( modifier = Modifier @@ -67,14 +73,28 @@ fun SignUpPage(navControllerLogin: () -> Unit, services : IServices) { horizontalAlignment = Alignment.CenterHorizontally ) { - TitlePageComponent(R.string.titleSignUp,Color.White) - IdentifiantTextFieldSign(R.string.IdentifiantLogin,identifiant = username,onValueChange = { username = it }) - EmailTextFieldSign(R.string.EmailSignUp, email, onValueChange = { email = it }) - PassWdTextFieldSign(R.string.PasswdLogin,password, onValueChange = { password = it },passwordVisible,onPasswordVisibilityChange = { passwordVisible = !passwordVisible }) - PassWdConfirmTextFieldSign(R.string.ConfirmPassWdSignUp,confirmPassword,onValueChange = { confirmPassword = it },passwordVisible,onPasswordVisibilityChange = { passwordVisible = !passwordVisible }) + TitlePageComponent(R.string.titleSignUp,Color.White) // Page Title + + IdentifiantTextFieldSign(R.string.IdentifiantLogin,signInState.username){ // Username + signInUserVM.setUsername(it) + } + + EmailTextFieldSign(R.string.EmailSignUp, signInState.email){ // Email + signInUserVM.setEmail(it) + } + PassWdTextFieldSign(R.string.PasswdLogin,signInState.password){ // Password + signInUserVM.setPassword(it) + } + + PassWdConfirmTextFieldSign(R.string.ConfirmPassWdSignUp,signInState.confirmPassword){ // confirm Password + signInUserVM.setConfirmPassword(it) + } + SpaceHeightComponent(16) - ConnexionButtonSign(R.string.ButtonSignUp,18, Color.White, Color.Black, username, email, password, confirmPassword, services, navControllerLogin) + + ConnexionButtonSign(R.string.ButtonSignUp,18, Color.White, Color.Black, signInState.username, signInState.email, signInState.password, signInState.confirmPassword, signInUserVM, navControllerLogin) SpaceHeightComponent(16) + ReturnLogin(R.string.ButtonLogin,12, Color.White, navController = navControllerLogin) } @@ -82,8 +102,6 @@ fun SignUpPage(navControllerLogin: () -> Unit, services : IServices) { } - - @Composable fun IdentifiantTextFieldSign(textIdentifiantResId : Int, identifiant: String, onValueChange: (String) -> Unit){ val textIdentifiant = stringResource(id = textIdentifiantResId) @@ -120,8 +138,10 @@ fun EmailTextFieldSign(textIdentifiantResId: Int, email: String, onValueChange: } @Composable -fun PassWdTextFieldSign(textpasswdResId : Int, passwd: String, onValueChange: (String) -> Unit, passwordVisible: Boolean, onPasswordVisibilityChange: () -> Unit){ +fun PassWdTextFieldSign(textpasswdResId : Int, passwd: String, onValueChange: (String) -> Unit){ val textpasswd = stringResource(id = textpasswdResId) + var passwordVisible by remember { mutableStateOf(false) } + Column(modifier = Modifier.padding(top = 10.dp)) { OutlinedTextField( value = passwd, @@ -133,7 +153,9 @@ fun PassWdTextFieldSign(textpasswdResId : Int, passwd: String, onValueChange: (S keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password), visualTransformation = if (passwordVisible) VisualTransformation.None else PasswordVisualTransformation(), trailingIcon = { - IconButton(onClick = onPasswordVisibilityChange) { + IconButton(onClick = { + passwordVisible = !passwordVisible + }) { Icon(imageVector = Icons.Default.Check, contentDescription = "Valider") } }, @@ -143,8 +165,10 @@ fun PassWdTextFieldSign(textpasswdResId : Int, passwd: String, onValueChange: (S } @Composable -fun PassWdConfirmTextFieldSign(textpasswdResId : Int,confirmPassword: String, onValueChange: (String) -> Unit, passwordVisible: Boolean, onPasswordVisibilityChange: () -> Unit){ +fun PassWdConfirmTextFieldSign(textpasswdResId : Int,confirmPassword: String, onValueChange: (String) -> Unit){ val textpasswd = stringResource(id = textpasswdResId) + var passwordVisible by remember { mutableStateOf(false) } + Column(modifier = Modifier.padding(top = 10.dp)) { OutlinedTextField( value = confirmPassword, @@ -156,7 +180,7 @@ fun PassWdConfirmTextFieldSign(textpasswdResId : Int,confirmPassword: String, on keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password), visualTransformation = if (passwordVisible) VisualTransformation.None else PasswordVisualTransformation(), trailingIcon = { - IconButton(onClick = onPasswordVisibilityChange) { + IconButton(onClick = {passwordVisible = !passwordVisible}) { Icon(imageVector = Icons.Default.Check, contentDescription = "Valider") } }, @@ -187,7 +211,7 @@ fun ConnexionButtonSign( email: String, password: String, confirmPassword: String, - service: IServices, + viewModel: SignInUserViewModel, navController: ()-> Unit ) { val title = stringResource(id = titleResId) @@ -207,7 +231,7 @@ fun ConnexionButtonSign( passwordErrorEmpty = password.isBlank() || confirmPassword.isBlank() if (!emailError && !passwordError && !usernameError && !passwordErrorEmpty) { - usernameErrorExist = !service.CreateUser(username, email, password, service) + usernameErrorExist = !viewModel.createUser(username, email, password) if(!usernameErrorExist){ navController() // retour à la page login } 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 index cf6c8c5..10e361d 100644 --- 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 @@ -6,8 +6,4 @@ 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/states/SignInUserState.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/states/SignInUserState.kt new file mode 100644 index 0000000..ec44087 --- /dev/null +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/states/SignInUserState.kt @@ -0,0 +1,8 @@ +package com.example.what_the_fantasy.ui.states + +data class SignInUserState ( + val username : String ="", + val email : String ="", + val password : String ="", + val confirmPassword : String ="", +) \ No newline at end of file diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/viewModels/SignInUserViewModel.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/viewModels/SignInUserViewModel.kt new file mode 100644 index 0000000..42033f8 --- /dev/null +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/viewModels/SignInUserViewModel.kt @@ -0,0 +1,35 @@ +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.SignInUserState +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.asStateFlow +import kotlinx.coroutines.flow.update + +class SignInUserViewModel: ViewModel() { + private val services = ServicesStub() // faire repository qui gère les services Stub et API + private val _userState = MutableStateFlow(SignInUserState()) + val userState : StateFlow = _userState.asStateFlow() + + fun createUser(username: String, email: String, passwd: String) : Boolean{ + return services.CreateUser(username, email, passwd) + } + + fun setUsername(username : String){ + _userState.update { it.copy(username=username) } + } + + fun setEmail(email : String){ + _userState.update { it.copy(email=email) } + } + + fun setPassword(password : String){ + _userState.update { it.copy(password=password) } + } + fun setConfirmPassword(confirmPassword : String){ + _userState.update { it.copy(confirmPassword=confirmPassword) } + } + +} \ No newline at end of file From 1d9d3edc69f6b67c7c579c43b2725c7fc9af0b3e Mon Sep 17 00:00:00 2001 From: beaulaton Date: Wed, 26 Mar 2025 11:55:49 +0100 Subject: [PATCH 07/15] Ajout icon visible password --- .../VisibleIconPasswordComponent.kt | 28 +++++++++++++++++++ .../what_the_fantasy/ui/screens/LoginPage.kt | 2 ++ .../what_the_fantasy/ui/screens/ProfilPage.kt | 5 +++- .../what_the_fantasy/ui/screens/SignUpPage.kt | 16 +++++------ .../main/res/drawable/password_no_visible.xml | 18 ++++++++++++ .../main/res/drawable/password_visible.xml | 14 ++++++++++ 6 files changed, 73 insertions(+), 10 deletions(-) create mode 100644 What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/components/VisibleIconPasswordComponent.kt create mode 100644 What_The_Fantasy/app/src/main/res/drawable/password_no_visible.xml create mode 100644 What_The_Fantasy/app/src/main/res/drawable/password_visible.xml diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/components/VisibleIconPasswordComponent.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/components/VisibleIconPasswordComponent.kt new file mode 100644 index 0000000..cc502b9 --- /dev/null +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/components/VisibleIconPasswordComponent.kt @@ -0,0 +1,28 @@ +package com.example.what_the_fantasy.ui.components + +import androidx.compose.foundation.layout.size +import androidx.compose.material3.Icon +import androidx.compose.material3.MaterialTheme +import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import androidx.compose.ui.res.painterResource +import androidx.compose.ui.unit.dp +import com.example.what_the_fantasy.R + +@Composable +fun VisibleIconPasswordComponent(passwordVisible : Boolean){ + Icon( + painterResource( + if(passwordVisible){ + R.drawable.password_visible + } + else{ + R.drawable.password_no_visible + } + ), + contentDescription = "visible", + modifier = Modifier + .size(20.dp), + tint = MaterialTheme.colorScheme.primary + ) +} \ 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 7a14869..ac18a16 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 @@ -39,6 +39,7 @@ import com.example.what_the_fantasy.data.services.IServices import com.example.what_the_fantasy.ui.components.ErrorMessageProfileComponent import com.example.what_the_fantasy.ui.components.SpaceHeightComponent import com.example.what_the_fantasy.ui.components.TitlePageComponent +import com.example.what_the_fantasy.ui.components.VisibleIconPasswordComponent 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 @@ -117,6 +118,7 @@ fun PassWdTextField(textpasswdResId : Int, password : String, onValueChange: (St visualTransformation = if (passwordVisible) VisualTransformation.None else PasswordVisualTransformation(), trailingIcon = { IconButton(onClick = { passwordVisible = !passwordVisible }) { + VisibleIconPasswordComponent(passwordVisible) } }, shape = RoundedCornerShape(16.dp) diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/ProfilPage.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/ProfilPage.kt index 46893a8..a4f3eda 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/ProfilPage.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/ProfilPage.kt @@ -53,6 +53,7 @@ import com.example.what_the_fantasy.ui.components.ErrorMessageProfileComponent import com.example.what_the_fantasy.ui.components.NavBar import com.example.what_the_fantasy.ui.components.SpaceHeightComponent import com.example.what_the_fantasy.ui.components.TitlePageComponent +import com.example.what_the_fantasy.ui.components.VisibleIconPasswordComponent import com.example.what_the_fantasy.ui.theme.gradienBox @Composable @@ -315,6 +316,7 @@ fun EditPasswd(index: Int, service: IServices) { confirmPassword = it passwordError = newPassword != it // Vérifier si les mots de passe correspondent }, + passwordVisible = passwordVisible, onPasswordVisibilityChange = { passwordVisible = it }, passwordError = passwordError, @@ -327,6 +329,7 @@ fun EditPasswd(index: Int, service: IServices) { } else { DisplayPassword(onEdit = { isEditingPassword = true }) // Afficher l'option pour modifier le mot de passe } + } @Composable @@ -400,7 +403,7 @@ fun PasswordTextField( visualTransformation = if (passwordVisible) VisualTransformation.None else PasswordVisualTransformation(), trailingIcon = { IconButton(onClick = { onPasswordVisibilityChange(!passwordVisible) }) { - // Ajout d'une icône pour montrer/masquer le mot de passe + VisibleIconPasswordComponent(passwordVisible) } }, isError = isError diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/SignUpPage.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/SignUpPage.kt index 314e7b7..b1b38a1 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/SignUpPage.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/SignUpPage.kt @@ -1,6 +1,5 @@ package com.example.what_the_fantasy.ui.screens -import android.util.Log import androidx.compose.foundation.background import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Box @@ -8,6 +7,7 @@ import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.size import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.text.KeyboardOptions @@ -18,6 +18,7 @@ import androidx.compose.material3.Button import androidx.compose.material3.ButtonDefaults import androidx.compose.material3.Icon import androidx.compose.material3.IconButton +import androidx.compose.material3.MaterialTheme import androidx.compose.material3.OutlinedTextField import androidx.compose.material3.Text import androidx.compose.runtime.Composable @@ -30,6 +31,7 @@ import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.graphics.Color +import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.input.KeyboardType import androidx.compose.ui.text.input.PasswordVisualTransformation @@ -38,21 +40,17 @@ import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.lifecycle.viewmodel.compose.viewModel import com.example.what_the_fantasy.R +import com.example.what_the_fantasy.data.model.Image import com.example.what_the_fantasy.ui.components.SpaceHeightComponent import com.example.what_the_fantasy.ui.components.TitlePageComponent 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.components.ErrorMessageProfileComponent -import com.example.what_the_fantasy.ui.states.SignInUserState +import com.example.what_the_fantasy.ui.components.VisibleIconPasswordComponent import com.example.what_the_fantasy.ui.viewModels.SignInUserViewModel @Composable fun SignUpPage(navControllerLogin: () -> Unit) { - var username by remember { mutableStateOf("") } - var email by remember { mutableStateOf("") } - var password by remember { mutableStateOf("") } - var confirmPassword by remember { mutableStateOf("") } - val signInUserVM : SignInUserViewModel = viewModel() val signInState by signInUserVM.userState.collectAsState() @@ -156,7 +154,7 @@ fun PassWdTextFieldSign(textpasswdResId : Int, passwd: String, onValueChange: (S IconButton(onClick = { passwordVisible = !passwordVisible }) { - Icon(imageVector = Icons.Default.Check, contentDescription = "Valider") + VisibleIconPasswordComponent(passwordVisible) } }, shape = RoundedCornerShape(16.dp) @@ -181,7 +179,7 @@ fun PassWdConfirmTextFieldSign(textpasswdResId : Int,confirmPassword: String, on visualTransformation = if (passwordVisible) VisualTransformation.None else PasswordVisualTransformation(), trailingIcon = { IconButton(onClick = {passwordVisible = !passwordVisible}) { - Icon(imageVector = Icons.Default.Check, contentDescription = "Valider") + VisibleIconPasswordComponent(passwordVisible) } }, shape = RoundedCornerShape(16.dp) diff --git a/What_The_Fantasy/app/src/main/res/drawable/password_no_visible.xml b/What_The_Fantasy/app/src/main/res/drawable/password_no_visible.xml new file mode 100644 index 0000000..0487de8 --- /dev/null +++ b/What_The_Fantasy/app/src/main/res/drawable/password_no_visible.xml @@ -0,0 +1,18 @@ + + + + + diff --git a/What_The_Fantasy/app/src/main/res/drawable/password_visible.xml b/What_The_Fantasy/app/src/main/res/drawable/password_visible.xml new file mode 100644 index 0000000..b966f2a --- /dev/null +++ b/What_The_Fantasy/app/src/main/res/drawable/password_visible.xml @@ -0,0 +1,14 @@ + + + + From 1830ededb936852bff9f5ecbdfcaa1c7b18a5d20 Mon Sep 17 00:00:00 2001 From: "leni.beaulaton" Date: Sat, 29 Mar 2025 15:01:20 +0100 Subject: [PATCH 08/15] View Model pour le current en cours --- .../data/services/IServices.kt | 3 +- .../data/services/ServicesStub.kt | 12 +-- .../what_the_fantasy/ui/screens/ProfilPage.kt | 57 ++++++++------- .../{InfoUserState.kt => CurrentUserState.kt} | 11 +-- .../ui/viewModels/CurrentUserViewModel.kt | 73 +++++++++++++++++++ .../ui/viewModels/InfoUserViewModel.kt | 20 ----- 6 files changed, 120 insertions(+), 56 deletions(-) rename What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/states/{InfoUserState.kt => CurrentUserState.kt} (51%) create mode 100644 What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/viewModels/CurrentUserViewModel.kt delete mode 100644 What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/viewModels/InfoUserViewModel.kt diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/IServices.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/IServices.kt index 5d753d5..3ca9bd8 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/IServices.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/IServices.kt @@ -2,6 +2,7 @@ package com.example.what_the_fantasy.data.services import com.example.what_the_fantasy.data.model.Favorite import com.example.what_the_fantasy.data.model.Quote +import com.example.what_the_fantasy.data.model.SrcLanguage import com.example.what_the_fantasy.data.model.User interface IServices { @@ -11,7 +12,7 @@ interface IServices { fun EditEmail(email : String, index : Int) : Boolean fun EditPasswd(passwd : String, index : Int) fun EditImage(imageURL : String, index : Int) - fun ChangeLangage(user: User) + fun ChangeLangage(index : Int): SrcLanguage fun CreateUser(username : String, email : String, passwd : String) : Boolean fun getFavorite(user: User): List diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesStub.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesStub.kt index 6bec6e7..0b76750 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesStub.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesStub.kt @@ -68,14 +68,16 @@ class ServicesStub : IServices { TODO("Not yet implemented") } - override fun ChangeLangage(user: User) { - if(user.langage == SrcLanguage.vo){ - user.langage = SrcLanguage.vf + override fun ChangeLangage(index : Int) : SrcLanguage{ + if(getAllUsers()[index].langage == SrcLanguage.vo){ + getAllUsers()[index].langage = SrcLanguage.vf } else{ - user.langage = SrcLanguage.vo + getAllUsers()[index].langage = SrcLanguage.vo } - logsUser.logDebugUserLangage(user, "ChangeLangue") + + logsUser.logDebugUserLangage(getAllUsers()[index], "ChangeLangue") + return getAllUsers()[index].langage } override fun CreateUser(username: String, email: String, passwd: String) : Boolean { diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/ProfilPage.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/ProfilPage.kt index a4f3eda..877603a 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/ProfilPage.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/ProfilPage.kt @@ -27,6 +27,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 @@ -44,9 +45,11 @@ 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 coil.compose.AsyncImage import com.example.what_the_fantasy.Logs.LogsUsers import com.example.what_the_fantasy.R +import com.example.what_the_fantasy.data.model.SrcLanguage import com.example.what_the_fantasy.data.model.User import com.example.what_the_fantasy.data.services.IServices import com.example.what_the_fantasy.ui.components.ErrorMessageProfileComponent @@ -54,7 +57,10 @@ import com.example.what_the_fantasy.ui.components.NavBar import com.example.what_the_fantasy.ui.components.SpaceHeightComponent import com.example.what_the_fantasy.ui.components.TitlePageComponent import com.example.what_the_fantasy.ui.components.VisibleIconPasswordComponent +import com.example.what_the_fantasy.ui.states.CurrentUserState import com.example.what_the_fantasy.ui.theme.gradienBox +import com.example.what_the_fantasy.ui.viewModels.AuthUserViewModel +import com.example.what_the_fantasy.ui.viewModels.CurrentUserViewModel @Composable fun ProfilPage(index: Int, @@ -66,6 +72,8 @@ fun ProfilPage(index: Int, services: IServices ) { val user = services.getUserById(index) ?: return + val currentUserVM : CurrentUserViewModel = viewModel() + val currentUserState by currentUserVM.currentUserState.collectAsState() NavBar(onProfile = true, index = index, @@ -99,21 +107,21 @@ fun ProfilPage(index: Int, ImageProfil(user.imgUrl, 120) SpaceHeightComponent(16) - EditUsername(user.username, index, services)// Édition du Username + EditUsername(currentUserState.username, index, currentUserVM)// Édition du Username SpaceHeightComponent(16) - EditEmail(user.email,index, services)// Édition du Email + EditEmail(currentUserState.email,index, currentUserVM)// Édition du Email Spacer(modifier = Modifier.height(8.dp)) - EditPasswd(index, services) + EditPasswd(index, currentUserVM) SpaceHeightComponent(16) // Bouton - //ButtonProfile(R.string.ButtonAddQuoteprofile, 18, Color.Black, Color.White,navUnLog) // Pas encore de navigation definie - //SpaceHeightComponent(16) - ButtonLanguage(R.string.ButtonLanguageprofile, 18, MaterialTheme.colorScheme.onPrimary, MaterialTheme.colorScheme.background,services, user) + ButtonProfile(R.string.ButtonAddQuoteprofile, 18, MaterialTheme.colorScheme.background,navSubmitQuote) // Pas encore de navigation definie SpaceHeightComponent(16) - ButtonUnLog(R.string.ButtonUnlogprofile, 18, MaterialTheme.colorScheme.onPrimary, MaterialTheme.colorScheme.background,navUnLog) + ButtonLanguage(R.string.ButtonLanguageprofile, 18, MaterialTheme.colorScheme.background,currentUserVM, currentUserState) + SpaceHeightComponent(16) + ButtonUnLog(R.string.ButtonUnlogprofile, 18, navUnLog) @@ -136,13 +144,13 @@ fun ImageProfil(imgProfil : String, size :Int){ } @Composable -fun EditEmail(userEmail: String, index: Int, service: IServices) { - var email by remember { mutableStateOf(userEmail) } +fun EditEmail(emailState: String, index: Int, currentUserVM: CurrentUserViewModel) { + var email by remember { mutableStateOf(emailState) } var isEditingEmail by remember { mutableStateOf(false) } var emailError by remember { mutableStateOf(false) } fun onDoneEditing() { - isEditingEmail =!service.EditEmail(email, index) + isEditingEmail =!currentUserVM.editEmail(email, index) } @@ -223,12 +231,12 @@ fun DisplayEmail(email: String, onEdit: () -> Unit) { @Composable -fun EditUsername(userName: String, index: Int, service : IServices) { - var username by remember { mutableStateOf(userName) } +fun EditUsername(usernameState: String, index: Int, currentUserVM : CurrentUserViewModel) { + var username by remember { mutableStateOf(usernameState) } var isEditingUsername by remember { mutableStateOf(false) } fun onDoneEditing() { - isEditingUsername= !service.EditUsername(username, index) + isEditingUsername= !currentUserVM.editUsername(username, index) } if (isEditingUsername) { @@ -292,7 +300,7 @@ fun DisplayUsername(username: String, onEdit: () -> Unit) { @Composable -fun EditPasswd(index: Int, service: IServices) { +fun EditPasswd(index: Int, currentUserVM: CurrentUserViewModel) { var password by remember { mutableStateOf("*******") } // Mot de passe actuel (affiché comme un masque) var isEditingPassword by remember { mutableStateOf(false) } var newPassword by remember { mutableStateOf("") } @@ -303,7 +311,7 @@ fun EditPasswd(index: Int, service: IServices) { // Fonction pour finaliser l'édition du mot de passe et appeler la méthode EditPasswd2 fun onDoneEditing() { // Appeler EditPasswd pour mettre à jour le mot de passe de l'utilisateur - service.EditPasswd(newPassword, index) + currentUserVM.editPassword(newPassword, index) isEditingPassword = false } @@ -322,12 +330,12 @@ fun EditPasswd(index: Int, service: IServices) { passwordError = passwordError, onDone = { if (!passwordError && newPassword.isNotEmpty()) { - onDoneEditing() // Appeler la fonction onDoneEditing() pour mettre à jour le mot de passe + onDoneEditing() // pour mettre à jour le mot de passe } } ) } else { - DisplayPassword(onEdit = { isEditingPassword = true }) // Afficher l'option pour modifier le mot de passe + DisplayPassword(onEdit = { isEditingPassword = true }) // pour modifier le mot de passe } } @@ -446,7 +454,7 @@ fun DisplayPassword(onEdit: () -> Unit) { } @Composable -fun ButtonUnLog(textResId : Int, size :Int, colorTexte : Color, colorButton : Color,navController: () -> Unit){ +fun ButtonUnLog(textResId : Int, size :Int,navController: () -> Unit){ val text = stringResource(id = textResId) val logsUser = LogsUsers() //gestion des logs pour les utilisateurs @@ -463,23 +471,22 @@ fun ButtonUnLog(textResId : Int, size :Int, colorTexte : Color, colorButton : Co } @Composable -fun ButtonLanguage(textResId : Int, size :Int, colorTexte : Color, colorButton : Color, service: IServices, user : User){ +fun ButtonLanguage(textResId : Int, size :Int, colorButton : Color, currentUserVM: CurrentUserViewModel, currentUserState : CurrentUserState){ val text = stringResource(id = textResId) - val currentLangage = remember { mutableStateOf(user.langage) } + Button( onClick = { - service.ChangeLangage(user) - currentLangage.value = user.langage + currentUserVM.editLangue(10) // a mettre a la place : currentUserState.id }, colors = ButtonDefaults.buttonColors(containerColor = colorButton), modifier = Modifier.fillMaxWidth(), ) { - Text("${text} (${currentLangage.value})", fontSize = size.sp, color = MaterialTheme.colorScheme.primary) + Text("${text} (${currentUserState.langue})", fontSize = size.sp, color = MaterialTheme.colorScheme.primary) } } @Composable -fun ButtonProfil(textResId : Int, size :Int, colorTexte : Color, colorButton : Color,navController: () -> Unit){ +fun ButtonProfile(textResId : Int, size :Int, colorButton : Color,navController: () -> Unit){ val text = stringResource(id = textResId) Button( @@ -489,6 +496,6 @@ fun ButtonProfil(textResId : Int, size :Int, colorTexte : Color, colorButton : C colors = ButtonDefaults.buttonColors(containerColor = colorButton), modifier = Modifier.fillMaxWidth(), ) { - Text(text, fontSize = size.sp, color = colorTexte) + Text(text, fontSize = size.sp, color = MaterialTheme.colorScheme.primary) } } \ No newline at end of file diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/states/InfoUserState.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/states/CurrentUserState.kt similarity index 51% rename from What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/states/InfoUserState.kt rename to What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/states/CurrentUserState.kt index e3fe6f7..290b6c5 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/states/InfoUserState.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/states/CurrentUserState.kt @@ -2,11 +2,12 @@ package com.example.what_the_fantasy.ui.states import com.example.what_the_fantasy.data.model.SrcLanguage -data class InfoUserState ( +data class CurrentUserState ( + var id : Int = -1, val imagePath : String ="", - val username :String="", - val email : String="", - val password : String="", - val confirmPassword : String="", + var username :String="", + var email : String="", + var password : String="", + var confirmPassword : String="", val langue : 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/CurrentUserViewModel.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/viewModels/CurrentUserViewModel.kt new file mode 100644 index 0000000..5cf381b --- /dev/null +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/viewModels/CurrentUserViewModel.kt @@ -0,0 +1,73 @@ +package com.example.what_the_fantasy.ui.viewModels + +import androidx.lifecycle.ViewModel +import com.example.what_the_fantasy.data.model.SrcLanguage +import com.example.what_the_fantasy.data.model.User +import com.example.what_the_fantasy.data.services.ServicesStub +import com.example.what_the_fantasy.ui.states.CurrentUserState +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.asStateFlow +import kotlinx.coroutines.flow.update + +class CurrentUserViewModel : ViewModel(){ + private val services = ServicesStub() // faire repository qui gère les services Stub et API + private val _currentUserState = MutableStateFlow(CurrentUserState()) + var currentUserState : StateFlow = _currentUserState.asStateFlow() + + + fun setId(id : Int){ + _currentUserState.update {it.copy(id = id)} + } + + fun setUsername(username : String){ + _currentUserState.update {it.copy(username = username)} + } + + fun setEmail(email : String){ + _currentUserState.update {it.copy(email = email)} + } + + fun setPassword(password : String){ + _currentUserState.update {it.copy(password = password)} + } + + fun setConfirmPassword(password : String){ + _currentUserState.update {it.copy(confirmPassword = password)} + } + + fun setLangue(langue : SrcLanguage){ + _currentUserState.update {it.copy(langue = langue)} + } + + + + + fun editUsername(username : String, index : Int) : Boolean{ + _currentUserState.update {it.copy(username = username)} + return services.EditUsername(username, index) + } + + fun editEmail(email : String, index : Int) : Boolean{ + _currentUserState.update { + it.copy(email = email) + } + return services.EditEmail(email, index) + } + + fun editPassword(password : String, index : Int){ + services.EditPasswd(password, index) + + _currentUserState.update { + it.copy(password = password) + } + } + + fun editLangue(index : Int){ + val langage = services.ChangeLangage(index) + + _currentUserState.update { + it.copy(langue = langage) + } + } +} \ No newline at end of file diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/viewModels/InfoUserViewModel.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/viewModels/InfoUserViewModel.kt deleted file mode 100644 index 3f5fc1a..0000000 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/viewModels/InfoUserViewModel.kt +++ /dev/null @@ -1,20 +0,0 @@ -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.InfoUserState -import kotlinx.coroutines.flow.MutableStateFlow -import kotlinx.coroutines.flow.StateFlow -import kotlinx.coroutines.flow.asStateFlow -import kotlinx.coroutines.flow.update - -class InfoUserViewModel : ViewModel(){ - private val services = ServicesStub() // faire repository qui gère les services Stub et API - private val _userInfoState = MutableStateFlow(InfoUserState()) - val InfoUserState : StateFlow = _userInfoState.asStateFlow() - - fun setUsername(username : String){ - _userInfoState.update { it.copy(username=username) } - - } -} \ No newline at end of file From 5ed3f5f1ec56a6e909c524716a07314a75c562c2 Mon Sep 17 00:00:00 2001 From: "leni.beaulaton" Date: Sat, 29 Mar 2025 16:58:23 +0100 Subject: [PATCH 09/15] init en cours --- .../what_the_fantasy/ui/screens/ProfilPage.kt | 11 +++------- .../ui/states/CurrentUserState.kt | 2 +- .../ui/viewModels/CurrentUserViewModel.kt | 22 ++++++++++++++++--- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/ProfilPage.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/ProfilPage.kt index 877603a..1980730 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/ProfilPage.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/ProfilPage.kt @@ -49,8 +49,6 @@ import androidx.lifecycle.viewmodel.compose.viewModel import coil.compose.AsyncImage import com.example.what_the_fantasy.Logs.LogsUsers import com.example.what_the_fantasy.R -import com.example.what_the_fantasy.data.model.SrcLanguage -import com.example.what_the_fantasy.data.model.User import com.example.what_the_fantasy.data.services.IServices import com.example.what_the_fantasy.ui.components.ErrorMessageProfileComponent import com.example.what_the_fantasy.ui.components.NavBar @@ -58,8 +56,6 @@ import com.example.what_the_fantasy.ui.components.SpaceHeightComponent import com.example.what_the_fantasy.ui.components.TitlePageComponent import com.example.what_the_fantasy.ui.components.VisibleIconPasswordComponent import com.example.what_the_fantasy.ui.states.CurrentUserState -import com.example.what_the_fantasy.ui.theme.gradienBox -import com.example.what_the_fantasy.ui.viewModels.AuthUserViewModel import com.example.what_the_fantasy.ui.viewModels.CurrentUserViewModel @Composable @@ -71,7 +67,6 @@ fun ProfilPage(index: Int, navSubmitQuote: () -> Unit, services: IServices ) { - val user = services.getUserById(index) ?: return val currentUserVM : CurrentUserViewModel = viewModel() val currentUserState by currentUserVM.currentUserState.collectAsState() @@ -104,7 +99,7 @@ fun ProfilPage(index: Int, SpaceHeightComponent(16) // Image de profil - ImageProfil(user.imgUrl, 120) + ImageProfil(currentUserState.imagePath, 120) SpaceHeightComponent(16) EditUsername(currentUserState.username, index, currentUserVM)// Édition du Username @@ -476,12 +471,12 @@ fun ButtonLanguage(textResId : Int, size :Int, colorButton : Color, currentUserV Button( onClick = { - currentUserVM.editLangue(10) // a mettre a la place : currentUserState.id + currentUserVM.editLangue(currentUserState.id) }, colors = ButtonDefaults.buttonColors(containerColor = colorButton), modifier = Modifier.fillMaxWidth(), ) { - Text("${text} (${currentUserState.langue})", fontSize = size.sp, color = MaterialTheme.colorScheme.primary) + Text("${text} (${currentUserState.langage})", fontSize = size.sp, color = MaterialTheme.colorScheme.primary) } } diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/states/CurrentUserState.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/states/CurrentUserState.kt index 290b6c5..cc1a7f6 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/states/CurrentUserState.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/states/CurrentUserState.kt @@ -9,5 +9,5 @@ data class CurrentUserState ( var email : String="", var password : String="", var confirmPassword : String="", - val langue : SrcLanguage = SrcLanguage.vo + 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/CurrentUserViewModel.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/viewModels/CurrentUserViewModel.kt index 5cf381b..6a65d70 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/viewModels/CurrentUserViewModel.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/viewModels/CurrentUserViewModel.kt @@ -1,20 +1,33 @@ package com.example.what_the_fantasy.ui.viewModels import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope import com.example.what_the_fantasy.data.model.SrcLanguage -import com.example.what_the_fantasy.data.model.User import com.example.what_the_fantasy.data.services.ServicesStub import com.example.what_the_fantasy.ui.states.CurrentUserState import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.update +import kotlinx.coroutines.launch class CurrentUserViewModel : ViewModel(){ private val services = ServicesStub() // faire repository qui gère les services Stub et API private val _currentUserState = MutableStateFlow(CurrentUserState()) var currentUserState : StateFlow = _currentUserState.asStateFlow() + init{ + viewModelScope.launch { + services.getUserById(10)?.let { // A changer : renvoie le meme user pour le moment + setId(it.id) + setUsername(it.username) + setEmail(it.email) + setPassword(it.password) + setLangue(it.langage) + setImage(it.imgUrl) + } + } + } fun setId(id : Int){ _currentUserState.update {it.copy(id = id)} @@ -37,7 +50,10 @@ class CurrentUserViewModel : ViewModel(){ } fun setLangue(langue : SrcLanguage){ - _currentUserState.update {it.copy(langue = langue)} + _currentUserState.update {it.copy(langage = langue)} + } + fun setImage(imagePath : String){ + _currentUserState.update {it.copy(imagePath = imagePath)} } @@ -67,7 +83,7 @@ class CurrentUserViewModel : ViewModel(){ val langage = services.ChangeLangage(index) _currentUserState.update { - it.copy(langue = langage) + it.copy(langage = langage) } } } \ No newline at end of file From 6045bfdfe6f20b4c077375801ea7b1914a470dda Mon Sep 17 00:00:00 2001 From: "leni.beaulaton" Date: Sat, 29 Mar 2025 17:12:24 +0100 Subject: [PATCH 10/15] Lien avec profil --- .../what_the_fantasy/data/local/UserStub.kt | 22 +++++++++---------- .../data/services/ServicesStub.kt | 2 +- .../ui/navigations/AppNavigator.kt | 1 - .../what_the_fantasy/ui/screens/ProfilPage.kt | 11 +++++----- 4 files changed, 17 insertions(+), 19 deletions(-) diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/local/UserStub.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/local/UserStub.kt index 0863cd4..5c50ae7 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/local/UserStub.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/local/UserStub.kt @@ -6,16 +6,16 @@ import com.example.what_the_fantasy.data.model.User object UserStub { //LE MOT DE PASSE POUR TOUS LES UTILISATEURS EST : 1234 val users: MutableList = mutableListOf( - User(1, "Aragorn123", "aragorn@example.com", "2022-01-15", "https://img.freepik.com/vecteurs-libre/personnage-guerrier-fantaisie_1045-185.jpg?size=338&ext=jpg", "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4", SrcLanguage.vf), //1234 - User(2, "Legolas456", "legolas@example.com", "2021-05-23", "https://img.freepik.com/vecteurs-libre/personnage-elfe-fantaisie_1045-186.jpg?size=338&ext=jpg", "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4", SrcLanguage.vf),//1234 - User(3, "Gandalf789", "gandalf@example.com", "2020-09-10", "https://img.freepik.com/vecteurs-libre/personnage-magicien-fantaisie_1045-187.jpg?size=338&ext=jpg", "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4", SrcLanguage.vf),//1234 - User(4, "FrodoBaggins", "frodo@example.com", "2023-03-18", "https://img.freepik.com/vecteurs-libre/personnage-hobbit-fantaisie_1045-188.jpg?size=338&ext=jpg", "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4", SrcLanguage.vf),//1234 - User(5, "Gimli999", "gimli@example.com", "2022-07-04", "https://img.freepik.com/vecteurs-libre/personnage-nain-fantaisie_1045-189.jpg?size=338&ext=jpg", "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4", SrcLanguage.vo),//1234 - User(6, "Galadriel321", "galadriel@example.com", "2021-11-30", "https://img.freepik.com/vecteurs-libre/personnage-elfe-femme-fantaisie_1045-190.jpg?size=338&ext=jpg", "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4", SrcLanguage.vo),//1234 - User(7, "Boromir654", "boromir@example.com", "2023-06-22", "https://img.freepik.com/vecteurs-libre/personnage-guerrier-homme-fantaisie_1045-191.jpg?size=338&ext=jpg", "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4", SrcLanguage.vo),//1234 - User(8, "Eowyn777", "eowyn@example.com", "2022-04-11", "https://img.freepik.com/vecteurs-libre/personnage-guerriere-femme-fantaisie_1045-192.jpg?size=338&ext=jpg", "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4", SrcLanguage.vo),//1234 - User(9, "Saruman888", "saruman@example.com", "2021-08-15", "https://img.freepik.com/vecteurs-libre/personnage-magicien-malefique-fantaisie_1045-193.jpg?size=338&ext=jpg", "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4", SrcLanguage.vo),//1234 - User(10, "Faramir222", "faramir@example.com", "2023-02-08", "https://img.freepik.com/vecteurs-libre/personnage-guerrier-homme-fantaisie_1045-194.jpg?size=338&ext=jpg", "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4", SrcLanguage.vo),//1234 - User(11, "dev", "testeur@example.com", "2023-02-08", "https://img.freepik.com/vecteurs-libre/personnage-guerrier-homme-fantaisie_1045-194.jpg?size=338&ext=jpg", "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4", SrcLanguage.vo)//1234 + User(0, "Aragorn123", "aragorn@example.com", "2022-01-15", "https://img.freepik.com/vecteurs-libre/personnage-guerrier-fantaisie_1045-185.jpg?size=338&ext=jpg", "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4", SrcLanguage.vf), //1234 + User(1, "Legolas456", "legolas@example.com", "2021-05-23", "https://img.freepik.com/vecteurs-libre/personnage-elfe-fantaisie_1045-186.jpg?size=338&ext=jpg", "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4", SrcLanguage.vf),//1234 + User(2, "Gandalf789", "gandalf@example.com", "2020-09-10", "https://img.freepik.com/vecteurs-libre/personnage-magicien-fantaisie_1045-187.jpg?size=338&ext=jpg", "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4", SrcLanguage.vf),//1234 + User(3, "FrodoBaggins", "frodo@example.com", "2023-03-18", "https://img.freepik.com/vecteurs-libre/personnage-hobbit-fantaisie_1045-188.jpg?size=338&ext=jpg", "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4", SrcLanguage.vf),//1234 + User(4, "Gimli999", "gimli@example.com", "2022-07-04", "https://img.freepik.com/vecteurs-libre/personnage-nain-fantaisie_1045-189.jpg?size=338&ext=jpg", "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4", SrcLanguage.vo),//1234 + User(5, "Galadriel321", "galadriel@example.com", "2021-11-30", "https://img.freepik.com/vecteurs-libre/personnage-elfe-femme-fantaisie_1045-190.jpg?size=338&ext=jpg", "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4", SrcLanguage.vo),//1234 + User(6, "Boromir654", "boromir@example.com", "2023-06-22", "https://img.freepik.com/vecteurs-libre/personnage-guerrier-homme-fantaisie_1045-191.jpg?size=338&ext=jpg", "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4", SrcLanguage.vo),//1234 + User(7, "Eowyn777", "eowyn@example.com", "2022-04-11", "https://img.freepik.com/vecteurs-libre/personnage-guerriere-femme-fantaisie_1045-192.jpg?size=338&ext=jpg", "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4", SrcLanguage.vo),//1234 + User(8, "Saruman888", "saruman@example.com", "2021-08-15", "https://img.freepik.com/vecteurs-libre/personnage-magicien-malefique-fantaisie_1045-193.jpg?size=338&ext=jpg", "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4", SrcLanguage.vo),//1234 + User(9, "Faramir222", "faramir@example.com", "2023-02-08", "https://img.freepik.com/vecteurs-libre/personnage-guerrier-homme-fantaisie_1045-194.jpg?size=338&ext=jpg", "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4", SrcLanguage.vo),//1234 + User(10, "dev", "testeur@example.com", "2023-02-08", "https://img.freepik.com/vecteurs-libre/personnage-guerrier-homme-fantaisie_1045-194.jpg?size=338&ext=jpg", "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4", SrcLanguage.vo)//1234 ) } \ No newline at end of file diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesStub.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesStub.kt index 0b76750..f427cdb 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesStub.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesStub.kt @@ -110,7 +110,7 @@ class ServicesStub : IServices { override fun getAllUsers(): List = users override fun getUserById(id: Int): User? { - return (users.find { it.id == id+1 }) + return (users.find { it.id == id }) } override fun SearchQuote(quote: String) { diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/navigations/AppNavigator.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/navigations/AppNavigator.kt index f0c6864..36ab27b 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/navigations/AppNavigator.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/navigations/AppNavigator.kt @@ -128,7 +128,6 @@ fun AppNavigator() { popUpTo(profil) { inclusive = true } } }, - services = services ) } composable { diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/ProfilPage.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/ProfilPage.kt index 1980730..dec28da 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/ProfilPage.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/ProfilPage.kt @@ -1,5 +1,6 @@ package com.example.what_the_fantasy.ui.screens +import android.util.Log import android.util.Patterns import androidx.compose.foundation.background import androidx.compose.foundation.clickable @@ -49,7 +50,6 @@ import androidx.lifecycle.viewmodel.compose.viewModel import coil.compose.AsyncImage import com.example.what_the_fantasy.Logs.LogsUsers import com.example.what_the_fantasy.R -import com.example.what_the_fantasy.data.services.IServices import com.example.what_the_fantasy.ui.components.ErrorMessageProfileComponent import com.example.what_the_fantasy.ui.components.NavBar import com.example.what_the_fantasy.ui.components.SpaceHeightComponent @@ -65,13 +65,12 @@ fun ProfilPage(index: Int, navQuiz: (Int) -> Unit, navUnLog: () -> Unit, navSubmitQuote: () -> Unit, - services: IServices ) { val currentUserVM : CurrentUserViewModel = viewModel() val currentUserState by currentUserVM.currentUserState.collectAsState() NavBar(onProfile = true, - index = index, + index = currentUserState.id, navControllerFavorite = navFavorite, navControllerAccueil = navAccueil, navControllerProfil = {}, @@ -102,13 +101,13 @@ fun ProfilPage(index: Int, ImageProfil(currentUserState.imagePath, 120) SpaceHeightComponent(16) - EditUsername(currentUserState.username, index, currentUserVM)// Édition du Username + EditUsername(currentUserState.username, currentUserState.id, currentUserVM)// Édition du Username SpaceHeightComponent(16) - EditEmail(currentUserState.email,index, currentUserVM)// Édition du Email + EditEmail(currentUserState.email,currentUserState.id, currentUserVM)// Édition du Email Spacer(modifier = Modifier.height(8.dp)) - EditPasswd(index, currentUserVM) + EditPasswd(currentUserState.id, currentUserVM) SpaceHeightComponent(16) // Bouton From 210d8c575f04c05e1fc91db40c2403ad18448cd3 Mon Sep 17 00:00:00 2001 From: "leni.beaulaton" Date: Sat, 29 Mar 2025 19:01:51 +0100 Subject: [PATCH 11/15] ViewModel AppNavigation pour SignUp et Login --- .../data/services/IServices.kt | 4 +++- .../data/services/ServicesStub.kt | 5 +++- .../ui/navigations/AppNavigator.kt | 24 +++++++++++++++++++ .../what_the_fantasy/ui/screens/LoginPage.kt | 7 +++--- .../what_the_fantasy/ui/screens/ProfilPage.kt | 1 - .../what_the_fantasy/ui/screens/SignUpPage.kt | 7 +++--- .../ui/states/AuthUserState.kt | 1 + .../ui/viewModels/AuthUserViewModel.kt | 5 +++- 8 files changed, 44 insertions(+), 10 deletions(-) diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/IServices.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/IServices.kt index 3ca9bd8..ae6156e 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/IServices.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/IServices.kt @@ -4,10 +4,12 @@ import com.example.what_the_fantasy.data.model.Favorite import com.example.what_the_fantasy.data.model.Quote import com.example.what_the_fantasy.data.model.SrcLanguage import com.example.what_the_fantasy.data.model.User +import com.example.what_the_fantasy.ui.states.AuthUserState +import kotlinx.coroutines.flow.StateFlow interface IServices { - fun validLogin(username : String, passwd : String, navController: (Int) -> Unit): Boolean + fun validLogin(username : String, passwd : String, userSession : StateFlow, navController: (Int) -> Unit): Boolean fun EditUsername(username : String, index : Int) : Boolean fun EditEmail(email : String, index : Int) : Boolean fun EditPasswd(passwd : String, index : Int) diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesStub.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesStub.kt index f427cdb..7b4a5bf 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesStub.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesStub.kt @@ -10,17 +10,20 @@ import com.example.what_the_fantasy.data.model.Favorite import com.example.what_the_fantasy.data.model.Quote import com.example.what_the_fantasy.data.model.SrcLanguage import com.example.what_the_fantasy.ui.components.hashPassword +import com.example.what_the_fantasy.ui.states.AuthUserState +import kotlinx.coroutines.flow.StateFlow import java.time.LocalDate class ServicesStub : IServices { val logsUser = LogsUsers() //gestion des logs pour les utilisateurs - override fun validLogin(username : String, passwd : String, navController: (Int) -> Unit): Boolean{ + override fun validLogin(username : String, passwd : String, userSession : StateFlow, navController: (Int) -> Unit): Boolean{ users.forEachIndexed { index, user -> val hashPassWd = hashPassword(passwd) if (user.username == username && user.password == hashPassWd) { + userSession.value.id = index navController(index) logsUser.logInformationUserConnect(user, "UserConnect") return true diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/navigations/AppNavigator.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/navigations/AppNavigator.kt index 36ab27b..ecb496f 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/navigations/AppNavigator.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/navigations/AppNavigator.kt @@ -6,13 +6,19 @@ import androidx.compose.foundation.layout.padding import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Scaffold import androidx.compose.runtime.Composable +import androidx.compose.runtime.collectAsState +import androidx.compose.runtime.getValue import androidx.compose.ui.Modifier +import androidx.lifecycle.viewmodel.compose.viewModel import androidx.navigation.compose.NavHost import androidx.navigation.compose.composable import androidx.navigation.compose.rememberNavController import androidx.navigation.toRoute import com.example.what_the_fantasy.data.services.ServicesStub import com.example.what_the_fantasy.ui.screens.* +import com.example.what_the_fantasy.ui.viewModels.AuthUserViewModel +import com.example.what_the_fantasy.ui.viewModels.CurrentUserViewModel +import com.example.what_the_fantasy.ui.viewModels.SignInUserViewModel import kotlinx.serialization.Serializable @Serializable @@ -63,11 +69,24 @@ fun AppNavigator() { val navController = rememberNavController() val services = ServicesStub() + //ViewModel pour l'authentification + val authUserVM : AuthUserViewModel = viewModel() + val authState by authUserVM.userState.collectAsState() + + //ViewModel pour l'inscription + val signInUserVM : SignInUserViewModel = viewModel() + val signInState by signInUserVM.userState.collectAsState() + + //ViewModel pour l'utilisateur + val currentUserVM : CurrentUserViewModel = viewModel() + val currentUserState by currentUserVM.currentUserState.collectAsState() + Scaffold( modifier = Modifier.fillMaxSize(), containerColor = MaterialTheme.colorScheme.onPrimary ) { paddingValues -> Box(modifier = Modifier.padding(paddingValues)) { + NavHost(navController, startDestination = Login) { composable { LoginPage( @@ -77,8 +96,11 @@ fun AppNavigator() { popUpTo(Login) { inclusive = true } } }, + authUserVM = authUserVM, + authState = authState ) } + composable { val accueil: Accueil = it.toRoute() AccueilPage( @@ -150,6 +172,8 @@ fun AppNavigator() { popUpTo(Login) { inclusive = true } } }, + signInUserVM = signInUserVM, + signInState = signInState ) } composable { 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 ac18a16..05e8a47 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 @@ -41,14 +41,15 @@ import com.example.what_the_fantasy.ui.components.SpaceHeightComponent import com.example.what_the_fantasy.ui.components.TitlePageComponent import com.example.what_the_fantasy.ui.components.VisibleIconPasswordComponent import com.example.what_the_fantasy.ui.components.hashPassword +import com.example.what_the_fantasy.ui.states.AuthUserState 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) { - val authUserVM : AuthUserViewModel = viewModel() - val authState by authUserVM.userState.collectAsState() +fun LoginPage(navControllerSignUp: () -> Unit, navControllerProfil: (Int) -> Unit, authUserVM : AuthUserViewModel, authState : AuthUserState) { +// val authUserVM : AuthUserViewModel = viewModel() +// val authState by authUserVM.userState.collectAsState() Box( modifier = Modifier diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/ProfilPage.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/ProfilPage.kt index dec28da..13dc9be 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/ProfilPage.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/ProfilPage.kt @@ -1,6 +1,5 @@ package com.example.what_the_fantasy.ui.screens -import android.util.Log import android.util.Patterns import androidx.compose.foundation.background import androidx.compose.foundation.clickable diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/SignUpPage.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/SignUpPage.kt index b1b38a1..5acf8b8 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/SignUpPage.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/SignUpPage.kt @@ -47,12 +47,13 @@ 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.components.ErrorMessageProfileComponent import com.example.what_the_fantasy.ui.components.VisibleIconPasswordComponent +import com.example.what_the_fantasy.ui.states.SignInUserState import com.example.what_the_fantasy.ui.viewModels.SignInUserViewModel @Composable -fun SignUpPage(navControllerLogin: () -> Unit) { - val signInUserVM : SignInUserViewModel = viewModel() - val signInState by signInUserVM.userState.collectAsState() +fun SignUpPage(navControllerLogin: () -> Unit, signInUserVM :SignInUserViewModel,signInState : SignInUserState) { +// val signInUserVM : SignInUserViewModel = viewModel() +// val signInState by signInUserVM.userState.collectAsState() Box( modifier = Modifier 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 index 10e361d..f3263f1 100644 --- 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 @@ -4,6 +4,7 @@ import com.example.what_the_fantasy.data.model.SrcLanguage import com.example.what_the_fantasy.data.model.User data class AuthUserState ( + var id : Int = -1, val username : String = "", val password: String = "", ) \ 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 index 9fc6bfb..0f49325 100644 --- 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 @@ -14,6 +14,9 @@ class AuthUserViewModel : ViewModel(){ val userState : StateFlow = _userState.asStateFlow() + fun setId(id : Int){ + _userState.update { it.copy(id=id) } + } fun setUsername(username : String){ _userState.update { it.copy(username=username) } } @@ -23,6 +26,6 @@ class AuthUserViewModel : ViewModel(){ } fun validLogin(username : String, passwd : String, navController: (Int) -> Unit) : Boolean{ - return services.validLogin(username,passwd,navController) + return services.validLogin(username,passwd,userState, navController) } } \ No newline at end of file From 2e98af24501864cb7e550e19de7a822e65104117 Mon Sep 17 00:00:00 2001 From: "leni.beaulaton" Date: Sat, 29 Mar 2025 19:26:58 +0100 Subject: [PATCH 12/15] Lien avec le profil --- .../data/services/IServices.kt | 7 ++++++- .../data/services/ServicesStub.kt | 9 ++++++-- .../ui/navigations/AppNavigator.kt | 6 ++++-- .../what_the_fantasy/ui/screens/LoginPage.kt | 19 ++++++++++++----- .../what_the_fantasy/ui/screens/ProfilPage.kt | 12 ++++++----- .../ui/viewModels/AuthUserViewModel.kt | 4 ++-- .../ui/viewModels/CurrentUserViewModel.kt | 21 +++++++++++++++---- 7 files changed, 57 insertions(+), 21 deletions(-) diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/IServices.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/IServices.kt index ae6156e..ac9c8e1 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/IServices.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/IServices.kt @@ -9,7 +9,12 @@ import kotlinx.coroutines.flow.StateFlow interface IServices { - fun validLogin(username : String, passwd : String, userSession : StateFlow, navController: (Int) -> Unit): Boolean + fun validLogin(username : String, + passwd : String, + userSession : StateFlow, + navController: (Int) -> Unit, + initialierCurrentUser : (Int) ->Unit): Boolean + fun EditUsername(username : String, index : Int) : Boolean fun EditEmail(email : String, index : Int) : Boolean fun EditPasswd(passwd : String, index : Int) diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesStub.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesStub.kt index 7b4a5bf..33b68e3 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesStub.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesStub.kt @@ -18,12 +18,17 @@ class ServicesStub : IServices { val logsUser = LogsUsers() //gestion des logs pour les utilisateurs - override fun validLogin(username : String, passwd : String, userSession : StateFlow, navController: (Int) -> Unit): Boolean{ + override fun validLogin(username : String, + passwd : String, + userSession : StateFlow, + navController: (Int) -> Unit, + initialierCurrentUser : (Int) ->Unit): Boolean{ users.forEachIndexed { index, user -> val hashPassWd = hashPassword(passwd) if (user.username == username && user.password == hashPassWd) { - userSession.value.id = index + //userSession.value.id = index + initialierCurrentUser(index) navController(index) logsUser.logInformationUserConnect(user, "UserConnect") return true diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/navigations/AppNavigator.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/navigations/AppNavigator.kt index ecb496f..e80ecf9 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/navigations/AppNavigator.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/navigations/AppNavigator.kt @@ -97,7 +97,8 @@ fun AppNavigator() { } }, authUserVM = authUserVM, - authState = authState + authState = authState, + initialierCurrentUser ={currentUserVM.initialiseCurrentUser(it)} ) } @@ -140,7 +141,6 @@ fun AppNavigator() { composable { val profil: Profil = it.toRoute() ProfilPage( - index = profil.userIndex, navFavorite = { navController.navigate(Favorite(profil.userIndex)) }, navAccueil = { navController.navigate(Accueil(profil.userIndex)) }, navQuiz = { navController.navigate(QuizMenu(profil.userIndex)) }, @@ -150,6 +150,8 @@ fun AppNavigator() { popUpTo(profil) { inclusive = true } } }, + currentUserVM = currentUserVM, + currentUserState = currentUserState, ) } composable { 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 05e8a47..e669129 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 @@ -47,7 +47,12 @@ 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, authUserVM : AuthUserViewModel, authState : AuthUserState) { +fun LoginPage(navControllerSignUp: () -> Unit, + navControllerProfil: (Int) -> Unit, + authUserVM : AuthUserViewModel, + authState : AuthUserState, + initialierCurrentUser : (Int) -> Unit) { + // val authUserVM : AuthUserViewModel = viewModel() // val authState by authUserVM.userState.collectAsState() @@ -69,11 +74,15 @@ fun LoginPage(navControllerSignUp: () -> Unit, navControllerProfil: (Int) -> Uni TitlePageComponent(R.string.titleLogin, MaterialTheme.colorScheme.primary) SpaceHeightComponent(20) - ConnexionButtonLogin(authUserVM,IdentifiantTextField(R.string.IdentifiantLogin, authState.username){ + + ConnexionButtonLogin(authUserVM, + IdentifiantTextField(R.string.IdentifiantLogin, authState.username){ authUserVM.setUsername(it) }, PassWdTextField(R.string.PasswdLogin, authState.password){ authUserVM.setPassword(it) - }, R.string.ButtonLogin,18,navControllerProfil) + }, R.string.ButtonLogin,18,navControllerProfil){ + initialierCurrentUser(it) + } SpaceHeightComponent(16) CreateAccountButton(R.string.ButtonCreateLogin,12, MaterialTheme.colorScheme.primary, navControllerSignUp) } @@ -130,11 +139,11 @@ fun PassWdTextField(textpasswdResId : Int, password : String, onValueChange: (St @Composable -fun ConnexionButtonLogin(authUserVM : AuthUserViewModel, username : String, passwd : String, titleResId : Int, size : Int, navController: (Int) -> Unit){ +fun ConnexionButtonLogin(authUserVM : AuthUserViewModel, username : String, passwd : String, titleResId : Int, size : Int, navController: (Int) -> Unit, initialierCurrentUser : (Int) -> Unit){ val title = stringResource(id = titleResId) var showError by remember { mutableStateOf(false) } Button( - onClick = { showError = !authUserVM.validLogin(username, passwd, navController) + onClick = { showError = !authUserVM.validLogin(username, passwd, navController, initialierCurrentUser) }, colors = ButtonDefaults.buttonColors(containerColor = MaterialTheme.colorScheme.background), modifier = Modifier diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/ProfilPage.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/ProfilPage.kt index 13dc9be..5447e63 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/ProfilPage.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/ProfilPage.kt @@ -1,5 +1,6 @@ package com.example.what_the_fantasy.ui.screens +import android.util.Log import android.util.Patterns import androidx.compose.foundation.background import androidx.compose.foundation.clickable @@ -58,16 +59,17 @@ import com.example.what_the_fantasy.ui.states.CurrentUserState import com.example.what_the_fantasy.ui.viewModels.CurrentUserViewModel @Composable -fun ProfilPage(index: Int, - navFavorite: (Int) -> Unit, +fun ProfilPage(navFavorite: (Int) -> Unit, navAccueil: (Int) -> Unit, navQuiz: (Int) -> Unit, navUnLog: () -> Unit, navSubmitQuote: () -> Unit, + currentUserVM : CurrentUserViewModel, + currentUserState : CurrentUserState, ) { - val currentUserVM : CurrentUserViewModel = viewModel() - val currentUserState by currentUserVM.currentUserState.collectAsState() - +// val currentUserVM : CurrentUserViewModel = viewModel() +// val currentUserState by currentUserVM.currentUserState.collectAsState() + Log.e("Profil","${currentUserState.id}") NavBar(onProfile = true, index = currentUserState.id, navControllerFavorite = navFavorite, 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 index 0f49325..193cb2b 100644 --- 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 @@ -25,7 +25,7 @@ class AuthUserViewModel : ViewModel(){ _userState.update { it.copy(password=password) } } - fun validLogin(username : String, passwd : String, navController: (Int) -> Unit) : Boolean{ - return services.validLogin(username,passwd,userState, navController) + fun validLogin(username : String, passwd : String, navController: (Int) -> Unit, initialierCurrentUser : (Int) -> Unit) : Boolean{ + return services.validLogin(username,passwd,userState, navController, initialierCurrentUser) } } \ No newline at end of file diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/viewModels/CurrentUserViewModel.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/viewModels/CurrentUserViewModel.kt index 6a65d70..e4fa71b 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/viewModels/CurrentUserViewModel.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/viewModels/CurrentUserViewModel.kt @@ -1,5 +1,6 @@ package com.example.what_the_fantasy.ui.viewModels +import android.util.Log import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.example.what_the_fantasy.data.model.SrcLanguage @@ -16,9 +17,21 @@ class CurrentUserViewModel : ViewModel(){ private val _currentUserState = MutableStateFlow(CurrentUserState()) var currentUserState : StateFlow = _currentUserState.asStateFlow() - init{ - viewModelScope.launch { - services.getUserById(10)?.let { // A changer : renvoie le meme user pour le moment +// init{ +// viewModelScope.launch { +// services.getUserById(10)?.let { // A changer : renvoie le meme user pour le moment +// setId(it.id) +// setUsername(it.username) +// setEmail(it.email) +// setPassword(it.password) +// setLangue(it.langage) +// setImage(it.imgUrl) +// } +// } +// } + + fun initialiseCurrentUser(index : Int){ + services.getUserById(index)?.let { setId(it.id) setUsername(it.username) setEmail(it.email) @@ -26,7 +39,7 @@ class CurrentUserViewModel : ViewModel(){ setLangue(it.langage) setImage(it.imgUrl) } - } + Log.e("ProfilInit", "${currentUserState.value.id}") } fun setId(id : Int){ From 9d54fce5912e4fa1a99aa0894a24a9d128f7b0ff Mon Sep 17 00:00:00 2001 From: "leni.beaulaton" Date: Sat, 29 Mar 2025 19:58:43 +0100 Subject: [PATCH 13/15] =?UTF-8?q?random=20image=20lors=20de=20la=20cr?= =?UTF-8?q?=C3=A9ation=20de=20compte?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../data/services/IServices.kt | 1 - .../data/services/ServicesStub.kt | 18 ++++++++++++------ .../what_the_fantasy/ui/screens/SignUpPage.kt | 3 +-- .../ui/states/AuthUserState.kt | 1 - .../ui/viewModels/AuthUserViewModel.kt | 6 ++---- 5 files changed, 15 insertions(+), 14 deletions(-) diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/IServices.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/IServices.kt index ac9c8e1..4ca3efa 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/IServices.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/IServices.kt @@ -11,7 +11,6 @@ interface IServices { fun validLogin(username : String, passwd : String, - userSession : StateFlow, navController: (Int) -> Unit, initialierCurrentUser : (Int) ->Unit): Boolean diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesStub.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesStub.kt index 33b68e3..0b01ea9 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesStub.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesStub.kt @@ -5,6 +5,7 @@ import com.example.what_the_fantasy.data.local.UserStub.users import com.example.what_the_fantasy.data.model.User import com.example.what_the_fantasy.Logs.LogsUsers import com.example.what_the_fantasy.data.local.FavoriteStub.favorites +import com.example.what_the_fantasy.data.local.ImageStub.allImages import com.example.what_the_fantasy.data.local.QuoteStub.quotes import com.example.what_the_fantasy.data.model.Favorite import com.example.what_the_fantasy.data.model.Quote @@ -20,14 +21,12 @@ class ServicesStub : IServices { override fun validLogin(username : String, passwd : String, - userSession : StateFlow, navController: (Int) -> Unit, initialierCurrentUser : (Int) ->Unit): Boolean{ users.forEachIndexed { index, user -> val hashPassWd = hashPassword(passwd) if (user.username == username && user.password == hashPassWd) { - //userSession.value.id = index initialierCurrentUser(index) navController(index) logsUser.logInformationUserConnect(user, "UserConnect") @@ -97,7 +96,7 @@ class ServicesStub : IServices { val nbUser = userStub.size if(!isUsernameExist(username) && !isEmailExist(email)){ - val user = User(nbUser+1,username, email, date,randomImage(userStub), passwordhash, SrcLanguage.vo) + val user = User(nbUser,username, email, date,randomImage(), passwordhash, SrcLanguage.vo) users.add(user)//ajout au stub //Afficher tous les users @@ -110,7 +109,6 @@ class ServicesStub : IServices { override fun getFavorite(user: User): List { val favorite = favorites return favorite[0].quote - //return emptyList() } override fun getAllFavorite(): List = favorites @@ -143,8 +141,16 @@ class ServicesStub : IServices { return date.toString() } - fun randomImage(usersImage : List) : String{ - return "https://img.freepik.com/vecteurs-libre/personnage-guerrier-homme-fantaisie_1045-194.jpg?size=338&ext=jpg" + fun randomImage() : String{ + val sizeList = allImages.size + val randomNb = (0..sizeList).random() + allImages.forEach{image -> + if(image.id == randomNb){ + return image.url + } + + } + return allImages[0].url } fun isUsernameExist(username : String) : Boolean{ diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/SignUpPage.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/SignUpPage.kt index 5acf8b8..3f7857c 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/SignUpPage.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/SignUpPage.kt @@ -52,8 +52,7 @@ import com.example.what_the_fantasy.ui.viewModels.SignInUserViewModel @Composable fun SignUpPage(navControllerLogin: () -> Unit, signInUserVM :SignInUserViewModel,signInState : SignInUserState) { -// val signInUserVM : SignInUserViewModel = viewModel() -// val signInState by signInUserVM.userState.collectAsState() + Box( modifier = Modifier 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 index f3263f1..10e361d 100644 --- 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 @@ -4,7 +4,6 @@ import com.example.what_the_fantasy.data.model.SrcLanguage import com.example.what_the_fantasy.data.model.User data class AuthUserState ( - var id : Int = -1, val username : String = "", val password: String = "", ) \ 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 index 193cb2b..7aa9682 100644 --- 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 @@ -14,9 +14,7 @@ class AuthUserViewModel : ViewModel(){ val userState : StateFlow = _userState.asStateFlow() - fun setId(id : Int){ - _userState.update { it.copy(id=id) } - } + fun setUsername(username : String){ _userState.update { it.copy(username=username) } } @@ -26,6 +24,6 @@ class AuthUserViewModel : ViewModel(){ } fun validLogin(username : String, passwd : String, navController: (Int) -> Unit, initialierCurrentUser : (Int) -> Unit) : Boolean{ - return services.validLogin(username,passwd,userState, navController, initialierCurrentUser) + return services.validLogin(username,passwd, navController, initialierCurrentUser) } } \ No newline at end of file From c8131965559b746f240854361977edb9f00c42b3 Mon Sep 17 00:00:00 2001 From: "leni.beaulaton" Date: Sat, 29 Mar 2025 20:28:24 +0100 Subject: [PATCH 14/15] ViewModel pour NavBar --- .../data/services/ServicesStub.kt | 2 - .../what_the_fantasy/ui/components/NavBar.kt | 38 ++++++++++++------- .../ui/navigations/AppNavigator.kt | 35 ++++++++++++----- .../ui/screens/AccueilPage.kt | 15 +++++--- .../ui/screens/FavoritePage.kt | 9 ++++- .../what_the_fantasy/ui/screens/ProfilPage.kt | 9 ++--- .../ui/screens/QuizEndPage.kt | 8 +++- .../what_the_fantasy/ui/screens/QuizMenu.kt | 11 ++++-- .../what_the_fantasy/ui/screens/QuizPage.kt | 8 +++- .../what_the_fantasy/ui/screens/QuotePage.kt | 9 ++++- .../ui/screens/RecapSubmitPage.kt | 9 ++++- .../ui/screens/SubmitQuotePage.kt | 9 ++++- 12 files changed, 111 insertions(+), 51 deletions(-) diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesStub.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesStub.kt index 0b01ea9..05fbddf 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesStub.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesStub.kt @@ -11,8 +11,6 @@ import com.example.what_the_fantasy.data.model.Favorite import com.example.what_the_fantasy.data.model.Quote import com.example.what_the_fantasy.data.model.SrcLanguage import com.example.what_the_fantasy.ui.components.hashPassword -import com.example.what_the_fantasy.ui.states.AuthUserState -import kotlinx.coroutines.flow.StateFlow import java.time.LocalDate class ServicesStub : IServices { diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/components/NavBar.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/components/NavBar.kt index a69a6dc..5d9a60e 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/components/NavBar.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/components/NavBar.kt @@ -1,5 +1,6 @@ package com.example.what_the_fantasy.ui.components +import androidx.compose.foundation.Image import androidx.compose.foundation.background import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box @@ -10,7 +11,9 @@ import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.fillMaxHeight import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size +import androidx.compose.foundation.shape.CircleShape import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material.icons.Icons import androidx.compose.material.icons.rounded.* @@ -18,10 +21,10 @@ import androidx.compose.material3.BottomAppBar import androidx.compose.material3.Icon import androidx.compose.material3.IconButton import androidx.compose.material3.IconButtonColors -import androidx.compose.material3.IconToggleButton import androidx.compose.material3.MaterialTheme import androidx.compose.material3.NavigationBar import androidx.compose.runtime.Composable +import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment @@ -32,20 +35,24 @@ import androidx.compose.ui.graphics.painter.Painter import androidx.compose.ui.graphics.vector.ImageVector import androidx.compose.ui.res.painterResource import androidx.compose.ui.unit.dp +import androidx.lifecycle.viewmodel.compose.viewModel +import coil.compose.rememberAsyncImagePainter +import coil.compose.rememberImagePainter import com.example.what_the_fantasy.R -import com.example.what_the_fantasy.ui.theme.* +import com.example.what_the_fantasy.ui.states.CurrentUserState +import com.example.what_the_fantasy.ui.viewModels.CurrentUserViewModel @Composable -fun NavBar(onProfile : Boolean = false , - onFavorite : Boolean = false , - onAccueil : Boolean = false , - onQuiz : Boolean = false , - index:Int, +fun NavBar(onProfile : Boolean = false, + onFavorite : Boolean = false, + onAccueil : Boolean = false, + onQuiz : Boolean = false, + currentUserVM : CurrentUserViewModel, + currentUserState : CurrentUserState, navControllerProfil: (Int) -> Unit, navControllerFavorite:(Int) -> Unit, navControllerAccueil: (Int) -> Unit, navControllerQuiz: (Int) -> Unit, - content : @Composable ()-> Unit ) { var theme by remember { mutableStateOf(true) } @@ -64,7 +71,7 @@ fun NavBar(onProfile : Boolean = false , Arrangement.SpaceBetween, verticalAlignment = Alignment.Bottom ) { - ButtonIconVectorInt(Icons.Rounded.AccountCircle,"Profile",navControllerProfil,index,onProfile) + ButtonIconVectorInt(currentUserState.imagePath,"Profile",navControllerProfil,currentUserState.id,onProfile) IconButton(onClick = { theme=!theme}, @@ -100,17 +107,17 @@ fun NavBar(onProfile : Boolean = false , ButtonIconPainterInt(painterResource( if(onFavorite)R.drawable.favorite_button_full else R.drawable.favorite_button_empty - ),"Favorite",navControllerFavorite,index,onFavorite) + ),"Favorite",navControllerFavorite,currentUserState.id,onFavorite) ButtonIconPainterInt(painterResource( if(onAccueil)R.drawable.home_button_full else R.drawable.home_button_empty - ),"Accueil",navControllerAccueil,index,onAccueil) + ),"Accueil",navControllerAccueil,currentUserState.id,onAccueil) ButtonIconPainterInt(painterResource( if(onQuiz)R.drawable.quiz_button_full else R.drawable.quiz_button_empty - ),"Quiz",navControllerQuiz,index,onQuiz) + ),"Quiz",navControllerQuiz,currentUserState.id,onQuiz) } } @@ -118,7 +125,7 @@ fun NavBar(onProfile : Boolean = false , } @Composable -fun ButtonIconVectorInt(img : ImageVector, name : String, nav : (Int)->Unit ,index: Int,onPage : Boolean){ +fun ButtonIconVectorInt(img : String, name : String, nav : (Int)->Unit ,index: Int,onPage : Boolean){ IconButton(onClick = {nav(index)}, enabled = !onPage, colors = IconButtonColors(Color.Transparent, MaterialTheme.colorScheme.onBackground,//couleur quand il n'est pas selectionné @@ -126,10 +133,13 @@ fun ButtonIconVectorInt(img : ImageVector, name : String, nav : (Int)->Unit ,ind modifier = Modifier .size(60.dp) ) { - Icon(img, + + Image( + painter = rememberAsyncImagePainter(img), contentDescription = name, modifier = Modifier .fillMaxSize() + .clip(CircleShape) // Pour rendre l'image circulaire ) } } diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/navigations/AppNavigator.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/navigations/AppNavigator.kt index e80ecf9..8b4f148 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/navigations/AppNavigator.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/navigations/AppNavigator.kt @@ -105,7 +105,6 @@ fun AppNavigator() { composable { val accueil: Accueil = it.toRoute() AccueilPage( - index = accueil.userIndex, navFavorite = { navController.navigate(Favorite(accueil.userIndex)) }, navQuiz = { navController.navigate(QuizMenu(accueil.userIndex)) }, navProfil = { navController.navigate(Profil(accueil.userIndex)) }, @@ -117,7 +116,9 @@ fun AppNavigator() { ) ) }, - services = services + services = services, + currentUserVM = currentUserVM, + currentUserState = currentUserState, ) } composable { @@ -135,7 +136,9 @@ fun AppNavigator() { ) ) }, - services = services + services = services, + currentUserVM = currentUserVM, + currentUserState = currentUserState, ) } composable { @@ -163,7 +166,9 @@ fun AppNavigator() { navQuiz = { navController.navigate(QuizMenu(quote.userIndex)) }, navProfil = { navController.navigate(Profil(quote.userIndex)) }, navFavorite = { navController.navigate(Favorite(quote.userIndex)) }, - service = services + service = services, + currentUserVM = currentUserVM, + currentUserState = currentUserState, ) } composable { SearchPage() } @@ -197,7 +202,9 @@ fun AppNavigator() { source ) ) - } + }, + currentUserVM = currentUserVM, + currentUserState = currentUserState, ) } composable { @@ -209,20 +216,24 @@ fun AppNavigator() { source = recapSubmit.source, navAccueil = { navController.navigate(Accueil(recapSubmit.userIndex)) }, navFavorite = { navController.navigate(Favorite(recapSubmit.userIndex)) }, - navProfil = { navController.navigate(Profil(recapSubmit.userIndex)) } + navProfil = { navController.navigate(Profil(recapSubmit.userIndex)) }, + currentUserVM = currentUserVM, + currentUserState = currentUserState, ) } composable { val quizMenu: QuizMenu = it.toRoute() QuizMenu( - index = quizMenu.userIndex, + currentUserVM = currentUserVM, + currentUserState = currentUserState, navAccueil = { navController.navigate(Accueil(quizMenu.userIndex)) }, navFavorite = { navController.navigate(Favorite(quizMenu.userIndex)) }, navProfil = { navController.navigate(Profil(quizMenu.userIndex)) }, navControllerQuiz = { idQuiz -> navController.navigate(Quiz(quizMenu.userIndex, idQuiz)) - } + }, + ) } composable { @@ -236,7 +247,9 @@ fun AppNavigator() { navControllerQuizEnd = { idQuiz, pts -> navController.navigate(QuizEnd(quiz.userIndex, idQuiz, pts)) }, - idQuiz = quiz.idQuiz + idQuiz = quiz.idQuiz, + currentUserVM = currentUserVM, + currentUserState = currentUserState, ) } composable { @@ -248,7 +261,9 @@ fun AppNavigator() { navAccueil = { navController.navigate(Accueil(quizEnd.userIndex)) }, navFavorite = { navController.navigate(Favorite(quizEnd.userIndex)) }, navProfil = { navController.navigate(Profil(quizEnd.userIndex)) }, - navQuiz = { navController.navigate(QuizMenu(quizEnd.userIndex)) } + navQuiz = { navController.navigate(QuizMenu(quizEnd.userIndex)) }, + currentUserVM = currentUserVM, + currentUserState = currentUserState, ) } } diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/AccueilPage.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/AccueilPage.kt index 6161a81..0b7016c 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/AccueilPage.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/AccueilPage.kt @@ -21,28 +21,31 @@ import com.example.what_the_fantasy.data.local.DailyQuoteStub import com.example.what_the_fantasy.data.services.IServices import com.example.what_the_fantasy.ui.components.NavBar import com.example.what_the_fantasy.ui.components.QuoteLittle +import com.example.what_the_fantasy.ui.states.CurrentUserState import com.example.what_the_fantasy.ui.theme.colorBackground +import com.example.what_the_fantasy.ui.viewModels.CurrentUserViewModel @Composable fun AccueilPage( - index: Int, navFavorite: (Int) -> Unit, navQuiz: (Int) -> Unit, navProfil: (Int) -> Unit, navQuote: (Int) -> Unit, - services: IServices + services: IServices, + currentUserVM : CurrentUserViewModel, + currentUserState : CurrentUserState, -) { + ) { var itemCount by remember { mutableStateOf(15) } val dailyQuote = DailyQuoteStub.dailyQuote val quotes = services.getAllQuote().take(itemCount) - val user = services.getUserById(index) ?: return val titleDalyQuote = stringResource(R.string.TitleHomeDailyQuote) val titleSuggestion = stringResource(R.string.TitleHomeSuggestion) NavBar( onAccueil = true, - index = index, + currentUserVM = currentUserVM, + currentUserState = currentUserState, navControllerFavorite = navFavorite, navControllerAccueil = { }, navControllerProfil = navProfil, @@ -82,7 +85,7 @@ fun AccueilPage( Column(Modifier.clickable {navQuote(quote.id)} ) { - if(quote.language == user.langage){ + if(quote.language == currentUserState.langage){ QuoteLittle(quote) Spacer(modifier = Modifier.height(16.dp)) } diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/FavoritePage.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/FavoritePage.kt index 647aab2..2ec1e50 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/FavoritePage.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/FavoritePage.kt @@ -21,7 +21,9 @@ import com.example.what_the_fantasy.data.local.FavoriteStub import com.example.what_the_fantasy.data.services.IServices import com.example.what_the_fantasy.ui.components.NavBar import com.example.what_the_fantasy.ui.components.QuoteLittle +import com.example.what_the_fantasy.ui.states.CurrentUserState import com.example.what_the_fantasy.ui.theme.colorBackground +import com.example.what_the_fantasy.ui.viewModels.CurrentUserViewModel @Composable fun FavoritePage( @@ -30,7 +32,9 @@ fun FavoritePage( navQuiz: (Int) -> Unit, navProfil: (Int) -> Unit, navQuote: (Int) -> Unit, - services: IServices + services: IServices, + currentUserVM : CurrentUserViewModel, + currentUserState : CurrentUserState, ) { val user = services.getUserById(index) ?: return @@ -38,7 +42,8 @@ fun FavoritePage( val TitlePage = stringResource(R.string.TitleFavorite) NavBar(onFavorite = true, - index = index, + currentUserVM = currentUserVM, + currentUserState = currentUserState, navControllerFavorite = { }, navControllerAccueil = navAccueil, navControllerProfil = navProfil, diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/ProfilPage.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/ProfilPage.kt index 5447e63..a910941 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/ProfilPage.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/ProfilPage.kt @@ -28,7 +28,6 @@ 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 @@ -46,7 +45,6 @@ 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 coil.compose.AsyncImage import com.example.what_the_fantasy.Logs.LogsUsers import com.example.what_the_fantasy.R @@ -67,11 +65,10 @@ fun ProfilPage(navFavorite: (Int) -> Unit, currentUserVM : CurrentUserViewModel, currentUserState : CurrentUserState, ) { -// val currentUserVM : CurrentUserViewModel = viewModel() -// val currentUserState by currentUserVM.currentUserState.collectAsState() - Log.e("Profil","${currentUserState.id}") + NavBar(onProfile = true, - index = currentUserState.id, + currentUserVM = currentUserVM, + currentUserState = currentUserState, navControllerFavorite = navFavorite, navControllerAccueil = navAccueil, navControllerProfil = {}, diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/QuizEndPage.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/QuizEndPage.kt index bad6590..fa27756 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/QuizEndPage.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/QuizEndPage.kt @@ -17,6 +17,8 @@ import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import com.example.what_the_fantasy.data.local.QuizStub import com.example.what_the_fantasy.ui.components.NavBar +import com.example.what_the_fantasy.ui.states.CurrentUserState +import com.example.what_the_fantasy.ui.viewModels.CurrentUserViewModel val gradient = Brush.linearGradient( colors = listOf(Color(0xFF7B1FA2), Color(0xFF311B92)), @@ -33,8 +35,12 @@ fun QuizEndPage( navAccueil: (Int) -> Unit, navProfil:(Int) -> Unit, navQuiz: (Int) -> Unit, + currentUserVM : CurrentUserViewModel, + currentUserState : CurrentUserState, ) { - NavBar(index = index, + NavBar( + currentUserVM = currentUserVM, + currentUserState = currentUserState, navControllerFavorite = navFavorite, navControllerAccueil = navAccueil, navControllerProfil = navProfil, diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/QuizMenu.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/QuizMenu.kt index 35509ac..52b5778 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/QuizMenu.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/QuizMenu.kt @@ -31,17 +31,22 @@ import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import com.example.what_the_fantasy.data.local.QuizStub import com.example.what_the_fantasy.ui.components.NavBar +import com.example.what_the_fantasy.ui.states.CurrentUserState +import com.example.what_the_fantasy.ui.viewModels.CurrentUserViewModel @Composable fun QuizMenu( - index: Int, + currentUserVM : CurrentUserViewModel, + currentUserState : CurrentUserState, navFavorite: (Int) -> Unit, navAccueil: (Int) -> Unit, navProfil:(Int) -> Unit, - navControllerQuiz: (Int) -> Unit + navControllerQuiz: (Int) -> Unit, + ) { NavBar(onQuiz = true, - index = index, + currentUserVM = currentUserVM, + currentUserState = currentUserState, navControllerFavorite = navFavorite, navControllerAccueil = navAccueil, navControllerProfil = navProfil, diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/QuizPage.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/QuizPage.kt index 14048e4..2899fa2 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/QuizPage.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/QuizPage.kt @@ -19,10 +19,14 @@ import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import com.example.what_the_fantasy.data.local.QuizStub import com.example.what_the_fantasy.ui.components.NavBar +import com.example.what_the_fantasy.ui.states.CurrentUserState +import com.example.what_the_fantasy.ui.viewModels.CurrentUserViewModel @Composable fun QuizPage( index: Int, + currentUserVM : CurrentUserViewModel, + currentUserState : CurrentUserState, navFavorite: (Int) -> Unit, navAccueil: (Int) -> Unit, navProfil:(Int) -> Unit, @@ -56,7 +60,9 @@ fun QuizPage( if (idCurrentQuestion < questions.size - 1) idCurrentQuestion++ else navControllerQuizEnd(idQuiz, pts) // Retour menu } - NavBar(index = index, + NavBar( + currentUserVM = currentUserVM, + currentUserState = currentUserState, navControllerFavorite = navFavorite, navControllerAccueil = navAccueil, navControllerProfil = navProfil, diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/QuotePage.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/QuotePage.kt index 16dcfac..11ba5d3 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/QuotePage.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/QuotePage.kt @@ -35,11 +35,13 @@ import com.example.what_the_fantasy.R import coil.compose.AsyncImage import com.example.what_the_fantasy.data.services.IServices import com.example.what_the_fantasy.ui.components.NavBar +import com.example.what_the_fantasy.ui.states.CurrentUserState 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.theme.iconText import com.example.what_the_fantasy.ui.theme.likeIcon import com.example.what_the_fantasy.ui.theme.whiteBackcgroundText +import com.example.what_the_fantasy.ui.viewModels.CurrentUserViewModel @Composable fun QuotePage( @@ -49,12 +51,15 @@ fun QuotePage( navAccueil: (Int) -> Unit, navFavorite:(Int) -> Unit, navQuiz: (Int) -> Unit, - navProfil:(Int) -> Unit) + navProfil:(Int) -> Unit, + currentUserVM : CurrentUserViewModel, + currentUserState : CurrentUserState,) { var quote = service.getQuote(quoteId) ?: return val context = LocalContext.current NavBar(onProfile = true, - index = index, + currentUserVM = currentUserVM, + currentUserState = currentUserState, navControllerFavorite = navFavorite, navControllerAccueil = navAccueil, navControllerProfil = navProfil, diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/RecapSubmitPage.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/RecapSubmitPage.kt index 1c7a3db..fe3404a 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/RecapSubmitPage.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/RecapSubmitPage.kt @@ -39,6 +39,8 @@ import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.style.TextAlign import com.example.what_the_fantasy.data.model.Character +import com.example.what_the_fantasy.ui.states.CurrentUserState +import com.example.what_the_fantasy.ui.viewModels.CurrentUserViewModel @Composable @@ -49,10 +51,13 @@ fun RecapSubmitPage( navProfil:(Int) -> Unit, quoteContent : String, character: String, - source: String + source: String, + currentUserVM : CurrentUserViewModel, + currentUserState : CurrentUserState, ) { NavBar(onQuiz = true, - index = index, + currentUserVM = currentUserVM, + currentUserState = currentUserState, navControllerFavorite = navFavorite, navControllerAccueil = navAccueil, navControllerProfil = navProfil, diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/SubmitQuotePage.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/SubmitQuotePage.kt index 675dfac..48d9c06 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/SubmitQuotePage.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/SubmitQuotePage.kt @@ -34,8 +34,10 @@ import com.example.what_the_fantasy.ui.components.ErrorMessageSubmitQuoteCompone import com.example.what_the_fantasy.ui.components.NavBar import com.example.what_the_fantasy.ui.components.SpaceHeightComponent import com.example.what_the_fantasy.ui.components.TitlePageComponent +import com.example.what_the_fantasy.ui.states.CurrentUserState 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.CurrentUserViewModel @Composable fun SubmitQuotePage( @@ -44,10 +46,13 @@ fun SubmitQuotePage( navAccueil: (Int) -> Unit, navProfil:(Int) -> Unit, navControllerQuiz: (Int) -> Unit, - navRecap: (String, String, String) -> Unit + navRecap: (String, String, String) -> Unit, + currentUserVM : CurrentUserViewModel, + currentUserState : CurrentUserState, ) { NavBar( - index = index, + currentUserVM = currentUserVM, + currentUserState = currentUserState, navControllerFavorite = navFavorite, navControllerAccueil = navAccueil, navControllerProfil = navProfil, From 60ec8300f44ed563e29a011540ca9087c06bcffb Mon Sep 17 00:00:00 2001 From: "leni.beaulaton" Date: Sun, 30 Mar 2025 12:35:20 +0200 Subject: [PATCH 15/15] Nettoyage --- .../ui/navigations/AppNavigator.kt | 88 +++++++++---------- .../ui/screens/AccueilPage.kt | 4 +- .../ui/screens/FavoritePage.kt | 3 +- .../what_the_fantasy/ui/screens/LoginPage.kt | 2 - .../what_the_fantasy/ui/screens/ProfilPage.kt | 6 +- .../ui/screens/QuizEndPage.kt | 1 - .../what_the_fantasy/ui/screens/QuizPage.kt | 1 - .../what_the_fantasy/ui/screens/QuotePage.kt | 1 - .../ui/screens/RecapSubmitPage.kt | 1 - .../ui/screens/SubmitQuotePage.kt | 3 +- .../ui/viewModels/CurrentUserViewModel.kt | 23 ++--- 11 files changed, 53 insertions(+), 80 deletions(-) diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/navigations/AppNavigator.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/navigations/AppNavigator.kt index 8b4f148..5925767 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/navigations/AppNavigator.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/navigations/AppNavigator.kt @@ -103,16 +103,16 @@ fun AppNavigator() { } composable { - val accueil: Accueil = it.toRoute() + //val accueil: Accueil = it.toRoute() AccueilPage( - navFavorite = { navController.navigate(Favorite(accueil.userIndex)) }, - navQuiz = { navController.navigate(QuizMenu(accueil.userIndex)) }, - navProfil = { navController.navigate(Profil(accueil.userIndex)) }, + navFavorite = { navController.navigate(Favorite(currentUserState.id)) }, + navQuiz = { navController.navigate(QuizMenu(currentUserState.id)) }, + navProfil = { navController.navigate(Profil(currentUserState.id)) }, navQuote = { quoteId -> navController.navigate( OneQuote( quoteId, - accueil.userIndex + currentUserState.id ) ) }, @@ -122,17 +122,16 @@ fun AppNavigator() { ) } composable { - val favorite: Favorite = it.toRoute() + //val favorite: Favorite = it.toRoute() FavoritePage( - index = favorite.userIndex, - navAccueil = { navController.navigate(Accueil(favorite.userIndex)) }, - navQuiz = { navController.navigate(QuizMenu(favorite.userIndex)) }, - navProfil = { navController.navigate(Profil(favorite.userIndex)) }, + navAccueil = { navController.navigate(Accueil(currentUserState.id)) }, + navQuiz = { navController.navigate(QuizMenu(currentUserState.id)) }, + navProfil = { navController.navigate(Profil(currentUserState.id)) }, navQuote = { quoteId -> navController.navigate( OneQuote( quoteId, - favorite.userIndex + currentUserState.id ) ) }, @@ -144,10 +143,10 @@ fun AppNavigator() { composable { val profil: Profil = it.toRoute() ProfilPage( - navFavorite = { navController.navigate(Favorite(profil.userIndex)) }, - navAccueil = { navController.navigate(Accueil(profil.userIndex)) }, - navQuiz = { navController.navigate(QuizMenu(profil.userIndex)) }, - navSubmitQuote = { navController.navigate(SubmitQuote(profil.userIndex)) }, + navFavorite = { navController.navigate(Favorite(currentUserState.id)) }, + navAccueil = { navController.navigate(Accueil(currentUserState.id)) }, + navQuiz = { navController.navigate(QuizMenu(currentUserState.id)) }, + navSubmitQuote = { navController.navigate(SubmitQuote(currentUserState.id)) }, navUnLog = { navController.navigate(Login) { popUpTo(profil) { inclusive = true } @@ -161,11 +160,10 @@ fun AppNavigator() { val quote: OneQuote = it.toRoute() QuotePage( quoteId = quote.quoteId, - index = quote.userIndex, - navAccueil = { navController.navigate(Accueil(quote.userIndex)) }, - navQuiz = { navController.navigate(QuizMenu(quote.userIndex)) }, - navProfil = { navController.navigate(Profil(quote.userIndex)) }, - navFavorite = { navController.navigate(Favorite(quote.userIndex)) }, + navAccueil = { navController.navigate(Accueil(currentUserState.id)) }, + navQuiz = { navController.navigate(QuizMenu(currentUserState.id)) }, + navProfil = { navController.navigate(Profil(currentUserState.id)) }, + navFavorite = { navController.navigate(Favorite(currentUserState.id)) }, service = services, currentUserVM = currentUserVM, currentUserState = currentUserState, @@ -184,19 +182,18 @@ fun AppNavigator() { ) } composable { - val submitQuote: SubmitQuote = it.toRoute() + //val submitQuote: SubmitQuote = it.toRoute() SubmitQuotePage( - index = submitQuote.userIndex, - navAccueil = { navController.navigate(Accueil(submitQuote.userIndex)) }, - navFavorite = { navController.navigate(Favorite(submitQuote.userIndex)) }, - navProfil = { navController.navigate(Profil(submitQuote.userIndex)) }, + navAccueil = { navController.navigate(Accueil(currentUserState.id)) }, + navFavorite = { navController.navigate(Favorite(currentUserState.id)) }, + navProfil = { navController.navigate(Profil(currentUserState.id)) }, navControllerQuiz = { idQuiz -> - navController.navigate(Quiz(submitQuote.userIndex, idQuiz)) + navController.navigate(Quiz(currentUserState.id, idQuiz)) }, navRecap = { quoteContent, character, source -> navController.navigate( RecapSubmit( - submitQuote.userIndex, + currentUserState.id, quoteContent, character, source @@ -210,28 +207,27 @@ fun AppNavigator() { composable { val recapSubmit: RecapSubmit = it.toRoute() RecapSubmitPage( - index = recapSubmit.userIndex, quoteContent = recapSubmit.quoteContent, character = recapSubmit.character, source = recapSubmit.source, - navAccueil = { navController.navigate(Accueil(recapSubmit.userIndex)) }, - navFavorite = { navController.navigate(Favorite(recapSubmit.userIndex)) }, - navProfil = { navController.navigate(Profil(recapSubmit.userIndex)) }, + navAccueil = { navController.navigate(Accueil(currentUserState.id)) }, + navFavorite = { navController.navigate(Favorite(currentUserState.id)) }, + navProfil = { navController.navigate(Profil(currentUserState.id)) }, currentUserVM = currentUserVM, currentUserState = currentUserState, ) } composable { - val quizMenu: QuizMenu = it.toRoute() + //val quizMenu: QuizMenu = it.toRoute() QuizMenu( currentUserVM = currentUserVM, currentUserState = currentUserState, - navAccueil = { navController.navigate(Accueil(quizMenu.userIndex)) }, - navFavorite = { navController.navigate(Favorite(quizMenu.userIndex)) }, - navProfil = { navController.navigate(Profil(quizMenu.userIndex)) }, + navAccueil = { navController.navigate(Accueil(currentUserState.id)) }, + navFavorite = { navController.navigate(Favorite(currentUserState.id)) }, + navProfil = { navController.navigate(Profil(currentUserState.id)) }, navControllerQuiz = { idQuiz -> - navController.navigate(Quiz(quizMenu.userIndex, idQuiz)) + navController.navigate(Quiz(currentUserState.id, idQuiz)) }, ) @@ -239,13 +235,12 @@ fun AppNavigator() { composable { val quiz: Quiz = it.toRoute() QuizPage( - index = quiz.userIndex, - navAccueil = { navController.navigate(Accueil(quiz.userIndex)) }, - navFavorite = { navController.navigate(Favorite(quiz.userIndex)) }, - navProfil = { navController.navigate(Profil(quiz.userIndex)) }, - navQuiz = { navController.navigate(QuizMenu(quiz.userIndex)) }, + navAccueil = { navController.navigate(Accueil(currentUserState.id)) }, + navFavorite = { navController.navigate(Favorite(currentUserState.id)) }, + navProfil = { navController.navigate(Profil(currentUserState.id)) }, + navQuiz = { navController.navigate(QuizMenu(currentUserState.id)) }, navControllerQuizEnd = { idQuiz, pts -> - navController.navigate(QuizEnd(quiz.userIndex, idQuiz, pts)) + navController.navigate(QuizEnd(currentUserState.id, idQuiz, pts)) }, idQuiz = quiz.idQuiz, currentUserVM = currentUserVM, @@ -257,11 +252,10 @@ fun AppNavigator() { QuizEndPage( idQuiz = quizEnd.idQuiz, points = quizEnd.pts, - index = quizEnd.userIndex, - navAccueil = { navController.navigate(Accueil(quizEnd.userIndex)) }, - navFavorite = { navController.navigate(Favorite(quizEnd.userIndex)) }, - navProfil = { navController.navigate(Profil(quizEnd.userIndex)) }, - navQuiz = { navController.navigate(QuizMenu(quizEnd.userIndex)) }, + navAccueil = { navController.navigate(Accueil(currentUserState.id)) }, + navFavorite = { navController.navigate(Favorite(currentUserState.id)) }, + navProfil = { navController.navigate(Profil(currentUserState.id)) }, + navQuiz = { navController.navigate(QuizMenu(currentUserState.id)) }, currentUserVM = currentUserVM, currentUserState = currentUserState, ) diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/AccueilPage.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/AccueilPage.kt index 0b7016c..779768d 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/AccueilPage.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/AccueilPage.kt @@ -11,7 +11,6 @@ import androidx.compose.material3.Text import androidx.compose.runtime.* import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier -import androidx.compose.ui.graphics.Color import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.dp @@ -22,7 +21,6 @@ import com.example.what_the_fantasy.data.services.IServices import com.example.what_the_fantasy.ui.components.NavBar import com.example.what_the_fantasy.ui.components.QuoteLittle import com.example.what_the_fantasy.ui.states.CurrentUserState -import com.example.what_the_fantasy.ui.theme.colorBackground import com.example.what_the_fantasy.ui.viewModels.CurrentUserViewModel @Composable @@ -36,7 +34,7 @@ fun AccueilPage( currentUserState : CurrentUserState, ) { - var itemCount by remember { mutableStateOf(15) } + var itemCount by remember { mutableIntStateOf(15) } val dailyQuote = DailyQuoteStub.dailyQuote val quotes = services.getAllQuote().take(itemCount) diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/FavoritePage.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/FavoritePage.kt index 2ec1e50..dab291e 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/FavoritePage.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/FavoritePage.kt @@ -27,7 +27,6 @@ import com.example.what_the_fantasy.ui.viewModels.CurrentUserViewModel @Composable fun FavoritePage( - index: Int, navAccueil: (Int) -> Unit, navQuiz: (Int) -> Unit, navProfil: (Int) -> Unit, @@ -36,7 +35,7 @@ fun FavoritePage( currentUserVM : CurrentUserViewModel, currentUserState : CurrentUserState, ) { - val user = services.getUserById(index) ?: return + val user = services.getUserById(currentUserState.id) ?: return val quotes = services.getFavorite(user) 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 e669129..266364f 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 @@ -53,8 +53,6 @@ fun LoginPage(navControllerSignUp: () -> Unit, authState : AuthUserState, initialierCurrentUser : (Int) -> Unit) { -// val authUserVM : AuthUserViewModel = viewModel() -// val authState by authUserVM.userState.collectAsState() Box( modifier = Modifier diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/ProfilPage.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/ProfilPage.kt index a910941..792f0cc 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/ProfilPage.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/ProfilPage.kt @@ -113,7 +113,7 @@ fun ProfilPage(navFavorite: (Int) -> Unit, SpaceHeightComponent(16) ButtonLanguage(R.string.ButtonLanguageprofile, 18, MaterialTheme.colorScheme.background,currentUserVM, currentUserState) SpaceHeightComponent(16) - ButtonUnLog(R.string.ButtonUnlogprofile, 18, navUnLog) + ButtonUnLog(R.string.ButtonUnlogprofile, 18, navUnLog,currentUserVM) @@ -446,14 +446,16 @@ fun DisplayPassword(onEdit: () -> Unit) { } @Composable -fun ButtonUnLog(textResId : Int, size :Int,navController: () -> Unit){ +fun ButtonUnLog(textResId : Int, size :Int,navController: () -> Unit, currentUserVM: CurrentUserViewModel){ val text = stringResource(id = textResId) val logsUser = LogsUsers() //gestion des logs pour les utilisateurs Button( onClick = { + currentUserVM.clearCurrentUser() navController() logsUser.unlogInformationUserConnect("UserUnLog") + }, colors = ButtonDefaults.buttonColors(containerColor = MaterialTheme.colorScheme.background), modifier = Modifier.fillMaxWidth(), diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/QuizEndPage.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/QuizEndPage.kt index fa27756..4f1fe82 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/QuizEndPage.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/QuizEndPage.kt @@ -30,7 +30,6 @@ val gradient = Brush.linearGradient( fun QuizEndPage( idQuiz: Int, points: Int, - index: Int, navFavorite: (Int) -> Unit, navAccueil: (Int) -> Unit, navProfil:(Int) -> Unit, diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/QuizPage.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/QuizPage.kt index 2899fa2..0691a47 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/QuizPage.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/QuizPage.kt @@ -24,7 +24,6 @@ import com.example.what_the_fantasy.ui.viewModels.CurrentUserViewModel @Composable fun QuizPage( - index: Int, currentUserVM : CurrentUserViewModel, currentUserState : CurrentUserState, navFavorite: (Int) -> Unit, diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/QuotePage.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/QuotePage.kt index 11ba5d3..5afde11 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/QuotePage.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/QuotePage.kt @@ -47,7 +47,6 @@ import com.example.what_the_fantasy.ui.viewModels.CurrentUserViewModel fun QuotePage( quoteId : Int, service : IServices, - index : Int, navAccueil: (Int) -> Unit, navFavorite:(Int) -> Unit, navQuiz: (Int) -> Unit, diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/RecapSubmitPage.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/RecapSubmitPage.kt index fe3404a..a810c22 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/RecapSubmitPage.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/RecapSubmitPage.kt @@ -45,7 +45,6 @@ import com.example.what_the_fantasy.ui.viewModels.CurrentUserViewModel @Composable fun RecapSubmitPage( - index: Int, navFavorite: (Int) -> Unit, navAccueil: (Int) -> Unit, navProfil:(Int) -> Unit, diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/SubmitQuotePage.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/SubmitQuotePage.kt index 48d9c06..6a532ee 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/SubmitQuotePage.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/SubmitQuotePage.kt @@ -41,7 +41,6 @@ import com.example.what_the_fantasy.ui.viewModels.CurrentUserViewModel @Composable fun SubmitQuotePage( - index: Int, navFavorite: (Int) -> Unit, navAccueil: (Int) -> Unit, navProfil:(Int) -> Unit, @@ -89,7 +88,7 @@ fun SubmitQuotePage( navRecap ) SpaceHeightComponent(20) - BackButton(R.string.titleButtonBack, 12, Color.White,navProfil, index) + BackButton(R.string.titleButtonBack, 12, Color.White,navProfil, currentUserState.id) } } } diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/viewModels/CurrentUserViewModel.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/viewModels/CurrentUserViewModel.kt index e4fa71b..2085ba2 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/viewModels/CurrentUserViewModel.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/viewModels/CurrentUserViewModel.kt @@ -2,7 +2,6 @@ package com.example.what_the_fantasy.ui.viewModels import android.util.Log import androidx.lifecycle.ViewModel -import androidx.lifecycle.viewModelScope import com.example.what_the_fantasy.data.model.SrcLanguage import com.example.what_the_fantasy.data.services.ServicesStub import com.example.what_the_fantasy.ui.states.CurrentUserState @@ -10,25 +9,12 @@ import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.update -import kotlinx.coroutines.launch class CurrentUserViewModel : ViewModel(){ private val services = ServicesStub() // faire repository qui gère les services Stub et API private val _currentUserState = MutableStateFlow(CurrentUserState()) var currentUserState : StateFlow = _currentUserState.asStateFlow() -// init{ -// viewModelScope.launch { -// services.getUserById(10)?.let { // A changer : renvoie le meme user pour le moment -// setId(it.id) -// setUsername(it.username) -// setEmail(it.email) -// setPassword(it.password) -// setLangue(it.langage) -// setImage(it.imgUrl) -// } -// } -// } fun initialiseCurrentUser(index : Int){ services.getUserById(index)?.let { @@ -39,7 +25,11 @@ class CurrentUserViewModel : ViewModel(){ setLangue(it.langage) setImage(it.imgUrl) } - Log.e("ProfilInit", "${currentUserState.value.id}") + + } + + fun clearCurrentUser(){ + _currentUserState.value = CurrentUserState() } fun setId(id : Int){ @@ -87,9 +77,6 @@ class CurrentUserViewModel : ViewModel(){ fun editPassword(password : String, index : Int){ services.EditPasswd(password, index) - _currentUserState.update { - it.copy(password = password) - } } fun editLangue(index : Int){