Skip to content

Commit

Permalink
Add support for walkingleg , when leg.transportation.product.class is…
Browse files Browse the repository at this point in the history
… 100 or 99
  • Loading branch information
ksharma-xyz committed Oct 21, 2024
1 parent 9c462d7 commit ea999aa
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ sealed class TransportMode {
abstract val name: String
abstract val colorCode: String
abstract val productClass: Int

/**
* Priority to sort the transport modes in Search Results
*/
abstract val priority: Int

@Serializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,27 @@ data class TimeTableState(
get() = (originUtcDateTime + destinationTime + transportModeLines
.joinToString { it.lineName }).filter { it.isLetterOrDigit() }

data class Leg(
// modeType - legs.transportation.product.class
// lineName - legs.transportation.disassembledName
val transportModeLine: TransportModeLine,
sealed class Leg {
data class WalkingLeg(
val duration: String, // "10mins"
) : Leg()

// transportation.description -> "Burwood to Liverpool",
val displayText: String, // "towards X via X"
data class TransportLeg(
// modeType - legs.transportation.product.class
// lineName - legs.transportation.disassembledName
val transportModeLine: TransportModeLine,

// leg.stopSequence.size (leg.duration seconds)
val stopsInfo: String, // "4 stops (12 min)"
// transportation.description -> "Burwood to Liverpool",
val displayText: String, // "towards X via X"

val stops: ImmutableList<Stop>,
// leg.stopSequence.size (leg.duration seconds)
val stopsInfo: String, // "4 stops (12 min)"

val walkInterchange: WalkInterchange? = null,
)
val stops: ImmutableList<Stop>,

val walkInterchange: WalkInterchange? = null,
) : Leg()
}

data class WalkInterchange(
// leg.footPathInfo.duration seconds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,16 @@ fun JourneyDetailCard(
width = 2.dp,
brush = Brush.verticalGradient(
colors = if (legList.size >= 2) {
legList.map { it.transportModeLine.transportMode.colorCode.hexToComposeColor() }
legList
.filterIsInstance<TimeTableState.JourneyCardInfo.Leg.TransportLeg>()
.map {
it.transportModeLine.transportMode.colorCode.hexToComposeColor()
}
} else {
val color =
legList.first().transportModeLine.transportMode.colorCode.hexToComposeColor()
legList
.filterIsInstance<TimeTableState.JourneyCardInfo.Leg.TransportLeg>()
.first().transportModeLine.transportMode.colorCode.hexToComposeColor()
listOf(color, color)
}),
shape = RoundedCornerShape(12.dp)
Expand All @@ -81,12 +87,14 @@ fun JourneyDetailCard(
Text(
text = timeToDeparture,
style = KrailTheme.typography.titleLarge,
color = legList.first().transportModeLine.transportMode.colorCode.hexToComposeColor()
color = legList.filterIsInstance<TimeTableState.JourneyCardInfo.Leg.TransportLeg>()
.first().transportModeLine.transportMode.colorCode.hexToComposeColor()
)
Text(
text = "Platform $platformNumber",
style = KrailTheme.typography.titleLarge,
color = legList.first().transportModeLine.transportMode.colorCode.hexToComposeColor()
color = legList.filterIsInstance<TimeTableState.JourneyCardInfo.Leg.TransportLeg>()
.first().transportModeLine.transportMode.colorCode.hexToComposeColor()
)
}

Expand Down Expand Up @@ -142,54 +150,66 @@ fun JourneyDetailCard(
)

legList.forEachIndexed { index, leg ->
if (leg.walkInterchange?.position == TimeTableState.JourneyCardInfo.WalkPosition.BEFORE) {
leg.walkInterchange?.duration?.let { duration ->
when (leg) {
is TimeTableState.JourneyCardInfo.Leg.WalkingLeg -> {
WalkingLeg(
duration = duration,
duration = leg.duration,
modifier = Modifier
.padding(horizontal = 16.dp),
)
}
}

Spacer(
modifier = Modifier
.fillMaxWidth()
.height(8.dp)
)
is TimeTableState.JourneyCardInfo.Leg.TransportLeg -> {
if (leg.walkInterchange?.position == TimeTableState.JourneyCardInfo.WalkPosition.BEFORE) {
leg.walkInterchange?.duration?.let { duration ->
WalkingLeg(
duration = duration,
modifier = Modifier
.padding(horizontal = 16.dp),
)
}
}

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

Spacer(
modifier = Modifier
.fillMaxWidth()
.height(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 = if (index == legList.lastIndex) leg.stops.last().time else leg.stops.first().time,
stopName = if (index == legList.lastIndex) leg.stops.last().name else leg.stops.first().name,
isWheelchairAccessible = false,
modifier = Modifier.padding(start = 8.dp, end = 8.dp),
)
}

if (leg.walkInterchange?.position == TimeTableState.JourneyCardInfo.WalkPosition.AFTER) {
leg.walkInterchange?.duration?.let { duration ->
WalkingLeg(
duration = duration,
Spacer(
modifier = Modifier
.padding(horizontal = 16.dp),
.fillMaxWidth()
.height(8.dp)
)

if (leg.walkInterchange?.position == TimeTableState.JourneyCardInfo.WalkPosition.AFTER) {
leg.walkInterchange?.duration?.let { duration ->
WalkingLeg(
duration = duration,
modifier = Modifier
.padding(horizontal = 16.dp),
)
}
}
}
}
}
Expand All @@ -205,7 +225,7 @@ private fun PreviewJourneyDetailCard() {
platformNumber = '1',
totalTravelTime = "1h 30mins",
legList = listOf(
TimeTableState.JourneyCardInfo.Leg(
TimeTableState.JourneyCardInfo.Leg.TransportLeg(
transportModeLine = TransportModeLine(
transportMode = TransportMode.Bus(),
lineName = "600",
Expand Down Expand Up @@ -238,7 +258,7 @@ private fun PreviewJourneyDetailCardWalkBefore() {
platformNumber = '1',
totalTravelTime = "1h 30mins",
legList = listOf(
TimeTableState.JourneyCardInfo.Leg(
TimeTableState.JourneyCardInfo.Leg.TransportLeg(
walkInterchange = TimeTableState.JourneyCardInfo.WalkInterchange(
duration = "5 mins",
position = TimeTableState.JourneyCardInfo.WalkPosition.BEFORE,
Expand Down Expand Up @@ -275,7 +295,7 @@ private fun PreviewJourneyDetailCardWalkAfter() {
platformNumber = '1',
totalTravelTime = "1h 30mins",
legList = listOf(
TimeTableState.JourneyCardInfo.Leg(
TimeTableState.JourneyCardInfo.Leg.TransportLeg(
walkInterchange = TimeTableState.JourneyCardInfo.WalkInterchange(
duration = "5 mins",
position = TimeTableState.JourneyCardInfo.WalkPosition.AFTER,
Expand Down Expand Up @@ -313,7 +333,7 @@ private fun PreviewMultiLegJourneyDetailCard() {
platformNumber = '1',
totalTravelTime = "1h 30mins",
legList = listOf(
TimeTableState.JourneyCardInfo.Leg(
TimeTableState.JourneyCardInfo.Leg.TransportLeg(
walkInterchange = TimeTableState.JourneyCardInfo.WalkInterchange(
duration = "5 mins",
position = TimeTableState.JourneyCardInfo.WalkPosition.AFTER,
Expand All @@ -335,7 +355,7 @@ private fun PreviewMultiLegJourneyDetailCard() {
),
).toImmutableList()
),
TimeTableState.JourneyCardInfo.Leg(
TimeTableState.JourneyCardInfo.Leg.TransportLeg(
transportModeLine = TransportModeLine(
transportMode = TransportMode.Train(),
lineName = "T5",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,15 @@ internal fun TripResponse.buildJourneyList(): ImmutableList<TimeTableState.Journ
?.let {
it.takeIf { it != '0' && it.isLetterOrDigit() }?.uppercaseChar()
}
Timber.d("PlatformText: $platformText -- ${firstPublicTransportLeg?.stopSequence?.firstOrNull()?.properties?.platform}")

legs?.forEachIndexed { index, it ->
Timber.d(
"TransportMode #$index: ${it.transportation?.product?.productClass}, " +
"name: ${it.transportation?.product?.name}, " +
"stops: ${it.stopSequence?.size}, " +
"duration: ${it.duration}"
)
}

if (legs != null && originTimeUTC != null && arrivalTimeUTC != null && firstPublicTransportLeg != null && totalStops > 0) {
TimeTableState.JourneyCardInfo(
Expand Down Expand Up @@ -83,21 +91,28 @@ private fun TripResponse.Leg.toUiModel(): TimeTableState.JourneyCardInfo.Leg? {
val displayDuration = duration.toDisplayString()
val stops = stopSequence?.mapNotNull { it.toUiModel() }?.toImmutableList()

return if (transportMode != null && lineName != null && displayText != null && numberOfStops != null && stops != null) {
TimeTableState.JourneyCardInfo.Leg(
transportModeLine = TransportModeLine(
transportMode = transportMode,
lineName = lineName,
),
displayText = displayText,
stopsInfo = "$numberOfStops stops ($displayDuration)",
stops = stops,
walkInterchange = footPathInfo?.firstOrNull()?.let { foot ->
foot.toWalkInterchange(foot.duration.toDisplayString())
},
)
} else {
null
return when {
// Walking Leg - Always check before public transport leg
isWalkingLeg() -> {
TimeTableState.JourneyCardInfo.Leg.WalkingLeg(duration = displayDuration)
}

else -> { // Public Transport Leg
if (transportMode != null && lineName != null && displayText != null && numberOfStops != null && stops != null) {
TimeTableState.JourneyCardInfo.Leg.TransportLeg(
transportModeLine = TransportModeLine(
transportMode = transportMode,
lineName = lineName,
),
displayText = displayText,
stopsInfo = "$numberOfStops stops ($displayDuration)",
stops = stops,
walkInterchange = footPathInfo?.firstOrNull()?.let { foot ->
foot.toWalkInterchange(foot.duration.toDisplayString())
},
)
} else null
}
}
}

Expand Down Expand Up @@ -191,3 +206,6 @@ private fun String.fromUTCToDisplayTimeString() = this.utcToAEST().aestToHHMM()
private fun Long?.toDisplayString(): String {
return "${this?.div(60)} mins"
}

private fun TripResponse.Leg.isWalkingLeg(): Boolean =
transportation?.product?.productClass == 99L || transportation?.product?.productClass == 100L

0 comments on commit ea999aa

Please sign in to comment.