Skip to content

Commit

Permalink
Update API to accept date and time parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
ksharma-xyz committed Nov 30, 2024
1 parent fded83d commit 79fead7
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,20 @@ class RealTripPlanningService(private val httpClient: HttpClient) : TripPlanning
override suspend fun trip(
originStopId: String,
destinationStopId: String,
depArr: DepArr,
date: String?,
time: String?,
): TripResponse = withContext(Dispatchers.IO) {

httpClient.get("$NSW_TRANSPORT_BASE_URL/v1/tp/trip") {
url {
parameters.append(TripRequestParams.nameOrigin, originStopId)
parameters.append(TripRequestParams.nameDestination, destinationStopId)

parameters.append(TripRequestParams.depArrMacro, "dep")
parameters.append(TripRequestParams.depArrMacro, depArr.macro)
date?.let { parameters.append(TripRequestParams.itdDate, date) }
time?.let { parameters.append(TripRequestParams.itdTime, time) }

parameters.append(TripRequestParams.typeDestination, "any")
parameters.append(TripRequestParams.calcNumberOfTrips, "6")
parameters.append(TripRequestParams.typeOrigin, "any")
Expand Down Expand Up @@ -56,5 +62,9 @@ class RealTripPlanningService(private val httpClient: HttpClient) : TripPlanning
}
}.body()
}
}

enum class DepArr(val macro: String) {
DEP("dep"),
ARR("arr")
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,23 @@ internal const val NSW_TRANSPORT_BASE_URL = "https://api.transport.nsw.gov.au"

interface TripPlanningService {

suspend fun trip(originStopId: String, destinationStopId: String): TripResponse
suspend fun trip(
originStopId: String,
destinationStopId: String,
depArr: DepArr = DepArr.DEP,

/**
* YYYYMMDD format.
* E.g. 20160901 refers to 1 September 2016.
*/
date: String? = null,

/**
* HHMM 24-hour format.
* E.g. 0830 means 8:30am and 2030 means 8:30pm.
*/
time: String? = null,
): TripResponse

suspend fun stopFinder(
stopSearchQuery: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ import xyz.ksharma.krail.trip.planner.ui.components.themeBackgroundColor
import xyz.ksharma.krail.trip.planner.ui.components.themeContentColor
import xyz.ksharma.krail.trip.planner.ui.datetimeselector.JourneyTimeOptions.ARRIVE
import xyz.ksharma.krail.trip.planner.ui.datetimeselector.JourneyTimeOptions.LEAVE
import xyz.ksharma.krail.trip.planner.ui.state.searchstop.model.StopItem
import xyz.ksharma.krail.trip.planner.ui.state.searchstop.model.StopItem.Companion
import xyz.ksharma.krail.trip.planner.ui.timetable.ActionButton

@OptIn(ExperimentalMaterial3Api::class)
Expand Down Expand Up @@ -234,6 +232,19 @@ data class DateTimeSelectionItem(

fun toJsonString() = Json.encodeToString(serializer(), this)

fun toHHMM(): String {
val hh = hour.toString().padStart(2, '0')
val mm = minute.toString().padStart(2, '0')
return "$hh$mm"
}

fun toYYYYMMDD(): String {
val yyyy = date.year.toString()
val mm = date.monthNumber.toString().padStart(2, '0')
val dd = date.dayOfMonth.toString().padStart(2, '0')
return "$yyyy$mm$dd"
}

@Suppress("ConstPropertyName")
companion object {
private const val serialVersionUID: Long = 1L
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ import xyz.ksharma.krail.core.datetime.DateTimeHelper.toGenericFormattedTimeStri
import xyz.ksharma.krail.sandook.Sandook
import xyz.ksharma.krail.trip.planner.network.api.model.TripResponse
import xyz.ksharma.krail.trip.planner.network.api.ratelimit.RateLimiter
import xyz.ksharma.krail.trip.planner.network.api.service.DepArr
import xyz.ksharma.krail.trip.planner.network.api.service.TripPlanningService
import xyz.ksharma.krail.trip.planner.ui.datetimeselector.DateTimeSelectionItem
import xyz.ksharma.krail.trip.planner.ui.datetimeselector.JourneyTimeOptions
import xyz.ksharma.krail.trip.planner.ui.datetimeselector.JourneyTimeOptions.*
import xyz.ksharma.krail.trip.planner.ui.state.alerts.ServiceAlert
import xyz.ksharma.krail.trip.planner.ui.state.timetable.TimeTableState
import xyz.ksharma.krail.trip.planner.ui.state.timetable.TimeTableUiEvent
Expand Down Expand Up @@ -67,6 +71,7 @@ class TimeTableViewModel(
val expandedJourneyId: StateFlow<String?> = _expandedJourneyId

private var tripInfo: Trip? = null
private var dateTimeSelectionItem: DateTimeSelectionItem? = null

fun onEvent(event: TimeTableUiEvent) {
when (event) {
Expand Down Expand Up @@ -116,6 +121,13 @@ class TimeTableViewModel(
val tripResponse = tripPlanningService.trip(
originStopId = tripInfo!!.fromStopId,
destinationStopId = tripInfo!!.toStopId,
date = dateTimeSelectionItem?.toYYYYMMDD(),
time = dateTimeSelectionItem?.toHHMM(),
depArr = when (dateTimeSelectionItem?.option) {
LEAVE -> DepArr.DEP
ARRIVE -> DepArr.ARR
else -> DepArr.DEP
}
)
Result.success(tripResponse)
}.getOrElse { error ->
Expand Down

0 comments on commit 79fead7

Please sign in to comment.