From db175c17f938d27f88a6dc08e06c9f24a5ea3d68 Mon Sep 17 00:00:00 2001 From: "leni.beaulaton" Date: Sat, 5 Apr 2025 15:42:27 +0200 Subject: [PATCH] Affiche les info utilisateur profil --- .../what_the_fantasy/data/model/User.kt | 7 +++++++ .../data/retrofit/ApiService.kt | 6 ++++-- .../data/services/IServices.kt | 5 ++++- .../data/services/ServicesAPI.kt | 8 ++++++-- .../data/services/ServicesStub.kt | 5 ++++- .../ui/navigations/AppNavigator.kt | 2 ++ .../what_the_fantasy/ui/screens/LoginPage.kt | 15 ++++++++++++--- .../what_the_fantasy/ui/screens/ProfilPage.kt | 1 + .../ui/viewModels/AuthUserViewModel.kt | 4 ++-- .../ui/viewModels/CurrentUserViewModel.kt | 19 +++++++++++++++---- 10 files changed, 57 insertions(+), 15 deletions(-) diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/model/User.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/model/User.kt index eea94ba..9a4fe0a 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/model/User.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/model/User.kt @@ -1,11 +1,18 @@ package com.example.what_the_fantasy.data.model +import com.google.gson.annotations.SerializedName + class User( val id:Int, + @SerializedName("pseudo") var username:String, + @SerializedName("email") var email:String, + @SerializedName("date") var date:String, + @SerializedName("imageProfil") val imgUrl: String, + var password: String, var langage : SrcLanguage ) \ No newline at end of file diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/retrofit/ApiService.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/retrofit/ApiService.kt index 8f8520a..15c7c82 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/retrofit/ApiService.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/retrofit/ApiService.kt @@ -8,6 +8,7 @@ import retrofit2.converter.gson.GsonConverterFactory import retrofit2.http.Body import retrofit2.http.GET import retrofit2.http.POST +import retrofit2.http.Path import retrofit2.http.Query data class UserUpdateRequest( @@ -25,11 +26,12 @@ interface UserApiService { @Query("username") username: String ): User - @GET("users/") + @GET("users/{id}") suspend fun getUserById( - @Query("id") id: Int + @Path("id") id: Int ): User + @POST("users") suspend fun updateUser( @Query("id") id: Int, 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 0b8f688..a16d76d 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 @@ -6,6 +6,7 @@ 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 com.example.what_the_fantasy.ui.viewModels.CurrentUserViewModel import kotlinx.coroutines.flow.StateFlow interface IServices { @@ -13,7 +14,9 @@ interface IServices { suspend fun validLogin(username : String, passwd : String, navController: (Int) -> Unit, - initialierCurrentUser : (Int) ->Unit): Boolean + initialierCurrentUser : (Int) ->Unit, + currentUserViewModel: CurrentUserViewModel + ): Boolean suspend fun EditUsername(username : String, index : Int) : Boolean suspend fun EditEmail(email : String, index : Int) : Boolean diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesAPI.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesAPI.kt index 9de0606..b5d0a0b 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesAPI.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesAPI.kt @@ -1,5 +1,6 @@ package com.example.what_the_fantasy.data.services +import android.util.Log import com.example.what_the_fantasy.data.local.UserStub.users import com.example.what_the_fantasy.data.model.Comment import com.example.what_the_fantasy.data.model.Favorite @@ -8,6 +9,7 @@ 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.retrofit.RetrofitInstance import com.example.what_the_fantasy.data.retrofit.UserUpdateRequest +import com.example.what_the_fantasy.ui.viewModels.CurrentUserViewModel //import com.example.what_the_fantasy.data.model.Comment //import com.example.what_the_fantasy.data.model.Favorite @@ -21,7 +23,8 @@ class ServicesAPI : IServices { username: String, passwd: String, navController: (Int) -> Unit, - initialierCurrentUser: (Int) -> Unit + initialierCurrentUser: (Int) -> Unit, + currentUserViewModel: CurrentUserViewModel ): Boolean { try { val response = RetrofitInstance.api.getUserByUsername(username) // on récupère l'utilisateur @@ -32,6 +35,7 @@ class ServicesAPI : IServices { if (passwd == hashedPasswordAPI) { // on compare les deux mots de passe navController(index) initialierCurrentUser(index) + currentUserViewModel.setUser(response) return true } else { @@ -131,7 +135,7 @@ class ServicesAPI : IServices { TODO("Not yet implemented") } - override suspend fun getUserById(id: Int): User? { + override suspend fun getUserById(id: Int): User { return RetrofitInstance.api.getUserById(id) } 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 d184729..c6363fb 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 @@ -12,6 +12,7 @@ import com.example.what_the_fantasy.data.local.CommentStub.comments import com.example.what_the_fantasy.data.model.Comment 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.viewModels.CurrentUserViewModel import java.time.LocalDate class ServicesStub : IServices { @@ -21,7 +22,9 @@ class ServicesStub : IServices { override suspend fun validLogin(username : String, passwd : String, navController: (Int) -> Unit, - initialierCurrentUser : (Int) ->Unit): Boolean{ + initialierCurrentUser : (Int) ->Unit, + currentUserViewModel: CurrentUserViewModel + ): Boolean{ users.forEachIndexed { index, user -> val hashPassWd = hashPassword(passwd) 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 01ffa7f..9b99147 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 @@ -1,5 +1,6 @@ package com.example.what_the_fantasy.ui.navigations +import android.util.Log import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.padding @@ -112,6 +113,7 @@ fun AppNavigator() { }, authUserVM = authUserVM, authState = authState, + currentUserViewModel = currentUserVM, initialierCurrentUser ={ coroutineScope.launch { currentUserVM.initialiseCurrentUser(it) 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 ed771f8..1b0a7e5 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,6 +45,7 @@ import com.example.what_the_fantasy.data.services.hashPassword import com.example.what_the_fantasy.ui.components.VisibleIconPasswordComponent import com.example.what_the_fantasy.ui.states.AuthUserState import com.example.what_the_fantasy.ui.viewModels.AuthUserViewModel +import com.example.what_the_fantasy.ui.viewModels.CurrentUserViewModel import kotlinx.coroutines.launch @Composable @@ -52,6 +53,7 @@ fun LoginPage(navControllerSignUp: () -> Unit, navControllerProfil: (Int) -> Unit, authUserVM : AuthUserViewModel, authState : AuthUserState, + currentUserViewModel : CurrentUserViewModel, initialierCurrentUser : (Int) -> Unit) { @@ -79,7 +81,7 @@ fun LoginPage(navControllerSignUp: () -> Unit, authUserVM.setUsername(it) }, PassWdTextField(R.string.PasswdLogin, authState.password){ authUserVM.setPassword(it) - }, R.string.ButtonLogin,18,navControllerProfil){ + }, R.string.ButtonLogin,18,currentUserViewModel,navControllerProfil){ initialierCurrentUser(it) } SpaceHeightComponent(16) @@ -138,14 +140,21 @@ fun PassWdTextField(textpasswdResId : Int, password : String, onValueChange: (St @Composable -fun ConnexionButtonLogin(authUserVM : AuthUserViewModel, username : String, passwd : String, titleResId : Int, size : Int, navController: (Int) -> Unit, initialierCurrentUser : (Int) -> Unit){ +fun ConnexionButtonLogin(authUserVM : AuthUserViewModel, + username : String, + passwd : String, + titleResId : Int, + size : Int, + currentUserViewModel: CurrentUserViewModel, + navController: (Int) -> Unit, + initialierCurrentUser : (Int) -> Unit){ val title = stringResource(id = titleResId) var showError by remember { mutableStateOf(false) } val coroutineScope = rememberCoroutineScope() Button( onClick = { coroutineScope.launch { - val result = authUserVM.validLogin(username, passwd, navController, initialierCurrentUser) + val result = authUserVM.validLogin(username, passwd, navController, initialierCurrentUser, currentUserViewModel) showError = !result } }, 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 3dcc293..5319114 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 @@ -58,6 +58,7 @@ 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.viewModels.CurrentUserViewModel +import kotlinx.coroutines.delay import kotlinx.coroutines.launch @Composable 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 7c458b9..dcc58b7 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 @@ -26,7 +26,7 @@ class AuthUserViewModel : ViewModel(){ _userState.update { it.copy(password=password) } } - suspend fun validLogin(username : String, passwd : String, navController: (Int) -> Unit, initialierCurrentUser : (Int) -> Unit) : Boolean{ - return services.validLogin(username,passwd, navController, initialierCurrentUser) + suspend fun validLogin(username : String, passwd : String, navController: (Int) -> Unit, initialierCurrentUser : (Int) -> Unit, currentUserViewModel: CurrentUserViewModel) : Boolean{ + return services.validLogin(username,passwd, navController, initialierCurrentUser,currentUserViewModel) } } \ 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 f84df69..1920bdc 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 @@ -3,6 +3,7 @@ package com.example.what_the_fantasy.ui.viewModels import android.util.Log 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.ServicesAPI import com.example.what_the_fantasy.data.services.ServicesStub import com.example.what_the_fantasy.ui.states.CurrentUserState @@ -12,23 +13,33 @@ import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.update class CurrentUserViewModel : ViewModel(){ - private val services = ServicesStub() - //private val services = ServicesAPI() // faire repository qui gère les services Stub et API + //private val services = ServicesStub() + private val services = ServicesAPI() // faire repository qui gère les services Stub et API private val _currentUserState = MutableStateFlow(CurrentUserState()) var currentUserState : StateFlow = _currentUserState.asStateFlow() suspend fun initialiseCurrentUser(index : Int){ - services.getUserById(index)?.let { + services.getUserById(index).let { setId(it.id) setUsername(it.username) setEmail(it.email) setPassword(it.password) - setLangue(it.langage) + //setLangue(it.langage) // A rajouter quand on aura le langage setImage(it.imgUrl) } } + fun setUser(user: User) { + _currentUserState.value = CurrentUserState( + id = user.id, + username = user.username, + email = user.email, + password = user.password, + imagePath = user.imgUrl, + ) + } + fun clearCurrentUser(){ _currentUserState.value = CurrentUserState() }