View Model pour le current en cours

pull/51/head
Leni BEAULATON 3 weeks ago
parent 1d9d3edc69
commit 1830ededb9

@ -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.Favorite
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.User import com.example.what_the_fantasy.data.model.User
interface IServices { interface IServices {
@ -11,7 +12,7 @@ interface IServices {
fun EditEmail(email : String, index : Int) : Boolean fun EditEmail(email : String, index : Int) : Boolean
fun EditPasswd(passwd : String, index : Int) fun EditPasswd(passwd : String, index : Int)
fun EditImage(imageURL : 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 CreateUser(username : String, email : String, passwd : String) : Boolean
fun getFavorite(user: User): List<Quote> fun getFavorite(user: User): List<Quote>

@ -68,14 +68,16 @@ class ServicesStub : IServices {
TODO("Not yet implemented") TODO("Not yet implemented")
} }
override fun ChangeLangage(user: User) { override fun ChangeLangage(index : Int) : SrcLanguage{
if(user.langage == SrcLanguage.vo){ if(getAllUsers()[index].langage == SrcLanguage.vo){
user.langage = SrcLanguage.vf getAllUsers()[index].langage = SrcLanguage.vf
} }
else{ 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 { override fun CreateUser(username: String, email: String, passwd: String) : Boolean {

@ -27,6 +27,7 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember 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.text.input.VisualTransformation
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import coil.compose.AsyncImage import coil.compose.AsyncImage
import com.example.what_the_fantasy.Logs.LogsUsers import com.example.what_the_fantasy.Logs.LogsUsers
import com.example.what_the_fantasy.R 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.model.User
import com.example.what_the_fantasy.data.services.IServices 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.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.SpaceHeightComponent
import com.example.what_the_fantasy.ui.components.TitlePageComponent 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.theme.gradienBox 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 @Composable
fun ProfilPage(index: Int, fun ProfilPage(index: Int,
@ -66,6 +72,8 @@ fun ProfilPage(index: Int,
services: IServices services: IServices
) { ) {
val user = services.getUserById(index) ?: return val user = services.getUserById(index) ?: return
val currentUserVM : CurrentUserViewModel = viewModel()
val currentUserState by currentUserVM.currentUserState.collectAsState()
NavBar(onProfile = true, NavBar(onProfile = true,
index = index, index = index,
@ -99,21 +107,21 @@ fun ProfilPage(index: Int,
ImageProfil(user.imgUrl, 120) ImageProfil(user.imgUrl, 120)
SpaceHeightComponent(16) SpaceHeightComponent(16)
EditUsername(user.username, index, services)// Édition du Username EditUsername(currentUserState.username, index, currentUserVM)// Édition du Username
SpaceHeightComponent(16) SpaceHeightComponent(16)
EditEmail(user.email,index, services)// Édition du Email EditEmail(currentUserState.email,index, currentUserVM)// Édition du Email
Spacer(modifier = Modifier.height(8.dp)) Spacer(modifier = Modifier.height(8.dp))
EditPasswd(index, services) EditPasswd(index, currentUserVM)
SpaceHeightComponent(16) SpaceHeightComponent(16)
// Bouton // Bouton
//ButtonProfile(R.string.ButtonAddQuoteprofile, 18, Color.Black, Color.White,navUnLog) // Pas encore de navigation definie ButtonProfile(R.string.ButtonAddQuoteprofile, 18, MaterialTheme.colorScheme.background,navSubmitQuote) // Pas encore de navigation definie
//SpaceHeightComponent(16)
ButtonLanguage(R.string.ButtonLanguageprofile, 18, MaterialTheme.colorScheme.onPrimary, MaterialTheme.colorScheme.background,services, user)
SpaceHeightComponent(16) 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 @Composable
fun EditEmail(userEmail: String, index: Int, service: IServices) { fun EditEmail(emailState: String, index: Int, currentUserVM: CurrentUserViewModel) {
var email by remember { mutableStateOf(userEmail) } var email by remember { mutableStateOf(emailState) }
var isEditingEmail by remember { mutableStateOf(false) } var isEditingEmail by remember { mutableStateOf(false) }
var emailError by remember { mutableStateOf(false) } var emailError by remember { mutableStateOf(false) }
fun onDoneEditing() { fun onDoneEditing() {
isEditingEmail =!service.EditEmail(email, index) isEditingEmail =!currentUserVM.editEmail(email, index)
} }
@ -223,12 +231,12 @@ fun DisplayEmail(email: String, onEdit: () -> Unit) {
@Composable @Composable
fun EditUsername(userName: String, index: Int, service : IServices) { fun EditUsername(usernameState: String, index: Int, currentUserVM : CurrentUserViewModel) {
var username by remember { mutableStateOf(userName) } var username by remember { mutableStateOf(usernameState) }
var isEditingUsername by remember { mutableStateOf(false) } var isEditingUsername by remember { mutableStateOf(false) }
fun onDoneEditing() { fun onDoneEditing() {
isEditingUsername= !service.EditUsername(username, index) isEditingUsername= !currentUserVM.editUsername(username, index)
} }
if (isEditingUsername) { if (isEditingUsername) {
@ -292,7 +300,7 @@ fun DisplayUsername(username: String, onEdit: () -> Unit) {
@Composable @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 password by remember { mutableStateOf("*******") } // Mot de passe actuel (affiché comme un masque)
var isEditingPassword by remember { mutableStateOf(false) } var isEditingPassword by remember { mutableStateOf(false) }
var newPassword by remember { mutableStateOf("") } 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 // Fonction pour finaliser l'édition du mot de passe et appeler la méthode EditPasswd2
fun onDoneEditing() { fun onDoneEditing() {
// Appeler EditPasswd pour mettre à jour le mot de passe de l'utilisateur // Appeler EditPasswd pour mettre à jour le mot de passe de l'utilisateur
service.EditPasswd(newPassword, index) currentUserVM.editPassword(newPassword, index)
isEditingPassword = false isEditingPassword = false
} }
@ -322,12 +330,12 @@ fun EditPasswd(index: Int, service: IServices) {
passwordError = passwordError, passwordError = passwordError,
onDone = { onDone = {
if (!passwordError && newPassword.isNotEmpty()) { 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 { } 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 @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 text = stringResource(id = textResId)
val logsUser = LogsUsers() //gestion des logs pour les utilisateurs val logsUser = LogsUsers() //gestion des logs pour les utilisateurs
@ -463,23 +471,22 @@ fun ButtonUnLog(textResId : Int, size :Int, colorTexte : Color, colorButton : Co
} }
@Composable @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 text = stringResource(id = textResId)
val currentLangage = remember { mutableStateOf(user.langage) }
Button( Button(
onClick = { onClick = {
service.ChangeLangage(user) currentUserVM.editLangue(10) // a mettre a la place : currentUserState.id
currentLangage.value = user.langage
}, },
colors = ButtonDefaults.buttonColors(containerColor = colorButton), colors = ButtonDefaults.buttonColors(containerColor = colorButton),
modifier = Modifier.fillMaxWidth(), 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 @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) val text = stringResource(id = textResId)
Button( Button(
@ -489,6 +496,6 @@ fun ButtonProfil(textResId : Int, size :Int, colorTexte : Color, colorButton : C
colors = ButtonDefaults.buttonColors(containerColor = colorButton), colors = ButtonDefaults.buttonColors(containerColor = colorButton),
modifier = Modifier.fillMaxWidth(), modifier = Modifier.fillMaxWidth(),
) { ) {
Text(text, fontSize = size.sp, color = colorTexte) Text(text, fontSize = size.sp, color = MaterialTheme.colorScheme.primary)
} }
} }

@ -2,11 +2,12 @@ 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.SrcLanguage
data class InfoUserState ( data class CurrentUserState (
var id : Int = -1,
val imagePath : String ="", val imagePath : String ="",
val username :String="", var username :String="",
val email : String="", var email : String="",
val password : String="", var password : String="",
val confirmPassword : String="", var confirmPassword : String="",
val langue : SrcLanguage = SrcLanguage.vo val langue : SrcLanguage = SrcLanguage.vo
) )

@ -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> = _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)
}
}
}

@ -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<InfoUserState> = _userInfoState.asStateFlow()
fun setUsername(username : String){
_userInfoState.update { it.copy(username=username) }
}
}
Loading…
Cancel
Save