From 60c1eb2234556dd616a1c0f9c454a215425d435a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A2=D0=B0=D1=82=D1=8C=D1=8F=D0=BD=D0=B0=20=D0=90=D1=80?= =?UTF-8?q?=D1=82=D0=B0=D0=BC=D0=BE=D0=BD=D0=BE=D0=B2=D0=B0?= Date: Mon, 25 Dec 2023 17:47:51 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BC=D0=B0=D0=BB=D0=B5=D0=BD=D1=8C=D0=BA?= =?UTF-8?q?=D0=BE=20=D0=BE=D0=BF=D1=82=D0=B8=D0=BC=D0=B8=D0=B7=D0=B8=D1=80?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BB=D0=B0=20=D0=BF=D0=BE=D0=B8=D1=81=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/repository/RestFlightRepository.kt | 56 +++---------------- .../database/dao/FlightDao.kt | 2 +- .../navigation/BottomBarScreen.kt | 8 +-- .../airticketrentservice/screen/FlightEdit.kt | 3 +- .../airticketrentservice/screen/FlightInfo.kt | 1 - .../screen/FoundFlights.kt | 18 +----- .../airticketrentservice/screen/MainPage.kt | 12 ++-- .../airticketrentservice/screen/TicketEdit.kt | 6 +- .../viewModel/FindFlightsViewModel.kt | 13 ----- .../viewModel/TicketEditViewModel.kt | 2 + server/data.json | 8 +-- server/data.json.bak | 20 +++---- 12 files changed, 44 insertions(+), 105 deletions(-) diff --git a/app/src/main/java/ru/ulstu/is/airticketrentservice/api/repository/RestFlightRepository.kt b/app/src/main/java/ru/ulstu/is/airticketrentservice/api/repository/RestFlightRepository.kt index ce67367..be8ac3d 100644 --- a/app/src/main/java/ru/ulstu/is/airticketrentservice/api/repository/RestFlightRepository.kt +++ b/app/src/main/java/ru/ulstu/is/airticketrentservice/api/repository/RestFlightRepository.kt @@ -61,37 +61,21 @@ class RestFlightRepository( override suspend fun getFlightById(flightId: Int): Flight = service.getFlight(flightId).toFlight() -// override suspend fun findFlights( -// from: String, -// to: String, -// departureDate: String -// ): List { -// val existFlights = dbFlightRepository.getAllFlights().associateBy { it.id }.toMutableMap() -// -// service.findFlights(from, to, departureDate) -// .map { it.toFlight() } -// .forEach { flight -> -// existFlights[flight.id] = flight -// } -// return existFlights.map { it.value }.sortedBy { it.id } -// } - override suspend fun findFlights( from: String, to: String, departureDate: String ): List { - return try { - val foundFlights = service.findFlights(from, to, departureDate) - if (foundFlights.isNotEmpty()) { - foundFlights.map { it.toFlight() } - } else { - emptyList() + Log.d(RestFlightRepository::class.simpleName, "Find flights") + val existFlights = dbFlightRepository.findFlights(from, to, departureDate).associateBy { it.id }.toMutableMap() + + service.findFlights(from, to, departureDate) + .map { it.toFlight() } + .forEach { flight -> + existFlights[flight.id] = flight } - } catch (e: Exception) { - e.message?.let { Log.d(RestFlightRepository::class.simpleName, it) } - emptyList() - } + + return existFlights.map { it.value }.sortedBy { it.id } } override suspend fun getAllFlights(): List { @@ -113,28 +97,6 @@ class RestFlightRepository( return existFlights.map { it.value }.sortedBy { it.id } } - // flight.direction_from == from || -// flight.direction_to == to || -// flight.departure_date == departureDate - -// override suspend fun findFlights( -// from: String, -// to: String, -// departureDate: String -// ): List { -// Log.d(RestFlightRepository::class.simpleName, "Find flights") -// -// -// val existFlights = dbFlightRepository.findFlights(from, to, departureDate).associateBy { it.id }.toMutableMap() -// -// service.findFlights(from, to, departureDate) -// .map { it.toFlight() } -// .forEach { flight -> -// existFlights[flight.id] = flight -// } -// -// return existFlights.map { it.value }.sortedBy { it.id } -// } override suspend fun insertFlight(flight: Flight) { service.createFlight(flight.toFlightRemote()).toFlight() diff --git a/app/src/main/java/ru/ulstu/is/airticketrentservice/database/dao/FlightDao.kt b/app/src/main/java/ru/ulstu/is/airticketrentservice/database/dao/FlightDao.kt index ddf4c1b..8a24109 100644 --- a/app/src/main/java/ru/ulstu/is/airticketrentservice/database/dao/FlightDao.kt +++ b/app/src/main/java/ru/ulstu/is/airticketrentservice/database/dao/FlightDao.kt @@ -17,7 +17,7 @@ interface FlightDao { @Query("select * from flights where flights.id = :flightId") fun getFlightById(flightId: Int): Flow - @Query("select * from flights where flights.direction_from like :from or flights.direction_to like :to or flights.departure_date like :departureDate") + @Query("select * from flights where (:from = ' ' or flights.direction_from like :from) and (:to = ' ' or flights.direction_to like :to) and (:departureDate = ' ' or flights.departure_date like :departureDate)") suspend fun findFlights(from: String, to: String, departureDate: String): List @Query("SELECT * FROM flights ORDER BY id ASC") diff --git a/app/src/main/java/ru/ulstu/is/airticketrentservice/navigation/BottomBarScreen.kt b/app/src/main/java/ru/ulstu/is/airticketrentservice/navigation/BottomBarScreen.kt index 3dcbc71..deb14ef 100644 --- a/app/src/main/java/ru/ulstu/is/airticketrentservice/navigation/BottomBarScreen.kt +++ b/app/src/main/java/ru/ulstu/is/airticketrentservice/navigation/BottomBarScreen.kt @@ -7,7 +7,6 @@ import androidx.compose.material.icons.filled.AirplanemodeActive import androidx.compose.material.icons.filled.FlightTakeoff import androidx.compose.material.icons.filled.Payments import androidx.compose.ui.graphics.vector.ImageVector -import ru.ulstu.`is`.airticketrentservice.R sealed class BottomBarScreen( val route: String, @@ -59,14 +58,15 @@ sealed class BottomBarScreen( } } object FoundFlights: BottomBarScreen( - route = "found-flights/{direction_from}-{direction_to}-{departure_date}", + route = "found-flights/{direction_from}/{direction_to}/{departure_date}", title ="", icon = Icons.Filled.FlightTakeoff ) { - fun passText(direction_from: String, direction_to: String, departure_date: String): String { - return "found-flights/$direction_from-$direction_to-$departure_date" + fun passText(direction_from: String?, direction_to: String?, departure_date: String?): String { + return "found-flights/${direction_from?:" "}/${direction_to?:" "}/${departure_date?:" "}" } } + object UserEdit: BottomBarScreen( route = "user-edit/{id}", title ="", diff --git a/app/src/main/java/ru/ulstu/is/airticketrentservice/screen/FlightEdit.kt b/app/src/main/java/ru/ulstu/is/airticketrentservice/screen/FlightEdit.kt index 9ca9391..c955557 100644 --- a/app/src/main/java/ru/ulstu/is/airticketrentservice/screen/FlightEdit.kt +++ b/app/src/main/java/ru/ulstu/is/airticketrentservice/screen/FlightEdit.kt @@ -51,7 +51,8 @@ fun FlightEdit( navController.popBackStack() } }, - onUpdate = viewModel::updateUiState) + onUpdate = viewModel::updateUiState + ) } @OptIn(ExperimentalMaterial3Api::class) diff --git a/app/src/main/java/ru/ulstu/is/airticketrentservice/screen/FlightInfo.kt b/app/src/main/java/ru/ulstu/is/airticketrentservice/screen/FlightInfo.kt index 7714647..a0f0e27 100644 --- a/app/src/main/java/ru/ulstu/is/airticketrentservice/screen/FlightInfo.kt +++ b/app/src/main/java/ru/ulstu/is/airticketrentservice/screen/FlightInfo.kt @@ -46,7 +46,6 @@ fun FlightInfo( FlightInfo( flightUiState = flightViewModel.flightUiState, onClick = { - flightViewModel.setCurrentFlight(ticketViewModel.ticketUiState.ticketDetails.flightId) val route = BottomBarScreen.TicketView.passId(0.toString()) navController.navigate(route) } diff --git a/app/src/main/java/ru/ulstu/is/airticketrentservice/screen/FoundFlights.kt b/app/src/main/java/ru/ulstu/is/airticketrentservice/screen/FoundFlights.kt index cfc2c72..a41be93 100644 --- a/app/src/main/java/ru/ulstu/is/airticketrentservice/screen/FoundFlights.kt +++ b/app/src/main/java/ru/ulstu/is/airticketrentservice/screen/FoundFlights.kt @@ -1,35 +1,19 @@ package ru.ulstu.`is`.airticketrentservice.screen -import androidx.compose.animation.animateColorAsState -import androidx.compose.animation.core.animateFloatAsState -import androidx.compose.foundation.background import androidx.compose.foundation.clickable -import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items -import androidx.compose.material.icons.Icons -import androidx.compose.material.icons.outlined.Delete import androidx.compose.material3.Card import androidx.compose.material3.CardDefaults -import androidx.compose.material3.DismissDirection -import androidx.compose.material3.DismissState -import androidx.compose.material3.ExperimentalMaterial3Api -import androidx.compose.material3.Icon import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Scaffold -import androidx.compose.material3.SwipeToDismiss import androidx.compose.material3.Text -import androidx.compose.material3.rememberDismissState import androidx.compose.runtime.Composable -import androidx.compose.runtime.getValue -import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier -import androidx.compose.ui.draw.scale -import androidx.compose.ui.graphics.Color import androidx.compose.ui.res.colorResource import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.dp @@ -102,7 +86,7 @@ private fun FlightListItem( modifier = modifier.padding(all = 10.dp) ) { Text(text = "${flight.direction_from} --> ${flight.direction_to}") - Text(text = "${flight.one_ticket_cost}") + Text(text = "Цена: ${flight.one_ticket_cost}") } } } diff --git a/app/src/main/java/ru/ulstu/is/airticketrentservice/screen/MainPage.kt b/app/src/main/java/ru/ulstu/is/airticketrentservice/screen/MainPage.kt index 9c3b455..25bc60e 100644 --- a/app/src/main/java/ru/ulstu/is/airticketrentservice/screen/MainPage.kt +++ b/app/src/main/java/ru/ulstu/is/airticketrentservice/screen/MainPage.kt @@ -85,9 +85,9 @@ fun MainPage( modifier: Modifier = Modifier, navController: NavController ) { - val from = remember { mutableStateOf("") } - val to = remember { mutableStateOf("") } - val departureDate = remember { mutableStateOf("") } + val from = remember { mutableStateOf(null) } + val to = remember { mutableStateOf(null) } + val departureDate = remember { mutableStateOf(null) } val state = rememberScrollState() @@ -125,7 +125,7 @@ fun MainPage( TextField(modifier = Modifier .fillMaxWidth() .padding(all = 10.dp), - value = from.value, onValueChange = { from.value = it }, + value = from.value?:"", onValueChange = { from.value = it }, colors = TextFieldDefaults.colors( focusedContainerColor = Color.LightGray.copy(.2f), unfocusedContainerColor = Color.LightGray.copy(.2f), @@ -141,7 +141,7 @@ fun MainPage( TextField(modifier = Modifier .fillMaxWidth() .padding(all = 10.dp), - value = to.value, onValueChange = { to.value = it }, + value = to.value?:"", onValueChange = { to.value = it }, colors = TextFieldDefaults.colors( focusedContainerColor = Color.LightGray.copy(.2f), unfocusedContainerColor = Color.LightGray.copy(.2f), @@ -198,7 +198,7 @@ fun MainPage( Text("Выбрать дату вылета") } - Text(text = " ${departureDate.value}", fontSize = 15.sp, color = Color.DarkGray) + Text(text = " ${departureDate.value?:""}", fontSize = 15.sp, color = Color.DarkGray) Button( modifier = Modifier diff --git a/app/src/main/java/ru/ulstu/is/airticketrentservice/screen/TicketEdit.kt b/app/src/main/java/ru/ulstu/is/airticketrentservice/screen/TicketEdit.kt index 546adff..f703c8c 100644 --- a/app/src/main/java/ru/ulstu/is/airticketrentservice/screen/TicketEdit.kt +++ b/app/src/main/java/ru/ulstu/is/airticketrentservice/screen/TicketEdit.kt @@ -11,6 +11,9 @@ import androidx.compose.material3.Text import androidx.compose.material3.TextField import androidx.compose.material3.TextFieldDefaults import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableDoubleStateOf +import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier @@ -39,6 +42,7 @@ fun TicketEdit( ) { val coroutineScope = rememberCoroutineScope() flightViewModel.setCurrentFlight(viewModel.ticketUiState.ticketDetails.flightId) + val totalCost by remember { mutableDoubleStateOf(flightViewModel.flightUiState.flightDetails.one_ticket_cost * viewModel.ticketUiState.ticketDetails.passengers_count.toDouble()) } TicketEdit( ticketUiState = viewModel.ticketUiState, flightUiState = flightViewModel.flightUiState, @@ -49,7 +53,7 @@ fun TicketEdit( } }, onUpdate = viewModel::updateUiState, - totalCost = flightViewModel.flightUiState.flightDetails.one_ticket_cost * viewModel.ticketUiState.ticketDetails.passengers_count.toDouble() + totalCost = totalCost ) } diff --git a/app/src/main/java/ru/ulstu/is/airticketrentservice/viewModel/FindFlightsViewModel.kt b/app/src/main/java/ru/ulstu/is/airticketrentservice/viewModel/FindFlightsViewModel.kt index c9d519a..f6a2624 100644 --- a/app/src/main/java/ru/ulstu/is/airticketrentservice/viewModel/FindFlightsViewModel.kt +++ b/app/src/main/java/ru/ulstu/is/airticketrentservice/viewModel/FindFlightsViewModel.kt @@ -7,15 +7,8 @@ import androidx.lifecycle.SavedStateHandle import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.flow.SharingStarted -import kotlinx.coroutines.flow.StateFlow -import kotlinx.coroutines.flow.map -import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.launch import kotlinx.coroutines.withContext -import ru.ulstu.`is`.airticketrentservice.api.AppService -import ru.ulstu.`is`.airticketrentservice.api.model.toFlight -import ru.ulstu.`is`.airticketrentservice.database.AppContainer import ru.ulstu.`is`.airticketrentservice.database.models.Flight import ru.ulstu.`is`.airticketrentservice.database.repository.FlightRepository @@ -41,9 +34,3 @@ class FindFlightsViewModel( } data class FoundFlightsUiState(val flightList: List = listOf()) - -// var foundFlightsList: List by mutableStateOf(emptyList()) -// private set - - -//foundFlightsList = appService.foundFlights().map { it.toFlight() } diff --git a/app/src/main/java/ru/ulstu/is/airticketrentservice/viewModel/TicketEditViewModel.kt b/app/src/main/java/ru/ulstu/is/airticketrentservice/viewModel/TicketEditViewModel.kt index df0ecab..807f1a6 100644 --- a/app/src/main/java/ru/ulstu/is/airticketrentservice/viewModel/TicketEditViewModel.kt +++ b/app/src/main/java/ru/ulstu/is/airticketrentservice/viewModel/TicketEditViewModel.kt @@ -1,5 +1,6 @@ package ru.ulstu.`is`.airticketrentservice.viewModel +import android.util.Log import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.setValue @@ -25,6 +26,7 @@ class TicketEditViewModel( private val ticketUid: Int = checkNotNull(savedStateHandle["id"]) + init { viewModelScope.launch { withContext(Dispatchers.IO) { diff --git a/server/data.json b/server/data.json index 858ac75..ed2c1e6 100644 --- a/server/data.json +++ b/server/data.json @@ -35,7 +35,7 @@ { "id": 6, "direction_from": "Ульяновск", - "direction_to": "Москва ", + "direction_to": "Москва", "departure_date": "26-12-2023", "arrival_date": "26-12-2023", "tickets_count": 50, @@ -43,7 +43,7 @@ }, { "direction_from": "Хабаровск", - "direction_to": "Кострома ", + "direction_to": "Кострома", "departure_date": "5-1-2024", "arrival_date": "5-1-2024", "tickets_count": 200, @@ -83,7 +83,7 @@ "id": 2, "surname": "Артамонова", "name": "Татьяна", - "patronymic": "Валерьевн", + "patronymic": "Валерьевна", "date_of_birth": "7-11-2003", "email": "usertt@mail.ru", "password": "usertt", @@ -128,7 +128,7 @@ "password": "password", "role": "user", "id": 6 - }, + }, { "surname": "ккк", "name": "ккк", diff --git a/server/data.json.bak b/server/data.json.bak index d3e8e4d..ed2c1e6 100644 --- a/server/data.json.bak +++ b/server/data.json.bak @@ -35,7 +35,7 @@ { "id": 6, "direction_from": "Ульяновск", - "direction_to": "Москва ", + "direction_to": "Москва", "departure_date": "26-12-2023", "arrival_date": "26-12-2023", "tickets_count": 50, @@ -43,7 +43,7 @@ }, { "direction_from": "Хабаровск", - "direction_to": "Кострома ", + "direction_to": "Кострома", "departure_date": "5-1-2024", "arrival_date": "5-1-2024", "tickets_count": 200, @@ -128,7 +128,7 @@ "password": "password", "role": "user", "id": 6 - }, + }, { "surname": "ккк", "name": "ккк", @@ -141,17 +141,17 @@ } ], "rents": [ - { - "id": 1, + { + "id": 1, "status": "Ожидает подтверждения", "userId": 2, - "ticketId": 1 - }, - { - "id": 2, + "ticketId": 1 + }, + { + "id": 2, "status": "Подтверждено", "userId": 2, "ticketId": 2 - } + } ] } \ No newline at end of file