Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve trip request parameters #28

Merged
merged 1 commit into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import androidx.compose.runtime.Immutable
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import ch.opentransportdata.ojp.data.dto.request.tir.PlaceReferenceDto
import ch.opentransportdata.ojp.data.dto.request.tir.TripParamsDto
import ch.opentransportdata.ojp.data.dto.response.PlaceResultDto
import ch.opentransportdata.ojp.data.dto.response.delivery.TripDeliveryDto
import ch.opentransportdata.ojp.data.dto.response.place.StopPlaceDto
import ch.opentransportdata.ojp.domain.model.Result
import ch.opentransportdata.presentation.MainActivity
import kotlinx.coroutines.flow.MutableStateFlow
Expand Down Expand Up @@ -107,10 +109,28 @@ class TripResultViewModel(

viewModelScope.launch {
state.update { it.copy(isLoading = true) }
val originRef = PlaceReferenceDto(
ref = (origin.place.placeType as? StopPlaceDto)?.stopPlaceRef,
stationName = (origin.place.placeType as? StopPlaceDto)?.name,
position = origin.place.position
)
val destinationRef = PlaceReferenceDto(
ref = (destination.place.placeType as? StopPlaceDto)?.stopPlaceRef,
stationName = (destination.place.placeType as? StopPlaceDto)?.name,
position = destination.place.position
)
val viaRef = via?.let {
PlaceReferenceDto(
ref = (it.place.placeType as? StopPlaceDto)?.stopPlaceRef,
stationName = (it.place.placeType as? StopPlaceDto)?.name,
position = it.place.position
)
}

val response = MainActivity.ojpSdk.requestTrips(
origin = origin,
destination = destination,
via = via,
origin = originRef,
destination = destinationRef,
via = viaRef,
time = LocalDateTime.now(),
params = TripParamsDto(
numberOfResults = 10,
Expand Down
7 changes: 4 additions & 3 deletions sdk/src/main/java/ch/opentransportdata/ojp/OjpSdk.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.opentransportdata.ojp

import ch.opentransportdata.ojp.data.dto.request.tir.PlaceReferenceDto
import ch.opentransportdata.ojp.data.dto.request.tir.TripParamsDto
import ch.opentransportdata.ojp.data.dto.response.PlaceResultDto
import ch.opentransportdata.ojp.data.dto.response.delivery.TripDeliveryDto
Expand Down Expand Up @@ -77,9 +78,9 @@ class OjpSdk(
* @return [TripDeliveryDto] object with related trip information
*/
suspend fun requestTrips(
origin: PlaceResultDto,
destination: PlaceResultDto,
via: PlaceResultDto? = null,
origin: PlaceReferenceDto,
destination: PlaceReferenceDto,
via: PlaceReferenceDto? = null,
time: LocalDateTime,
isSearchForDepartureTime: Boolean = true,
params: TripParamsDto?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ import com.tickaroo.tikxml.annotation.Xml

/**
* Created by Michael Ruppen on 07.06.2024
*
* Either [ref] and [stationName] has to be set or [position].
*/
//todo: normally should create all the reference objects
@Xml(name = "PlaceRef")
internal data class PlaceReferenceDto(
data class PlaceReferenceDto(
//If more types needed, create custom typeAdapter and parse only necessary
@PropertyElement(name = "StopPlaceRef")
val ref: String? = null,
@Element(name = "StopPlaceName")
val stationName: NameDto?,
val stationName: NameDto?,
@Element(name = "GeoPosition") //todo: check if schema is correct (when working on backend), think of solution where only send this if is Address
val position: GeoPositionDto? = null
)
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package ch.opentransportdata.ojp.data.remote.trip

import ch.opentransportdata.ojp.data.dto.OjpDto
import ch.opentransportdata.ojp.data.dto.request.tir.PlaceReferenceDto
import ch.opentransportdata.ojp.data.dto.request.tir.TripParamsDto
import ch.opentransportdata.ojp.data.dto.response.PlaceResultDto
import java.time.LocalDateTime

/**
* Created by Michael Ruppen on 27.06.2024
*/
internal interface RemoteTripDataSource {
suspend fun requestTrips(
origin: PlaceResultDto,
destination: PlaceResultDto,
via: PlaceResultDto? = null,
origin: PlaceReferenceDto,
destination: PlaceReferenceDto,
via: PlaceReferenceDto? = null,
time: LocalDateTime,
isSearchForDepartureTime: Boolean,
params: TripParamsDto?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import ch.opentransportdata.ojp.data.dto.OjpDto
import ch.opentransportdata.ojp.data.dto.request.OjpRequestDto
import ch.opentransportdata.ojp.data.dto.request.ServiceRequestDto
import ch.opentransportdata.ojp.data.dto.request.tir.*
import ch.opentransportdata.ojp.data.dto.response.PlaceResultDto
import ch.opentransportdata.ojp.data.dto.response.place.AddressDto
import ch.opentransportdata.ojp.data.dto.response.place.StopPlaceDto
import ch.opentransportdata.ojp.data.dto.response.NameDto
import ch.opentransportdata.ojp.data.remote.OjpService
import ch.opentransportdata.ojp.domain.usecase.Initializer
import kotlinx.coroutines.Dispatchers
Expand All @@ -26,61 +24,26 @@ internal class RemoteTripDataSourceImpl(


override suspend fun requestTrips(
origin: PlaceResultDto,
destination: PlaceResultDto,
via: PlaceResultDto?,
origin: PlaceReferenceDto,
destination: PlaceReferenceDto,
via: PlaceReferenceDto?,
time: LocalDateTime,
isSearchForDepartureTime: Boolean,
params: TripParamsDto?
): OjpDto = withContext(Dispatchers.IO) {
val requestTime = LocalDateTime.now()

val originName = when (origin.place.placeType) {
is StopPlaceDto -> origin.place.placeType.name
is AddressDto -> origin.place.placeType.name
else -> null
}

val originPlace = PlaceContextDto(
placeReference = PlaceReferenceDto(
ref = (origin.place.placeType as? StopPlaceDto)?.stopPlaceRef,
stationName = originName,
position = origin.place.position
),
placeReference = origin,
departureArrivalTime = if (isSearchForDepartureTime) time else null
)

val destinationName = when (destination.place.placeType) {
is StopPlaceDto -> destination.place.placeType.name
is AddressDto -> destination.place.placeType.name
else -> null
}

val destinationPlace = PlaceContextDto(
placeReference = PlaceReferenceDto(
ref = (destination.place.placeType as? StopPlaceDto)?.stopPlaceRef,
stationName = destinationName,
position = destination.place.position
),
placeReference = destination,
departureArrivalTime = if (isSearchForDepartureTime) null else time
)

val vias = via?.let {
val viaName = when (it.place.placeType) {
is StopPlaceDto -> it.place.placeType.name
is AddressDto -> it.place.placeType.name
else -> null
}

listOf(
TripVia(
viaPoint = PlaceReferenceDto(
ref = (it.place.placeType as? StopPlaceDto)?.stopPlaceRef,
stationName = viaName,
position = it.place.position
)
)
)
listOf(TripVia(viaPoint = it))
}

val request = createRequest(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.opentransportdata.ojp.data.repository

import ch.opentransportdata.ojp.data.dto.request.tir.PlaceReferenceDto
import ch.opentransportdata.ojp.data.dto.request.tir.TripParamsDto
import ch.opentransportdata.ojp.data.dto.response.PlaceResultDto
import ch.opentransportdata.ojp.data.dto.response.delivery.LocationInformationDeliveryDto
Expand Down Expand Up @@ -58,9 +59,9 @@ internal class OjpRepositoryImpl(
}

override suspend fun requestTrips(
origin: PlaceResultDto,
destination: PlaceResultDto,
via: PlaceResultDto?,
origin: PlaceReferenceDto,
destination: PlaceReferenceDto,
via: PlaceReferenceDto?,
time: LocalDateTime,
isSearchForDepartureTime: Boolean,
params: TripParamsDto?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.opentransportdata.ojp.domain.repository

import ch.opentransportdata.ojp.data.dto.request.tir.PlaceReferenceDto
import ch.opentransportdata.ojp.data.dto.request.tir.TripParamsDto
import ch.opentransportdata.ojp.data.dto.response.PlaceResultDto
import ch.opentransportdata.ojp.data.dto.response.delivery.TripDeliveryDto
Expand All @@ -20,9 +21,9 @@ internal interface OjpRepository {
): Result<List<PlaceResultDto>>

suspend fun requestTrips(
origin: PlaceResultDto,
destination: PlaceResultDto,
via: PlaceResultDto? = null,
origin: PlaceReferenceDto,
destination: PlaceReferenceDto,
via: PlaceReferenceDto? = null,
time: LocalDateTime,
isSearchForDepartureTime: Boolean,
params: TripParamsDto?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ch.opentransportdata.ojp.domain.usecase

import ch.opentransportdata.ojp.data.dto.request.tir.PlaceReferenceDto
import ch.opentransportdata.ojp.data.dto.request.tir.TripParamsDto
import ch.opentransportdata.ojp.data.dto.response.PlaceResultDto
import ch.opentransportdata.ojp.data.dto.response.delivery.TripDeliveryDto
import ch.opentransportdata.ojp.data.dto.response.tir.TripResultDto
import ch.opentransportdata.ojp.data.dto.response.tir.trips.TripDto
Expand All @@ -23,9 +23,9 @@ internal class RequestTrips(
private var state = TripRequestState()

suspend operator fun invoke(
origin: PlaceResultDto,
destination: PlaceResultDto,
via: PlaceResultDto? = null,
origin: PlaceReferenceDto,
destination: PlaceReferenceDto,
via: PlaceReferenceDto? = null,
time: LocalDateTime,
isSearchForDepartureTime: Boolean,
params: TripParamsDto?
Expand Down Expand Up @@ -124,9 +124,9 @@ internal class RequestTrips(
}

data class TripRequestState(
val origin: PlaceResultDto? = null,
val destination: PlaceResultDto? = null,
val via: PlaceResultDto? = null,
val origin: PlaceReferenceDto? = null,
val destination: PlaceReferenceDto? = null,
val via: PlaceReferenceDto? = null,
val time: LocalDateTime? = null,
val isSearchForDepartureTime: Boolean = true,
val params: TripParamsDto? = null,
Expand Down
Loading