|
@@ -1,5 +1,7 @@
|
|
|
package com.example.mystictale.Screen.signIn
|
|
|
|
|
|
+import android.util.Patterns
|
|
|
+import android.widget.Toast
|
|
|
import androidx.compose.foundation.Image
|
|
|
import androidx.compose.foundation.clickable
|
|
|
import androidx.compose.foundation.gestures.detectTapGestures
|
|
@@ -18,6 +20,7 @@ import androidx.compose.material3.ButtonColors
|
|
|
import androidx.compose.material3.MaterialTheme
|
|
|
import androidx.compose.material3.Text
|
|
|
import androidx.compose.runtime.Composable
|
|
|
+import androidx.compose.runtime.getValue
|
|
|
import androidx.compose.runtime.mutableStateOf
|
|
|
import androidx.compose.runtime.remember
|
|
|
import androidx.compose.ui.Alignment
|
|
@@ -26,6 +29,7 @@ import androidx.compose.ui.draw.paint
|
|
|
import androidx.compose.ui.graphics.Color
|
|
|
import androidx.compose.ui.input.pointer.pointerInput
|
|
|
import androidx.compose.ui.layout.ContentScale
|
|
|
+import androidx.compose.ui.platform.LocalContext
|
|
|
import androidx.compose.ui.platform.LocalFocusManager
|
|
|
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
|
|
|
import androidx.compose.ui.res.painterResource
|
|
@@ -35,23 +39,37 @@ import androidx.compose.ui.unit.dp
|
|
|
import androidx.compose.ui.unit.sp
|
|
|
import androidx.navigation.NavController
|
|
|
import com.example.mystictale.R
|
|
|
+import com.example.mystictale.ViewModels.AuthViewModel
|
|
|
+import com.example.mystictale.models.UserState
|
|
|
import com.example.mystictale.navigation.Screens
|
|
|
import com.example.mystictale.resources.components.GenericTextField
|
|
|
+import com.example.mystictale.resources.components.Loading
|
|
|
import com.example.mystictale.resources.components.PasswordTextField
|
|
|
import com.example.mystictale.ui.theme.DarkPurple
|
|
|
import com.example.mystictale.ui.theme.Grey
|
|
|
import com.example.mystictale.ui.theme.OpenSans
|
|
|
|
|
|
@Composable
|
|
|
-fun SignIn(navController: NavController) {
|
|
|
+fun SignIn(navController: NavController, viewModel: AuthViewModel) {
|
|
|
val focusManager = LocalFocusManager.current
|
|
|
val keyboardController = LocalSoftwareKeyboardController.current
|
|
|
+ val context = LocalContext.current
|
|
|
val email = remember {
|
|
|
mutableStateOf("")
|
|
|
}
|
|
|
val password = remember {
|
|
|
mutableStateOf("")
|
|
|
}
|
|
|
+ val flagEmail = remember {
|
|
|
+ mutableStateOf(false)
|
|
|
+ }
|
|
|
+ val flagPassword = remember {
|
|
|
+ mutableStateOf(false)
|
|
|
+ }
|
|
|
+ val userState by viewModel.userState
|
|
|
+ val flag = remember {
|
|
|
+ mutableStateOf(false)
|
|
|
+ }
|
|
|
Column(
|
|
|
Modifier
|
|
|
.fillMaxSize()
|
|
@@ -64,8 +82,7 @@ fun SignIn(navController: NavController) {
|
|
|
keyboardController?.hide()
|
|
|
focusManager.clearFocus()
|
|
|
})
|
|
|
- }
|
|
|
- .padding(10.dp),
|
|
|
+ },
|
|
|
verticalArrangement = Arrangement.SpaceBetween,
|
|
|
horizontalAlignment = Alignment.CenterHorizontally
|
|
|
) {
|
|
@@ -96,7 +113,11 @@ fun SignIn(navController: NavController) {
|
|
|
)
|
|
|
}
|
|
|
}
|
|
|
- Column(modifier = Modifier.padding(10.dp)) {
|
|
|
+ Column(
|
|
|
+ modifier = Modifier
|
|
|
+ .padding(20.dp)
|
|
|
+ .padding(start = 15.dp, end = 5.dp)
|
|
|
+ ) {
|
|
|
Spacer(modifier = Modifier.height(30.dp))
|
|
|
Text(
|
|
|
text = "Авторизация",
|
|
@@ -105,21 +126,59 @@ fun SignIn(navController: NavController) {
|
|
|
fontSize = 24.sp,
|
|
|
color = Color.White,
|
|
|
modifier = Modifier
|
|
|
- .fillMaxWidth(0.9f)
|
|
|
- .padding(start = 35.dp),
|
|
|
+ .fillMaxWidth(),
|
|
|
style = MaterialTheme.typography.headlineMedium
|
|
|
)
|
|
|
Spacer(modifier = Modifier.height(30.dp))
|
|
|
|
|
|
GenericTextField(
|
|
|
email.value,
|
|
|
- { email.value = it },
|
|
|
+ {
|
|
|
+ email.value = it
|
|
|
+ flagEmail.value = false
|
|
|
+ },
|
|
|
"E-mail",
|
|
|
KeyboardType.Email,
|
|
|
- false
|
|
|
+ flagEmail.value
|
|
|
)
|
|
|
+ if (flagEmail.value) {
|
|
|
+ if (email.value.isEmpty()) {
|
|
|
+ Text(
|
|
|
+ "Введите почту",
|
|
|
+ fontWeight = FontWeight.Light,
|
|
|
+ fontFamily = OpenSans,
|
|
|
+ fontSize = 13.sp,
|
|
|
+ color = Color.Red
|
|
|
+ )
|
|
|
+ } else if (!Patterns.EMAIL_ADDRESS.matcher(email.value).matches()) {
|
|
|
+ Text(
|
|
|
+ "Неверная почта",
|
|
|
+ fontWeight = FontWeight.Light,
|
|
|
+ fontFamily = OpenSans,
|
|
|
+ fontSize = 13.sp,
|
|
|
+ color = Color.Red
|
|
|
+ )
|
|
|
+ }
|
|
|
+ }
|
|
|
Spacer(modifier = Modifier.height(30.dp))
|
|
|
- PasswordTextField(password.value, { password.value = it }, "Пароль",false)
|
|
|
+ PasswordTextField(
|
|
|
+ password.value,
|
|
|
+ {
|
|
|
+ password.value = it
|
|
|
+ flagPassword.value = false
|
|
|
+ },
|
|
|
+ "Пароль",
|
|
|
+ flagPassword.value
|
|
|
+ )
|
|
|
+ if (flagPassword.value) {
|
|
|
+ Text(
|
|
|
+ "Введите пароль",
|
|
|
+ fontWeight = FontWeight.Light,
|
|
|
+ fontFamily = OpenSans,
|
|
|
+ fontSize = 13.sp,
|
|
|
+ color = Color.Red
|
|
|
+ )
|
|
|
+ }
|
|
|
Spacer(modifier = Modifier.height(30.dp))
|
|
|
Text(
|
|
|
"Забыли пароль?",
|
|
@@ -128,7 +187,6 @@ fun SignIn(navController: NavController) {
|
|
|
fontWeight = FontWeight.Light,
|
|
|
color = Grey,
|
|
|
modifier = Modifier
|
|
|
- .padding(start = 35.dp)
|
|
|
.clickable { navController.navigate(Screens.PasswordRecovery.route) }
|
|
|
)
|
|
|
}
|
|
@@ -136,10 +194,31 @@ fun SignIn(navController: NavController) {
|
|
|
|
|
|
Box(Modifier.padding(bottom = 50.dp)) {
|
|
|
Button(
|
|
|
- onClick = { },
|
|
|
+ onClick = {
|
|
|
+ if (!Patterns.EMAIL_ADDRESS.matcher(email.value)
|
|
|
+ .matches() || email.value.isEmpty()
|
|
|
+ ) {
|
|
|
+ flagEmail.value = true
|
|
|
+ }
|
|
|
+ if (password.value.isEmpty()) {
|
|
|
+ flagPassword.value = true
|
|
|
+ }
|
|
|
+ if (Patterns.EMAIL_ADDRESS.matcher(email.value)
|
|
|
+ .matches() && email.value.isNotEmpty() && password.value.isNotEmpty()
|
|
|
+ ) {
|
|
|
+ viewModel.onSignInEmailPassword(
|
|
|
+ emailUser = email.value,
|
|
|
+ passwordUser = password.value,
|
|
|
+ context = context
|
|
|
+ )
|
|
|
+ flag.value = true
|
|
|
+
|
|
|
+ }
|
|
|
+ },
|
|
|
modifier = Modifier
|
|
|
- .width(310.dp)
|
|
|
- .height(48.dp),
|
|
|
+ .fillMaxWidth()
|
|
|
+ .height(48.dp)
|
|
|
+ .padding(horizontal = 30.dp),
|
|
|
shape = RoundedCornerShape(12.dp),
|
|
|
colors = ButtonColors(
|
|
|
containerColor = DarkPurple,
|
|
@@ -156,4 +235,23 @@ fun SignIn(navController: NavController) {
|
|
|
|
|
|
|
|
|
}
|
|
|
+ if (flag.value) {
|
|
|
+ when (userState) {
|
|
|
+ is UserState.Loading -> {
|
|
|
+ Loading()
|
|
|
+ }
|
|
|
+
|
|
|
+ is UserState.Success -> {
|
|
|
+ navController.navigate(Screens.Start.route)
|
|
|
+ flag.value = false
|
|
|
+ }
|
|
|
+
|
|
|
+ is UserState.Error -> {
|
|
|
+ val message = (userState as UserState.Error).message
|
|
|
+ Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
|
|
|
+ flag.value = false
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|