Les pages se partagent les services

pull/22/head
Leni BEAULATON 2 months ago
parent d3cc0bc1d3
commit ad29a49ccb

@ -3,95 +3,38 @@ package com.example.what_the_fantasy.data.local
import com.example.what_the_fantasy.data.model.User import com.example.what_the_fantasy.data.model.User
object UserStub { object UserStub {
val user1 = User( //LE MOT DE PASSE POUR TOUS LES UTILISATEURS EST : 1234
id = 1, val users: MutableList<User> = mutableListOf(
username = "Aragorn123", User(1, "Aragorn123", "aragorn@example.com", "2022-01-15", "https://img.freepik.com/vecteurs-libre/personnage-guerrier-fantaisie_1045-185.jpg?size=338&ext=jpg", "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4"), //1234
email = "aragorn@example.com", User(2, "Legolas456", "legolas@example.com", "2021-05-23", "https://img.freepik.com/vecteurs-libre/personnage-elfe-fantaisie_1045-186.jpg?size=338&ext=jpg", "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4"),//1234
date = "2022-01-15", User(3, "Gandalf789", "gandalf@example.com", "2020-09-10", "https://img.freepik.com/vecteurs-libre/personnage-magicien-fantaisie_1045-187.jpg?size=338&ext=jpg", "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4"),//1234
imgUrl = "https://img.freepik.com/vecteurs-libre/personnage-guerrier-fantaisie_1045-185.jpg?size=338&ext=jpg", User(4, "FrodoBaggins", "frodo@example.com", "2023-03-18", "https://img.freepik.com/vecteurs-libre/personnage-hobbit-fantaisie_1045-188.jpg?size=338&ext=jpg", "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4"),//1234
password = "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4") // 1234 User(5, "Gimli999", "gimli@example.com", "2022-07-04", "https://img.freepik.com/vecteurs-libre/personnage-nain-fantaisie_1045-189.jpg?size=338&ext=jpg", "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4"),//1234
User(6, "Galadriel321", "galadriel@example.com", "2021-11-30", "https://img.freepik.com/vecteurs-libre/personnage-elfe-femme-fantaisie_1045-190.jpg?size=338&ext=jpg", "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4"),//1234
val user2 = User( User(7, "Boromir654", "boromir@example.com", "2023-06-22", "https://img.freepik.com/vecteurs-libre/personnage-guerrier-homme-fantaisie_1045-191.jpg?size=338&ext=jpg", "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4"),//1234
id = 2, User(8, "Eowyn777", "eowyn@example.com", "2022-04-11", "https://img.freepik.com/vecteurs-libre/personnage-guerriere-femme-fantaisie_1045-192.jpg?size=338&ext=jpg", "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4"),//1234
username = "Legolas456", User(9, "Saruman888", "saruman@example.com", "2021-08-15", "https://img.freepik.com/vecteurs-libre/personnage-magicien-malefique-fantaisie_1045-193.jpg?size=338&ext=jpg", "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4"),//1234
email = "legolas@example.com", User(10, "Faramir222", "faramir@example.com", "2023-02-08", "https://img.freepik.com/vecteurs-libre/personnage-guerrier-homme-fantaisie_1045-194.jpg?size=338&ext=jpg", "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4"),//1234
date = "2021-05-23", User(11, "dev", "testeur@example.com", "2023-02-08", "https://img.freepik.com/vecteurs-libre/personnage-guerrier-homme-fantaisie_1045-194.jpg?size=338&ext=jpg", "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4")//1234
imgUrl = "https://img.freepik.com/vecteurs-libre/personnage-elfe-fantaisie_1045-186.jpg?size=338&ext=jpg", )
password = "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4") // 1234
val user3 = User(
id = 3,
username = "Gandalf789",
email = "gandalf@example.com",
date = "2020-09-10",
imgUrl = "https://img.freepik.com/vecteurs-libre/personnage-magicien-fantaisie_1045-187.jpg?size=338&ext=jpg",
password = "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4") // 1234
val user4 = User(
id = 4,
username = "FrodoBaggins",
email = "frodo@example.com",
date = "2023-03-18",
imgUrl = "https://img.freepik.com/vecteurs-libre/personnage-hobbit-fantaisie_1045-188.jpg?size=338&ext=jpg",
password = "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4") // 1234
val user5 = User(
id = 5,
username = "Gimli999",
email = "gimli@example.com",
date = "2022-07-04",
imgUrl = "https://img.freepik.com/vecteurs-libre/personnage-nain-fantaisie_1045-189.jpg?size=338&ext=jpg",
password = "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4") // 1234
val user6 = User(
id = 6,
username = "Galadriel321",
email = "galadriel@example.com",
date = "2021-11-30",
imgUrl = "https://img.freepik.com/vecteurs-libre/personnage-elfe-femme-fantaisie_1045-190.jpg?size=338&ext=jpg",
password = "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4") // 1234
val user7 = User(
id = 7,
username = "Boromir654",
email = "boromir@example.com",
date = "2023-06-22",
imgUrl = "https://img.freepik.com/vecteurs-libre/personnage-guerrier-homme-fantaisie_1045-191.jpg?size=338&ext=jpg",
password = "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4") // 1234
val user8 = User(
id = 8,
username = "Eowyn777",
email = "eowyn@example.com",
date = "2022-04-11",
imgUrl = "https://img.freepik.com/vecteurs-libre/personnage-guerriere-femme-fantaisie_1045-192.jpg?size=338&ext=jpg",
password = "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4") // 1234
val user9 = User(
id = 9,
username = "Saruman888",
email = "saruman@example.com",
date = "2021-08-15",
imgUrl = "https://img.freepik.com/vecteurs-libre/personnage-magicien-malefique-fantaisie_1045-193.jpg?size=338&ext=jpg",
password = "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4") // 1234
val user10 = User( fun addUser(user: User) {
id = 10, users.add(user)
username = "Faramir222", }
email = "faramir@example.com",
date = "2023-02-08",
imgUrl = "https://img.freepik.com/vecteurs-libre/personnage-guerrier-homme-fantaisie_1045-194.jpg?size=338&ext=jpg",
password = "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4") // 1234
val user11 = User( //
id = 10, // // Modifier un utilisateur
username = "dev", // fun updateUser(id: Int, updatedUser: User) {
email = "testeur@example.com", // val index = users.indexOfFirst { it.id == id }
date = "2023-02-08", // if (index != -1) {
imgUrl = "https://img.freepik.com/vecteurs-libre/personnage-guerrier-homme-fantaisie_1045-194.jpg?size=338&ext=jpg", // users[index] = updatedUser
password = "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4") // 1234 // }
// }
//
// // Supprimer un utilisateur
// fun removeUser(id: Int) {
// users.removeAll { it.id == id }
// }
//
val allUsers: List<User> = listOf(
user1, user2, user3, user4, user5, user6, user7, user8, user9, user10, user11
)
} }

