diff --git a/What_The_Fantasy/app/build.gradle.kts b/What_The_Fantasy/app/build.gradle.kts index c40a7f8..48eaded 100644 --- a/What_The_Fantasy/app/build.gradle.kts +++ b/What_The_Fantasy/app/build.gradle.kts @@ -79,6 +79,6 @@ dependencies { //Retrofit implementation(libs.retrofit) implementation(libs.converter.gson) - + implementation(libs.gson) } \ No newline at end of file diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/model/SrcLanguage.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/model/SrcLanguage.kt index 9cfa08a..0454a7f 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/model/SrcLanguage.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/model/SrcLanguage.kt @@ -1,6 +1,31 @@ package com.example.what_the_fantasy.data.model +import com.google.gson.* +import java.lang.reflect.Type + +enum class SrcLanguage(val code:Int) { + vf(1), + vo(0); + + companion object { + fun fromCode(code: Int): SrcLanguage? = values().find { it.code == code } + } +} + +class LangAdapter : JsonSerializer, JsonDeserializer { + override fun serialize( + src: SrcLanguage?, + typeOfSrc: Type?, + context: JsonSerializationContext? + ): JsonElement = JsonPrimitive(src?.code) + + override fun deserialize( + json: JsonElement?, + typeOfT: Type?, + context: JsonDeserializationContext? + ): SrcLanguage { + val code = json?.asInt + return SrcLanguage.fromCode(code ?: throw JsonParseException("Code null")) + ?: throw JsonParseException("Unknown Status code: $code") + } -enum class SrcLanguage { - vf, - vo } \ No newline at end of file diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/model/User.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/model/User.kt index 9a4fe0a..c7355c1 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/model/User.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/model/User.kt @@ -14,5 +14,7 @@ class User( val imgUrl: String, var password: String, + + @SerializedName("lang") var langage : SrcLanguage ) \ 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 43bb27d..31bf2aa 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,9 +1,12 @@ 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.Quote import com.example.what_the_fantasy.data.model.SrcLanguage import com.example.what_the_fantasy.data.model.User +import com.google.gson.Gson +import com.google.gson.GsonBuilder import retrofit2.Response import retrofit2.Retrofit import retrofit2.converter.gson.GsonConverterFactory @@ -72,10 +75,14 @@ interface ApiService { object RetrofitInstance { private const val BASE_URL = "https://codefirst.iut.uca.fr/containers/WhatTheFantasy-web-services/api/v1/" + private val gson: Gson = GsonBuilder() + .registerTypeAdapter(SrcLanguage::class.java, LangAdapter()) + .create() + val api: ApiService by lazy { Retrofit.Builder() .baseUrl(BASE_URL) - .addConverterFactory(GsonConverterFactory.create()) + .addConverterFactory(GsonConverterFactory.create(gson)) .build() .create(ApiService::class.java) } 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 99b4d1d..230702e 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 @@ -20,7 +20,7 @@ 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) + suspend fun EditPasswd(passwd : String, index : Int,currentUserViewModel : CurrentUserViewModel): Boolean suspend fun EditImage(index : Int,currentUserViewModel : CurrentUserViewModel) : String suspend fun ChangeLangage(index : Int, currentUserViewModel : CurrentUserViewModel): SrcLanguage fun isUsernameExist(username : String) : Boolean 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 f61ce8d..5b6452f 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 @@ -95,6 +95,7 @@ class ServicesAPI : IServices { val userExistsResponse = RetrofitInstance.api.checkIfEmailExists(email) if (userExistsResponse) { + Log.d("EditEmail", "Exception occurred: The email alredy exist") return false } @@ -125,8 +126,34 @@ class ServicesAPI : IServices { } } - override suspend fun EditPasswd(passwd: String, index: Int) { - TODO() + override suspend fun EditPasswd(passwd: String, index: Int, currentUserViewModel: CurrentUserViewModel): Boolean { + try { + val responseUser = RetrofitInstance.api.getUserById(index) + + val updatedUser = User( + id = index, + username = responseUser.username, + email = responseUser.username, + password = passwd, + //password = hashPassword(passwd), + imgUrl = responseUser.imgUrl, + langage = responseUser.langage, + date = responseUser.date + ) + + val response = RetrofitInstance.api.updateUsername(index, updatedUser) + + if (response.isSuccessful) { + currentUserViewModel.setUser(updatedUser) + return true + } else { + return false + } + } catch (e: Exception) { + e.printStackTrace() + Log.d("EditPasswd", "Exception occurred: ${e.message}") + return false + } } override suspend fun EditImage(index: Int, currentUserViewModel : CurrentUserViewModel): String { 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 78b9b6e..9d1840d 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 @@ -64,11 +64,11 @@ class ServicesStub : IServices { return false } - override suspend fun EditPasswd(passwd: String, index : Int) { + override suspend fun EditPasswd(passwd: String, index: Int, currentUserViewModel: CurrentUserViewModel): Boolean { val user = getUserById(index) val passwordhash = hashPassword(passwd) user?.password = passwordhash - + return true //Afficher tous les users en log //logsUser.logDebugAllUsers(getAllUsers(), "PasswordUpdate") } 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 350b082..8ce73f7 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 @@ -25,7 +25,7 @@ class CurrentUserViewModel : ViewModel(){ setUsername(it.username) setEmail(it.email) setPassword(it.password) - //setLangue(it.langage) // A rajouter quand on aura le langage + setLangue(it.langage) // A rajouter quand on aura le langage setImage(it.imgUrl) } } @@ -94,7 +94,7 @@ class CurrentUserViewModel : ViewModel(){ suspend fun editPassword(password : String, index : Int){ - services.EditPasswd(password, index) + services.EditPasswd(password, index, this) } diff --git a/What_The_Fantasy/gradle/libs.versions.toml b/What_The_Fantasy/gradle/libs.versions.toml index 0f71b3d..7b1f00a 100644 --- a/What_The_Fantasy/gradle/libs.versions.toml +++ b/What_The_Fantasy/gradle/libs.versions.toml @@ -5,6 +5,7 @@ coilCompose = "2.2.1" coilComposeVersion = "1.4.0" converterGson = "2.9.0" +gson = "2.10.1" kotlin = "1.9.0" coreKtx = "1.10.1" junit = "4.13.2" @@ -26,6 +27,7 @@ coil-compose = { module = "io.coil-kt:coil-compose", version.ref = "coilCompose" coil-compose-v140 = { module = "io.coil-kt:coil-compose", version.ref = "coilComposeVersion" } converter-gson = { module = "com.squareup.retrofit2:converter-gson", version.ref = "converterGson" } +gson = { module = "com.google.code.gson:gson", version.ref = "gson" } junit = { group = "junit", name = "junit", version.ref = "junit" } androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" } androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }