forked from Olympic/NTO-2025-Android-TeamTask
booking fix + ui
This commit is contained in:
@@ -1,11 +1,10 @@
|
||||
package ru.myitschool.work.data.model
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class BookingInfoResponse(
|
||||
val dates: Map<String, List<PlaceInfo>>? = null // can be nil because web api
|
||||
)
|
||||
|
||||
typealias BookingInfoResponse = Map<String, List<PlaceInfo>>
|
||||
|
||||
@Serializable
|
||||
data class PlaceInfo(
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package ru.myitschool.work.data.repo
|
||||
|
||||
import ru.myitschool.work.data.model.BookingInfoResponse
|
||||
import ru.myitschool.work.data.model.PlaceInfo
|
||||
import ru.myitschool.work.data.source.AuthException
|
||||
import ru.myitschool.work.data.source.NetworkDataSource
|
||||
|
||||
class BookRepository(private val authRepository: AuthRepository) {
|
||||
|
||||
suspend fun getAvailableBookings(): Result<BookingInfoResponse> {
|
||||
suspend fun getAvailableBookings(): Result<Map<String, List<PlaceInfo>>> {
|
||||
val code = authRepository.getCurrentCode()
|
||||
return if (code != null) {
|
||||
NetworkDataSource.getBookingInfo(code)
|
||||
|
||||
@@ -20,7 +20,7 @@ import kotlinx.coroutines.withContext
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.json.Json
|
||||
import ru.myitschool.work.core.Constants
|
||||
import ru.myitschool.work.data.model.BookingInfoResponse
|
||||
import ru.myitschool.work.data.model.PlaceInfo
|
||||
import ru.myitschool.work.data.model.UserInfoResponse
|
||||
import java.net.ConnectException
|
||||
import java.net.SocketTimeoutException
|
||||
@@ -133,26 +133,24 @@ object NetworkDataSource {
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getBookingInfo(code: String): Result<BookingInfoResponse> = withContext(Dispatchers.IO) {
|
||||
suspend fun getBookingInfo(code: String): Result<Map<String, List<PlaceInfo>>> = withContext(Dispatchers.IO) {
|
||||
return@withContext runCatching {
|
||||
val response = client.get(getUrl(code, Constants.BOOKING_URL))
|
||||
|
||||
|
||||
|
||||
when (response.status) {
|
||||
HttpStatusCode.OK -> {
|
||||
try {
|
||||
val body = response.body<BookingInfoResponse>()
|
||||
val body = response.body<Map<String, List<PlaceInfo>>>()
|
||||
println("Parsed response: $body")
|
||||
body
|
||||
} catch (e: Exception) {
|
||||
println("Parsing error: ${e.message}")
|
||||
BookingInfoResponse(emptyMap())
|
||||
emptyMap()
|
||||
}
|
||||
}
|
||||
HttpStatusCode.NoContent -> {
|
||||
println("No content received")
|
||||
BookingInfoResponse(emptyMap())
|
||||
emptyMap()
|
||||
}
|
||||
else -> {
|
||||
val errorMsg = response.body<String>().ifBlank {
|
||||
@@ -163,6 +161,7 @@ object NetworkDataSource {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun book(code: String, date: String, placeId: Int): Result<Unit> = withContext(Dispatchers.IO) {
|
||||
return@withContext runCatching {
|
||||
val response = client.post(getUrl(code, Constants.BOOK_URL)) {
|
||||
|
||||
@@ -11,6 +11,7 @@ import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import ru.myitschool.work.data.model.PlaceInfo
|
||||
import ru.myitschool.work.data.repo.AuthRepository
|
||||
import ru.myitschool.work.data.repo.BookRepository
|
||||
import java.text.SimpleDateFormat
|
||||
@@ -71,7 +72,7 @@ class BookViewModel(private val bookRepo: BookRepository) : ViewModel() {
|
||||
|
||||
bookRepo.getAvailableBookings().fold(
|
||||
onSuccess = { response ->
|
||||
val datesMap = response.dates ?: emptyMap()
|
||||
val datesMap = response ?: emptyMap()
|
||||
|
||||
val dateItems = datesMap.entries
|
||||
.sortedBy { (date, _) -> parseDate(date) }
|
||||
@@ -101,7 +102,6 @@ class BookViewModel(private val bookRepo: BookRepository) : ViewModel() {
|
||||
}
|
||||
},
|
||||
onFailure = { error ->
|
||||
println("Error loading bookings: ${error.message}")
|
||||
_uiState.update {
|
||||
BookState.Data(
|
||||
error = error.message ?: "Ошибка загрузки данных"
|
||||
|
||||
@@ -113,6 +113,14 @@ fun MainScreen(
|
||||
Column(
|
||||
modifier = Modifier.fillMaxSize()
|
||||
) {
|
||||
Text(
|
||||
text = "",
|
||||
color = Color.Red,
|
||||
modifier = Modifier
|
||||
.testTag(TestIds.Main.ERROR)
|
||||
.height(0.dp)
|
||||
)
|
||||
|
||||
Card(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
|
||||
Reference in New Issue
Block a user