main #6

Closed
student-20690 wants to merge 20 commits from (deleted):main into main
4 changed files with 46 additions and 19 deletions
Showing only changes of commit ad113e0438 - Show all commits

View File

@@ -1,6 +1,7 @@
package ru.myitschool.work.ui.screen.auth
sealed interface AuthAction {
data class ShowError(val message: String) : AuthAction
data class ShowError(val message: String?) : AuthAction
object LogIn : AuthAction
}

View File

@@ -1,5 +1,6 @@
package ru.myitschool.work.ui.screen.auth
import android.util.Log
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
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.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import io.ktor.util.collections.setValue
import ru.myitschool.work.App
import ru.myitschool.work.R
import ru.myitschool.work.core.OurConstants.SHABLON
import ru.myitschool.work.core.TestIds
@@ -39,13 +42,18 @@ fun AuthScreen(
navController: NavController
) {
val state by viewModel.uiState.collectAsState()
viewModel.onIntent(AuthIntent.CheckLogIntent)
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)
}
}
Log.d("AnnaKonda", state.javaClass.toString())
Column(
modifier = Modifier
.fillMaxSize()
@@ -65,6 +73,7 @@ fun AuthScreen(
modifier = Modifier.size(64.dp)
)
}
is AuthState.LoggedIn -> {
navController.navigate(MainScreenDestination)
}
@@ -78,10 +87,21 @@ private fun Content(
state: AuthState.Data
) {
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))
TextField(
modifier = Modifier.testTag(TestIds.Auth.CODE_INPUT).fillMaxWidth(),
modifier = Modifier
.testTag(TestIds.Auth.CODE_INPUT)
.fillMaxWidth(),
value = inputText,
onValueChange = {
inputText = it
@@ -91,22 +111,20 @@ private fun Content(
)
Spacer(modifier = Modifier.size(16.dp))
Button(
modifier = Modifier.testTag(TestIds.Auth.SIGN_BUTTON).fillMaxWidth(),
modifier = Modifier
.testTag(TestIds.Auth.SIGN_BUTTON)
.fillMaxWidth(),
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))
} else {
errorText = App.context.getString(R.string.auth_nasty_code)
}
},
enabled = true
) { Text(stringResource(R.string.auth_sign_in)) }
ShowError(errorText) // TODO: раскидать в коде когда показывается ошибка и когда нет
}
@Composable
fun ShowError(text : String?){
if (text != null){
Text(text, modifier = Modifier.testTag(TestIds.Auth.ERROR))
if (errorText != null) {
Text(errorText.toString(), modifier = Modifier.testTag(TestIds.Auth.ERROR))
}
}
}

View File

@@ -1,6 +1,7 @@
package ru.myitschool.work.ui.screen.auth
import android.util.Log
import androidx.compose.ui.res.stringResource
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.Dispatchers
@@ -11,6 +12,8 @@ import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
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.source.DataStoreDataSource.authFlow
import ru.myitschool.work.domain.auth.CheckAndSaveAuthCodeUseCase
@@ -37,19 +40,23 @@ class AuthViewModel : ViewModel() {
if (error.message != null) {
_actionFlow.emit(AuthAction.ShowError(error.message.toString()))
}
_uiState.update { AuthState.Data }
}
)
}
}
is AuthIntent.TextInput -> Unit
is AuthIntent.CheckLogIntent -> {
viewModelScope.launch {
_uiState.update { AuthState.Loading }
authFlow().collect {
Log.d("AnnaKonda", it)
if (it != "0"){
if (it != "0") {
_actionFlow.emit(AuthAction.LogIn)
_uiState.update { AuthState.LoggedIn }
} else {
_actionFlow.emit(AuthAction.ShowError(App.context.getString(R.string.auth_wrong_code)))
_uiState.update { AuthState.Data }
}
}
@@ -57,5 +64,4 @@ class AuthViewModel : ViewModel() {
}
}
}
}

View File

@@ -4,4 +4,6 @@
<string name="auth_title">Привет! Введи код для авторизации</string>
<string name="auth_label">Код</string>
<string name="auth_sign_in">Войти</string>
<string name="auth_wrong_code">Введён неверный код</string>
<string name="auth_nasty_code">Неправильный формат кода</string>
</resources>