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