From 2e98af24501864cb7e550e19de7a822e65104117 Mon Sep 17 00:00:00 2001 From: "leni.beaulaton" Date: Sat, 29 Mar 2025 19:26:58 +0100 Subject: [PATCH] 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){