From d384dc7f9e6ccc919356b21306f90fe0ef64e9f4 Mon Sep 17 00:00:00 2001 From: "maxime.rocher" Date: Fri, 7 Mar 2025 11:28:28 +0100 Subject: [PATCH] Page favoris --- .../data/local/FavoriteStub.kt | 8 + .../ui/navigations/AppNavigator.kt | 241 ++++++++++-------- .../ui/screens/FavoritePage.kt | 41 ++- 3 files changed, 170 insertions(+), 120 deletions(-) diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/local/FavoriteStub.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/local/FavoriteStub.kt index 43f3673..b8c44f0 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/local/FavoriteStub.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/local/FavoriteStub.kt @@ -1,6 +1,7 @@ package com.example.what_the_fantasy.data.local import com.example.what_the_fantasy.data.model.Favorite +import com.example.what_the_fantasy.data.model.Quote object FavoriteStub { val favorites: MutableList = mutableListOf( @@ -16,4 +17,11 @@ object FavoriteStub { Favorite(UserStub.users[9], QuoteStub.quote20), Favorite(UserStub.users[10], QuoteStub.quote11) ) + + fun getFavoritesByUser(userId: Int): List { + return favorites + .filter { it.user.id == userId } + .map { it.quote } + } + } \ No newline at end of file diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/navigations/AppNavigator.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/navigations/AppNavigator.kt index 9b98a52..9fab1fc 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/navigations/AppNavigator.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/navigations/AppNavigator.kt @@ -1,6 +1,12 @@ package com.example.what_the_fantasy.ui.navigations +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.padding +import androidx.compose.material3.Scaffold import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color import androidx.navigation.compose.NavHost import androidx.navigation.compose.composable import androidx.navigation.compose.rememberNavController @@ -16,6 +22,7 @@ import com.example.what_the_fantasy.ui.screens.QuotePage import com.example.what_the_fantasy.ui.screens.SearchPage import com.example.what_the_fantasy.ui.screens.SignUpPage import com.example.what_the_fantasy.ui.screens.SubmitQuotePage +import com.example.what_the_fantasy.ui.theme.colorNavBar sealed class Destination(val route: String) { @@ -40,7 +47,6 @@ sealed class Destination(val route: String) { } data object Quiz : Destination("Quiz") data object QuizEnd : Destination("QuizEnd/{idQuiz}/{pts}") { - // Ajout paramètres idQuiz et pts fun createIdAndPts(idQuiz : Int, pts : Int) = "QuizEnd/$idQuiz/$pts" } } @@ -49,122 +55,131 @@ sealed class Destination(val route: String) { fun AppNavigator() { val navController = rememberNavController() val services = ServicesStub() - NavHost(navController, startDestination = Destination.Login.route) { - composable(Destination.Login.route) { - LoginPage( - navControllerSignUp = { - navController.navigate(Destination.SignUp.route) - }, - navControllerProfil = { userIndex -> - navController.navigate(Destination.Profil.createRoute(userIndex)) { - // Vider pile de navigation pour empêcher le retour à la page Login - popUpTo(Destination.Login.route) { inclusive = true } - } - }, - services - ) - } - composable(Destination.Accueil.route) { backStackEntry -> - val userIndex = backStackEntry.arguments?.getString("userIndex")?.toInt() ?: -1 - AccueilPage( - index = userIndex, - navFavorite = { userIndex -> - navController.navigate(Destination.Favorite.createRoute(userIndex)) { - popUpTo(Destination.Accueil.createRoute(userIndex)) { inclusive = false } - } - }, - navQuiz = { userIndex -> - navController.navigate(Destination.QuizMenu.createRoute(userIndex)) { - popUpTo(Destination.Accueil.createRoute(userIndex)) { inclusive = false } - } - }, - navProfil = { userIndex -> - navController.navigate(Destination.Profil.createRoute(userIndex)) { - popUpTo(Destination.Accueil.createRoute(userIndex)) { inclusive = false } - } + + Scaffold( + modifier = Modifier.fillMaxSize(), + containerColor = colorNavBar + ) { paddingValues -> + Box(modifier = Modifier.padding(paddingValues)) { + NavHost(navController, startDestination = Destination.Login.route) { + composable(Destination.Login.route) { + LoginPage( + navControllerSignUp = { + navController.navigate(Destination.SignUp.route) + }, + navControllerProfil = { userIndex -> + navController.navigate(Destination.Profil.createRoute(userIndex)) { + popUpTo(Destination.Login.route) { inclusive = true } + } + }, + services + ) } - ) - } - composable(Destination.Favorite.route) { - val userIndex = it.arguments?.getString("userIndex")?.toInt() ?: -1 - FavoritePage( - index = userIndex, - navAccueil ={ userIndex -> - navController.navigate(Destination.Accueil.createRoute(userIndex)) // Passe l'index à Profil - }, - navQuiz = { userIndex -> - navController.navigate(Destination.QuizMenu.createRoute(userIndex)) // Passe l'index à Profil - }, - navProfil = { userIndex -> - navController.navigate(Destination.Profil.createRoute(userIndex)) // Passe l'index à Profil + composable(Destination.Accueil.route) { backStackEntry -> + val userIndex = backStackEntry.arguments?.getString("userIndex")?.toInt() ?: -1 + AccueilPage( + index = userIndex, + navFavorite = { userIndex -> + navController.navigate(Destination.Favorite.createRoute(userIndex)) { + popUpTo(Destination.Accueil.createRoute(userIndex)) { inclusive = false } + } + }, + navQuiz = { userIndex -> + navController.navigate(Destination.QuizMenu.createRoute(userIndex)) { + popUpTo(Destination.Accueil.createRoute(userIndex)) { inclusive = false } + } + }, + navProfil = { userIndex -> + navController.navigate(Destination.Profil.createRoute(userIndex)) { + popUpTo(Destination.Accueil.createRoute(userIndex)) { inclusive = false } + } + } + ) } - ) } - composable(Destination.Profil.route) { - // Récupère l'index passé dans la route - val userIndex = it.arguments?.getString("userIndex")?.toInt() ?: -1 - ProfilPage( - index = userIndex, - navFavorite = { userIndex -> - navController.navigate(Destination.Favorite.createRoute(userIndex)) // Passe l'index à Profil - }, - navAccueil ={ userIndex -> - navController.navigate(Destination.Accueil.createRoute(userIndex)) // Passe l'index à Profil - }, - navQuiz = { userIndex -> - navController.navigate(Destination.QuizMenu.createRoute(userIndex)) // Passe l'index à Profil - }, - navUnLog = { - navController.navigate(Destination.Login.route) { - // Vider pile de navigation pour empêcher le retour à la page profil - popUpTo(Destination.Profil.route) { inclusive = true } - } - }, - services = services - ) - } - composable(Destination.Quote.route) { QuotePage() } - composable(Destination.Search.route) { SearchPage() } - composable(Destination.SignUp.route) { SignUpPage( - navControllerLogin = { - navController.navigate(Destination.Login.route){ - // Vider pile de navigation pour empêcher le retour à la page Sign up - popUpTo(Destination.Login.route) { inclusive = true } + composable(Destination.Favorite.route) { + val userIndex = it.arguments?.getString("userIndex")?.toInt() ?: -1 + FavoritePage( + index = userIndex, + navAccueil = { userIndex -> + navController.navigate(Destination.Accueil.createRoute(userIndex)) + }, + navQuiz = { userIndex -> + navController.navigate(Destination.QuizMenu.createRoute(userIndex)) + }, + navProfil = { userIndex -> + navController.navigate(Destination.Profil.createRoute(userIndex)) + }, + services + ) } - },services) } - composable(Destination.SubmitQuote.route) { SubmitQuotePage() } - - composable(Destination.QuizMenu.route) { - val userIndex = it.arguments?.getString("userIndex")?.toInt() ?: -1 - QuizMenu( - index = userIndex, - navAccueil = { userIndex -> - navController.navigate(Destination.Accueil.createRoute(userIndex)) // Passe l'index à Profil - }, - navFavorite = { userIndex -> - navController.navigate(Destination.Favorite.createRoute(userIndex)) // Passe l'index à Profil - }, - navControllerQuiz = { navController.navigate(Destination.Quiz.route) }, - navProfil = { userIndex -> - navController.navigate(Destination.Profil.createRoute(userIndex)) // Passe l'index à Profil + composable(Destination.Profil.route) { + val userIndex = it.arguments?.getString("userIndex")?.toInt() ?: -1 + ProfilPage( + index = userIndex, + navFavorite = { userIndex -> + navController.navigate(Destination.Favorite.createRoute(userIndex)) + }, + navAccueil = { userIndex -> + navController.navigate(Destination.Accueil.createRoute(userIndex)) + }, + navQuiz = { userIndex -> + navController.navigate(Destination.QuizMenu.createRoute(userIndex)) + }, + navUnLog = { + navController.navigate(Destination.Login.route) { + popUpTo(Destination.Profil.route) { inclusive = true } + } + }, + services = services + ) } - ) - } - composable(Destination.Quiz.route) { - QuizPage( - navControllerQuizEnd = { idQuiz, pts -> - navController.navigate(Destination.QuizEnd.createIdAndPts(idQuiz, pts)) - }, - navControllerQuizMenu = { navController.navigate(Destination.QuizMenu.route) } - ) - } - composable(Destination.QuizEnd.route) { backStackEntry -> - val idQuiz = backStackEntry.arguments?.getString("idQuiz")?.toInt() ?: 0 - val pts = backStackEntry.arguments?.getString("pts")?.toInt() ?: 0 - QuizEndPage( - idQuiz, - pts, - navControllerQuizMenu = { navController.navigate(Destination.QuizMenu.route) } - ) + composable(Destination.Quote.route) { QuotePage() } + composable(Destination.Search.route) { SearchPage() } + composable(Destination.SignUp.route) { + SignUpPage( + navControllerLogin = { + navController.navigate(Destination.Login.route) { + popUpTo(Destination.Login.route) { inclusive = true } + } + }, + services + ) + } + composable(Destination.SubmitQuote.route) { SubmitQuotePage() } + composable(Destination.QuizMenu.route) { + val userIndex = it.arguments?.getString("userIndex")?.toInt() ?: -1 + QuizMenu( + index = userIndex, + navAccueil = { userIndex -> + navController.navigate(Destination.Accueil.createRoute(userIndex)) + }, + navFavorite = { userIndex -> + navController.navigate(Destination.Favorite.createRoute(userIndex)) + }, + navControllerQuiz = { navController.navigate(Destination.Quiz.route) }, + navProfil = { userIndex -> + navController.navigate(Destination.Profil.createRoute(userIndex)) + } + ) + } + composable(Destination.Quiz.route) { + QuizPage( + navControllerQuizEnd = { idQuiz, pts -> + navController.navigate(Destination.QuizEnd.createIdAndPts(idQuiz, pts)) + }, + navControllerQuizMenu = { navController.navigate(Destination.QuizMenu.route) } + ) + } + composable(Destination.QuizEnd.route) { backStackEntry -> + val idQuiz = backStackEntry.arguments?.getString("idQuiz")?.toInt() ?: 0 + val pts = backStackEntry.arguments?.getString("pts")?.toInt() ?: 0 + QuizEndPage( + idQuiz, + pts, + navControllerQuizMenu = { navController.navigate(Destination.QuizMenu.route) } + ) + } + } } } } diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/FavoritePage.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/FavoritePage.kt index 234884a..0bd3724 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/FavoritePage.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/FavoritePage.kt @@ -1,17 +1,25 @@ package com.example.what_the_fantasy.ui.screens import androidx.compose.foundation.background -import androidx.compose.foundation.layout.Box -import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.* +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.lazy.items +import androidx.compose.material3.Button +import androidx.compose.material3.CircularProgressIndicator import androidx.compose.material3.Text -import androidx.compose.runtime.Composable +import androidx.compose.runtime.* import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color +import androidx.compose.ui.text.style.TextAlign +import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp +import com.example.what_the_fantasy.data.local.DailyQuoteStub +import com.example.what_the_fantasy.data.local.FavoriteStub +import com.example.what_the_fantasy.data.local.QuoteStub import com.example.what_the_fantasy.data.services.IServices import com.example.what_the_fantasy.ui.components.NavBar +import com.example.what_the_fantasy.ui.components.QuoteLittle import com.example.what_the_fantasy.ui.theme.colorBackground @Composable @@ -19,8 +27,12 @@ fun FavoritePage( index: Int, navAccueil: (Int) -> Unit, navQuiz: (Int) -> Unit, - navProfil:(Int) -> Unit + navProfil:(Int) -> Unit, + services: IServices ) { + val user = services.getUserById(index) ?: return + val quotes = FavoriteStub.getFavoritesByUser(user.id) + NavBar(onFavorite = true, index = index, navControllerFavorite = { }, @@ -32,9 +44,24 @@ fun FavoritePage( modifier = Modifier .fillMaxSize() .background(colorBackground), - contentAlignment = Alignment.Center + contentAlignment = Alignment.TopCenter ){ - + LazyColumn { + item{ + Text( + text = "▶ Favoris ◀", + color = Color.White, + fontSize = 24.sp, + modifier = Modifier + .fillMaxWidth() + .padding(16.dp), + textAlign = TextAlign.Center + ) + } + items(quotes) { quote -> + QuoteLittle(quote) + } + } } } } \ No newline at end of file