auth without server done

This commit is contained in:
2025-11-28 08:27:06 +03:00
parent ad113e0438
commit 8fa01383d4
4 changed files with 36 additions and 7 deletions

View 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))
}
}
}

View File

@@ -2,6 +2,6 @@ package ru.myitschool.work.ui.screen.auth
sealed interface 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
}

View File

@@ -34,6 +34,7 @@ import ru.myitschool.work.App
import ru.myitschool.work.R
import ru.myitschool.work.core.OurConstants.SHABLON
import ru.myitschool.work.core.TestIds
import ru.myitschool.work.core.Utils
import ru.myitschool.work.ui.nav.MainScreenDestination
@Composable
@@ -50,7 +51,9 @@ fun AuthScreen(
LaunchedEffect(event.value) {
if (event.value is AuthAction.LogIn) {
navController.navigate(MainScreenDestination)
if ((event.value as AuthAction.LogIn).isLogged) {
navController.navigate(MainScreenDestination)
}
}
}
Log.d("AnnaKonda", state.javaClass.toString())
@@ -88,12 +91,16 @@ private fun Content(
) {
var inputText by remember { mutableStateOf("") }
var errorText: String? by remember { mutableStateOf(null) }
var btnEnabled: Boolean by remember { mutableStateOf(false) }
val event = viewModel.actionFlow.collectAsState(initial = null)
LaunchedEffect(event.value) {
if (event.value is AuthAction.ShowError) {
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)
.fillMaxWidth(),
onClick = {
if (!inputText.isEmpty() && inputText.length == 4 && inputText.matches(Regex(SHABLON))) {
if (Utils.CheckCodeInput(inputText)) {
viewModel.onIntent(AuthIntent.Send(inputText))
} else {
errorText = App.context.getString(R.string.auth_nasty_code)
}
},
enabled = true
enabled = btnEnabled
) { Text(stringResource(R.string.auth_sign_in)) }
if (errorText != null) {

View File

@@ -14,6 +14,7 @@ import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import ru.myitschool.work.App
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.source.DataStoreDataSource.authFlow
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 -> {
viewModelScope.launch {
_uiState.update { AuthState.Loading }
authFlow().collect {
Log.d("AnnaKonda", it)
if (it != "0") {
_actionFlow.emit(AuthAction.LogIn)
_actionFlow.emit(AuthAction.LogIn(true))
_uiState.update { AuthState.LoggedIn }
} else {
_actionFlow.emit(AuthAction.ShowError(App.context.getString(R.string.auth_wrong_code)))