ViewModel Inscription

pull/51/head
Leni BEAULATON 4 weeks ago
parent 4489ca930e
commit 311d876e1c

@ -13,7 +13,7 @@ interface IServices {
fun EditImage(imageURL : String, index : Int)
fun ChangeLangage(user: User)
fun CreateUser(username : String, email : String, passwd : String, services : IServices) : Boolean
fun CreateUser(username : String, email : String, passwd : String) : Boolean
fun getFavorite(user: User): List<Quote>
fun getAllUsers(): List<User>

@ -78,9 +78,10 @@ class ServicesStub : IServices {
logsUser.logDebugUserLangage(user, "ChangeLangue")
}
override fun CreateUser(username: String, email: String, passwd: String, services : IServices) : Boolean {
override fun CreateUser(username: String, email: String, passwd: String) : Boolean {
val date =dateDuJour()
val passwordhash = hashPassword(passwd)
val services = ServicesStub()
val userStub = services.getAllUsers()
val nbUser = userStub.size

@ -151,7 +151,6 @@ fun AppNavigator() {
popUpTo(Login) { inclusive = true }
}
},
services = services
)
}
composable<SubmitQuote> {

@ -1,5 +1,6 @@
package com.example.what_the_fantasy.ui.screens
import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
@ -20,6 +21,7 @@ import androidx.compose.material3.IconButton
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
@ -34,21 +36,25 @@ 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 com.example.what_the_fantasy.R
import com.example.what_the_fantasy.data.services.IServices
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.theme.colorBackground
import com.example.what_the_fantasy.ui.theme.gradienBox
import com.example.what_the_fantasy.ui.components.ErrorMessageProfileComponent
import com.example.what_the_fantasy.ui.states.SignInUserState
import com.example.what_the_fantasy.ui.viewModels.SignInUserViewModel
@Composable
fun SignUpPage(navControllerLogin: () -> Unit, services : IServices) {
fun SignUpPage(navControllerLogin: () -> Unit) {
var username by remember { mutableStateOf("") }
var email by remember { mutableStateOf("") }
var password by remember { mutableStateOf("") }
var confirmPassword by remember { mutableStateOf("") }
var passwordVisible by remember { mutableStateOf(false) }
val signInUserVM : SignInUserViewModel = viewModel()
val signInState by signInUserVM.userState.collectAsState()
Box(
modifier = Modifier
@ -67,14 +73,28 @@ fun SignUpPage(navControllerLogin: () -> Unit, services : IServices) {
horizontalAlignment = Alignment.CenterHorizontally
) {
TitlePageComponent(R.string.titleSignUp,Color.White)
IdentifiantTextFieldSign(R.string.IdentifiantLogin,identifiant = username,onValueChange = { username = it })
EmailTextFieldSign(R.string.EmailSignUp, email, onValueChange = { email = it })
PassWdTextFieldSign(R.string.PasswdLogin,password, onValueChange = { password = it },passwordVisible,onPasswordVisibilityChange = { passwordVisible = !passwordVisible })
PassWdConfirmTextFieldSign(R.string.ConfirmPassWdSignUp,confirmPassword,onValueChange = { confirmPassword = it },passwordVisible,onPasswordVisibilityChange = { passwordVisible = !passwordVisible })
TitlePageComponent(R.string.titleSignUp,Color.White) // Page Title
IdentifiantTextFieldSign(R.string.IdentifiantLogin,signInState.username){ // Username
signInUserVM.setUsername(it)
}
EmailTextFieldSign(R.string.EmailSignUp, signInState.email){ // Email
signInUserVM.setEmail(it)
}
PassWdTextFieldSign(R.string.PasswdLogin,signInState.password){ // Password
signInUserVM.setPassword(it)
}
PassWdConfirmTextFieldSign(R.string.ConfirmPassWdSignUp,signInState.confirmPassword){ // confirm Password
signInUserVM.setConfirmPassword(it)
}
SpaceHeightComponent(16)
ConnexionButtonSign(R.string.ButtonSignUp,18, Color.White, Color.Black, username, email, password, confirmPassword, services, navControllerLogin)
ConnexionButtonSign(R.string.ButtonSignUp,18, Color.White, Color.Black, signInState.username, signInState.email, signInState.password, signInState.confirmPassword, signInUserVM, navControllerLogin)
SpaceHeightComponent(16)
ReturnLogin(R.string.ButtonLogin,12, Color.White, navController = navControllerLogin)
}
@ -82,8 +102,6 @@ fun SignUpPage(navControllerLogin: () -> Unit, services : IServices) {
}
@Composable
fun IdentifiantTextFieldSign(textIdentifiantResId : Int, identifiant: String, onValueChange: (String) -> Unit){
val textIdentifiant = stringResource(id = textIdentifiantResId)
@ -120,8 +138,10 @@ fun EmailTextFieldSign(textIdentifiantResId: Int, email: String, onValueChange:
}
@Composable
fun PassWdTextFieldSign(textpasswdResId : Int, passwd: String, onValueChange: (String) -> Unit, passwordVisible: Boolean, onPasswordVisibilityChange: () -> Unit){
fun PassWdTextFieldSign(textpasswdResId : Int, passwd: String, onValueChange: (String) -> Unit){
val textpasswd = stringResource(id = textpasswdResId)
var passwordVisible by remember { mutableStateOf(false) }
Column(modifier = Modifier.padding(top = 10.dp)) {
OutlinedTextField(
value = passwd,
@ -133,7 +153,9 @@ fun PassWdTextFieldSign(textpasswdResId : Int, passwd: String, onValueChange: (S
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password),
visualTransformation = if (passwordVisible) VisualTransformation.None else PasswordVisualTransformation(),
trailingIcon = {
IconButton(onClick = onPasswordVisibilityChange) {
IconButton(onClick = {
passwordVisible = !passwordVisible
}) {
Icon(imageVector = Icons.Default.Check, contentDescription = "Valider")
}
},
@ -143,8 +165,10 @@ fun PassWdTextFieldSign(textpasswdResId : Int, passwd: String, onValueChange: (S
}
@Composable
fun PassWdConfirmTextFieldSign(textpasswdResId : Int,confirmPassword: String, onValueChange: (String) -> Unit, passwordVisible: Boolean, onPasswordVisibilityChange: () -> Unit){
fun PassWdConfirmTextFieldSign(textpasswdResId : Int,confirmPassword: String, onValueChange: (String) -> Unit){
val textpasswd = stringResource(id = textpasswdResId)
var passwordVisible by remember { mutableStateOf(false) }
Column(modifier = Modifier.padding(top = 10.dp)) {
OutlinedTextField(
value = confirmPassword,
@ -156,7 +180,7 @@ fun PassWdConfirmTextFieldSign(textpasswdResId : Int,confirmPassword: String, on
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password),
visualTransformation = if (passwordVisible) VisualTransformation.None else PasswordVisualTransformation(),
trailingIcon = {
IconButton(onClick = onPasswordVisibilityChange) {
IconButton(onClick = {passwordVisible = !passwordVisible}) {
Icon(imageVector = Icons.Default.Check, contentDescription = "Valider")
}
},
@ -187,7 +211,7 @@ fun ConnexionButtonSign(
email: String,
password: String,
confirmPassword: String,
service: IServices,
viewModel: SignInUserViewModel,
navController: ()-> Unit
) {
val title = stringResource(id = titleResId)
@ -207,7 +231,7 @@ fun ConnexionButtonSign(
passwordErrorEmpty = password.isBlank() || confirmPassword.isBlank()
if (!emailError && !passwordError && !usernameError && !passwordErrorEmpty) {
usernameErrorExist = !service.CreateUser(username, email, password, service)
usernameErrorExist = !viewModel.createUser(username, email, password)
if(!usernameErrorExist){
navController() // retour à la page login
}

@ -6,8 +6,4 @@ import com.example.what_the_fantasy.data.model.User
data class AuthUserState (
val username : String = "",
val password: String = "",
//val email:String = "",
//val date:String = "",
//val imgUrl: String = "",
//val langage : SrcLanguage = SrcLanguage.vo
)

@ -0,0 +1,8 @@
package com.example.what_the_fantasy.ui.states
data class SignInUserState (
val username : String ="",
val email : String ="",
val password : String ="",
val confirmPassword : String ="",
)

@ -0,0 +1,35 @@
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.SignInUserState
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
class SignInUserViewModel: ViewModel() {
private val services = ServicesStub() // faire repository qui gère les services Stub et API
private val _userState = MutableStateFlow(SignInUserState())
val userState : StateFlow<SignInUserState> = _userState.asStateFlow()
fun createUser(username: String, email: String, passwd: String) : Boolean{
return services.CreateUser(username, email, passwd)
}
fun setUsername(username : String){
_userState.update { it.copy(username=username) }
}
fun setEmail(email : String){
_userState.update { it.copy(email=email) }
}
fun setPassword(password : String){
_userState.update { it.copy(password=password) }
}
fun setConfirmPassword(confirmPassword : String){
_userState.update { it.copy(confirmPassword=confirmPassword) }
}
}
Loading…
Cancel
Save