маленько оптимизировала поиск

This commit is contained in:
Татьяна Артамонова 2023-12-25 17:47:51 +04:00
parent 51771c73c0
commit 60c1eb2234
12 changed files with 44 additions and 105 deletions

View File

@ -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<Flight> {
// 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<Flight> {
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<Flight> {
@ -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<Flight> {
// 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()

View File

@ -17,7 +17,7 @@ interface FlightDao {
@Query("select * from flights where flights.id = :flightId")
fun getFlightById(flightId: Int): Flow<Flight>
@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<Flight>
@Query("SELECT * FROM flights ORDER BY id ASC")

View File

@ -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 ="",

View File

@ -51,7 +51,8 @@ fun FlightEdit(
navController.popBackStack()
}
},
onUpdate = viewModel::updateUiState)
onUpdate = viewModel::updateUiState
)
}
@OptIn(ExperimentalMaterial3Api::class)

View File

@ -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)
}

View File

@ -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}")
}
}
}

View File

@ -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<String?>(null) }
val to = remember { mutableStateOf<String?>(null) }
val departureDate = remember { mutableStateOf<String?>(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

View File

@ -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
)
}

View File

@ -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<Flight> = listOf())
// var foundFlightsList: List<Flight> by mutableStateOf(emptyList())
// private set
//foundFlightsList = appService.foundFlights().map { it.toFlight() }

View File

@ -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) {

View File

@ -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": "ккк",

View File

@ -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
}
}
]
}