avancé sur les requetes retrofit

android
Patrick BRUGIERE 1 year ago
parent 051ddf7978
commit c9647fa011

@ -50,6 +50,7 @@ android {
}
dependencies {
implementation ("com.squareup.retrofit2:converter-gson:2.9.0")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9")
implementation("androidx.compose.material:material:1.6.4")
implementation("androidx.core:core-ktx:1.12.0")

@ -0,0 +1,8 @@
package sae.android.sae_2a.data
data class Group(
val id : Long,
val num : Int,
val year : Int,
val sector : String
)

@ -1,7 +1,8 @@
package sae.android.sae_2a.data
data class Vocabulary(
val name: String,
val aut: String?,
val word : String,
val LangueName: String,
val words: Map<String, String>
)

@ -0,0 +1,27 @@
package sae.android.sae_2a.service
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
object ApiClient {
private val BASE_URL: String = "https://codefirst.iut.uca.fr/containers/antoinejourdain-api_container/api/v1/"
private val httpClient : OkHttpClient by lazy {
OkHttpClient.Builder().build()
}
private val gson : Gson by lazy {
GsonBuilder().setLenient().create()
}
private val retrofit : Retrofit by lazy {
Retrofit.Builder()
.baseUrl(BASE_URL)
.client(httpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
}
}

@ -0,0 +1,22 @@
package sae.android.sae_2a.service
import retrofit2.Response
import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.Path
import sae.android.sae_2a.data.Group
import sae.android.sae_2a.data.Vocabulary
interface GroupService {
@GET("Group")
suspend fun getVocabulary(@Body index : Int, @Body count : Int) : Response<MutableList<Group>>
@GET("Group/{id}")
suspend fun getVocById(@Path("id") id : Int) : Response<Group>
}

@ -0,0 +1,37 @@
package sae.android.sae_2a.service
import retrofit2.Response
import retrofit2.http.Body
import retrofit2.http.DELETE
import retrofit2.http.GET
import retrofit2.http.POST
import retrofit2.http.PUT
import retrofit2.http.Path
import retrofit2.http.Query
import sae.android.sae_2a.data.Vocabulary
interface UserService {
@GET("Vocabulary")
suspend fun getVocabulary(@Body index : Int, @Body count: Int) : Response<MutableList<Vocabulary>>
@GET("Vocabulary/{word}")
suspend fun getVocByWord(@Path("word") word : String) : Response<Vocabulary>
@PUT("Vocabulary/{vocabulary}")
suspend fun updateVoc(@Path("vocabulary") vocabulary: Vocabulary) : Response<Vocabulary>
@DELETE("Vocabulary/{word}")
suspend fun deleteVoc(@Path("word") word: String) : Response<Vocabulary>
@POST("Vocabulary/{vocabulary}")
suspend fun addVoc(@Path("vocabulary") vocabulary: Vocabulary) : Response<Vocabulary>
@GET("Vocabulary/langue/{langue}")
suspend fun getByLangue(@Path("langue") langue : String,@Body index : Int, @Body count : Int) : Response<Vocabulary>
@POST("Vocabulary/AddTranslation")
suspend fun addTranslation(@Query("vocId") vocId : String,@Query("translationId") translationId : Long ) : Response<Vocabulary>
}

@ -91,7 +91,7 @@ fun VocCard(vocabulary: Vocabulary){
modifier = Modifier
.size(150.dp, 150.dp)
.border(2.dp, Color.DarkGray, shape = RoundedCornerShape(8.dp, 8.dp))) {
Text(vocabulary.name,
Text(vocabulary.word,
modifier = Modifier
.fillMaxWidth()
.border(2.dp, Color.DarkGray, shape = RoundedCornerShape(8.dp, 8.dp)),
@ -104,7 +104,7 @@ fun VocCard(vocabulary: Vocabulary){
.weight(1f)
.align(Alignment.CenterHorizontally)
)
Text( stringResource(id = R.string.created_by) + (vocabulary.aut ?: stringResource(id = R.string.unknown)),
Text( stringResource(id = R.string.created_by) + (vocabulary.word ?: stringResource(id = R.string.unknown)),
modifier = Modifier
.wrapContentHeight(Alignment.Bottom)
.align(Alignment.End)
@ -135,12 +135,12 @@ fun VocabularyDetails(vocabulary: Vocabulary){
stickyHeader(
) {
Text(
vocabulary.name, fontSize = 30.sp,
vocabulary.LangueName, fontSize = 30.sp,
textAlign = TextAlign.Center,
modifier = Modifier.fillMaxWidth()
)
Text(
stringResource(R.string.created_by) + (vocabulary.aut
stringResource(R.string.created_by) + (vocabulary.word
?: stringResource(id = R.string.unknown)),
fontSize = 20.sp,
textAlign = TextAlign.Center,

@ -22,11 +22,15 @@ import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import sae.android.sae_2a.R
import sae.android.sae_2a.VM.LoginViewModel
import sae.android.sae_2a.data.LoginRepository
import sae.android.sae_2a.data.LoginResponseParser
@Composable
fun LoginScreen(onLoginClicked: (String, String) -> Unit) {
var username by remember { mutableStateOf("") }
var password by remember { mutableStateOf("") }
var login = LoginViewModel(LoginRepository(LoginResponseParser())).login("")
Column(
modifier = Modifier

Loading…
Cancel
Save