Encore des routes

ConnectAPI
Leni BEAULATON 3 weeks ago
parent 8362377241
commit 6eec8158c8

@ -1,6 +1,11 @@
package com.example.what_the_fantasy.data.model package com.example.what_the_fantasy.data.model
import com.google.gson.annotations.SerializedName
data class Image( data class Image(
@SerializedName("idImage")
val id: Int, val id: Int,
@SerializedName("imagePath")
val url: String val url: String
) )

@ -1,16 +1,26 @@
package com.example.what_the_fantasy.data.model package com.example.what_the_fantasy.data.model
import java.util.Date import com.google.gson.annotations.SerializedName
data class Quote ( data class Quote (
val id: Int, val id: Int,
val content: String, val content: String,
@SerializedName("like")
var likes: Int, var likes: Int,
@SerializedName("langage")
val language: SrcLanguage, val language: SrcLanguage,
val character: String, val character: String,
@SerializedName("titleSource")
val source: String, val source: String,
@SerializedName("imagePath")
val imgUrl: String, val imgUrl: String,
val type: SrcType, val type: SrcType,
@SerializedName("dateSource")
val date: Int val date: Int
) )

@ -1,5 +1,7 @@
package com.example.what_the_fantasy.data.retrofit 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.SrcLanguage
import com.example.what_the_fantasy.data.model.User import com.example.what_the_fantasy.data.model.User
import retrofit2.Response import retrofit2.Response
@ -12,16 +14,8 @@ import retrofit2.http.PUT
import retrofit2.http.Path import retrofit2.http.Path
import retrofit2.http.Query import retrofit2.http.Query
data class UserUpdateRequest(
val username: String,
val email: String,
val passwd: String,
val langage: SrcLanguage,
val imgUrl: String,
) interface ApiService {
interface UserApiService {
@GET("users/username") @GET("users/username")
suspend fun getUserByUsername( suspend fun getUserByUsername(
@ -49,17 +43,40 @@ interface UserApiService {
@Query("id") id: Int, @Query("id") id: Int,
@Body user: User // Envoie un objet `User` avec les nouvelles données @Body user: User // Envoie un objet `User` avec les nouvelles données
): Response<Unit> ): Response<Unit>
@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<Image>
@GET("image/{id}")
suspend fun getImageById(
@Path("id") id: Int
): Image
} }
object RetrofitInstance { object RetrofitInstance {
private const val BASE_URL = "https://codefirst.iut.uca.fr/containers/WhatTheFantasy-web-services/api/v1/" 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() Retrofit.Builder()
.baseUrl(BASE_URL) .baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create()) .addConverterFactory(GsonConverterFactory.create())
.build() .build()
.create(UserApiService::class.java) .create(ApiService::class.java)
} }
} }

@ -21,8 +21,8 @@ interface IServices {
suspend fun EditUsername(username : String, index : Int, currentUserViewModel: CurrentUserViewModel) : Boolean suspend fun EditUsername(username : String, index : Int, currentUserViewModel: CurrentUserViewModel) : Boolean
suspend fun EditEmail(email : String, index : Int,currentUserViewModel: CurrentUserViewModel) : Boolean suspend fun EditEmail(email : String, index : Int,currentUserViewModel: CurrentUserViewModel) : Boolean
suspend fun EditPasswd(passwd : String, index : Int) suspend fun EditPasswd(passwd : String, index : Int)
fun EditImage(index : Int) : String suspend fun EditImage(index : Int,currentUserViewModel : CurrentUserViewModel) : String
fun ChangeLangage(index : Int): SrcLanguage suspend fun ChangeLangage(index : Int, currentUserViewModel : CurrentUserViewModel): SrcLanguage
fun isUsernameExist(username : String) : Boolean fun isUsernameExist(username : String) : Boolean
fun isEmailExist(email : String) : Boolean fun isEmailExist(email : String) : Boolean
@ -41,6 +41,6 @@ interface IServices {
fun getAllFavorite(): List<Favorite> fun getAllFavorite(): List<Favorite>
fun getAllQuote(): List<Quote> fun getAllQuote(): List<Quote>
fun getSomeQuotes(nb: Int, page: Int) : MutableList<Quote> fun getSomeQuotes(nb: Int, page: Int) : MutableList<Quote>
suspend fun getDalyQuote(langage : SrcLanguage) : Quote
fun search(type : String ,search:String ,indexCount: Int): List<Quote> fun search(type : String ,search:String ,indexCount: Int): List<Quote>
} }