@ -1,13 +1,19 @@
package com.example.what_the_fantasy.data.services package com.example.what_the_fantasy.data.services
import com.example.what_the_fantasy.data.local.UserStub.users
import com.example.what_the_fantasy.data.model.User
interface IServices { interface IServices {
fun EditUsername(username : String) fun EditUsername(username : String)
fun EditEmail(email : String) fun EditEmail(email : String)
fun EditPasswd(passwd : String, passwdValid : String) fun EditPasswd(passwd : String, passwdValid : String)
fun EditImage(imageURL : String) fun EditImage(imageURL : String)
fun CreateUser(username : String, email : String, passwd : String, imageURL: String) : Boolean fun CreateUser(username : String, email : String, passwd : String, imageURL: String, services : IServices) : Boolean
fun getFavorite(username: String) fun getFavorite(username: String)
fun getAllUsers(): List<User>
fun getUserById(id: Int): User?
fun SearchQuote(quote : String) fun SearchQuote(quote : String)

@ -1,5 +1,7 @@
package com.example.what_the_fantasy.data.services package com.example.what_the_fantasy.data.services
import com.example.what_the_fantasy.data.model.User
class ServicesAPI : IServices { class ServicesAPI : IServices {
override fun EditUsername(username: String) { override fun EditUsername(username: String) {
TODO("Not yet implemented") TODO("Not yet implemented")
@ -17,7 +19,7 @@ class ServicesAPI : IServices {
TODO("Not yet implemented") TODO("Not yet implemented")
} }
override fun CreateUser(username: String, email: String, passwd: String, imageURL: String) : Boolean { override fun CreateUser(username: String, email: String, passwd: String, imageURL: String, services: IServices) : Boolean {
TODO("Not yet implemented") TODO("Not yet implemented")
} }
@ -28,4 +30,12 @@ class ServicesAPI : IServices {
override fun getFavorite(username: String) { override fun getFavorite(username: String) {
TODO("Not yet implemented") TODO("Not yet implemented")
} }
override fun getAllUsers(): List<User> {
TODO("Not yet implemented")
}
override fun getUserById(id: Int): User? {
TODO("Not yet implemented")
}
} }

@ -3,6 +3,7 @@ package com.example.what_the_fantasy.data.services
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.util.Log import android.util.Log
import com.example.what_the_fantasy.data.local.UserStub import com.example.what_the_fantasy.data.local.UserStub
import com.example.what_the_fantasy.data.local.UserStub.users
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.components.hashPassword import com.example.what_the_fantasy.ui.components.hashPassword
import java.time.LocalDate import java.time.LocalDate
@ -24,22 +25,26 @@ class ServicesStub : IServices {
TODO("Not yet implemented") TODO("Not yet implemented")
} }
override fun CreateUser(username: String, email: String, passwd: String, imageURL: String) : Boolean { override fun CreateUser(username: String, email: String, passwd: String, imageURL: String, services : IServices) : Boolean {
val date =dateDuJour() val date =dateDuJour()
val passwordhash = hashPassword(passwd) val passwordhash = hashPassword(passwd)
//Check si user existe déjà //Check si user existe déjà
val users = UserStub.allUsers val userStub = services.getAllUsers()
for (user in users) { val nbUser = userStub.size
for (user in userStub) {
if (user.username == username) { if (user.username == username) {
return false return false
} }
} }
val user = User(100,username, email, imageURL, date, passwordhash) val user = User(nbUser+1,username, email, imageURL, date, passwordhash)
//A ajouter au stub users.add(user)//ajout au stub
//Afficher tous les users
for(user in userStub){
Log.e("CreateUser", "User created: ${user.username} => ${user.password}") Log.e("CreateUser", "User created: ${user.username} => ${user.password}")
}
return true return true
} }
@SuppressLint("NewApi") @SuppressLint("NewApi")
@ -47,6 +52,15 @@ class ServicesStub : IServices {
val date = LocalDate.now() val date = LocalDate.now()
return date.toString() return date.toString()
} }
// Récupérer tous les utilisateurs
override fun getAllUsers(): List<User> = users
override fun getUserById(id: Int): User? {
return (users.find { it.id == id+1 })
}
override fun SearchQuote(quote: String) { override fun SearchQuote(quote: String) {
TODO("Not yet implemented") TODO("Not yet implemented")
} }

@ -4,6 +4,7 @@ import androidx.compose.runtime.Composable
import androidx.navigation.compose.NavHost import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController import androidx.navigation.compose.rememberNavController
import com.example.what_the_fantasy.data.services.ServicesStub
import com.example.what_the_fantasy.ui.screens.AccueilPage import com.example.what_the_fantasy.ui.screens.AccueilPage
import com.example.what_the_fantasy.ui.screens.FavoritePage import com.example.what_the_fantasy.ui.screens.FavoritePage
import com.example.what_the_fantasy.ui.screens.LoginPage import com.example.what_the_fantasy.ui.screens.LoginPage
@ -33,28 +34,28 @@ sealed class Destination(val route: String) {
@Composable @Composable
fun AppNavigator() { fun AppNavigator() {
val navController = rememberNavController() val navController = rememberNavController()
val services = ServicesStub() // A changer si l'on veut passer au service API
NavHost(navController, startDestination = Destination.Login.route) { NavHost(navController, startDestination = Destination.Login.route) {
composable(Destination.Login.route) { composable(Destination.Login.route) {
LoginPage( LoginPage(
navControllerSignUp = { navController.navigate(Destination.SignUp.route) }, navControllerSignUp = { navController.navigate(Destination.SignUp.route) },
navControllerProfil = { userIndex -> navControllerProfil = { userIndex ->
navController.navigate(Destination.Profil.createRoute(userIndex)) // Passe l'index à Profil navController.navigate(Destination.Profil.createRoute(userIndex)) // Passe l'index à Profil
} },
services
) )
} }
composable(Destination.Accueil.route) { AccueilPage() } composable(Destination.Accueil.route) { AccueilPage() }
composable(Destination.Favorite.route) { FavoritePage() } composable(Destination.Favorite.route) { FavoritePage() }
composable(Destination.Profil.route) { backStackEntry -> composable(Destination.Profil.route) {
// Récupère l'index passé dans la route // Récupère l'index passé dans la route
val userIndex = backStackEntry.arguments?.getString("userIndex")?.toInt() ?: -1 val userIndex = it.arguments?.getString("userIndex")?.toInt() ?: -1
ProfilPage(index = userIndex, navController = navController) ProfilPage(index = userIndex, navController = navController, services)
} }
composable(Destination.Quiz.route) { QuizPage(navController) } composable(Destination.Quiz.route) { QuizPage(navController) }
composable(Destination.Quote.route) { QuotePage() } composable(Destination.Quote.route) { QuotePage() }
composable(Destination.Search.route) { SearchPage() } composable(Destination.Search.route) { SearchPage() }
composable(Destination.SignUp.route) { SignUpPage(navController) } composable(Destination.SignUp.route) { SignUpPage(navController, services) }
composable(Destination.SubmitQuote.route) { SubmitQuotePage() } composable(Destination.SubmitQuote.route) { SubmitQuotePage() }
} }
} }

@ -39,6 +39,8 @@ import androidx.navigation.compose.composable
import com.example.what_the_fantasy.R import com.example.what_the_fantasy.R
import com.example.what_the_fantasy.data.local.UserStub import com.example.what_the_fantasy.data.local.UserStub
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.ServicesStub
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.hashPassword import com.example.what_the_fantasy.ui.components.hashPassword
@ -47,10 +49,8 @@ import com.example.what_the_fantasy.ui.theme.gradienBox
import java.security.MessageDigest import java.security.MessageDigest
@Composable @Composable
fun LoginPage(navControllerSignUp: () -> Unit, navControllerProfil: (Int) -> Unit) { fun LoginPage(navControllerSignUp: () -> Unit, navControllerProfil: (Int) -> Unit, services : IServices) {
val users = services.getAllUsers()
val users = UserStub.allUsers;
Box( Box(
modifier = Modifier modifier = Modifier

@ -61,6 +61,9 @@ import coil.compose.AsyncImage
import com.example.what_the_fantasy.R import com.example.what_the_fantasy.R
import com.example.what_the_fantasy.data.local.QuestionStub import com.example.what_the_fantasy.data.local.QuestionStub
import com.example.what_the_fantasy.data.local.UserStub import com.example.what_the_fantasy.data.local.UserStub
//import com.example.what_the_fantasy.data.local.UserStub.users
import com.example.what_the_fantasy.data.services.IServices
import com.example.what_the_fantasy.data.services.ServicesStub
import com.example.what_the_fantasy.ui.components.ErrorMessageProfileComponent import com.example.what_the_fantasy.ui.components.ErrorMessageProfileComponent
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
@ -68,10 +71,9 @@ import com.example.what_the_fantasy.ui.theme.What_The_FantasyTheme
import com.example.what_the_fantasy.ui.theme.gradienBox import com.example.what_the_fantasy.ui.theme.gradienBox
@Composable @Composable
fun ProfilPage(index: Int, navController: NavController) { fun ProfilPage(index: Int, navController: NavController, services: IServices) {
val userList = UserStub.allUsers val user = services.getUserById(index) ?: return
val user = userList[index]
Box( Box(
modifier = Modifier modifier = Modifier

@ -43,6 +43,7 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import androidx.navigation.NavController import androidx.navigation.NavController
import com.example.what_the_fantasy.R 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.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.theme.What_The_FantasyTheme import com.example.what_the_fantasy.ui.theme.What_The_FantasyTheme
@ -52,13 +53,13 @@ import com.example.what_the_fantasy.data.services.ServicesStub
import com.example.what_the_fantasy.ui.components.ErrorMessageProfileComponent import com.example.what_the_fantasy.ui.components.ErrorMessageProfileComponent
@Composable @Composable
fun SignUpPage(navController: NavController) { fun SignUpPage(navController: NavController, services : IServices) {
var username by remember { mutableStateOf("") } var username by remember { mutableStateOf("") }
var email by remember { mutableStateOf("") } var email by remember { mutableStateOf("") }
var password by remember { mutableStateOf("") } var password by remember { mutableStateOf("") }
var confirmPassword by remember { mutableStateOf("") } var confirmPassword by remember { mutableStateOf("") }
var passwordVisible by remember { mutableStateOf(false) } var passwordVisible by remember { mutableStateOf(false) }
val servicesStub = ServicesStub() //val servicesStub = ServicesStub()
Box( Box(
modifier = Modifier modifier = Modifier
@ -82,7 +83,7 @@ fun SignUpPage(navController: NavController) {
PassWdTextFieldSign(R.string.PasswdLogin,password, onValueChange = { password = it },passwordVisible,onPasswordVisibilityChange = { passwordVisible = !passwordVisible }) PassWdTextFieldSign(R.string.PasswdLogin,password, onValueChange = { password = it },passwordVisible,onPasswordVisibilityChange = { passwordVisible = !passwordVisible })
PassWdConfirmTextFieldSign(R.string.ConfirmPassWdSignUp,confirmPassword,onValueChange = { confirmPassword = it },passwordVisible,onPasswordVisibilityChange = { passwordVisible = !passwordVisible }) PassWdConfirmTextFieldSign(R.string.ConfirmPassWdSignUp,confirmPassword,onValueChange = { confirmPassword = it },passwordVisible,onPasswordVisibilityChange = { passwordVisible = !passwordVisible })
SpaceHeightComponent(16) SpaceHeightComponent(16)
ConnexionButtonSign(R.string.ButtonSignUp,18, Color.White, Color.Black, username, email, password, confirmPassword, servicesStub, navController = navController) ConnexionButtonSign(R.string.ButtonSignUp,18, Color.White, Color.Black, username, email, password, confirmPassword, services, navController = navController)
SpaceHeightComponent(16) SpaceHeightComponent(16)
ReturnLogin(R.string.ButtonLogin,12, Color.White, navController = navController) ReturnLogin(R.string.ButtonLogin,12, Color.White, navController = navController)
@ -196,7 +197,7 @@ fun ConnexionButtonSign(
email: String, email: String,
password: String, password: String,
confirmPassword: String, confirmPassword: String,
service: ServicesStub, service: IServices,
navController: NavController navController: NavController
) { ) {
val title = stringResource(id = titleResId) val title = stringResource(id = titleResId)
@ -215,7 +216,7 @@ fun ConnexionButtonSign(
usernameErrorEmpty = username.isBlank() usernameErrorEmpty = username.isBlank()
if (!emailError && !passwordError && !usernameErrorEmpty) { if (!emailError && !passwordError && !usernameErrorEmpty) {
usernameErrorExist = !service.CreateUser(username, email, password, "https://img.freepik.com/vecteurs-libre/personnage-magicien-malefique-fantaisie_1045-193.jpg?size=338&ext=jpg") usernameErrorExist = !service.CreateUser(username, email, password, "https://img.freepik.com/vecteurs-libre/personnage-magicien-malefique-fantaisie_1045-193.jpg?size=338&ext=jpg", service)
} }
}, },
colors = ButtonDefaults.buttonColors(containerColor = colorButton), colors = ButtonDefaults.buttonColors(containerColor = colorButton),

Loading…
Cancel
Save