Author: Adam Evyčędo <git@apiote.xyz>
find journeys with params
%!v(PANIC=String method: strings: negative Repeat count)
diff --git a/app/src/main/java/xyz/apiote/bimba/czwek/api/transitousJourney.kt b/app/src/main/java/xyz/apiote/bimba/czwek/api/transitousJourney.kt index be51e850fc9262faf148f370e7acb8a8318b27de..c1cdf7f3c39aeea223a67d41f4b38c91beb0cbb4 100644 --- a/app/src/main/java/xyz/apiote/bimba/czwek/api/transitousJourney.kt +++ b/app/src/main/java/xyz/apiote/bimba/czwek/api/transitousJourney.kt @@ -8,26 +8,37 @@ import android.content.Context import android.util.Log import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext +import okhttp3.OkHttpClient import xyz.apiote.bimba.czwek.R import xyz.apiote.bimba.czwek.api.transitous.api.RoutingApi +import xyz.apiote.bimba.czwek.api.transitous.model.PedestrianProfile import xyz.apiote.bimba.czwek.repo.Colour import xyz.apiote.bimba.czwek.repo.CongestionLevel import xyz.apiote.bimba.czwek.repo.Event import xyz.apiote.bimba.czwek.repo.Journey +import xyz.apiote.bimba.czwek.repo.JourneyParams import xyz.apiote.bimba.czwek.repo.Leg import xyz.apiote.bimba.czwek.repo.LineStub import xyz.apiote.bimba.czwek.repo.LineType import xyz.apiote.bimba.czwek.repo.OccupancyStatus import xyz.apiote.bimba.czwek.repo.Place import xyz.apiote.bimba.czwek.repo.Position +import xyz.apiote.bimba.czwek.repo.TimeReference import xyz.apiote.bimba.czwek.repo.TrafficResponseException import xyz.apiote.bimba.czwek.repo.Vehicle import xyz.apiote.bimba.czwek.units.Metre import xyz.apiote.bimba.czwek.units.Mps import xyz.apiote.bimba.czwek.units.Second +import java.time.Duration import java.time.ZoneId +import java.time.ZonedDateTime -suspend fun getJourney(from: Place, to: Place, context: Context): List<Journey> { +suspend fun getJourney( + from: Place, + to: Place, + params: JourneyParams, + context: Context +): List<Journey> { if (!isNetworkAvailable(context)) { throw TrafficResponseException(0, "", Error(0, R.string.error_offline, R.drawable.error_net)) } @@ -35,7 +46,28 @@ return withContext(Dispatchers.IO) { Log.d("Journeys", "from: ${from.planString()}") Log.d("Journeys", "to: ${to.planString()}") - val response = RoutingApi().plan(from.planString(), to.planString(), maxTransfers = null) + Log.d( + "Journeys", "params: $params; time: ${ + ZonedDateTime.of(params.getSafeDate(), params.getSafeTime(), ZoneId.systemDefault()) + .toOffsetDateTime() + }" + ) + val client = OkHttpClient.Builder() + .callTimeout(Duration.ofSeconds(60)) + .readTimeout(Duration.ofSeconds(60)) + .writeTimeout(Duration.ofSeconds(60)) + .connectTimeout(Duration.ofSeconds(60)).build() + val response = RoutingApi(client = client).plan( + from.planString(), + to.planString(), + maxTransfers = null, + maxTravelTime = null, + time = ZonedDateTime.of(params.getSafeDate(), params.getSafeTime(), ZoneId.systemDefault()) + .toOffsetDateTime(), + arriveBy = params.timeReference == TimeReference.ARRIVE_BY, + requireBikeTransport = params.bicycle, + pedestrianProfile = if (params.wheelchairAccessible) PedestrianProfile.WHEELCHAIR else PedestrianProfile.FOOT, + ) Log.d("Journeys", "${response.itineraries}") response.itineraries.map { val legs: List<Leg> = it.legs.map { diff --git a/app/src/main/java/xyz/apiote/bimba/czwek/journeys/JourneysActivity.kt b/app/src/main/java/xyz/apiote/bimba/czwek/journeys/JourneysActivity.kt index cda4b2d339642c1596e99e5ebfeb6b5b11f999d7..df585e19e87fbc83d7dac59de1f5af37351ec5e5 100644 --- a/app/src/main/java/xyz/apiote/bimba/czwek/journeys/JourneysActivity.kt +++ b/app/src/main/java/xyz/apiote/bimba/czwek/journeys/JourneysActivity.kt @@ -33,7 +33,6 @@ import xyz.apiote.bimba.czwek.R import xyz.apiote.bimba.czwek.databinding.ActivityJourneysBinding import xyz.apiote.bimba.czwek.dpToPixelI import xyz.apiote.bimba.czwek.repo.JourneyParams -import xyz.apiote.bimba.czwek.repo.LineType import xyz.apiote.bimba.czwek.repo.Place import kotlin.math.max import kotlin.math.min @@ -72,6 +71,13 @@ @Suppress("DEPRECATION") intent.getParcelableExtra(DESTINATION_PARAM) } ?: throw Exception("Destination not given") + private fun getJourneyParams(): JourneyParams = if (Build.VERSION.SDK_INT > Build.VERSION_CODES.TIRAMISU) { + intent.getParcelableExtra(PARAMS_PARAM, JourneyParams::class.java) + } else { + @Suppress("DEPRECATION") + intent.getParcelableExtra(PARAMS_PARAM) + } ?: throw Exception("Params not given") + override fun onCreate(savedInstanceState: Bundle?) { enableEdgeToEdge() super.onCreate(savedInstanceState) @@ -104,6 +110,7 @@ binding.journeys.layoutManager = LinearLayoutManager(this) val origin = getOrigin() val destination = getDestination() + val params = getJourneyParams() journeysViewModel.journeys.observe(this) { zoomMap(100) @@ -154,7 +161,7 @@ } val shape = Polyline() val paint = shape.outlinePaint paint.color = leg.start.vehicle.Line.colour.toInt() // TODO contrast - if (leg.start.vehicle.Line.kind == LineType.WALK) { // TODO and other active + if (leg.start.vehicle.Line.kind.isActive()) { paint.setPathEffect(DashPathEffect(floatArrayOf(10f, 10f), 0f)) } shape.setPoints(shapePoints) @@ -166,7 +173,7 @@ // todo move map accordingly } } } - journeysViewModel.getJourneys(this, origin, destination) + journeysViewModel.getJourneys(this, origin, destination, params) } override fun onStart() { diff --git a/app/src/main/java/xyz/apiote/bimba/czwek/journeys/JourneysViewModel.kt b/app/src/main/java/xyz/apiote/bimba/czwek/journeys/JourneysViewModel.kt index 4081f971ed450472ea46d74b26238ba8d4aa8e22..40857260ab38b817daed4199d8050bc667de02ef 100644 --- a/app/src/main/java/xyz/apiote/bimba/czwek/journeys/JourneysViewModel.kt +++ b/app/src/main/java/xyz/apiote/bimba/czwek/journeys/JourneysViewModel.kt @@ -11,23 +11,26 @@ import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import kotlinx.coroutines.launch import xyz.apiote.bimba.czwek.repo.Journey +import xyz.apiote.bimba.czwek.repo.JourneyParams import xyz.apiote.bimba.czwek.repo.Place +import java.net.SocketTimeoutException class JourneysViewModel : ViewModel() { private val _journeys = MutableLiveData<List<Journey>>() val journeys: LiveData<List<Journey>> = _journeys - fun getJourneys(context: Context, origin: Place, destination: Place) { + fun getJourneys(context: Context, origin: Place, destination: Place, params: JourneyParams) { viewModelScope.launch { Log.i("Journeys", "getting journeys") - _journeys.value = xyz.apiote.bimba.czwek.api.getJourney(origin, destination, context) - Log.i("Journeys", "got ${journeys.value?.size} journeys") - - journeys.value?.forEach { - Log.i("Journeys", "$it") + try { + _journeys.value = + xyz.apiote.bimba.czwek.api.getJourney(origin, destination, params, context) + Log.i("Journeys", "got ${journeys.value?.size} journeys") + } catch (e: SocketTimeoutException) { + _journeys.value = emptyList<Journey>() + Log.e("Journeys", "timeout: $e") } } - } -} \ No newline at end of file +} diff --git a/app/src/main/java/xyz/apiote/bimba/czwek/repo/JourneyParams.kt b/app/src/main/java/xyz/apiote/bimba/czwek/repo/JourneyParams.kt index 7c67c644d249c7d3e230ebb847d997508707f061..6202c01e394e923f2dc5bac7e90ceb472b5499ed 100644 --- a/app/src/main/java/xyz/apiote/bimba/czwek/repo/JourneyParams.kt +++ b/app/src/main/java/xyz/apiote/bimba/czwek/repo/JourneyParams.kt @@ -6,8 +6,10 @@ package xyz.apiote.bimba.czwek.repo import android.os.Parcelable import kotlinx.parcelize.Parcelize +import java.time.Instant import java.time.LocalDate import java.time.LocalTime +import java.time.ZoneId enum class TimeReference { @@ -22,8 +24,12 @@ } } @Parcelize -data class JourneyParams(val timeReference: TimeReference, val date: LocalDate?, val time: LocalTime?, val wheelchairAccessible: Boolean, val bicycle: Boolean) : Parcelable -/* - date ?: Instant.now().atZone(ZoneId.systemDefault()).toLocalDate() - time ?: Instant.now().atZone(ZoneId.systemDefault()).toLocalTime() -*/ \ No newline at end of file +data class JourneyParams(val timeReference: TimeReference, val date: LocalDate?, val time: LocalTime?, val wheelchairAccessible: Boolean, val bicycle: Boolean) : Parcelable { + fun getSafeDate(): LocalDate { + return date ?: Instant.now().atZone(ZoneId.systemDefault()).toLocalDate() + } + + fun getSafeTime(): LocalTime { + return time ?: Instant.now().atZone(ZoneId.systemDefault()).toLocalTime() + } +} \ No newline at end of file diff --git a/app/src/main/java/xyz/apiote/bimba/czwek/repo/LineType.kt b/app/src/main/java/xyz/apiote/bimba/czwek/repo/LineType.kt index 29612aa9cd60bebeefcd4423bc7bb84ad1164368..e8203ccf59987b50e216e07ced4f05e124ad2822 100644 --- a/app/src/main/java/xyz/apiote/bimba/czwek/repo/LineType.kt +++ b/app/src/main/java/xyz/apiote/bimba/czwek/repo/LineType.kt @@ -12,6 +12,14 @@ enum class LineType { UNKNOWN, TRAM, BUS, TROLLEYBUS, METRO, RAIL, FERRY, CABLE_TRAM, CABLE_CAR, FUNICULAR, MONORAIL, PLANE, WALK; + fun isActive(): Boolean { + return this == WALK + } + + fun isPasive(): Boolean { + return !isActive() + } + companion object { fun of(t: LineTypeV1): LineType { return when (t) {