Compare commits
9 Commits
main
...
c4913ec0bc
| Author | SHA1 | Date | |
|---|---|---|---|
| c4913ec0bc | |||
| 2ecbc7339a | |||
| 00162984a9 | |||
| bc7e14ab06 | |||
| b98c7f410f | |||
| 67e26d142a | |||
|
|
286b752974 | ||
| cbc91ee472 | |||
|
|
d92ac5a10c |
@@ -8,9 +8,7 @@ object AuthRepository {
|
||||
|
||||
suspend fun checkAndSave(text: String): Result<Boolean> {
|
||||
return NetworkDataSource.checkAuth(text).onSuccess { success ->
|
||||
if (success) {
|
||||
codeCache = text
|
||||
}
|
||||
if (success) codeCache = text
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package ru.myitschool.work.data.repo
|
||||
|
||||
import ru.myitschool.work.data.source.NetworkDataSource
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlinx.serialization.Serializable
|
||||
import ru.myitschool.work.domain.auth.entities.BookingEntity
|
||||
import ru.myitschool.work.domain.auth.entities.UserEntity
|
||||
import kotlinx.serialization.json.Json
|
||||
|
||||
object MainRepository {
|
||||
|
||||
@Serializable
|
||||
data class BookingDto(
|
||||
val roomName: String,
|
||||
val time: String
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class UserDto(
|
||||
val name: String,
|
||||
val bookingList: List<BookingDto>
|
||||
)
|
||||
|
||||
private val json = Json { ignoreUnknownKeys = true }
|
||||
|
||||
private var codeCache: String? = null
|
||||
|
||||
|
||||
private var cachedUser: UserEntity? = null
|
||||
|
||||
suspend fun getUserInfo(code: String): Result<UserEntity> {
|
||||
return withContext(Dispatchers.IO) {
|
||||
runCatching {
|
||||
if (codeCache == code && cachedUser != null) return@runCatching cachedUser!!
|
||||
|
||||
codeCache = code
|
||||
val isAuth = NetworkDataSource.checkAuth(code).getOrThrow()
|
||||
if (!isAuth) throw Exception("Авторизация не пройдена")
|
||||
|
||||
val userJson = NetworkDataSource.getUserData(code).getOrThrow()
|
||||
val userResponse = json.decodeFromString<UserDto>(userJson)
|
||||
|
||||
val user = UserEntity(
|
||||
name = userResponse.name,
|
||||
bookingList = userResponse.bookingList.map { BookingEntity(it.roomName, it.time) }
|
||||
)
|
||||
|
||||
cachedUser = user
|
||||
user
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -29,7 +29,7 @@ object NetworkDataSource {
|
||||
}
|
||||
|
||||
suspend fun checkAuth(code: String): Result<Boolean> = withContext(Dispatchers.IO) {
|
||||
return@withContext runCatching {
|
||||
runCatching {
|
||||
val response = client.get(getUrl(code, Constants.AUTH_URL))
|
||||
when (response.status) {
|
||||
HttpStatusCode.OK -> true
|
||||
@@ -38,5 +38,15 @@ object NetworkDataSource {
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getUserData(code: String): Result<String> = withContext(Dispatchers.IO) {
|
||||
runCatching {
|
||||
val response = client.get(getUrl(code, Constants.INFO_URL))
|
||||
when (response.status) {
|
||||
HttpStatusCode.OK -> response.bodyAsText()
|
||||
else -> error(response.bodyAsText())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun getUrl(code: String, targetUrl: String) = "${Constants.HOST}/api/$code$targetUrl"
|
||||
}
|
||||
@@ -8,6 +8,10 @@ class CheckAndSaveAuthCodeUseCase(
|
||||
suspend operator fun invoke(
|
||||
text: String
|
||||
): Result<Unit> {
|
||||
|
||||
val isValid = text.length == 4 && text.all {it.isLetterOrDigit()}
|
||||
if (!isValid) return Result.failure(IllegalArgumentException("Неверный формат кода!"))
|
||||
|
||||
return repository.checkAndSave(text).mapCatching { success ->
|
||||
if (!success) error("Code is incorrect")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package ru.myitschool.work.domain.auth
|
||||
|
||||
import ru.myitschool.work.data.repo.MainRepository
|
||||
import ru.myitschool.work.domain.auth.entities.UserEntity
|
||||
|
||||
class GetUserInfoUseCase(
|
||||
private val repository: MainRepository
|
||||
) {
|
||||
suspend operator fun invoke(code: String): Result<UserEntity> {
|
||||
return repository.getUserInfo(code)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package ru.myitschool.work.domain.auth.entities
|
||||
|
||||
data class BookingEntity(
|
||||
val roomName: String,
|
||||
val time: String,
|
||||
)
|
||||
@@ -0,0 +1,6 @@
|
||||
package ru.myitschool.work.domain.auth.entities
|
||||
|
||||
data class UserEntity(
|
||||
val name: String,
|
||||
val bookingList: List<BookingEntity>
|
||||
)
|
||||
@@ -21,7 +21,6 @@ import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.platform.testTag
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
@@ -32,6 +31,7 @@ import ru.myitschool.work.R
|
||||
import ru.myitschool.work.core.TestIds
|
||||
import ru.myitschool.work.ui.nav.MainScreenDestination
|
||||
|
||||
|
||||
@Composable
|
||||
fun AuthScreen(
|
||||
viewModel: AuthViewModel = viewModel(),
|
||||
@@ -74,7 +74,12 @@ private fun Content(
|
||||
state: AuthState.Data
|
||||
) {
|
||||
var inputText by remember { mutableStateOf("") }
|
||||
|
||||
val isCodeValid = inputText.length == 4 && inputText.all { it.isLetterOrDigit() }
|
||||
val isButtonEnabled = isCodeValid
|
||||
|
||||
Spacer(modifier = Modifier.size(16.dp))
|
||||
|
||||
TextField(
|
||||
modifier = Modifier.testTag(TestIds.Auth.CODE_INPUT).fillMaxWidth(),
|
||||
value = inputText,
|
||||
@@ -82,16 +87,30 @@ private fun Content(
|
||||
inputText = it
|
||||
viewModel.onIntent(AuthIntent.TextInput(it))
|
||||
},
|
||||
label = { Text(stringResource(R.string.auth_label)) }
|
||||
label = { Text(stringResource(R.string.auth_label)) },
|
||||
placeholder = { Text("Код") },
|
||||
isError = state.error != null
|
||||
)
|
||||
|
||||
if (state.error != null) {
|
||||
Spacer(modifier = Modifier.size(8.dp))
|
||||
Text(
|
||||
text = state.error,
|
||||
color = MaterialTheme.colorScheme.error,
|
||||
modifier = Modifier.testTag(TestIds.Auth.ERROR)
|
||||
)
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.size(16.dp))
|
||||
|
||||
Button(
|
||||
modifier = Modifier.testTag(TestIds.Auth.SIGN_BUTTON).fillMaxWidth(),
|
||||
onClick = {
|
||||
viewModel.onIntent(AuthIntent.Send(inputText))
|
||||
},
|
||||
enabled = true
|
||||
enabled = isButtonEnabled
|
||||
) {
|
||||
Text(stringResource(R.string.auth_sign_in))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package ru.myitschool.work.ui.screen.auth
|
||||
|
||||
sealed interface AuthState {
|
||||
object Loading: AuthState
|
||||
object Data: AuthState
|
||||
sealed class AuthState {
|
||||
object Loading : AuthState()
|
||||
data class Data(
|
||||
val error: String? = null
|
||||
) : AuthState()
|
||||
}
|
||||
@@ -15,7 +15,7 @@ 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()
|
||||
@@ -24,20 +24,30 @@ class AuthViewModel : ViewModel() {
|
||||
fun onIntent(intent: AuthIntent) {
|
||||
when (intent) {
|
||||
is AuthIntent.Send -> {
|
||||
val code = intent.text
|
||||
viewModelScope.launch(Dispatchers.Default) {
|
||||
_uiState.update { AuthState.Loading }
|
||||
checkAndSaveAuthCodeUseCase.invoke("9999").fold(
|
||||
_uiState.value = AuthState.Loading
|
||||
checkAndSaveAuthCodeUseCase.invoke(code).fold(
|
||||
onSuccess = {
|
||||
_actionFlow.emit(Unit)
|
||||
},
|
||||
onFailure = { error ->
|
||||
error.printStackTrace()
|
||||
_actionFlow.emit(Unit)
|
||||
|
||||
val errorMsg = when {
|
||||
error.message?.contains("401") == true -> "Неверный код"
|
||||
else -> "Ошибка подключения"
|
||||
}
|
||||
_uiState.value = AuthState.Data(error = errorMsg)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
is AuthIntent.TextInput -> Unit
|
||||
|
||||
is AuthIntent.TextInput -> {
|
||||
if (_uiState.value is AuthState.Data) {
|
||||
_uiState.value = AuthState.Data(error = null)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package ru.myitschool.work.ui.screen.main
|
||||
|
||||
sealed interface MainState {
|
||||
object Loading : MainState
|
||||
data class Success(
|
||||
val name: String,
|
||||
val photoUrl: String,
|
||||
val bookings: List<BookingItem>,
|
||||
) : MainState
|
||||
|
||||
data class Error(
|
||||
val message: String,
|
||||
) : MainState
|
||||
}
|
||||
|
||||
data class BookingItem(
|
||||
val date: String,
|
||||
val place: String,
|
||||
)
|
||||
Reference in New Issue
Block a user