Correction TP 6 - P1
This commit is contained in:
@@ -21,11 +21,6 @@
|
|||||||
android:exported="true"
|
android:exported="true"
|
||||||
android:label="@string/title_activity_list_article"
|
android:label="@string/title_activity_list_article"
|
||||||
android:theme="@style/Theme.TpFilRouge">
|
android:theme="@style/Theme.TpFilRouge">
|
||||||
<intent-filter>
|
|
||||||
<action android:name="android.intent.action.MAIN" />
|
|
||||||
|
|
||||||
<category android:name="android.intent.category.LAUNCHER" />
|
|
||||||
</intent-filter>
|
|
||||||
</activity>
|
</activity>
|
||||||
<activity
|
<activity
|
||||||
android:name=".auth.ResetPasswordActivity"
|
android:name=".auth.ResetPasswordActivity"
|
||||||
@@ -42,6 +37,11 @@
|
|||||||
android:exported="true"
|
android:exported="true"
|
||||||
android:label="@string/app_name"
|
android:label="@string/app_name"
|
||||||
android:theme="@style/Theme.TpFilRouge" >
|
android:theme="@style/Theme.TpFilRouge" >
|
||||||
|
<intent-filter>
|
||||||
|
<action android:name="android.intent.action.MAIN" />
|
||||||
|
|
||||||
|
<category android:name="android.intent.category.LAUNCHER" />
|
||||||
|
</intent-filter>
|
||||||
</activity>
|
</activity>
|
||||||
</application>
|
</application>
|
||||||
|
|
||||||
|
|||||||
19
app/src/main/java/com/example/tpfilrouge/auth/AuthService.kt
Normal file
19
app/src/main/java/com/example/tpfilrouge/auth/AuthService.kt
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package com.example.tpfilrouge.auth
|
||||||
|
|
||||||
|
import com.example.tpfilrouge.api.RetrofitTools.Companion.retrofit
|
||||||
|
import com.example.tpfilrouge.api.ServiceResponseDTO
|
||||||
|
import com.example.tpfilrouge.article.Article
|
||||||
|
import retrofit2.http.Body
|
||||||
|
import retrofit2.http.GET
|
||||||
|
import retrofit2.http.POST
|
||||||
|
|
||||||
|
interface AuthService {
|
||||||
|
|
||||||
|
@POST("login")
|
||||||
|
suspend fun login(@Body loginRequestDTO: LoginRequestDTO) : ServiceResponseDTO<String>
|
||||||
|
|
||||||
|
object AuthApi {
|
||||||
|
val authService : AuthService by lazy { retrofit.create(AuthService::class.java) }
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -11,6 +11,8 @@ import androidx.compose.foundation.layout.Spacer
|
|||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.collectAsState
|
||||||
|
import androidx.compose.runtime.getValue
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.graphics.Color
|
import androidx.compose.ui.graphics.Color
|
||||||
@@ -22,22 +24,29 @@ import androidx.compose.ui.text.style.TextAlign
|
|||||||
import androidx.compose.ui.tooling.preview.Preview
|
import androidx.compose.ui.tooling.preview.Preview
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import com.example.tpfilrouge.R
|
import com.example.tpfilrouge.R
|
||||||
|
import com.example.tpfilrouge.article.ListArticleActivity
|
||||||
import com.example.tpfilrouge.ui.theme.EniButton
|
import com.example.tpfilrouge.ui.theme.EniButton
|
||||||
import com.example.tpfilrouge.ui.theme.EniPage
|
import com.example.tpfilrouge.ui.theme.EniPage
|
||||||
import com.example.tpfilrouge.ui.theme.EniTextField
|
import com.example.tpfilrouge.ui.theme.EniTextField
|
||||||
|
|
||||||
class LoginActivity : ComponentActivity() {
|
class LoginActivity : ComponentActivity() {
|
||||||
|
|
||||||
|
var loginViewModel = LoginViewModel();
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
enableEdgeToEdge()
|
enableEdgeToEdge()
|
||||||
setContent {
|
setContent {
|
||||||
LoginActivityPage()
|
LoginActivityPage(loginViewModel);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun LoginActivityPage() {
|
fun LoginActivityPage(loginViewModel: LoginViewModel) {
|
||||||
|
// Ecouter les changements de l'email et mot de passe saisies
|
||||||
|
val emailField by loginViewModel.email.collectAsState()
|
||||||
|
val passwordField by loginViewModel.password.collectAsState()
|
||||||
|
|
||||||
val context = LocalContext.current;
|
val context = LocalContext.current;
|
||||||
|
|
||||||
@@ -57,9 +66,14 @@ fun LoginActivityPage() {
|
|||||||
textAlign = TextAlign.Center,
|
textAlign = TextAlign.Center,
|
||||||
style = TextStyle(color = Color(0xCCFFFFFF))
|
style = TextStyle(color = Color(0xCCFFFFFF))
|
||||||
)
|
)
|
||||||
EniTextField(stringResource(R.string.email))
|
EniTextField(stringResource(R.string.email), value = emailField, onValueChange = { value -> loginViewModel.email.value = value; })
|
||||||
EniTextField(stringResource(R.string.password))
|
EniTextField(stringResource(R.string.password), value = passwordField, onValueChange = { value -> loginViewModel.password.value = value; })
|
||||||
EniButton(stringResource(R.string.connection), onClick = {})
|
EniButton(stringResource(R.string.connection), onClick = { loginViewModel.callLoginApi(
|
||||||
|
onSuccess = {
|
||||||
|
// je change de page (page article
|
||||||
|
context.startActivity(Intent(context, ListArticleActivity::class.java))
|
||||||
|
}
|
||||||
|
) })
|
||||||
EniButton(stringResource(R.string.forget_password),
|
EniButton(stringResource(R.string.forget_password),
|
||||||
onClick = {
|
onClick = {
|
||||||
// Changer de page (d'activity)
|
// Changer de page (d'activity)
|
||||||
@@ -88,5 +102,5 @@ fun LoginActivityPage() {
|
|||||||
)
|
)
|
||||||
@Composable
|
@Composable
|
||||||
fun LoginActivityPreview() {
|
fun LoginActivityPreview() {
|
||||||
LoginActivityPage()
|
LoginActivityPage(LoginViewModel())
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
package com.example.tpfilrouge.auth
|
||||||
|
|
||||||
|
data class LoginRequestDTO(var email: String, var password : String) {
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
package com.example.tpfilrouge.auth
|
||||||
|
|
||||||
|
import androidx.lifecycle.ViewModel
|
||||||
|
import androidx.lifecycle.viewModelScope
|
||||||
|
import com.example.tpfilrouge.helpers.AppDialogHelpers
|
||||||
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
|
class LoginViewModel : ViewModel() {
|
||||||
|
|
||||||
|
// Ecouter la saisie de email et password
|
||||||
|
var email = MutableStateFlow("isaac@gmail.com")
|
||||||
|
var password = MutableStateFlow("password")
|
||||||
|
|
||||||
|
fun callLoginApi(onSuccess : () -> Unit = {}){
|
||||||
|
AppDialogHelpers.get().showDialog("Tentive de connexion...");
|
||||||
|
|
||||||
|
viewModelScope.launch {
|
||||||
|
// Preparer le DTO à envoyer dans request body
|
||||||
|
// Le DTO qui contient : email et password
|
||||||
|
val loginRequest = LoginRequestDTO(email.value, password.value);
|
||||||
|
|
||||||
|
// Appel api async
|
||||||
|
val responseService = AuthService.AuthApi.authService.login(loginRequest);
|
||||||
|
|
||||||
|
// Fermer la popup de chargement aprés l'appel de API
|
||||||
|
AppDialogHelpers.get().closeDialog()
|
||||||
|
|
||||||
|
// Si connexion ok
|
||||||
|
if (responseService.code.equals("200")){
|
||||||
|
// Je notifie que c'est ok
|
||||||
|
println("Connexion OK")
|
||||||
|
// Note: J'appel la fonction que j'envoie en parametre de la fonction
|
||||||
|
onSuccess();
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
// Erreur
|
||||||
|
println("Connexion échouée")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user