редактирование билета, подсчет стоимости
This commit is contained in:
parent
8449a95211
commit
f2597c6d61
@ -0,0 +1,54 @@
|
||||
package ru.ulstu.`is`.airticketrentservice.viewModel
|
||||
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import kotlinx.coroutines.launch
|
||||
import ru.ulstu.`is`.airticketrentservice.database.models.Flight
|
||||
import ru.ulstu.`is`.airticketrentservice.database.repository.FlightRepository
|
||||
|
||||
class FlightDropDownViewModel(
|
||||
private val flightRepository: FlightRepository
|
||||
) : ViewModel() {
|
||||
var flightsDropDownListUiState by mutableStateOf(FlightsDropDownListUiState())
|
||||
private set
|
||||
|
||||
var flightDropDownUiState by mutableStateOf(FlightDropDownUiState())
|
||||
private set
|
||||
|
||||
init {
|
||||
viewModelScope.launch {
|
||||
flightsDropDownListUiState = FlightsDropDownListUiState(flightRepository.getAllFlights())
|
||||
}
|
||||
}
|
||||
|
||||
fun setCurrentDropDownFlight(flightId: Int) {
|
||||
val flight: Flight? =
|
||||
flightsDropDownListUiState.flightList.firstOrNull { flight -> flight.id == flightId }
|
||||
flight?.let { updateUiState(it) }
|
||||
}
|
||||
|
||||
fun updateUiState(flight: Flight) {
|
||||
flightDropDownUiState = FlightDropDownUiState(
|
||||
flight = flight
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
data class FlightsDropDownListUiState(val flightList: List<Flight> = listOf())
|
||||
|
||||
data class FlightDropDownUiState(
|
||||
val flight: Flight? = null
|
||||
)
|
||||
|
||||
fun Flight.toUiState() = FlightDropDownUiState(flight = Flight(
|
||||
id = id,
|
||||
direction_from = direction_from,
|
||||
direction_to = direction_to,
|
||||
departure_date = departure_date,
|
||||
arrival_date = arrival_date,
|
||||
tickets_count = tickets_count,
|
||||
one_ticket_cost = one_ticket_cost
|
||||
))
|
@ -0,0 +1,69 @@
|
||||
package ru.ulstu.`is`.airticketrentservice.viewModel
|
||||
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.lifecycle.SavedStateHandle
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import ru.ulstu.`is`.airticketrentservice.database.repository.TicketRepository
|
||||
|
||||
class TicketViewViewModel(
|
||||
savedStateHandle: SavedStateHandle,
|
||||
private val ticketRepository: TicketRepository,
|
||||
) : ViewModel() {
|
||||
|
||||
var ticketUiState by mutableStateOf(TicketUiState())
|
||||
private set
|
||||
|
||||
private val ticketUid: Int = checkNotNull(savedStateHandle["id"])
|
||||
private val flightUid: Int = checkNotNull(savedStateHandle["flightId"])
|
||||
init {
|
||||
viewModelScope.launch {
|
||||
withContext(Dispatchers.IO) {
|
||||
val ticket = ticketRepository.getTicketById(ticketUid)
|
||||
.toUiState(true)
|
||||
launch(Dispatchers.Main) {
|
||||
if (ticketUid > 0) {
|
||||
ticketUiState = ticket
|
||||
ticketUiState.ticketDetails.flightId = flightUid
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun updateUiState(ticketDetails: TicketDetails) {
|
||||
ticketUiState = TicketUiState(
|
||||
ticketDetails = ticketDetails,
|
||||
isEntryValid = validateInput(ticketDetails),
|
||||
ticket = ticketDetails.toTicket()
|
||||
)
|
||||
}
|
||||
|
||||
suspend fun saveTicket() {
|
||||
if (validateInput()) {
|
||||
if (ticketUid > 0) {
|
||||
ticketRepository.updateTicket(
|
||||
ticketUiState.ticketDetails.toTicket(ticketUid)
|
||||
)
|
||||
} else {
|
||||
ticketRepository.insertTicket(
|
||||
ticketUiState.ticketDetails.toTicket()
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun validateInput(uiState: TicketDetails = ticketUiState.ticketDetails): Boolean {
|
||||
return with(uiState) {
|
||||
passengers_count > 0
|
||||
&& ticket_cost > 0
|
||||
&& flightId > 0
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user