Merge branch 'android' of https://codefirst.iut.uca.fr/git/antoine.jourdain/SAE_2A_Anglais into android
commit
e8bf8156c5
@ -0,0 +1,28 @@
|
||||
package sae.android.sae_2a.VM
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
|
||||
import kotlinx.coroutines.launch
|
||||
import sae.android.sae_2a.data.LoginRepository
|
||||
import sae.android.sae_2a.data.LoginResponse
|
||||
|
||||
class LoginViewModel(
|
||||
private val loginRepository: LoginRepository
|
||||
): ViewModel() {
|
||||
|
||||
fun login( token: String) {
|
||||
viewModelScope.launch {
|
||||
val jsonBody = "{ token: \"$token\"}"
|
||||
val result = try {
|
||||
loginRepository.makeLoginRequest(jsonBody)
|
||||
} catch(e: Exception) {
|
||||
sae.android.sae_2a.data.Result.Error(Exception("Network request failed"))
|
||||
}
|
||||
when (result) {
|
||||
is sae.android.sae_2a.data.Result.Success<LoginResponse> -> print("sucess")
|
||||
else -> ("error")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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
|
||||
)
|
@ -0,0 +1,47 @@
|
||||
package sae.android.sae_2a.data
|
||||
|
||||
import android.provider.ContactsContract.CommonDataKinds.Website.URL
|
||||
import org.json.JSONObject
|
||||
import java.io.InputStream
|
||||
import java.net.HttpURLConnection
|
||||
import java.net.URL
|
||||
|
||||
class LoginResponseParser {
|
||||
fun parse(inputStream: InputStream): LoginResponse {
|
||||
val responseString = inputStream.bufferedReader().use { it.readText() }
|
||||
// Supposons que la réponse du serveur soit un JSON contenant une clé "token"
|
||||
val jsonObject = JSONObject(responseString)
|
||||
val token = jsonObject.getString("token")
|
||||
return LoginResponse(token)
|
||||
}
|
||||
}
|
||||
|
||||
data class LoginResponse(
|
||||
val token: String
|
||||
)
|
||||
|
||||
sealed class Result<out R> {
|
||||
data class Success<out T>(val data: T) : Result<T>()
|
||||
data class Error(val exception: Exception) : Result<Nothing>()
|
||||
}
|
||||
class LoginRepository(private val responseParser: LoginResponseParser) {
|
||||
|
||||
private val loginUrl = "https://codefirst.iut.uca.fr/containers/antoinejourdain-api_container/Auth/token"
|
||||
|
||||
// Function that makes the network request, blocking the current thread
|
||||
fun makeLoginRequest(
|
||||
jsonBody: String
|
||||
): Result<LoginResponse> {
|
||||
val url = URL(loginUrl)
|
||||
(url.openConnection() as? HttpURLConnection)?.run {
|
||||
requestMethod = "POST"
|
||||
setRequestProperty("Content-Type", "application/json; utf-8")
|
||||
setRequestProperty("Accept", "application/json")
|
||||
doOutput = true
|
||||
outputStream.write(jsonBody.toByteArray())
|
||||
return Result.Success(responseParser.parse(inputStream))
|
||||
}
|
||||
return Result.Error(Exception("Cannot open HttpURLConnection"))
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package sae.android.sae_2a.data
|
||||
|
||||
import androidx.annotation.StringRes
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.AccountBox
|
||||
import androidx.compose.material.icons.filled.Home
|
||||
import androidx.compose.material.icons.filled.Menu
|
||||
import androidx.compose.ui.graphics.vector.ImageVector
|
||||
import sae.android.sae_2a.R
|
||||
|
||||
sealed class Screen(val route: String, @StringRes val resourceId: Int, val image: ImageVector) {
|
||||
object Profile : Screen("profile", R.string.profilePicture, Icons.Filled.AccountBox )
|
||||
object Home : Screen("profileScreen", R.string.home, Icons.Filled.Home )
|
||||
object VocabularyList : Screen("VocabularyListScreen", R.string.voc_image_description, Icons.Filled.Menu)
|
||||
object Game : Screen("GameScreen", R.string.game ,Icons.Filled.Home)
|
||||
object Register : Screen("RegisterScreen", R.string.register,Icons.Filled.Home )
|
||||
object Login : Screen("LoginScreen", R.string.logIn , Icons.Filled.Home)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
@ -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>
|
||||
|
||||
}
|
Loading…
Reference in new issue