main #6
@@ -1,6 +1,7 @@
|
|||||||
package ru.myitschool.work.ui.screen.auth
|
package ru.myitschool.work.ui.screen.auth
|
||||||
|
|
||||||
sealed interface AuthAction {
|
sealed interface AuthAction {
|
||||||
data class ShowError(val message: String) : AuthAction
|
data class ShowError(val message: String?) : AuthAction
|
||||||
|
object LogIn : AuthAction
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package ru.myitschool.work.ui.screen.auth
|
package ru.myitschool.work.ui.screen.auth
|
||||||
|
|
||||||
|
import android.util.Log
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
import androidx.compose.foundation.layout.Column
|
import androidx.compose.foundation.layout.Column
|
||||||
import androidx.compose.foundation.layout.Spacer
|
import androidx.compose.foundation.layout.Spacer
|
||||||
@@ -28,6 +29,8 @@ import androidx.compose.ui.text.style.TextAlign
|
|||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||||
import androidx.navigation.NavController
|
import androidx.navigation.NavController
|
||||||
|
import io.ktor.util.collections.setValue
|
||||||
|
import ru.myitschool.work.App
|
||||||
import ru.myitschool.work.R
|
import ru.myitschool.work.R
|
||||||
import ru.myitschool.work.core.OurConstants.SHABLON
|
import ru.myitschool.work.core.OurConstants.SHABLON
|
||||||
import ru.myitschool.work.core.TestIds
|
import ru.myitschool.work.core.TestIds
|
||||||
@@ -39,13 +42,18 @@ fun AuthScreen(
|
|||||||
navController: NavController
|
navController: NavController
|
||||||
) {
|
) {
|
||||||
val state by viewModel.uiState.collectAsState()
|
val state by viewModel.uiState.collectAsState()
|
||||||
viewModel.onIntent(AuthIntent.CheckLogIntent)
|
|
||||||
|
|
||||||
LaunchedEffect(Unit) {
|
LaunchedEffect(Unit) {
|
||||||
viewModel.actionFlow.collect {
|
viewModel.onIntent(AuthIntent.CheckLogIntent)
|
||||||
|
}
|
||||||
|
|
||||||
|
val event = viewModel.actionFlow.collectAsState(initial = null)
|
||||||
|
|
||||||
|
LaunchedEffect(event.value) {
|
||||||
|
if (event.value is AuthAction.LogIn) {
|
||||||
navController.navigate(MainScreenDestination)
|
navController.navigate(MainScreenDestination)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Log.d("AnnaKonda", state.javaClass.toString())
|
||||||
Column(
|
Column(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxSize()
|
.fillMaxSize()
|
||||||
@@ -65,6 +73,7 @@ fun AuthScreen(
|
|||||||
modifier = Modifier.size(64.dp)
|
modifier = Modifier.size(64.dp)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
is AuthState.LoggedIn -> {
|
is AuthState.LoggedIn -> {
|
||||||
navController.navigate(MainScreenDestination)
|
navController.navigate(MainScreenDestination)
|
||||||
}
|
}
|
||||||
@@ -79,9 +88,20 @@ private fun Content(
|
|||||||
) {
|
) {
|
||||||
var inputText by remember { mutableStateOf("") }
|
var inputText by remember { mutableStateOf("") }
|
||||||
var errorText: String? by remember { mutableStateOf(null) }
|
var errorText: String? by remember { mutableStateOf(null) }
|
||||||
|
|
||||||
|
val event = viewModel.actionFlow.collectAsState(initial = null)
|
||||||
|
|
||||||
|
LaunchedEffect(event.value) {
|
||||||
|
if (event.value is AuthAction.ShowError) {
|
||||||
|
errorText = (event.value as AuthAction.ShowError).message
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Spacer(modifier = Modifier.size(16.dp))
|
Spacer(modifier = Modifier.size(16.dp))
|
||||||
TextField(
|
TextField(
|
||||||
modifier = Modifier.testTag(TestIds.Auth.CODE_INPUT).fillMaxWidth(),
|
modifier = Modifier
|
||||||
|
.testTag(TestIds.Auth.CODE_INPUT)
|
||||||
|
.fillMaxWidth(),
|
||||||
value = inputText,
|
value = inputText,
|
||||||
onValueChange = {
|
onValueChange = {
|
||||||
inputText = it
|
inputText = it
|
||||||
@@ -91,22 +111,20 @@ private fun Content(
|
|||||||
)
|
)
|
||||||
Spacer(modifier = Modifier.size(16.dp))
|
Spacer(modifier = Modifier.size(16.dp))
|
||||||
Button(
|
Button(
|
||||||
modifier = Modifier.testTag(TestIds.Auth.SIGN_BUTTON).fillMaxWidth(),
|
modifier = Modifier
|
||||||
|
.testTag(TestIds.Auth.SIGN_BUTTON)
|
||||||
|
.fillMaxWidth(),
|
||||||
onClick = {
|
onClick = {
|
||||||
if (!inputText.isEmpty() && inputText.length == 4 && inputText.matches(Regex(SHABLON))) {
|
if (!inputText.isEmpty() && inputText.length == 4 && inputText.matches(Regex(SHABLON))) {
|
||||||
viewModel.onIntent(AuthIntent.Send(inputText))
|
viewModel.onIntent(AuthIntent.Send(inputText))
|
||||||
|
} else {
|
||||||
|
errorText = App.context.getString(R.string.auth_nasty_code)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
enabled = true
|
enabled = true
|
||||||
|
|
||||||
) { Text(stringResource(R.string.auth_sign_in)) }
|
) { Text(stringResource(R.string.auth_sign_in)) }
|
||||||
ShowError(errorText) // TODO: раскидать в коде когда показывается ошибка и когда нет
|
if (errorText != null) {
|
||||||
|
Text(errorText.toString(), modifier = Modifier.testTag(TestIds.Auth.ERROR))
|
||||||
}
|
|
||||||
|
|
||||||
@Composable
|
|
||||||
fun ShowError(text : String?){
|
|
||||||
if (text != null){
|
|
||||||
Text(text, modifier = Modifier.testTag(TestIds.Auth.ERROR))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package ru.myitschool.work.ui.screen.auth
|
package ru.myitschool.work.ui.screen.auth
|
||||||
|
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
|
import androidx.compose.ui.res.stringResource
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.viewModelScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
@@ -11,6 +12,8 @@ import kotlinx.coroutines.flow.StateFlow
|
|||||||
import kotlinx.coroutines.flow.asStateFlow
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
import kotlinx.coroutines.flow.update
|
import kotlinx.coroutines.flow.update
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import ru.myitschool.work.App
|
||||||
|
import ru.myitschool.work.R
|
||||||
import ru.myitschool.work.data.repo.AuthRepository
|
import ru.myitschool.work.data.repo.AuthRepository
|
||||||
import ru.myitschool.work.data.source.DataStoreDataSource.authFlow
|
import ru.myitschool.work.data.source.DataStoreDataSource.authFlow
|
||||||
import ru.myitschool.work.domain.auth.CheckAndSaveAuthCodeUseCase
|
import ru.myitschool.work.domain.auth.CheckAndSaveAuthCodeUseCase
|
||||||
@@ -37,10 +40,12 @@ class AuthViewModel : ViewModel() {
|
|||||||
if (error.message != null) {
|
if (error.message != null) {
|
||||||
_actionFlow.emit(AuthAction.ShowError(error.message.toString()))
|
_actionFlow.emit(AuthAction.ShowError(error.message.toString()))
|
||||||
}
|
}
|
||||||
|
_uiState.update { AuthState.Data }
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
is AuthIntent.TextInput -> Unit
|
is AuthIntent.TextInput -> Unit
|
||||||
is AuthIntent.CheckLogIntent -> {
|
is AuthIntent.CheckLogIntent -> {
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
@@ -48,8 +53,10 @@ class AuthViewModel : ViewModel() {
|
|||||||
authFlow().collect {
|
authFlow().collect {
|
||||||
Log.d("AnnaKonda", it)
|
Log.d("AnnaKonda", it)
|
||||||
if (it != "0") {
|
if (it != "0") {
|
||||||
|
_actionFlow.emit(AuthAction.LogIn)
|
||||||
_uiState.update { AuthState.LoggedIn }
|
_uiState.update { AuthState.LoggedIn }
|
||||||
} else {
|
} else {
|
||||||
|
_actionFlow.emit(AuthAction.ShowError(App.context.getString(R.string.auth_wrong_code)))
|
||||||
_uiState.update { AuthState.Data }
|
_uiState.update { AuthState.Data }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -57,5 +64,4 @@ class AuthViewModel : ViewModel() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -4,4 +4,6 @@
|
|||||||
<string name="auth_title">Привет! Введи код для авторизации</string>
|
<string name="auth_title">Привет! Введи код для авторизации</string>
|
||||||
<string name="auth_label">Код</string>
|
<string name="auth_label">Код</string>
|
||||||
<string name="auth_sign_in">Войти</string>
|
<string name="auth_sign_in">Войти</string>
|
||||||
|
<string name="auth_wrong_code">Введён неверный код</string>
|
||||||
|
<string name="auth_nasty_code">Неправильный формат кода</string>
|
||||||
</resources>
|
</resources>
|
||||||
Reference in New Issue
Block a user