From ad5ae952ac6dd10d178868a293b6af9af80dfa1a Mon Sep 17 00:00:00 2001 From: "leni.beaulaton" Date: Thu, 3 Apr 2025 23:27:03 +0200 Subject: [PATCH] Quote ViewModel --- .../data/local/CommentStub.kt | 55 +++++++-------- .../what_the_fantasy/data/model/Quote.kt | 2 +- .../data/services/IServices.kt | 2 +- .../data/services/ServicesStub.kt | 5 +- .../ui/navigations/AppNavigator.kt | 10 ++- .../what_the_fantasy/ui/screens/QuotePage.kt | 68 +++++++++++++------ .../ui/states/QuoteInformationUserState.kt | 7 ++ .../QuoteInformationUserViewModel.kt | 43 ++++++++++++ 8 files changed, 136 insertions(+), 56 deletions(-) create mode 100644 What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/states/QuoteInformationUserState.kt create mode 100644 What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/viewModels/QuoteInformationUserViewModel.kt diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/local/CommentStub.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/local/CommentStub.kt index 0009b0c..47fd3a2 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/local/CommentStub.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/local/CommentStub.kt @@ -1,39 +1,34 @@ package com.example.what_the_fantasy.data.local +import com.example.what_the_fantasy.data.local.UserStub.users import com.example.what_the_fantasy.data.model.Comment import com.example.what_the_fantasy.data.model.User object CommentStub { val comments: MutableList = mutableListOf( - Comment("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","Dev","21-12-2005", "https://img.freepik.com/vecteurs-libre/personnage-guerrier-homme-fantaisie_1045-194.jpg?size=338&ext=jpg"), - Comment("Trop bien","Dev","21-12-2005", "https://img.freepik.com/vecteurs-libre/personnage-guerrier-homme-fantaisie_1045-194.jpg?size=338&ext=jpg"), - Comment("encore un Test","Dev","21-12-2005", "https://img.freepik.com/vecteurs-libre/personnage-guerrier-homme-fantaisie_1045-194.jpg?size=338&ext=jpg"), - Comment("Je suis la ","Dev","21-12-2005", "https://img.freepik.com/vecteurs-libre/personnage-guerrier-homme-fantaisie_1045-194.jpg?size=338&ext=jpg"), - Comment("J'en ai rien a foutre de la citation","Dev","21-12-2005", "https://img.freepik.com/vecteurs-libre/personnage-guerrier-homme-fantaisie_1045-194.jpg?size=338&ext=jpg"), - Comment("Android c'est bien","Dev","21-12-2005", "https://img.freepik.com/vecteurs-libre/personnage-guerrier-homme-fantaisie_1045-194.jpg?size=338&ext=jpg"), - Comment("je sais plus quoi mettre donc ca va être le bordel","Dev","21-12-2005", "https://img.freepik.com/vecteurs-libre/personnage-guerrier-homme-fantaisie_1045-194.jpg?size=338&ext=jpg"), - Comment("Et un test de plus","Dev","21-12-2005", "https://img.freepik.com/vecteurs-libre/personnage-guerrier-homme-fantaisie_1045-194.jpg?size=338&ext=jpg"), - Comment("La bombe à été activer","Dev","21-12-2005", "https://img.freepik.com/vecteurs-libre/personnage-guerrier-homme-fantaisie_1045-194.jpg?size=338&ext=jpg"), - Comment("J'aime pas les *****","Dev","21-12-2005", "https://img.freepik.com/vecteurs-libre/personnage-guerrier-homme-fantaisie_1045-194.jpg?size=338&ext=jpg"), - Comment("Test","Dev","21-12-2005", "https://img.freepik.com/vecteurs-libre/personnage-guerrier-homme-fantaisie_1045-194.jpg?size=338&ext=jpg"), - Comment("Trop bien","Dev","21-12-2005", "https://img.freepik.com/vecteurs-libre/personnage-guerrier-homme-fantaisie_1045-194.jpg?size=338&ext=jpg"), - Comment("encore un Test","Dev","21-12-2005", "https://img.freepik.com/vecteurs-libre/personnage-guerrier-homme-fantaisie_1045-194.jpg?size=338&ext=jpg"), - Comment("Je suis la ","Dev","21-12-2005", "https://img.freepik.com/vecteurs-libre/personnage-guerrier-homme-fantaisie_1045-194.jpg?size=338&ext=jpg"), - Comment("J'en ai rien a foutre de la citation","Dev","21-12-2005", "https://img.freepik.com/vecteurs-libre/personnage-guerrier-homme-fantaisie_1045-194.jpg?size=338&ext=jpg"), - Comment("Android c'est bien","Dev","21-12-2005", "https://img.freepik.com/vecteurs-libre/personnage-guerrier-homme-fantaisie_1045-194.jpg?size=338&ext=jpg"), - Comment("je sais plus quoi mettre donc ca va être le bordel","Dev","21-12-2005", "https://img.freepik.com/vecteurs-libre/personnage-guerrier-homme-fantaisie_1045-194.jpg?size=338&ext=jpg"), - Comment("Et un test de plus","Dev","21-12-2005", "https://img.freepik.com/vecteurs-libre/personnage-guerrier-homme-fantaisie_1045-194.jpg?size=338&ext=jpg"), - Comment("La bombe à été activer","Dev","21-12-2005", "https://img.freepik.com/vecteurs-libre/personnage-guerrier-homme-fantaisie_1045-194.jpg?size=338&ext=jpg"), - Comment("J'aime pas les *****","Dev","21-12-2005", "https://img.freepik.com/vecteurs-libre/personnage-guerrier-homme-fantaisie_1045-194.jpg?size=338&ext=jpg"), - Comment("Test","Dev","21-12-2005", "https://img.freepik.com/vecteurs-libre/personnage-guerrier-homme-fantaisie_1045-194.jpg?size=338&ext=jpg"), - Comment("Trop bien","Dev","21-12-2005", "https://img.freepik.com/vecteurs-libre/personnage-guerrier-homme-fantaisie_1045-194.jpg?size=338&ext=jpg"), - Comment("encore un Test","Dev","21-12-2005", "https://img.freepik.com/vecteurs-libre/personnage-guerrier-homme-fantaisie_1045-194.jpg?size=338&ext=jpg"), - Comment("Je suis la ","Dev","21-12-2005", "https://img.freepik.com/vecteurs-libre/personnage-guerrier-homme-fantaisie_1045-194.jpg?size=338&ext=jpg"), - Comment("J'en ai rien a foutre de la citation","Dev","21-12-2005", "https://img.freepik.com/vecteurs-libre/personnage-guerrier-homme-fantaisie_1045-194.jpg?size=338&ext=jpg"), - Comment("Android c'est bien","Dev","21-12-2005", "https://img.freepik.com/vecteurs-libre/personnage-guerrier-homme-fantaisie_1045-194.jpg?size=338&ext=jpg"), - Comment("je sais plus quoi mettre donc ca va être le bordel","Dev","21-12-2005", "https://img.freepik.com/vecteurs-libre/personnage-guerrier-homme-fantaisie_1045-194.jpg?size=338&ext=jpg"), - Comment("Et un test de plus","Dev","21-12-2005", "https://img.freepik.com/vecteurs-libre/personnage-guerrier-homme-fantaisie_1045-194.jpg?size=338&ext=jpg"), - Comment("La bombe à été activer","Dev","21-12-2005", "https://img.freepik.com/vecteurs-libre/personnage-guerrier-homme-fantaisie_1045-194.jpg?size=338&ext=jpg"), - Comment("J'aime pas les *****","Dev","21-12-2005", "https://img.freepik.com/vecteurs-libre/personnage-guerrier-homme-fantaisie_1045-194.jpg?size=338&ext=jpg"), + Comment("Franchement, si la Force était avec moi, je n'aurais jamais perdu mes clés ce matin !", users[0].username, "21-12-2005", users[0].imgUrl), + Comment("Je pourrais vraiment utiliser un peu de Force pour mes révisions !", users[10].username, "22-12-2005", users[10].imgUrl), + Comment("Si la Force était vraiment avec moi, je serais déjà en vacances !", users[2].username, "23-12-2005", users[2].imgUrl), + Comment("Avec toute cette Force, je devrais pouvoir arrêter de procrastiner !", users[3].username, "24-12-2005", users[3].imgUrl), + + Comment("La vie, c'est comme une boîte de chocolats… mais parfois tu tombes sur le chocolat que tu détestes.", users[4].username, "25-12-2005", users[4].imgUrl), + Comment("Ce qui est drôle, c'est que moi, je choisis toujours les chocolats que je veux. Pas de surprise !", users[5].username, "26-12-2005", users[5].imgUrl), + Comment("La vie, c'est comme une boîte de chocolats… mais parfois les petits chocolats ont un goût étrange.", users[6].username, "27-12-2005", users[6].imgUrl), + Comment("J'aimerais juste éviter la noix au fond de la boîte, c'est toujours le pire chocolat.", users[7].username, "28-12-2005", users[7].imgUrl), + + Comment("Est-ce que ça veut dire que je suis en train d'exister juste parce que je suis en train de lire ça ? 🤔", users[8].username, "29-12-2005", users[8].imgUrl), + Comment("Ah, Descartes… c'est fou, mais à chaque fois que je pense trop, je me sens plus perdu !", users[9].username, "30-12-2005", users[9].imgUrl), + Comment("Ouais, mais parfois j'aimerais bien être juste un peu plus ‘je suis’ et un peu moins ‘je pense’", users[0].username, "31-12-2005", users[0].imgUrl), + Comment("Je pense donc je suis… Mais je me demande parfois si je suis vraiment ce que je pense !", users[1].username, "01-01-2006", users[1].imgUrl), + + Comment("La montagne, c'est mon tas de linge à laver… et les petites pierres, ce sont mes excuses pour ne pas m'en occuper.", users[2].username, "02-01-2006", users[2].imgUrl), + Comment("La montagne, c'est mon carnet de tâches… et j'ai perdu ma motivation pour enlever les petites pierres.", users[3].username, "03-01-2006", users[3].imgUrl), + Comment("Ok, mais parfois, les petites pierres deviennent des montagnes en elles-mêmes...", users[4].username, "04-01-2006", users[4].imgUrl), + Comment("C'est facile à dire, mais je suis toujours coincé à déplacer la première petite pierre.", users[5].username, "05-01-2006", users[5].imgUrl), + + Comment("Et c'est aussi là qu'on reconnaît ceux qui ne répondent pas aux messages.", users[6].username, "06-01-2006", users[6].imgUrl), + Comment("C'est vrai… mais parfois, même les vrais amis se cachent sous un coussin pendant les moments difficiles.", users[7].username, "07-01-2006", users[7].imgUrl), + Comment("Les vrais amis, c'est ceux qui viennent t'aider à déplacer des meubles quand tu déménages. 😉", users[8].username, "08-01-2006", users[8].imgUrl), + Comment("Je préfère qu'un ami vienne avec des pizzas pendant l'adversité. C'est plus mon genre.", users[9].username, "09-01-2006", users[9].imgUrl) ) } \ No newline at end of file diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/model/Quote.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/model/Quote.kt index 75da8b1..5dd5d57 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/model/Quote.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/model/Quote.kt @@ -5,7 +5,7 @@ import java.util.Date data class Quote ( val id: Int, val content: String, - val likes: Int, + var likes: Int, val language: SrcLanguage, val character: String, val source: String, 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 f9fe1fc..8a89327 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 @@ -34,7 +34,7 @@ interface IServices { fun getQuote( id : Int): Quote? - fun isFavorite(id : Int, user: User): Boolean + fun isFavorite(idQuote : Int, iduser: Int): Boolean fun getAllFavorite(): List fun getAllQuote(): List fun getSomeQuotes(nb: Int, page: Int) : MutableList 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 6c82153..775f535 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 @@ -151,9 +151,10 @@ class ServicesStub : IServices { return quotes.subList(fromIndex, toIndex).toMutableList() } - override fun isFavorite(id: Int, user: User): Boolean { + override fun isFavorite(idQuote: Int, idUser: Int): Boolean { + val user = getUserById(idUser) ?: return false val quote = getFavorite(user) - return quote.find{ it.id == id } != null + return quote.find{ it.id == idQuote } != null } 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 616acef..4284aac 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 @@ -18,6 +18,7 @@ import com.example.what_the_fantasy.data.services.ServicesStub import com.example.what_the_fantasy.ui.screens.* import com.example.what_the_fantasy.ui.viewModels.AuthUserViewModel import com.example.what_the_fantasy.ui.viewModels.CurrentUserViewModel +import com.example.what_the_fantasy.ui.viewModels.QuoteInformationUserViewModel import com.example.what_the_fantasy.ui.viewModels.SearchViewModel import com.example.what_the_fantasy.ui.viewModels.SignInUserViewModel import kotlinx.serialization.Serializable @@ -86,6 +87,11 @@ fun AppNavigator() { //ViewModel pour la recherche val searchVM : SearchViewModel = viewModel() val searchState by searchVM.searchState.collectAsState() + + //ViewModel pour les commentaires et likes des citations + val quoteInformationUserVM : QuoteInformationUserViewModel = viewModel() + val quoteInformationUserState by quoteInformationUserVM.quoteState.collectAsState() + Scaffold( modifier = Modifier.fillMaxSize(), containerColor = MaterialTheme.colorScheme.onPrimary @@ -175,7 +181,9 @@ fun AppNavigator() { service = services, currentUserVM = currentUserVM, currentUserState = currentUserState, - navSearch = { navController.navigate(Search(currentUserState.id))} + navSearch = { navController.navigate(Search(currentUserState.id))}, + quoteInformationUserVM = quoteInformationUserVM, + quoteInformationUserState = quoteInformationUserState, ) } composable { diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/QuotePage.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/QuotePage.kt index c4c2d40..3919d6d 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/QuotePage.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/QuotePage.kt @@ -2,6 +2,7 @@ package com.example.what_the_fantasy.ui.screens import android.content.Context import android.content.Intent +import android.util.Log import androidx.compose.animation.AnimatedVisibility import androidx.compose.animation.expandVertically import androidx.compose.animation.fadeIn @@ -29,7 +30,6 @@ import androidx.compose.material.icons.automirrored.filled.ArrowBack import androidx.compose.material.icons.automirrored.filled.Send import androidx.compose.material.icons.filled.Favorite import androidx.compose.material.icons.filled.FavoriteBorder -import androidx.compose.material.icons.filled.MailOutline import androidx.compose.material.icons.filled.Share import androidx.compose.material3.Icon import androidx.compose.material3.IconButton @@ -56,16 +56,14 @@ import androidx.compose.ui.unit.sp import com.example.what_the_fantasy.R import coil.compose.AsyncImage import com.example.what_the_fantasy.data.model.Comment +import com.example.what_the_fantasy.data.model.Quote import com.example.what_the_fantasy.data.services.IServices import com.example.what_the_fantasy.logs.LogsUsers import com.example.what_the_fantasy.ui.components.NavBar import com.example.what_the_fantasy.ui.states.CurrentUserState -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.theme.iconText -import com.example.what_the_fantasy.ui.theme.likeIcon -import com.example.what_the_fantasy.ui.theme.whiteBackcgroundText +import com.example.what_the_fantasy.ui.states.QuoteInformationUserState import com.example.what_the_fantasy.ui.viewModels.CurrentUserViewModel +import com.example.what_the_fantasy.ui.viewModels.QuoteInformationUserViewModel var isCommentVisible by mutableStateOf(false) @@ -80,14 +78,20 @@ fun QuotePage( navSearch: () -> Unit, currentUserVM : CurrentUserViewModel, currentUserState : CurrentUserState, + quoteInformationUserVM : QuoteInformationUserViewModel, + quoteInformationUserState : QuoteInformationUserState ) { + // utiliser ViewModel val quote = service.getQuote(quoteId) ?: return + val context = LocalContext.current - val user = service.getUserById(currentUserState.id) ?: return - val favorite by remember { mutableStateOf(service.isFavorite(id = quoteId, user = user)) } - NavBar(onProfile = true, + val favorite by remember { mutableStateOf(service.isFavorite( + idQuote = quoteId, + iduser = currentUserState.id)) } + + NavBar( currentUserVM = currentUserVM, currentUserState = currentUserState, navControllerFavorite = navFavorite, @@ -121,7 +125,9 @@ fun QuotePage( userId = currentUserState.id, quoteId = quoteId, context = context, - service = service + quoteInformationUserVM = quoteInformationUserVM, + quoteInformationUserState = quoteInformationUserState, + quote = quote ) QuoteText( text = '"' + quote.content + '"' @@ -157,7 +163,8 @@ fun QuotePage( .align(alignment = Alignment.End) ) { LikeInfo( - likes = quote.likes + likes = quote.likes, + quoteInformationUserVM ) } } @@ -174,9 +181,12 @@ fun QuotePage( ), exit = slideOutVertically() + shrinkVertically() + fadeOut() ) { - Box(modifier = Modifier.fillMaxWidth().background(MaterialTheme.colorScheme.primary).fillMaxSize()){ + Box(modifier = Modifier + .fillMaxWidth() + .background(MaterialTheme.colorScheme.primary) + .fillMaxSize()){ Column { - AddComment(currentUserState.id, service) + AddComment(currentUserState.id, service, quoteInformationUserState, quoteInformationUserVM) LstComment(service.getComment(quoteId)) } } @@ -209,7 +219,14 @@ fun ImageQuote(imageUrl : String){ } @Composable -fun FunctionalIcon(isFavorite: Boolean, userId : Int, quoteId : Int, context : Context, service: IServices){ +fun FunctionalIcon(isFavorite: Boolean, + userId : Int, + quoteId : Int, + context : Context, + quoteInformationUserVM : QuoteInformationUserViewModel, + quoteInformationUserState : QuoteInformationUserState, + quote : Quote +){ val logsUsers = LogsUsers() Row(modifier = Modifier @@ -259,7 +276,9 @@ fun FunctionalIcon(isFavorite: Boolean, userId : Int, quoteId : Int, context : C if(isFavorite){ IconButton( onClick = { - service.SupFav(userId = userId, QuoteId = quoteId) + quoteInformationUserVM.supFav(userId = userId, idQuote = quoteId) + quote.likes = quoteInformationUserState.like + //Log.e("Like", "Coeur ${quote.likes} ${quoteInformationUserState.like}") logsUsers.favoriteInformationUserFalse("Favorite", quoteId, userId) }, //sup fav modifier = Modifier.padding(start = 20.dp) @@ -275,7 +294,8 @@ fun FunctionalIcon(isFavorite: Boolean, userId : Int, quoteId : Int, context : C else{ IconButton( onClick = { - service.AddFav(userId = userId, QuoteId = quoteId) + quoteInformationUserVM.addFav(userId = userId, idQuote = quoteId) + quote.likes = quoteInformationUserState.like logsUsers.favoriteInformationUserTrue("Favorite", quoteId, userId) }, //add fav modifier = Modifier.padding(start = 50.dp) @@ -316,7 +336,10 @@ fun InfoQuoteText(nameId : Int, text : String){ } @Composable -fun LikeInfo(likes : Int){ +fun LikeInfo(likes : Int, + quoteInformationUserVM : QuoteInformationUserViewModel){ + quoteInformationUserVM.setLike(likes) + //Log.e("Like", "LikeInfo => Nb Like : ${likes}") Text( text = likes.toString(), color = MaterialTheme.colorScheme.onPrimary @@ -329,8 +352,11 @@ fun LikeInfo(likes : Int){ } @Composable -fun AddComment(userId: Int, service: IServices) { - var text by remember { mutableStateOf("") } +fun AddComment(userId: Int, + service: IServices, + quoteInformationUserState: QuoteInformationUserState, + quoteInformationUserVM: QuoteInformationUserViewModel) { + // var text by remember { mutableStateOf(quoteInformationUserState.comment) } Row( modifier = Modifier @@ -348,8 +374,8 @@ fun AddComment(userId: Int, service: IServices) { ) } TextField( - value = text, - onValueChange = { text = it }, + value = quoteInformationUserState.comment, + onValueChange = { quoteInformationUserVM.setComment(it) }, label = { Text(stringResource(R.string.comment)) }, modifier = Modifier .weight(1f) diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/states/QuoteInformationUserState.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/states/QuoteInformationUserState.kt new file mode 100644 index 0000000..922cb9b --- /dev/null +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/states/QuoteInformationUserState.kt @@ -0,0 +1,7 @@ +package com.example.what_the_fantasy.ui.states + +data class QuoteInformationUserState ( + val comment : String ="", + val isFavorite : Boolean = false, + val like : Int = 0 +) \ No newline at end of file diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/viewModels/QuoteInformationUserViewModel.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/viewModels/QuoteInformationUserViewModel.kt new file mode 100644 index 0000000..438273a --- /dev/null +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/viewModels/QuoteInformationUserViewModel.kt @@ -0,0 +1,43 @@ +package com.example.what_the_fantasy.ui.viewModels + +import android.util.Log +import androidx.lifecycle.ViewModel +import com.example.what_the_fantasy.data.services.ServicesStub +import com.example.what_the_fantasy.ui.states.QuoteInformationUserState +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.asStateFlow +import kotlinx.coroutines.flow.update + +class QuoteInformationUserViewModel: ViewModel() { + private val services = ServicesStub() // faire repository qui gère les services Stub et API + private val _quoteState = MutableStateFlow(QuoteInformationUserState()) + val quoteState : StateFlow = _quoteState.asStateFlow() + + fun setComment(comment : String){ + _quoteState.update { it.copy(comment=comment) } + } + + fun setFavorite(isFavorite : Boolean){ + _quoteState.update { it.copy(isFavorite=isFavorite) } + } + + fun setLike(like : Int){ + if(like >= 0) { + _quoteState.update { it.copy(like=like) } + } + } + + fun addFav(userId: Int, idQuote: Int) { + services.AddFav(userId, idQuote) + setLike(quoteState.value.like + 1) + setFavorite(true) + } + + fun supFav(userId: Int, idQuote: Int) { + services.SupFav(userId, idQuote) + setLike(quoteState.value.like - 1) + setFavorite(false) + //Log.e("Like", "Service ${quoteState.value.like}") + } +} \ No newline at end of file