Skip to content

Commit

Permalink
Add business logic for WalkingInterchange
Browse files Browse the repository at this point in the history
  • Loading branch information
ksharma-xyz committed Oct 21, 2024
1 parent 70bc4d9 commit 0ad7d13
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,24 @@ data class TimeTableState(
val position: WalkPosition,
)

enum class WalkPosition {
enum class WalkPosition(val position: String) {
/**
* Need to walk before the Leg starts.
*/
BEFORE,
BEFORE("BEFORE"),

/**
* After displaying the Leg info, we need to walk.
*/
AFTER,
AFTER("AFTER"),

/**
* This indicates that the walking portion of the leg is the entire leg itself.
* In other words, the leg involves walking only, with no vehicle transportation involved.
* For example, if you're planning a trip from one location to another that involves walking
* the entire distance, the "position" would be "IDEST".
*/
IDEST,
IDEST("IDEST"),
}

data class Stop(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,24 @@ fun JourneyDetailCard(
.height(8.dp)
)

JourneyLeg(
transportModeLine = leg.transportModeLine,
stopsInfo = leg.stopsInfo,
departureTime = leg.stops.first().time,
stopName = leg.stops.first().name,
isWheelchairAccessible = false,
modifier = Modifier.padding(start = 8.dp, end = 8.dp),
)
if (leg.walkInterchange?.position == TimeTableState.JourneyCardInfo.WalkPosition.IDEST) {
leg.walkInterchange?.duration?.let { duration ->
WalkingLeg(
duration = duration,
modifier = Modifier
.padding(horizontal = 16.dp),
)
}
} else {
JourneyLeg(
transportModeLine = leg.transportModeLine,
stopsInfo = leg.stopsInfo,
departureTime = leg.stops.first().time,
stopName = leg.stops.first().name,
isWheelchairAccessible = false,
modifier = Modifier.padding(start = 8.dp, end = 8.dp),
)
}

Spacer(
modifier = Modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fun JourneyLeg(
.background(
color = transportModeLine.transportMode.colorCode
.hexToComposeColor()
.copy(alpha = 0.1f)
.copy(alpha = 0.2f)
)
.padding(vertical = 8.dp, horizontal = 8.dp),
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ internal fun TripResponse.buildJourneyList(): ImmutableList<TimeTableState.Journ
}
// Sometimes there are no legs, so we need to handle that case, can happen when, the train is Now.
val totalStops = legs?.sumOf { leg -> leg.stopSequence?.size ?: 0 } ?: 0
val platformText = firstPublicTransportLeg?.stopSequence?.firstOrNull()?.properties?.platform?.last()?.let {
it.takeIf { it != '0' && it.isLetterOrDigit() }?.uppercaseChar()
}
val platformText =
firstPublicTransportLeg?.stopSequence?.firstOrNull()?.properties?.platform?.last()
?.let {
it.takeIf { it != '0' && it.isLetterOrDigit() }?.uppercaseChar()
}
Timber.d("PlatformText: $platformText -- ${firstPublicTransportLeg?.stopSequence?.firstOrNull()?.properties?.platform}")

if (legs != null && originTimeUTC != null && arrivalTimeUTC != null && firstPublicTransportLeg != null && totalStops > 0) {
Expand Down Expand Up @@ -90,19 +92,45 @@ private fun TripResponse.Leg.toUiModel(): TimeTableState.JourneyCardInfo.Leg? {
displayText = displayText,
stopsInfo = "$numberOfStops stops ($displayDuration)",
stops = stops,
walkInterchange = null,
walkInterchange = footPathInfo?.firstOrNull()?.let { foot ->
foot.toWalkInterchange(foot.duration.toDisplayString())
},
)
} else {
null
}
}

internal fun TripResponse.FootPathInfo.toWalkInterchange(displayDuration: String): TimeTableState.JourneyCardInfo.WalkInterchange? {
return position?.let { position ->
when (position) {
TimeTableState.JourneyCardInfo.WalkPosition.BEFORE.position -> TimeTableState.JourneyCardInfo.WalkInterchange(
duration = displayDuration,
position = TimeTableState.JourneyCardInfo.WalkPosition.BEFORE,
)

TimeTableState.JourneyCardInfo.WalkPosition.AFTER.position -> TimeTableState.JourneyCardInfo.WalkInterchange(
duration = displayDuration,
position = TimeTableState.JourneyCardInfo.WalkPosition.AFTER,
)

TimeTableState.JourneyCardInfo.WalkPosition.IDEST.position -> TimeTableState.JourneyCardInfo.WalkInterchange(
duration = displayDuration,
position = TimeTableState.JourneyCardInfo.WalkPosition.IDEST,
)

else -> null
}
}
}

private fun TripResponse.StopSequence.toUiModel(): TimeTableState.JourneyCardInfo.Stop? {
val stopName = disassembledName ?: name
// For last leg there is no departure time, so using arrival time
// For first leg there is no arrival time, so using departure time.
val time =
departureTimeEstimated ?: departureTimePlanned ?: arrivalTimeEstimated ?: arrivalTimePlanned
departureTimeEstimated ?: departureTimePlanned ?: arrivalTimeEstimated
?: arrivalTimePlanned
return if (stopName != null && time != null) {
TimeTableState.JourneyCardInfo.Stop(
name = stopName,
Expand Down

0 comments on commit 0ad7d13

Please sign in to comment.