all done v1
This commit is contained in:
@@ -19,6 +19,8 @@ import kotlinx.serialization.json.Json
|
||||
import ru.myitschool.work.core.Constants
|
||||
import ru.myitschool.work.data.entity.Employee
|
||||
import kotlinx.serialization.json.*
|
||||
import ru.myitschool.work.App
|
||||
import ru.myitschool.work.R
|
||||
import ru.myitschool.work.data.entity.Booking
|
||||
import ru.myitschool.work.data.entity.Place
|
||||
import java.time.LocalDate
|
||||
@@ -45,11 +47,12 @@ object NetworkDataSource {
|
||||
|
||||
when (response.status) {
|
||||
HttpStatusCode.OK -> true
|
||||
HttpStatusCode.Unauthorized -> error("Wrong code!")
|
||||
else -> error("Request error: ${response.bodyAsText()}")
|
||||
HttpStatusCode.Unauthorized -> error(App.context.getString(R.string.auth_wrong_code))
|
||||
else -> error(App.context.getString(R.string.error_request, response.bodyAsText()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getUserInfo(code: String): Result<Employee> = withContext(Dispatchers.IO) {
|
||||
return@withContext runCatching {
|
||||
val response = client.get(getUrl(code, Constants.INFO_URL))
|
||||
@@ -58,21 +61,21 @@ object NetworkDataSource {
|
||||
HttpStatusCode.OK -> {
|
||||
val json = response.bodyAsText()
|
||||
if (json.isBlank()) {
|
||||
error("Пустой ответ от сервера")
|
||||
error(App.context.getString(R.string.error_empty_server_response))
|
||||
}
|
||||
|
||||
val jsonObject = try {
|
||||
Json.parseToJsonElement(json).jsonObject
|
||||
} catch (e: Exception) {
|
||||
error("Ошибка парсинга: ${e.message}")
|
||||
error(App.context.getString(R.string.error_parsing, e.message))
|
||||
}
|
||||
val name = jsonObject["name"]?.jsonPrimitive?.content
|
||||
?: error("Отсутствует поле 'name'")
|
||||
?: error(App.context.getString(R.string.error_missing_name_field))
|
||||
val photoUrl = jsonObject["photoUrl"]?.jsonPrimitive?.content
|
||||
?: error("Отсутствует поле 'photoUrl'")
|
||||
?: error(App.context.getString(R.string.error_missing_photo_url_field))
|
||||
|
||||
val bookingJson = jsonObject["booking"]?.jsonObject
|
||||
?: error("Отсутствует поле 'booking' в ответе")
|
||||
?: error(App.context.getString(R.string.error_missing_booking_field))
|
||||
|
||||
val employee = Employee(
|
||||
name = name,
|
||||
@@ -85,12 +88,12 @@ object NetworkDataSource {
|
||||
val date = LocalDate.parse(dateString)
|
||||
val bookingObj = bookingElement.jsonObject
|
||||
val bookingId = bookingObj["id"]?.jsonPrimitive?.long
|
||||
?: error("Отсутствует поле id")
|
||||
?: error(App.context.getString(R.string.error_missing_id_field))
|
||||
val placeString = bookingObj["place"]?.jsonPrimitive?.content
|
||||
?: error("Отсутствует поле 'place' $dateString")
|
||||
?: error(App.context.getString(R.string.error_missing_place_field, dateString))
|
||||
|
||||
if (placeString.isBlank()) {
|
||||
error("Пустое поле 'place' $dateString")
|
||||
error(App.context.getString(R.string.error_empty_place_field, dateString))
|
||||
}
|
||||
|
||||
val placeId = bookingId
|
||||
@@ -104,9 +107,9 @@ object NetworkDataSource {
|
||||
)
|
||||
bookingList.add(booking)
|
||||
}
|
||||
if (bookingList.isEmpty()) {
|
||||
error("Список бронирований пуст")
|
||||
}
|
||||
/* if (bookingList.isEmpty()) {
|
||||
error(App.context.getString(R.string.error_booking_list_empty))
|
||||
}*/
|
||||
employee.bookingList.addAll(bookingList)
|
||||
employee
|
||||
}
|
||||
@@ -130,9 +133,9 @@ object NetworkDataSource {
|
||||
val places = placesArray.jsonArray.map { placeElement ->
|
||||
val placeObj = placeElement.jsonObject
|
||||
val id = placeObj["id"]?.jsonPrimitive?.long
|
||||
?: error("Missing 'id' in place")
|
||||
?: error(App.context.getString(R.string.error_missing_id_in_place))
|
||||
val placeName = placeObj["place"]?.jsonPrimitive?.content
|
||||
?: error("Missing 'place' in place")
|
||||
?: error(App.context.getString(R.string.error_missing_place_in_place))
|
||||
Place(id, placeName)
|
||||
}
|
||||
if (places.isNotEmpty()) {
|
||||
@@ -142,7 +145,7 @@ object NetworkDataSource {
|
||||
availableBookings.toSortedMap()
|
||||
}
|
||||
|
||||
else -> error("Request error: ${response.bodyAsText()}")
|
||||
else -> error(App.context.getString(R.string.error_request, response.bodyAsText()))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -162,7 +165,10 @@ object NetworkDataSource {
|
||||
|
||||
when (response.status) {
|
||||
HttpStatusCode.OK -> true
|
||||
else -> error("Ошибка бронирования: ${response.bodyAsText()}")
|
||||
else -> {
|
||||
val errorBody = response.bodyAsText()
|
||||
error(if (errorBody.isNotBlank()) App.context.getString(R.string.error_booking, errorBody) else App.context.getString(R.string.error_booking_default))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,8 @@ import kotlinx.coroutines.flow.asSharedFlow
|
||||
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.entity.Place
|
||||
import ru.myitschool.work.data.repo.BookingRepository
|
||||
import ru.myitschool.work.domain.book.CreateBookingUseCase
|
||||
@@ -55,7 +57,7 @@ class BookViewModel : ViewModel() {
|
||||
_uiState.update {
|
||||
BookState.Data(
|
||||
isError = true,
|
||||
errorMessage = "Нет доступных дат для бронирования"
|
||||
errorMessage = App.context.getString(R.string.error_no_available_dates)
|
||||
)
|
||||
}
|
||||
} else {
|
||||
@@ -77,7 +79,7 @@ class BookViewModel : ViewModel() {
|
||||
_uiState.update {
|
||||
BookState.Data(
|
||||
isError = true,
|
||||
errorMessage = error.message ?: "Ошибка загрузки данных"
|
||||
errorMessage = error.message ?: App.context.getString(R.string.error_loading_data)
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -111,10 +113,9 @@ class BookViewModel : ViewModel() {
|
||||
}
|
||||
|
||||
private fun bookPlace() {
|
||||
// Раскомментируйте и измените этот блок
|
||||
val currentState = _uiState.value
|
||||
if (currentState is BookState.Data && currentState.selectedPlace != null && currentState.selectedDate != null) {
|
||||
val placeId = selectedPlaceId ?: return // Дополнительная проверка
|
||||
val placeId = selectedPlaceId ?: return
|
||||
val date = currentState.selectedDate
|
||||
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
@@ -124,7 +125,7 @@ class BookViewModel : ViewModel() {
|
||||
},
|
||||
onFailure = { error ->
|
||||
error.printStackTrace()
|
||||
_actionFlow.emit(BookAction.ShowError(error.message ?: "Ошибка бронирования"))
|
||||
_actionFlow.emit(BookAction.ShowError(error.message ?: App.context.getString(R.string.error_booking_default)))
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@@ -19,9 +19,6 @@ import ru.myitschool.work.ui.screen.auth.AuthIntent
|
||||
import ru.myitschool.work.ui.screen.auth.AuthState
|
||||
|
||||
class MainViewModel : ViewModel() {
|
||||
init {
|
||||
loadData()
|
||||
}
|
||||
private val repository by lazy{ MainRepository() }
|
||||
private val getUserDataUseCase by lazy { GetUserDataUseCase(repository) }
|
||||
|
||||
@@ -31,6 +28,10 @@ class MainViewModel : ViewModel() {
|
||||
private val _actionFlow: MutableSharedFlow<MainAction> = MutableSharedFlow()
|
||||
val actionFlow: SharedFlow<MainAction> = _actionFlow
|
||||
|
||||
init {
|
||||
loadData()
|
||||
}
|
||||
|
||||
fun onIntent(intent: MainIntent) {
|
||||
when (intent) {
|
||||
is MainIntent.LoadData -> {
|
||||
@@ -59,6 +60,7 @@ class MainViewModel : ViewModel() {
|
||||
if (error.message != null) {
|
||||
_actionFlow.emit(MainAction.ShowError(error.message.toString()))
|
||||
}
|
||||
_uiState.update { MainState.Data(null) }
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@@ -6,4 +6,23 @@
|
||||
<string name="auth_sign_in">Войти</string>
|
||||
<string name="auth_wrong_code">Введён неверный код</string>
|
||||
<string name="auth_nasty_code">Неправильный формат кода</string>
|
||||
|
||||
<string name="error_request">Ошибка запроса</string>
|
||||
<string name="error_empty_server_response">Пустой ответ от сервера</string>
|
||||
<string name="error_parsing">Ошибка парсинга</string>
|
||||
<string name="error_missing_name_field">В ответе отсутствует поле name</string>
|
||||
<string name="error_missing_photo_url_field">В ответе отсутствует поле photoUrl</string>
|
||||
<string name="error_missing_booking_field">В ответе отсутствует поле booking</string>
|
||||
<string name="error_missing_id_field">В ответе отсутствует поле id</string>
|
||||
<string name="error_missing_place_field">В ответе отсутствует поле place для даты</string>
|
||||
<string name="error_empty_place_field">В ответе поле place пусто для даты</string>
|
||||
<string name="error_booking_list_empty">Список бронирований пуст</string>
|
||||
<string name="error_missing_id_in_place">В информации о месте отсутствует id</string>
|
||||
<string name="error_missing_place_in_place">В информации о месте отсутствует place</string>
|
||||
<string name="error_booking">Ошибка бронирования</string>
|
||||
<string name="error_booking_default">Ошибка бронирования</string>
|
||||
|
||||
<string name="error_no_available_dates">Нет доступных дат для бронирования</string>
|
||||
<string name="error_loading_data">Ошибка загрузки данных</string>
|
||||
|
||||
</resources>
|
||||
Reference in New Issue
Block a user