Affiche les info utilisateur profil

ConnectAPI
Leni BEAULATON 2 weeks ago
parent 09be084651
commit db175c17f9

@ -1,11 +1,18 @@
package com.example.what_the_fantasy.data.model package com.example.what_the_fantasy.data.model
import com.google.gson.annotations.SerializedName
class User( class User(
val id:Int, val id:Int,
@SerializedName("pseudo")
var username:String, var username:String,
@SerializedName("email")
var email:String, var email:String,
@SerializedName("date")
var date:String, var date:String,
@SerializedName("imageProfil")
val imgUrl: String, val imgUrl: String,
var password: String, var password: String,
var langage : SrcLanguage var langage : SrcLanguage
) )

@ -8,6 +8,7 @@ import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.Body import retrofit2.http.Body
import retrofit2.http.GET import retrofit2.http.GET
import retrofit2.http.POST import retrofit2.http.POST
import retrofit2.http.Path
import retrofit2.http.Query import retrofit2.http.Query
data class UserUpdateRequest( data class UserUpdateRequest(
@ -25,11 +26,12 @@ interface UserApiService {
@Query("username") username: String @Query("username") username: String
): User ): User
@GET("users/") @GET("users/{id}")
suspend fun getUserById( suspend fun getUserById(
@Query("id") id: Int @Path("id") id: Int
): User ): User
@POST("users") @POST("users")
suspend fun updateUser( suspend fun updateUser(
@Query("id") id: Int, @Query("id") id: Int,

@ -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.SrcLanguage
import com.example.what_the_fantasy.data.model.User 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.states.AuthUserState
import com.example.what_the_fantasy.ui.viewModels.CurrentUserViewModel
import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.StateFlow
interface IServices { interface IServices {
@ -13,7 +14,9 @@ interface IServices {
suspend fun validLogin(username : String, suspend fun validLogin(username : String,
passwd : String, passwd : String,
navController: (Int) -> Unit, navController: (Int) -> Unit,
initialierCurrentUser : (Int) ->Unit): Boolean initialierCurrentUser : (Int) ->Unit,
currentUserViewModel: CurrentUserViewModel
): Boolean
suspend fun EditUsername(username : String, index : Int) : Boolean suspend fun EditUsername(username : String, index : Int) : Boolean
suspend fun EditEmail(email : String, index : Int) : Boolean suspend fun EditEmail(email : String, index : Int) : Boolean

@ -1,5 +1,6 @@
package com.example.what_the_fantasy.data.services 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.local.UserStub.users
import com.example.what_the_fantasy.data.model.Comment import com.example.what_the_fantasy.data.model.Comment
import com.example.what_the_fantasy.data.model.Favorite 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.model.User
import com.example.what_the_fantasy.data.retrofit.RetrofitInstance import com.example.what_the_fantasy.data.retrofit.RetrofitInstance
import com.example.what_the_fantasy.data.retrofit.UserUpdateRequest 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.Comment
//import com.example.what_the_fantasy.data.model.Favorite //import com.example.what_the_fantasy.data.model.Favorite
@ -21,7 +23,8 @@ class ServicesAPI : IServices {
username: String, username: String,
passwd: String, passwd: String,
navController: (Int) -> Unit, navController: (Int) -> Unit,
initialierCurrentUser: (Int) -> Unit initialierCurrentUser: (Int) -> Unit,
currentUserViewModel: CurrentUserViewModel
): Boolean { ): Boolean {
try { try {
val response = RetrofitInstance.api.getUserByUsername(username) // on récupère l'utilisateur 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 if (passwd == hashedPasswordAPI) { // on compare les deux mots de passe
navController(index) navController(index)
initialierCurrentUser(index) initialierCurrentUser(index)
currentUserViewModel.setUser(response)
return true return true
} }
else { else {
@ -131,7 +135,7 @@ class ServicesAPI : IServices {
TODO("Not yet implemented") TODO("Not yet implemented")
} }
override suspend fun getUserById(id: Int): User? { override suspend fun getUserById(id: Int): User {
return RetrofitInstance.api.getUserById(id) return RetrofitInstance.api.getUserById(id)
} }

@ -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.Comment
import com.example.what_the_fantasy.data.model.Quote 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.SrcLanguage
import com.example.what_the_fantasy.ui.viewModels.CurrentUserViewModel
import java.time.LocalDate import java.time.LocalDate
class ServicesStub : IServices { class ServicesStub : IServices {
@ -21,7 +22,9 @@ class ServicesStub : IServices {
override suspend fun validLogin(username : String, override suspend fun validLogin(username : String,
passwd : String, passwd : String,
navController: (Int) -> Unit, navController: (Int) -> Unit,
initialierCurrentUser : (Int) ->Unit): Boolean{ initialierCurrentUser : (Int) ->Unit,
currentUserViewModel: CurrentUserViewModel
): Boolean{
users.forEachIndexed { index, user -> users.forEachIndexed { index, user ->
val hashPassWd = hashPassword(passwd) val hashPassWd = hashPassword(passwd)

@ -1,5 +1,6 @@
package com.example.what_the_fantasy.ui.navigations package com.example.what_the_fantasy.ui.navigations
import android.util.Log
import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
@ -112,6 +113,7 @@ fun AppNavigator() {
}, },
authUserVM = authUserVM, authUserVM = authUserVM,
authState = authState, authState = authState,
currentUserViewModel = currentUserVM,
initialierCurrentUser ={ initialierCurrentUser ={
coroutineScope.launch { coroutineScope.launch {
currentUserVM.initialiseCurrentUser(it) currentUserVM.initialiseCurrentUser(it)

@ -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.components.VisibleIconPasswordComponent
import com.example.what_the_fantasy.ui.states.AuthUserState 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.AuthUserViewModel
import com.example.what_the_fantasy.ui.viewModels.CurrentUserViewModel
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@Composable @Composable
@ -52,6 +53,7 @@ fun LoginPage(navControllerSignUp: () -> Unit,
navControllerProfil: (Int) -> Unit, navControllerProfil: (Int) -> Unit,
authUserVM : AuthUserViewModel, authUserVM : AuthUserViewModel,
authState : AuthUserState, authState : AuthUserState,
currentUserViewModel : CurrentUserViewModel,
initialierCurrentUser : (Int) -> Unit) { initialierCurrentUser : (Int) -> Unit) {
@ -79,7 +81,7 @@ fun LoginPage(navControllerSignUp: () -> Unit,
authUserVM.setUsername(it) authUserVM.setUsername(it)
}, PassWdTextField(R.string.PasswdLogin, authState.password){ }, PassWdTextField(R.string.PasswdLogin, authState.password){
authUserVM.setPassword(it) authUserVM.setPassword(it)
}, R.string.ButtonLogin,18,navControllerProfil){ }, R.string.ButtonLogin,18,currentUserViewModel,navControllerProfil){
initialierCurrentUser(it) initialierCurrentUser(it)
} }
SpaceHeightComponent(16) SpaceHeightComponent(16)
@ -138,14 +140,21 @@ fun PassWdTextField(textpasswdResId : Int, password : String, onValueChange: (St
@Composable @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) val title = stringResource(id = titleResId)
var showError by remember { mutableStateOf(false) } var showError by remember { mutableStateOf(false) }
val coroutineScope = rememberCoroutineScope() val coroutineScope = rememberCoroutineScope()
Button( Button(
onClick = { onClick = {
coroutineScope.launch { coroutineScope.launch {
val result = authUserVM.validLogin(username, passwd, navController, initialierCurrentUser) val result = authUserVM.validLogin(username, passwd, navController, initialierCurrentUser, currentUserViewModel)
showError = !result showError = !result
} }
}, },

@ -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.components.VisibleIconPasswordComponent
import com.example.what_the_fantasy.ui.states.CurrentUserState import com.example.what_the_fantasy.ui.states.CurrentUserState
import com.example.what_the_fantasy.ui.viewModels.CurrentUserViewModel import com.example.what_the_fantasy.ui.viewModels.CurrentUserViewModel
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@Composable @Composable

@ -26,7 +26,7 @@ class AuthUserViewModel : ViewModel(){
_userState.update { it.copy(password=password) } _userState.update { it.copy(password=password) }
} }
suspend fun validLogin(username : String, passwd : String, navController: (Int) -> Unit, initialierCurrentUser : (Int) -> Unit) : Boolean{ suspend fun validLogin(username : String, passwd : String, navController: (Int) -> Unit, initialierCurrentUser : (Int) -> Unit, currentUserViewModel: CurrentUserViewModel) : Boolean{
return services.validLogin(username,passwd, navController, initialierCurrentUser) return services.validLogin(username,passwd, navController, initialierCurrentUser,currentUserViewModel)
} }
} }

@ -3,6 +3,7 @@ package com.example.what_the_fantasy.ui.viewModels
import android.util.Log import android.util.Log
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import com.example.what_the_fantasy.data.model.SrcLanguage 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.ServicesAPI
import com.example.what_the_fantasy.data.services.ServicesStub import com.example.what_the_fantasy.data.services.ServicesStub
import com.example.what_the_fantasy.ui.states.CurrentUserState import com.example.what_the_fantasy.ui.states.CurrentUserState
@ -12,23 +13,33 @@ import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update import kotlinx.coroutines.flow.update
class CurrentUserViewModel : ViewModel(){ class CurrentUserViewModel : ViewModel(){
private val services = ServicesStub() //private val services = ServicesStub()
//private val services = ServicesAPI() // faire repository qui gère les services Stub et API private val services = ServicesAPI() // faire repository qui gère les services Stub et API
private val _currentUserState = MutableStateFlow(CurrentUserState()) private val _currentUserState = MutableStateFlow(CurrentUserState())
var currentUserState : StateFlow<CurrentUserState> = _currentUserState.asStateFlow() var currentUserState : StateFlow<CurrentUserState> = _currentUserState.asStateFlow()
suspend fun initialiseCurrentUser(index : Int){ suspend fun initialiseCurrentUser(index : Int){
services.getUserById(index)?.let { services.getUserById(index).let {
setId(it.id) setId(it.id)
setUsername(it.username) setUsername(it.username)
setEmail(it.email) setEmail(it.email)
setPassword(it.password) setPassword(it.password)
setLangue(it.langage) //setLangue(it.langage) // A rajouter quand on aura le langage
setImage(it.imgUrl) 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(){ fun clearCurrentUser(){
_currentUserState.value = CurrentUserState() _currentUserState.value = CurrentUserState()
} }

Loading…
Cancel
Save