main #6
11
app/src/main/java/ru/myitschool/work/core/Utils.kt
Normal file
11
app/src/main/java/ru/myitschool/work/core/Utils.kt
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package ru.myitschool.work.core
|
||||||
|
|
||||||
|
import ru.myitschool.work.core.OurConstants.SHABLON
|
||||||
|
|
||||||
|
class Utils {
|
||||||
|
companion object {
|
||||||
|
fun CheckCodeInput(text : String) : Boolean{
|
||||||
|
return !text.isEmpty() && text.length == 4 && text.matches(Regex(SHABLON))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,6 +2,6 @@ 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
|
data class LogIn(val isLogged: Boolean): AuthAction
|
||||||
|
data class AuthBtnEnabled(val enabled: Boolean) : AuthAction
|
||||||
}
|
}
|
||||||
@@ -34,6 +34,7 @@ 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
|
||||||
|
import ru.myitschool.work.core.Utils
|
||||||
import ru.myitschool.work.ui.nav.MainScreenDestination
|
import ru.myitschool.work.ui.nav.MainScreenDestination
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
@@ -50,9 +51,11 @@ fun AuthScreen(
|
|||||||
|
|
||||||
LaunchedEffect(event.value) {
|
LaunchedEffect(event.value) {
|
||||||
if (event.value is AuthAction.LogIn) {
|
if (event.value is AuthAction.LogIn) {
|
||||||
|
if ((event.value as AuthAction.LogIn).isLogged) {
|
||||||
navController.navigate(MainScreenDestination)
|
navController.navigate(MainScreenDestination)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
Log.d("AnnaKonda", state.javaClass.toString())
|
Log.d("AnnaKonda", state.javaClass.toString())
|
||||||
Column(
|
Column(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
@@ -88,12 +91,16 @@ 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) }
|
||||||
|
var btnEnabled: Boolean by remember { mutableStateOf(false) }
|
||||||
|
|
||||||
val event = viewModel.actionFlow.collectAsState(initial = null)
|
val event = viewModel.actionFlow.collectAsState(initial = null)
|
||||||
|
|
||||||
LaunchedEffect(event.value) {
|
LaunchedEffect(event.value) {
|
||||||
if (event.value is AuthAction.ShowError) {
|
if (event.value is AuthAction.ShowError) {
|
||||||
errorText = (event.value as AuthAction.ShowError).message
|
errorText = (event.value as AuthAction.ShowError).message
|
||||||
|
} else if (event.value is AuthAction.AuthBtnEnabled){
|
||||||
|
Log.d("AnnaKonda", btnEnabled.toString())
|
||||||
|
btnEnabled = if ((event.value as AuthAction.AuthBtnEnabled).enabled){ true } else { false }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,13 +122,13 @@ private fun Content(
|
|||||||
.testTag(TestIds.Auth.SIGN_BUTTON)
|
.testTag(TestIds.Auth.SIGN_BUTTON)
|
||||||
.fillMaxWidth(),
|
.fillMaxWidth(),
|
||||||
onClick = {
|
onClick = {
|
||||||
if (!inputText.isEmpty() && inputText.length == 4 && inputText.matches(Regex(SHABLON))) {
|
if (Utils.CheckCodeInput(inputText)) {
|
||||||
viewModel.onIntent(AuthIntent.Send(inputText))
|
viewModel.onIntent(AuthIntent.Send(inputText))
|
||||||
} else {
|
} else {
|
||||||
errorText = App.context.getString(R.string.auth_nasty_code)
|
errorText = App.context.getString(R.string.auth_nasty_code)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
enabled = true
|
enabled = btnEnabled
|
||||||
|
|
||||||
) { Text(stringResource(R.string.auth_sign_in)) }
|
) { Text(stringResource(R.string.auth_sign_in)) }
|
||||||
if (errorText != null) {
|
if (errorText != null) {
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import kotlinx.coroutines.flow.update
|
|||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import ru.myitschool.work.App
|
import ru.myitschool.work.App
|
||||||
import ru.myitschool.work.R
|
import ru.myitschool.work.R
|
||||||
|
import ru.myitschool.work.core.Utils.Companion.CheckCodeInput
|
||||||
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
|
||||||
@@ -46,14 +47,24 @@ class AuthViewModel : ViewModel() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
is AuthIntent.TextInput -> Unit
|
is AuthIntent.TextInput -> {
|
||||||
|
viewModelScope.launch {
|
||||||
|
authFlow().collect {
|
||||||
|
if (CheckCodeInput(intent.text)) {
|
||||||
|
_actionFlow.emit(AuthAction.AuthBtnEnabled(true))
|
||||||
|
} else {
|
||||||
|
_actionFlow.emit(AuthAction.AuthBtnEnabled(false))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
is AuthIntent.CheckLogIntent -> {
|
is AuthIntent.CheckLogIntent -> {
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
_uiState.update { AuthState.Loading }
|
_uiState.update { AuthState.Loading }
|
||||||
authFlow().collect {
|
authFlow().collect {
|
||||||
Log.d("AnnaKonda", it)
|
Log.d("AnnaKonda", it)
|
||||||
if (it != "0") {
|
if (it != "0") {
|
||||||
_actionFlow.emit(AuthAction.LogIn)
|
_actionFlow.emit(AuthAction.LogIn(true))
|
||||||
_uiState.update { AuthState.LoggedIn }
|
_uiState.update { AuthState.LoggedIn }
|
||||||
} else {
|
} else {
|
||||||
_actionFlow.emit(AuthAction.ShowError(App.context.getString(R.string.auth_wrong_code)))
|
_actionFlow.emit(AuthAction.ShowError(App.context.getString(R.string.auth_wrong_code)))
|
||||||
|
|||||||
Reference in New Issue
Block a user