From 5e99fa71f21d844e79db50a5939eb0fe0996ee97 Mon Sep 17 00:00:00 2001 From: "kentin.brongniart" Date: Sun, 6 Apr 2025 20:49:00 +0200 Subject: [PATCH] API Accueil marche --- .../what_the_fantasy/data/model/Quote.kt | 15 ++++++- .../what_the_fantasy/data/model/SrcType.kt | 43 ++++++++++++++++++- .../data/retrofit/ApiService.kt | 3 ++ .../data/services/APIReponceList.kt | 6 +-- .../ui/navigations/AppNavigator.kt | 2 +- .../ui/screens/AccueilPage.kt | 17 ++++---- 6 files changed, 70 insertions(+), 16 deletions(-) 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 2090223..0242bbd 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 @@ -3,7 +3,11 @@ package com.example.what_the_fantasy.data.model import com.google.gson.annotations.SerializedName data class Quote ( + + @SerializedName("id") val id: Int, + + @SerializedName("content") val content: String, @SerializedName("like") @@ -11,6 +15,8 @@ data class Quote ( @SerializedName("langage") val language: SrcLanguage, + + @SerializedName("character") val character: String, @SerializedName("titleSource") @@ -18,9 +24,14 @@ data class Quote ( @SerializedName("imagePath") val imgUrl: String, - val type: SrcType, + + @SerializedName("type") + val type: SrcType , @SerializedName("dateSource") - val date: Int + val date: Int, + + @SerializedName("isValide") + val isValide : Boolean = true ) \ No newline at end of file diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/model/SrcType.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/model/SrcType.kt index a5c6112..97827a3 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/model/SrcType.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/model/SrcType.kt @@ -2,9 +2,50 @@ package com.example.what_the_fantasy.data.model import androidx.compose.ui.res.stringResource import com.example.what_the_fantasy.R +import com.google.gson.JsonDeserializationContext +import com.google.gson.JsonDeserializer +import com.google.gson.JsonElement +import com.google.gson.JsonParseException +import com.google.gson.JsonPrimitive +import com.google.gson.JsonSerializationContext +import com.google.gson.JsonSerializer +import java.lang.reflect.Type enum class SrcType (val value: String) { Movie("movie" ), VideoGame("videoGame"), - Series("series"), + Series("series"); + + companion object { + fun fromCode(value: Int): SrcType? = SrcType.values().find { + if (value==0){ + it.value == "movie" + } + else if (value==1){ + it.value == "videoGame" + } + else{ + it.value == "series" + } + } + } +} + +class SrcTypeAdapter : JsonSerializer, JsonDeserializer { + override fun serialize( + src: SrcType?, + typeOfSrc: Type?, + context: JsonSerializationContext? + ): JsonElement = JsonPrimitive(src?.value) + + override fun deserialize( + json: JsonElement?, + typeOfT: Type?, + context: JsonDeserializationContext? + ): SrcType { + val value = json?.asInt + return SrcType.fromCode(value ?: throw JsonParseException("Code null")) + ?: throw JsonParseException("Unknown Status code: $value") + } + } \ 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 e168dd8..3b53466 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 @@ -2,8 +2,10 @@ 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.LangAdapter +import com.example.what_the_fantasy.data.model.SrcTypeAdapter 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.SrcType import com.example.what_the_fantasy.data.model.User import com.example.what_the_fantasy.data.services.APIReponceList import com.google.gson.Gson @@ -89,6 +91,7 @@ object RetrofitInstance { private val gson: Gson = GsonBuilder() .registerTypeAdapter(SrcLanguage::class.java, LangAdapter()) + .registerTypeAdapter(SrcType::class.java, SrcTypeAdapter()) .create() val api: ApiService by lazy { diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/APIReponceList.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/APIReponceList.kt index cf19a5f..56a6bd2 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/APIReponceList.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/APIReponceList.kt @@ -7,11 +7,11 @@ data class APIReponceList ( val totalCount: Int, @SerializedName("pageIndex") - var index: Int, + val index: Int, @SerializedName("countPerPage") - var count: Int, + val count: Int, @SerializedName("items") - var items: MutableList, + val items: MutableList, ) 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 55963ea..be30590 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 @@ -139,7 +139,7 @@ fun AppNavigator() { ) }, navSearch = { navController.navigate(Search(currentUserState.id))}, - services = servicesStub, + services = services, currentUserVM = currentUserVM, currentUserState = currentUserState, ) 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 265e4a0..51a1cc0 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 @@ -52,7 +52,7 @@ fun AccueilPage( val titleDalyQuote = stringResource(R.string.TitleHomeDailyQuote) val titleSuggestion = stringResource(R.string.TitleHomeSuggestion) - val page = remember { mutableIntStateOf(1) } + val page = remember { mutableIntStateOf(0) } val quotes = remember { mutableStateListOf() } val state = rememberLazyListState() val layoutInfo = remember { derivedStateOf { state.layoutInfo } } @@ -61,11 +61,10 @@ fun AccueilPage( val fullyVisibleItemsInfo = visibleItemsInfo.toMutableList() val lastItem = if (fullyVisibleItemsInfo.isNotEmpty()) fullyVisibleItemsInfo.last() else null - val dailyQuote = DailyQuoteStub.dailyQuote - //var dailyQuote = remember{ Quote(-1,"",0,SrcLanguage.vo,"","","",SrcType.Movie,0) } - //LaunchedEffect(true){ - // dailyQuote = services.getDalyQuote(currentUserState.langage) - //} + val dailyQuote = remember { mutableStateOf(Quote(-1,"",0,SrcLanguage.vo,"","","",SrcType.Movie,0)) } + LaunchedEffect(true){ + dailyQuote.value=services.getDalyQuote(currentUserState.langage) + } LaunchedEffect(page.intValue) { if (!isLoading.value) { @@ -101,9 +100,9 @@ fun AccueilPage( .background(MaterialTheme.colorScheme.background) ) { LazyColumn(modifier = Modifier.weight(1f), state = state) { - if(dailyQuote.id!=-1) { + if(dailyQuote.value.id!=-1) { item { - Column(Modifier.clickable { navQuote(dailyQuote.id) }) { + Column(Modifier.clickable { navQuote(dailyQuote.value.id) }) { Text( text = titleDalyQuote, color = MaterialTheme.colorScheme.onBackground, @@ -113,7 +112,7 @@ fun AccueilPage( .padding(16.dp), textAlign = TextAlign.Center ) - QuoteLittle(dailyQuote) + QuoteLittle(dailyQuote.value) } } }