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