commit
This commit is contained in:
parent
b1e844c956
commit
03c16aacd7
@ -1,6 +1,7 @@
|
||||
package ru.ulstu.`is`.airticketrentservice
|
||||
|
||||
import android.app.Application
|
||||
import ru.ulstu.`is`.airticketrentservice.api.AppService
|
||||
import ru.ulstu.`is`.airticketrentservice.database.AppContainer
|
||||
import ru.ulstu.`is`.airticketrentservice.database.AppDataContainer
|
||||
|
||||
|
@ -28,14 +28,19 @@ interface AppService {
|
||||
@Query("_page") page: Int,
|
||||
@Query("_limit") limit: Int,
|
||||
): List<RentRemote>
|
||||
@GET("findFlights/{direction_from}-{direction_to}-{departure_date}")
|
||||
suspend fun findFlights(
|
||||
@Path("direction_from") from: String,
|
||||
@Path("direction_to") to: String,
|
||||
@Path("departure_date") departureDate: String
|
||||
@GET("searchFlights")
|
||||
suspend fun foundFlights(
|
||||
): List<FlightRemote>
|
||||
// suspend fun findFlights(
|
||||
// @Path("direction_from") from: String,
|
||||
// @Path("direction_to") to: String,
|
||||
// @Path("departure_date") departureDate: String
|
||||
// ): List<FlightRemote>
|
||||
@GET("tickets")
|
||||
suspend fun getAllTickets(): List<TicketRemote>
|
||||
|
||||
@GET("foundFlights")
|
||||
suspend fun getAllFlights(): List<FlightRemote>
|
||||
@GET("tickets")
|
||||
suspend fun getTickets(
|
||||
@Query("_page") page: Int,
|
||||
@ -126,7 +131,7 @@ interface AppService {
|
||||
@Path("id") id: Int,
|
||||
): TicketRemote
|
||||
|
||||
companion object {
|
||||
companion object : AppService {
|
||||
private const val BASE_URL = "http://192.168.1.100:8079/"
|
||||
|
||||
@Volatile
|
||||
|
@ -1,7 +1,9 @@
|
||||
package ru.ulstu.`is`.airticketrentservice.api.model
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.json.Json
|
||||
import ru.ulstu.`is`.airticketrentservice.database.models.Flight
|
||||
import java.io.File
|
||||
|
||||
@Serializable
|
||||
data class FlightRemote(
|
||||
@ -32,4 +34,22 @@ fun Flight.toFlightRemote(): FlightRemote = FlightRemote(
|
||||
arrival_date,
|
||||
tickets_count,
|
||||
one_ticket_cost
|
||||
)
|
||||
)
|
||||
|
||||
fun foundFlights(
|
||||
from: String,
|
||||
to: String,
|
||||
departureDate: String
|
||||
): List<Flight> {
|
||||
val json = File("C:\\Users\\User\\AndroidStudioProjects\\AirTicketRentService\\server").readText()
|
||||
|
||||
val flights = Json.decodeFromString<List<Flight>>(json)
|
||||
|
||||
val matchingFlights = flights.filter { flight ->
|
||||
flight.direction_from == from ||
|
||||
flight.direction_to == to ||
|
||||
flight.departure_date == departureDate
|
||||
}
|
||||
|
||||
return matchingFlights
|
||||
}
|
@ -14,6 +14,7 @@ import retrofit2.Call
|
||||
import retrofit2.Callback
|
||||
import retrofit2.Response
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import kotlinx.serialization.json.Json
|
||||
import ru.ulstu.`is`.airticketrentservice.api.AppService
|
||||
import ru.ulstu.`is`.airticketrentservice.api.model.toFlight
|
||||
import ru.ulstu.`is`.airticketrentservice.api.model.toFlightRemote
|
||||
@ -25,6 +26,7 @@ import ru.ulstu.`is`.airticketrentservice.database.repository.FlightRepository
|
||||
import ru.ulstu.`is`.airticketrentservice.database.repository.OfflineRemoteKeyRepository
|
||||
import ru.ulstu.`is`.airticketrentservice.api.mediator.FlightRemoteMediator
|
||||
import ru.ulstu.`is`.airticketrentservice.api.model.FlightRemote
|
||||
import java.io.File
|
||||
import kotlin.coroutines.resume
|
||||
import kotlin.coroutines.resumeWithException
|
||||
import kotlin.coroutines.suspendCoroutine
|
||||
@ -59,24 +61,46 @@ class RestFlightRepository(
|
||||
override suspend fun getFlightById(flightId: Int): Flight =
|
||||
service.getFlight(flightId).toFlight()
|
||||
|
||||
override suspend fun findFlights(
|
||||
override suspend fun foundFlights(
|
||||
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()
|
||||
val existFlights = dbFlightRepository.getAllFlights().associateBy { it.id }.toMutableMap()
|
||||
|
||||
service.findFlights(from, to, departureDate)
|
||||
service.foundFlights()
|
||||
.map { it.toFlight() }
|
||||
.forEach { flight ->
|
||||
flight.direction_from == from ||
|
||||
flight.direction_to == to ||
|
||||
flight.departure_date == departureDate
|
||||
existFlights[flight.id] = flight
|
||||
}
|
||||
|
||||
return existFlights.map { it.value }.sortedBy { it.id }
|
||||
|
||||
}
|
||||
|
||||
// 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()
|
||||
}
|
||||
|
@ -9,6 +9,6 @@ interface FlightRepository {
|
||||
suspend fun updateFlight(flight: Flight)
|
||||
suspend fun deleteFlight(flight: Flight)
|
||||
suspend fun getFlightById(flightId: Int): Flight
|
||||
suspend fun findFlights(from: String, to: String, departureDate: String): List<Flight>
|
||||
suspend fun foundFlights(from: String, to: String, departureDate: String): List<Flight>
|
||||
fun getFlights(): Flow<PagingData<Flight>>
|
||||
}
|
@ -11,6 +11,7 @@ import ru.ulstu.`is`.airticketrentservice.database.models.Flight
|
||||
import ru.ulstu.`is`.airticketrentservice.database.models.Rent
|
||||
|
||||
class OfflineFlightRepository(private val flightDao: FlightDao) : FlightRepository{
|
||||
suspend fun getAllFlights(): List<Flight> = flightDao.getAll()
|
||||
override suspend fun insertFlight(flight: Flight) = flightDao.insert(flight)
|
||||
override suspend fun updateFlight(flight: Flight) = flightDao.update(flight)
|
||||
override suspend fun deleteFlight(flight: Flight) = flightDao.delete(flight)
|
||||
@ -24,7 +25,7 @@ class OfflineFlightRepository(private val flightDao: FlightDao) : FlightReposito
|
||||
).flow
|
||||
|
||||
fun getAllFlightsPagingSource(): PagingSource<Int, Flight> = flightDao.getFlights()
|
||||
override suspend fun findFlights(from: String, to: String, departureDate: String): List<Flight> = flightDao.findFlights(from, to, departureDate)
|
||||
override suspend fun foundFlights(from: String, to: String, departureDate: String): List<Flight> = flightDao.findFlights(from, to, departureDate)
|
||||
suspend fun clearFlights() = flightDao.deleteAll()
|
||||
suspend fun insertFlights(flights: List<Flight>) =
|
||||
flightDao.insert(*flights.toTypedArray())
|
||||
|
@ -40,14 +40,13 @@ import ru.ulstu.`is`.airticketrentservice.database.models.Flight
|
||||
import ru.ulstu.`is`.airticketrentservice.navigation.BottomBarScreen
|
||||
import ru.ulstu.`is`.airticketrentservice.viewModel.AppViewModelProvider
|
||||
import ru.ulstu.`is`.airticketrentservice.viewModel.FindFlightsViewModel
|
||||
import ru.ulstu.`is`.airticketrentservice.viewModel.FoundFlightsUiState
|
||||
|
||||
@Composable
|
||||
fun FoundFlights(
|
||||
navController: NavController,
|
||||
viewModel: FindFlightsViewModel = viewModel(factory = AppViewModelProvider.Factory)
|
||||
) {
|
||||
val foundFlightsUiState = viewModel.foundFlightsUiState
|
||||
val foundFlightsUiState = viewModel.foundFlightsList
|
||||
|
||||
Scaffold(
|
||||
topBar = {}
|
||||
@ -56,7 +55,7 @@ fun FoundFlights(
|
||||
modifier = Modifier
|
||||
.padding(innerPadding)
|
||||
.fillMaxSize(),
|
||||
flightList = foundFlightsUiState.flightList,
|
||||
flightList = foundFlightsUiState,
|
||||
onClick = { uid: Int ->
|
||||
val route = BottomBarScreen.FlightInfo.passId(uid.toString())
|
||||
navController.navigate(route)
|
||||
|
@ -6,6 +6,7 @@ import androidx.lifecycle.viewmodel.CreationExtras
|
||||
import androidx.lifecycle.viewmodel.initializer
|
||||
import androidx.lifecycle.viewmodel.viewModelFactory
|
||||
import ru.ulstu.`is`.airticketrentservice.TicketApplication
|
||||
import ru.ulstu.`is`.airticketrentservice.api.AppService
|
||||
|
||||
object AppViewModelProvider {
|
||||
val Factory = viewModelFactory {
|
||||
@ -52,7 +53,8 @@ object AppViewModelProvider {
|
||||
initializer {
|
||||
FindFlightsViewModel(
|
||||
this.createSavedStateHandle(),
|
||||
ticketApplication().container.flightRestRepository
|
||||
ticketApplication().container.flightRestRepository,
|
||||
ticketApplication().container.appService
|
||||
)
|
||||
}
|
||||
initializer {
|
||||
|
@ -11,24 +11,36 @@ import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.stateIn
|
||||
import kotlinx.coroutines.launch
|
||||
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
|
||||
|
||||
class FindFlightsViewModel(
|
||||
savedStateHandle: SavedStateHandle,
|
||||
private val flightRepository: FlightRepository
|
||||
private val flightRepository: FlightRepository,
|
||||
|
||||
) : ViewModel() {
|
||||
private val from: String = checkNotNull(savedStateHandle["direction_from"])
|
||||
private val to: String = checkNotNull(savedStateHandle["direction_to"])
|
||||
private val departureDate: String = checkNotNull(savedStateHandle["departure_date"])
|
||||
|
||||
var foundFlightsUiState by mutableStateOf(FoundFlightsUiState())
|
||||
private val appService: AppService
|
||||
get() {
|
||||
return AppService
|
||||
}
|
||||
var foundFlightsList: List<Flight> by mutableStateOf(emptyList())
|
||||
private set
|
||||
|
||||
|
||||
|
||||
// var foundFlightsUiState by mutableStateOf(FoundFlightsUiState())
|
||||
// private set
|
||||
|
||||
init {
|
||||
viewModelScope.launch {
|
||||
foundFlightsUiState = FoundFlightsUiState(flightRepository.findFlights(from, to, departureDate))
|
||||
foundFlightsList = appService.foundFlights().map { it.toFlight() }
|
||||
//foundFlightsUiState = FoundFlightsUiState(flightRepository.foundFlights(from, to, departureDate))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,7 @@
|
||||
"flightId": 2
|
||||
}
|
||||
],
|
||||
"flights": [
|
||||
"searchFlights": [
|
||||
{
|
||||
"id": 2,
|
||||
"direction_from": "a",
|
||||
@ -17,15 +17,6 @@
|
||||
"tickets_count": 100,
|
||||
"one_ticket_cost": 1000
|
||||
},
|
||||
{
|
||||
"direction_from": "ульск",
|
||||
"direction_to": "мск",
|
||||
"departure_date": "24-12-2023",
|
||||
"arrival_date": "24-12-2023",
|
||||
"tickets_count": 20,
|
||||
"one_ticket_cost": 2000,
|
||||
"id": 3
|
||||
},
|
||||
{
|
||||
"direction_from": "Санкт-Петербург ",
|
||||
"direction_to": "Сочи",
|
||||
@ -36,14 +27,16 @@
|
||||
"id": 5
|
||||
},
|
||||
{
|
||||
"id": 6,
|
||||
"direction_from": "Ульяновск ",
|
||||
"direction_to": "Москва ",
|
||||
"departure_date": "26-12-2023",
|
||||
"arrival_date": "26-12-2023",
|
||||
"tickets_count": 40,
|
||||
"one_ticket_cost": 2680,
|
||||
"id": 6
|
||||
},
|
||||
"tickets_count": 50,
|
||||
"one_ticket_cost": 2680
|
||||
}
|
||||
],
|
||||
"flights": [
|
||||
{
|
||||
"direction_from": "Хабаровск ",
|
||||
"direction_to": "Кострома ",
|
||||
@ -121,6 +114,26 @@
|
||||
"password": "aaa",
|
||||
"role": "user",
|
||||
"id": 5
|
||||
},
|
||||
{
|
||||
"surname": "фффф",
|
||||
"name": "ффф",
|
||||
"patronymic": "ффф",
|
||||
"date_of_birth": "7-5-2005",
|
||||
"email": "userff@mail.ru",
|
||||
"password": "password",
|
||||
"role": "user",
|
||||
"id": 6
|
||||
},
|
||||
{
|
||||
"surname": "ккк",
|
||||
"name": "ккк",
|
||||
"patronymic": "ккк",
|
||||
"date_of_birth": "23-12-2011",
|
||||
"email": "kkk@mail.ru",
|
||||
"password": "kkk",
|
||||
"role": "user",
|
||||
"id": 7
|
||||
}
|
||||
],
|
||||
"rents": [
|
||||
|
@ -7,7 +7,7 @@
|
||||
"flightId": 2
|
||||
}
|
||||
],
|
||||
"flights": [
|
||||
"foundFlights": [
|
||||
{
|
||||
"id": 2,
|
||||
"direction_from": "a",
|
||||
@ -17,15 +17,6 @@
|
||||
"tickets_count": 100,
|
||||
"one_ticket_cost": 1000
|
||||
},
|
||||
{
|
||||
"direction_from": "ульск",
|
||||
"direction_to": "мск",
|
||||
"departure_date": "24-12-2023",
|
||||
"arrival_date": "24-12-2023",
|
||||
"tickets_count": 20,
|
||||
"one_ticket_cost": 2000,
|
||||
"id": 3
|
||||
},
|
||||
{
|
||||
"direction_from": "Санкт-Петербург ",
|
||||
"direction_to": "Сочи",
|
||||
@ -36,14 +27,16 @@
|
||||
"id": 5
|
||||
},
|
||||
{
|
||||
"id": 6,
|
||||
"direction_from": "Ульяновск ",
|
||||
"direction_to": "Москва ",
|
||||
"departure_date": "26-12-2023",
|
||||
"arrival_date": "26-12-2023",
|
||||
"tickets_count": 40,
|
||||
"one_ticket_cost": 2680,
|
||||
"id": 6
|
||||
},
|
||||
"tickets_count": 50,
|
||||
"one_ticket_cost": 2680
|
||||
}
|
||||
],
|
||||
"flights": [
|
||||
{
|
||||
"direction_from": "Хабаровск ",
|
||||
"direction_to": "Кострома ",
|
||||
@ -117,10 +110,30 @@
|
||||
"name": "шчшчгч",
|
||||
"patronymic": "шчшчшч",
|
||||
"date_of_birth": "10-12-2015",
|
||||
"email": "aaa@mail.ru",
|
||||
"email": "aa@mail.ru",
|
||||
"password": "aaa",
|
||||
"role": "user",
|
||||
"id": 5
|
||||
},
|
||||
{
|
||||
"surname": "фффф",
|
||||
"name": "ффф",
|
||||
"patronymic": "ффф",
|
||||
"date_of_birth": "7-5-2005",
|
||||
"email": "userff@mail.ru",
|
||||
"password": "password",
|
||||
"role": "user",
|
||||
"id": 6
|
||||
},
|
||||
{
|
||||
"surname": "ккк",
|
||||
"name": "ккк",
|
||||
"patronymic": "ккк",
|
||||
"date_of_birth": "23-12-2011",
|
||||
"email": "kkk@mail.ru",
|
||||
"password": "kkk",
|
||||
"role": "user",
|
||||
"id": 7
|
||||
}
|
||||
],
|
||||
"rents": [
|
||||
|
Loading…
Reference in New Issue
Block a user