|
@@ -1,40 +1,32 @@
|
|
|
package com.example.fitmarcetplacekuzminiv.Screens
|
|
|
|
|
|
-import android.os.Bundle
|
|
|
-import androidx.activity.ComponentActivity
|
|
|
-import androidx.activity.compose.setContent
|
|
|
-import androidx.activity.viewModels
|
|
|
import androidx.compose.foundation.layout.*
|
|
|
import androidx.compose.material3.*
|
|
|
import androidx.compose.runtime.*
|
|
|
import androidx.compose.ui.Alignment
|
|
|
import androidx.compose.ui.Modifier
|
|
|
-import androidx.compose.ui.tooling.preview.Preview
|
|
|
-import androidx.compose.ui.unit.dp
|
|
|
import androidx.compose.ui.text.input.PasswordVisualTransformation
|
|
|
import androidx.compose.ui.text.input.TextFieldValue
|
|
|
-import androidx.lifecycle.viewmodel.compose.viewModel
|
|
|
+import androidx.compose.ui.unit.dp
|
|
|
+import androidx.navigation.NavController
|
|
|
import com.example.fitmarcetplacekuzminiv.MainViewModel
|
|
|
+//Авторизация
|
|
|
+@Composable
|
|
|
+fun AuthScreen(navController: NavController, mainViewModel: MainViewModel) {
|
|
|
+ val email = remember { mutableStateOf("") }
|
|
|
+ val password = remember { mutableStateOf("") }
|
|
|
|
|
|
-class AuthScreenActivity : ComponentActivity() {
|
|
|
-
|
|
|
- // Инициализируем ViewModel
|
|
|
- private val mainViewModel: MainViewModel by viewModels()
|
|
|
+ // Слушаем результат авторизации
|
|
|
+ val authResult by mainViewModel.authResult
|
|
|
|
|
|
- override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
- super.onCreate(savedInstanceState)
|
|
|
- setContent {
|
|
|
- AuthScreen(mainViewModel)
|
|
|
+ // Если авторизация прошла успешно, переходим на main_screen
|
|
|
+ LaunchedEffect(authResult) {
|
|
|
+ if (authResult == "Success") {
|
|
|
+ navController.navigate("main_screen") {
|
|
|
+ popUpTo("auth_screen") { inclusive = true }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
-}
|
|
|
-
|
|
|
-@Composable
|
|
|
-fun AuthScreen(mainViewModel: MainViewModel = viewModel()) {
|
|
|
- var email by remember { mutableStateOf(TextFieldValue("")) }
|
|
|
- var password by remember { mutableStateOf(TextFieldValue("")) }
|
|
|
- var errorMessage by remember { mutableStateOf("") }
|
|
|
- var successMessage by remember { mutableStateOf("") }
|
|
|
|
|
|
Box(
|
|
|
modifier = Modifier.fillMaxSize(),
|
|
@@ -48,47 +40,31 @@ fun AuthScreen(mainViewModel: MainViewModel = viewModel()) {
|
|
|
.padding(16.dp)
|
|
|
) {
|
|
|
OutlinedTextField(
|
|
|
- value = email,
|
|
|
- onValueChange = { email = it },
|
|
|
+ value = email.value,
|
|
|
+ onValueChange = { newText -> email.value = newText },
|
|
|
label = { Text("Email") },
|
|
|
modifier = Modifier.fillMaxWidth()
|
|
|
)
|
|
|
Spacer(modifier = Modifier.height(16.dp))
|
|
|
OutlinedTextField(
|
|
|
- value = password,
|
|
|
- onValueChange = { password = it },
|
|
|
+ value = password.value,
|
|
|
+ onValueChange = { newText -> password.value = newText },
|
|
|
label = { Text("Password") },
|
|
|
visualTransformation = PasswordVisualTransformation(),
|
|
|
modifier = Modifier.fillMaxWidth()
|
|
|
)
|
|
|
Spacer(modifier = Modifier.height(24.dp))
|
|
|
Button(onClick = {
|
|
|
- // Вызов авторизации через email и пароль
|
|
|
- if (email.text.isNotEmpty() && password.text.isNotEmpty()) {
|
|
|
- mainViewModel.onSignInEmailPassword(email.text, password.text)
|
|
|
- } else {
|
|
|
- errorMessage = "Заполните все поля"
|
|
|
- }
|
|
|
+ mainViewModel.onSignInEmailPassword(email.value, password.value)
|
|
|
}) {
|
|
|
Text("Login")
|
|
|
}
|
|
|
|
|
|
- // Отображение ошибок или успешного входа
|
|
|
- if (errorMessage.isNotEmpty()) {
|
|
|
- Spacer(modifier = Modifier.height(16.dp))
|
|
|
- Text(text = errorMessage, color = MaterialTheme.colorScheme.error)
|
|
|
- }
|
|
|
-
|
|
|
- if (successMessage.isNotEmpty()) {
|
|
|
+ // Отображаем сообщение об ошибке, если авторизация не удалась
|
|
|
+ if (authResult == "Error") {
|
|
|
Spacer(modifier = Modifier.height(16.dp))
|
|
|
- Text(text = successMessage, color = MaterialTheme.colorScheme.primary)
|
|
|
+ Text(text = "Ошибка: неверный email или пароль", color = MaterialTheme.colorScheme.error)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
-@Preview(showBackground = true)
|
|
|
-@Composable
|
|
|
-fun PreviewAuthScreen() {
|
|
|
- AuthScreen()
|
|
|
-}
|