@ -1,6 +1,9 @@
package com.example.what_the_fantasy.data.services package com.example.what_the_fantasy.data.services
import android.os.Build
import android.util.Log 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.Comment
import com.example.what_the_fantasy.data.model.Favorite import com.example.what_the_fantasy.data.model.Favorite
import com.example.what_the_fantasy.data.model.Quote 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.model.User
import com.example.what_the_fantasy.data.retrofit.RetrofitInstance import com.example.what_the_fantasy.data.retrofit.RetrofitInstance
import com.example.what_the_fantasy.ui.viewModels.CurrentUserViewModel 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.Comment
//import com.example.what_the_fantasy.data.model.Favorite //import com.example.what_the_fantasy.data.model.Favorite
@ -124,12 +129,62 @@ class ServicesAPI : IServices {
TODO() TODO()
} }
override fun EditImage(index: Int): String { override suspend fun EditImage(index: Int, currentUserViewModel : CurrentUserViewModel): String {
TODO("Not yet implemented") 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 { override fun isUsernameExist(username: String): Boolean {
@ -192,8 +247,22 @@ class ServicesAPI : IServices {
TODO("Not yet implemented") 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<Quote> { override fun search(type: String, search: String, indexCount: Int): List<Quote> {
TODO("Not yet implemented") 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
}
} }

@ -52,7 +52,7 @@ class ServicesStub : IServices {
return false 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) val user = getUserById(index)
if(!isEmailExist(email)){ if(!isEmailExist(email)){
@ -73,11 +73,11 @@ class ServicesStub : IServices {
//logsUser.logDebugAllUsers(getAllUsers(), "PasswordUpdate") //logsUser.logDebugAllUsers(getAllUsers(), "PasswordUpdate")
} }
override fun EditImage(index : Int) : String { override suspend fun EditImage(index : Int,currentUserViewModel : CurrentUserViewModel) : String {
return randomImage() return randomImage()
} }
override fun ChangeLangage(index : Int) : SrcLanguage{ override suspend fun ChangeLangage(index : Int,currentUserViewModel : CurrentUserViewModel) : SrcLanguage{
if(getAllUsers()[index].langage == SrcLanguage.vo){ if(getAllUsers()[index].langage == SrcLanguage.vo){
getAllUsers()[index].langage = SrcLanguage.vf getAllUsers()[index].langage = SrcLanguage.vf
} }
@ -154,6 +154,11 @@ class ServicesStub : IServices {
return quotes.subList(fromIndex, toIndex).toMutableList() 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 { override suspend fun isFavorite(idQuote: Int, idUser: Int): Boolean {
val user = getUserById(idUser) ?: return false val user = getUserById(idUser) ?: return false
val quote = getFavorite(user) val quote = getFavorite(user)

@ -1,5 +1,6 @@
package com.example.what_the_fantasy.ui.screens package com.example.what_the_fantasy.ui.screens
import android.annotation.SuppressLint
import androidx.compose.foundation.background import androidx.compose.foundation.background
import androidx.compose.foundation.clickable import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.* 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.viewModels.CurrentUserViewModel
import com.example.what_the_fantasy.ui.theme.colorBackground import com.example.what_the_fantasy.ui.theme.colorBackground
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
@SuppressLint("CoroutineCreationDuringComposition")
@Composable @Composable
fun AccueilPage( fun AccueilPage(
navFavorite: () -> Unit, navFavorite: () -> Unit,
@ -39,6 +42,9 @@ fun AccueilPage(
currentUserState : CurrentUserState, currentUserState : CurrentUserState,
) { ) {
//val dailyQuote = services.getDalyQuote(currentUserState.langage)
val dailyQuote = DailyQuoteStub.dailyQuote val dailyQuote = DailyQuoteStub.dailyQuote
val titleDalyQuote = stringResource(R.string.TitleHomeDailyQuote) val titleDalyQuote = stringResource(R.string.TitleHomeDailyQuote)

@ -132,13 +132,18 @@ fun ProfilPage(navFavorite: () -> Unit,
@Composable @Composable
fun ImageProfil(size :Int,index: Int, currentUserVM: CurrentUserViewModel, currentUserState: CurrentUserState){ fun ImageProfil(size :Int,index: Int, currentUserVM: CurrentUserViewModel, currentUserState: CurrentUserState){
val coroutineScope = rememberCoroutineScope()
AsyncImage( AsyncImage(
model = currentUserState.imagePath, model = currentUserState.imagePath,
contentDescription = "Photo de profil", contentDescription = "Photo de profil",
modifier = Modifier modifier = Modifier
.size(size.dp) .size(size.dp)
.clip(CircleShape) .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 @Composable
fun ButtonLanguage(textResId : Int, size :Int,colorTexte : Color, colorButton : Color, currentUserVM: CurrentUserViewModel, currentUserState : CurrentUserState){ fun ButtonLanguage(textResId : Int, size :Int,colorTexte : Color, colorButton : Color, currentUserVM: CurrentUserViewModel, currentUserState : CurrentUserState){
val text = stringResource(id = textResId) val text = stringResource(id = textResId)
val coroutineScope = rememberCoroutineScope()
Button( Button(
onClick = { onClick = {
coroutineScope.launch {
currentUserVM.editLangue(currentUserState.id) currentUserVM.editLangue(currentUserState.id)
}
}, },
colors = ButtonDefaults.buttonColors(containerColor = colorButton), colors = ButtonDefaults.buttonColors(containerColor = colorButton),
modifier = Modifier.fillMaxWidth(), modifier = Modifier.fillMaxWidth(),

@ -98,19 +98,17 @@ class CurrentUserViewModel : ViewModel(){
} }
fun editLangue(index : Int){ suspend fun editLangue(index : Int){
val langage = services.ChangeLangage(index) val langage = services.ChangeLangage(index, this)
_currentUserState.update { _currentUserState.update {
it.copy(langage = langage) it.copy(langage = langage)
} }
} }
fun editImage(index : Int){ suspend fun editImage(index : Int){
// val image = services.EditImage(index)
//
// _currentUserState.update { // _currentUserState.update {
// it.copy(imagePath = image) // it.copy(imagePath = services.EditImage(index, this))
// } // }
} }
} }
Loading…
Cancel
Save