forked from Olympic/NTO-2025-Android-TeamTask
Autorization screen
This commit is contained in:
@@ -35,7 +35,7 @@ fun AppNavHost(
|
||||
Box(
|
||||
contentAlignment = Alignment.Center
|
||||
) {
|
||||
Text(text = "Hello")
|
||||
Text(text = "MainScreen")
|
||||
}
|
||||
}
|
||||
composable<BookScreenDestination> {
|
||||
|
||||
@@ -45,6 +45,9 @@ fun AuthScreen(
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
@@ -52,11 +55,7 @@ fun AuthScreen(
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
verticalArrangement = Arrangement.Center
|
||||
) {
|
||||
Text(
|
||||
text = stringResource(R.string.auth_title),
|
||||
style = MaterialTheme.typography.headlineSmall,
|
||||
textAlign = TextAlign.Center
|
||||
)
|
||||
|
||||
when (val currentState = state) {
|
||||
is AuthState.Data -> Content(viewModel, currentState)
|
||||
is AuthState.Loading -> {
|
||||
@@ -64,6 +63,7 @@ fun AuthScreen(
|
||||
modifier = Modifier.size(64.dp)
|
||||
)
|
||||
}
|
||||
is AuthState.Error -> Content(viewModel, currentState)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -71,15 +71,23 @@ fun AuthScreen(
|
||||
@Composable
|
||||
private fun Content(
|
||||
viewModel: AuthViewModel,
|
||||
state: AuthState.Data
|
||||
state: AuthState
|
||||
) {
|
||||
var inputText by remember { mutableStateOf("") }
|
||||
val inputText = when (state) {
|
||||
is AuthState.Data -> state.text
|
||||
is AuthState.Error -> state.text
|
||||
else -> "" }
|
||||
|
||||
Text(
|
||||
text = stringResource(R.string.auth_title),
|
||||
style = MaterialTheme.typography.headlineSmall,
|
||||
textAlign = TextAlign.Center
|
||||
)
|
||||
Spacer(modifier = Modifier.size(16.dp))
|
||||
TextField(
|
||||
modifier = Modifier.testTag(TestIds.Auth.CODE_INPUT).fillMaxWidth(),
|
||||
value = inputText,
|
||||
onValueChange = {
|
||||
inputText = it
|
||||
viewModel.onIntent(AuthIntent.TextInput(it))
|
||||
},
|
||||
label = { Text(stringResource(R.string.auth_label)) }
|
||||
@@ -89,9 +97,20 @@ private fun Content(
|
||||
modifier = Modifier.testTag(TestIds.Auth.SIGN_BUTTON).fillMaxWidth(),
|
||||
onClick = {
|
||||
viewModel.onIntent(AuthIntent.Send(inputText))
|
||||
|
||||
},
|
||||
enabled = true
|
||||
) {
|
||||
enabled = inputText.length == 4 && !inputText.isNullOrEmpty() && inputText.matches("[a-zA-Z0-9]+".toRegex()),
|
||||
) {
|
||||
Text(stringResource(R.string.auth_sign_in))
|
||||
}
|
||||
}
|
||||
Spacer(modifier = Modifier.size(24.dp))
|
||||
if (state is AuthState.Error) {
|
||||
Text(
|
||||
modifier = Modifier.testTag(TestIds.Auth.ERROR),
|
||||
text = state.message,
|
||||
style = MaterialTheme.typography.labelMedium,
|
||||
textAlign = TextAlign.Center,
|
||||
color = Color.Red
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,5 +2,6 @@ package ru.myitschool.work.ui.screen.auth
|
||||
|
||||
sealed interface AuthState {
|
||||
object Loading: AuthState
|
||||
object Data: AuthState
|
||||
class Data(val text: String = ""): AuthState
|
||||
data class Error(val text: String, val message: String) : AuthState
|
||||
}
|
||||
@@ -1,5 +1,8 @@
|
||||
package ru.myitschool.work.ui.screen.auth
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Context.MODE_PRIVATE
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
@@ -15,29 +18,43 @@ import ru.myitschool.work.domain.auth.CheckAndSaveAuthCodeUseCase
|
||||
|
||||
class AuthViewModel : ViewModel() {
|
||||
private val checkAndSaveAuthCodeUseCase by lazy { CheckAndSaveAuthCodeUseCase(AuthRepository) }
|
||||
private val _uiState = MutableStateFlow<AuthState>(AuthState.Data)
|
||||
private val _uiState = MutableStateFlow<AuthState>(AuthState.Data())
|
||||
val uiState: StateFlow<AuthState> = _uiState.asStateFlow()
|
||||
|
||||
private val _actionFlow: MutableSharedFlow<Unit> = MutableSharedFlow()
|
||||
val actionFlow: SharedFlow<Unit> = _actionFlow
|
||||
|
||||
|
||||
fun onIntent(intent: AuthIntent) {
|
||||
when (intent) {
|
||||
is AuthIntent.Send -> {
|
||||
viewModelScope.launch(Dispatchers.Default) {
|
||||
val currentText = when (val s = _uiState.value) {
|
||||
is AuthState.Data -> s.text
|
||||
is AuthState.Error -> s.text
|
||||
else -> ""
|
||||
}
|
||||
_uiState.update { AuthState.Loading }
|
||||
checkAndSaveAuthCodeUseCase.invoke("9999").fold(
|
||||
checkAndSaveAuthCodeUseCase.invoke(intent.text).fold(
|
||||
onSuccess = {
|
||||
|
||||
_actionFlow.emit(Unit)
|
||||
},
|
||||
onFailure = { error ->
|
||||
error.printStackTrace()
|
||||
_actionFlow.emit(Unit)
|
||||
_uiState.value = AuthState.Error(text = currentText,error.message ?: "Ошибка")
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
is AuthIntent.TextInput -> Unit
|
||||
is AuthIntent.TextInput -> {
|
||||
when (val stateValue = _uiState.value) {
|
||||
is AuthState.Data -> _uiState.value = AuthState.Data(text = intent.text)
|
||||
is AuthState.Error -> _uiState.value = AuthState.Data(intent.text)
|
||||
is AuthState.Loading -> Unit
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user