From 6eec8158c822c637f9882a1270f5d961a0b478ff Mon Sep 17 00:00:00 2001 From: "leni.beaulaton" Date: Sat, 5 Apr 2025 18:21:16 +0200 Subject: [PATCH] Encore des routes --- .../what_the_fantasy/data/model/Image.kt | 5 ++ .../what_the_fantasy/data/model/Quote.kt | 12 ++- .../data/retrofit/ApiService.kt | 39 +++++++--- .../data/services/IServices.kt | 6 +- .../data/services/ServicesAPI.kt | 77 ++++++++++++++++++- .../data/services/ServicesStub.kt | 11 ++- .../ui/screens/AccueilPage.kt | 6 ++ .../what_the_fantasy/ui/screens/ProfilPage.kt | 11 ++- .../ui/viewModels/CurrentUserViewModel.kt | 12 ++- 9 files changed, 148 insertions(+), 31 deletions(-) diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/model/Image.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/model/Image.kt index 1068afa..deec641 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/model/Image.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/model/Image.kt @@ -1,6 +1,11 @@ package com.example.what_the_fantasy.data.model +import com.google.gson.annotations.SerializedName + data class Image( + @SerializedName("idImage") val id: Int, + + @SerializedName("imagePath") val url: String ) 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 5dd5d57..2090223 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 @@ -1,16 +1,26 @@ package com.example.what_the_fantasy.data.model -import java.util.Date +import com.google.gson.annotations.SerializedName data class Quote ( val id: Int, val content: String, + + @SerializedName("like") var likes: Int, + + @SerializedName("langage") val language: SrcLanguage, val character: String, + + @SerializedName("titleSource") val source: String, + + @SerializedName("imagePath") val imgUrl: String, val type: SrcType, + + @SerializedName("dateSource") val date: Int ) \ No newline at end of file diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/retrofit/ApiService.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/retrofit/ApiService.kt index 3b570eb..43bb27d 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/retrofit/ApiService.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/retrofit/ApiService.kt @@ -1,5 +1,7 @@ package com.example.what_the_fantasy.data.retrofit +import com.example.what_the_fantasy.data.model.Image +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 import retrofit2.Response @@ -12,16 +14,8 @@ import retrofit2.http.PUT import retrofit2.http.Path import retrofit2.http.Query -data class UserUpdateRequest( - val username: String, - val email: String, - val passwd: String, - val langage: SrcLanguage, - val imgUrl: String, -) - -interface UserApiService { +interface ApiService { @GET("users/username") suspend fun getUserByUsername( @@ -49,17 +43,40 @@ interface UserApiService { @Query("id") id: Int, @Body user: User // Envoie un objet `User` avec les nouvelles données ): Response + + + + + @GET("quote/dailyquote") + suspend fun getDailyQuote( + @Query("year") year: Int, + @Query("month") month: Int, + @Query("day") day: Int, + @Query("lang") lang: SrcLanguage + ): Quote + + + @GET("image/all") + suspend fun getAllImages( + @Query("index") index: Int, + @Query("count") count: Int + ): List + + @GET("image/{id}") + suspend fun getImageById( + @Path("id") id: Int + ): Image } object RetrofitInstance { private const val BASE_URL = "https://codefirst.iut.uca.fr/containers/WhatTheFantasy-web-services/api/v1/" - val api: UserApiService by lazy { + val api: ApiService by lazy { Retrofit.Builder() .baseUrl(BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .build() - .create(UserApiService::class.java) + .create(ApiService::class.java) } } \ No newline at end of file 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 78dbd7d..99b4d1d 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 @@ -21,8 +21,8 @@ interface IServices { suspend fun EditUsername(username : String, index : Int, currentUserViewModel: CurrentUserViewModel) : Boolean suspend fun EditEmail(email : String, index : Int,currentUserViewModel: CurrentUserViewModel) : Boolean suspend fun EditPasswd(passwd : String, index : Int) - fun EditImage(index : Int) : String - fun ChangeLangage(index : Int): SrcLanguage + suspend fun EditImage(index : Int,currentUserViewModel : CurrentUserViewModel) : String + suspend fun ChangeLangage(index : Int, currentUserViewModel : CurrentUserViewModel): SrcLanguage fun isUsernameExist(username : String) : Boolean fun isEmailExist(email : String) : Boolean @@ -41,6 +41,6 @@ interface IServices { fun getAllFavorite(): List fun getAllQuote(): List fun getSomeQuotes(nb: Int, page: Int) : MutableList - + suspend fun getDalyQuote(langage : SrcLanguage) : Quote fun search(type : String ,search:String ,indexCount: Int): List } \ No newline at end of file diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesAPI.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesAPI.kt index 3d99a15..f61ce8d 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesAPI.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesAPI.kt @@ -1,6 +1,9 @@ package com.example.what_the_fantasy.data.services +import android.os.Build import android.util.Log +import androidx.annotation.RequiresApi +import com.example.what_the_fantasy.data.local.ImageStub.allImages import com.example.what_the_fantasy.data.model.Comment import com.example.what_the_fantasy.data.model.Favorite import com.example.what_the_fantasy.data.model.Quote @@ -8,6 +11,8 @@ 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.retrofit.RetrofitInstance import com.example.what_the_fantasy.ui.viewModels.CurrentUserViewModel +import java.time.LocalDate +import java.util.Date //import com.example.what_the_fantasy.data.model.Comment //import com.example.what_the_fantasy.data.model.Favorite @@ -124,12 +129,62 @@ class ServicesAPI : IServices { TODO() } - override fun EditImage(index: Int): String { - TODO("Not yet implemented") + override suspend fun EditImage(index: Int, currentUserViewModel : CurrentUserViewModel): String { + try { + + val responseUser = RetrofitInstance.api.getUserById(index) + + val updatedUser = User( + id = index, + username = responseUser.username, + email = responseUser.email,// Nouvel email d'utilisateur + password = responseUser.password, + imgUrl = randomImage(), + langage = responseUser.langage, + date = responseUser.date + ) + + val response = RetrofitInstance.api.updateUsername(index, updatedUser) + + if (response.isSuccessful) { + currentUserViewModel.setUser(updatedUser) + return updatedUser.imgUrl + } else { + return responseUser.imgUrl + } + } catch (e: Exception) { + e.printStackTrace() + return "" + Log.d("EditEmail", "Exception occurred: ${e.message}") + } } - override fun ChangeLangage(index: Int): SrcLanguage { - TODO("Not yet implemented") + + + + override suspend fun ChangeLangage(index: Int, currentUserViewModel : CurrentUserViewModel): SrcLanguage { + val responseUser = RetrofitInstance.api.getUserById(index) + var langage : SrcLanguage + + if(responseUser.langage == SrcLanguage.vf){ + langage = SrcLanguage.vo + } + else{ + langage = SrcLanguage.vf + } + + val updatedUser = User( + id = index, + username = responseUser.username, + email = responseUser.email, + password = responseUser.password, + imgUrl = responseUser.imgUrl, + langage = langage, + date = responseUser.date + ) + RetrofitInstance.api.updateUsername(index, updatedUser) + currentUserViewModel.setUser(updatedUser) + return langage } override fun isUsernameExist(username: String): Boolean { @@ -192,8 +247,22 @@ class ServicesAPI : IServices { TODO("Not yet implemented") } + @RequiresApi(Build.VERSION_CODES.O) + override suspend fun getDalyQuote(langage : SrcLanguage): Quote { + val today = LocalDate.now() + return RetrofitInstance.api.getDailyQuote(today.year, today.monthValue, today.dayOfMonth, langage) + } + override fun search(type: String, search: String, indexCount: Int): List { TODO("Not yet implemented") } + + //------------------------------------------------------- + suspend fun randomImage() : String{ + val imagesList = RetrofitInstance.api.getAllImages(0, 300) + val sizeList = imagesList.size + return RetrofitInstance.api.getImageById((0..sizeList).random()).url + } + } \ No newline at end of file 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 1fab8bb..78b9b6e 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 @@ -52,7 +52,7 @@ class ServicesStub : IServices { return false } - override suspend fun EditEmail(email: String, index : Int) : Boolean { + override suspend fun EditEmail(email: String, index : Int,currentUserViewModel: CurrentUserViewModel) : Boolean { val user = getUserById(index) if(!isEmailExist(email)){ @@ -73,11 +73,11 @@ class ServicesStub : IServices { //logsUser.logDebugAllUsers(getAllUsers(), "PasswordUpdate") } - override fun EditImage(index : Int) : String { + override suspend fun EditImage(index : Int,currentUserViewModel : CurrentUserViewModel) : String { return randomImage() } - override fun ChangeLangage(index : Int) : SrcLanguage{ + override suspend fun ChangeLangage(index : Int,currentUserViewModel : CurrentUserViewModel) : SrcLanguage{ if(getAllUsers()[index].langage == SrcLanguage.vo){ getAllUsers()[index].langage = SrcLanguage.vf } @@ -154,6 +154,11 @@ class ServicesStub : IServices { return quotes.subList(fromIndex, toIndex).toMutableList() } + + override suspend fun getDalyQuote(langage: SrcLanguage): Quote { + TODO("Not yet implemented") + } + override suspend fun isFavorite(idQuote: Int, idUser: Int): Boolean { val user = getUserById(idUser) ?: return false val quote = getFavorite(user) diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/AccueilPage.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/AccueilPage.kt index 3a5842c..d23a091 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/AccueilPage.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/AccueilPage.kt @@ -1,5 +1,6 @@ package com.example.what_the_fantasy.ui.screens +import android.annotation.SuppressLint import androidx.compose.foundation.background import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.* @@ -26,7 +27,9 @@ import com.example.what_the_fantasy.ui.states.CurrentUserState import com.example.what_the_fantasy.ui.viewModels.CurrentUserViewModel import com.example.what_the_fantasy.ui.theme.colorBackground import kotlinx.coroutines.delay +import kotlinx.coroutines.launch +@SuppressLint("CoroutineCreationDuringComposition") @Composable fun AccueilPage( navFavorite: () -> Unit, @@ -39,6 +42,9 @@ fun AccueilPage( currentUserState : CurrentUserState, ) { + + //val dailyQuote = services.getDalyQuote(currentUserState.langage) + val dailyQuote = DailyQuoteStub.dailyQuote val titleDalyQuote = stringResource(R.string.TitleHomeDailyQuote) 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 5319114..0a7c7f6 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 @@ -132,13 +132,18 @@ fun ProfilPage(navFavorite: () -> Unit, @Composable fun ImageProfil(size :Int,index: Int, currentUserVM: CurrentUserViewModel, currentUserState: CurrentUserState){ + val coroutineScope = rememberCoroutineScope() AsyncImage( model = currentUserState.imagePath, contentDescription = "Photo de profil", modifier = Modifier .size(size.dp) .clip(CircleShape) - .clickable{currentUserVM.editImage(index)} + .clickable{ + coroutineScope.launch { + currentUserVM.editImage(index) + } + } ) } @@ -490,10 +495,12 @@ fun ButtonUnLog(textResId : Int, size :Int, colorTexte : Color,colorButton : Col @Composable fun ButtonLanguage(textResId : Int, size :Int,colorTexte : Color, colorButton : Color, currentUserVM: CurrentUserViewModel, currentUserState : CurrentUserState){ val text = stringResource(id = textResId) - + val coroutineScope = rememberCoroutineScope() Button( onClick = { + coroutineScope.launch { currentUserVM.editLangue(currentUserState.id) + } }, colors = ButtonDefaults.buttonColors(containerColor = colorButton), modifier = Modifier.fillMaxWidth(), 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 index 7aa7bbb..350b082 100644 --- 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 @@ -98,19 +98,17 @@ class CurrentUserViewModel : ViewModel(){ } - fun editLangue(index : Int){ - val langage = services.ChangeLangage(index) + suspend fun editLangue(index : Int){ + val langage = services.ChangeLangage(index, this) _currentUserState.update { it.copy(langage = langage) } } - fun editImage(index : Int){ - // val image = services.EditImage(index) -// -// _currentUserState.update { -// it.copy(imagePath = image) + suspend fun editImage(index : Int){ +// _currentUserState.update { +// it.copy(imagePath = services.EditImage(index, this)) // } } } \ No newline at end of file