Skip to content

Commit

Permalink
Integrate JourneyCard in TimeTableScreen (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
ksharma-xyz authored Oct 18, 2024
1 parent cd9f164 commit c01e6d1
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 176 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ data class TimeTableState(
data class JourneyCardInfo(
val timeText: String, // "in x mins"

val platformText: String? = null, // "on Platform X"
val platformText: Char, // "on Platform X" or Stand A etc.

// If first leg is not walking then,
// leg.first.origin.departureTimeEstimated ?: leg.first.origin.departureTimePlanned
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.tooling.preview.PreviewLightDark
import androidx.compose.ui.unit.dp
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.toImmutableList
import xyz.ksharma.krail.design.system.LocalTextColor
import xyz.ksharma.krail.design.system.LocalTextStyle
Expand Down Expand Up @@ -77,8 +78,12 @@ fun JourneyCard(
.border(
width = 2.dp,
brush = Brush.linearGradient(
colors = transportModeList.map { it.colorCode.hexToComposeColor() }
),
colors = if (transportModeList.size >= 2) {
transportModeList.map { it.colorCode.hexToComposeColor() }
} else {
val color = transportModeList.first().colorCode.hexToComposeColor()
listOf(color, color)
}),
shape = RoundedCornerShape(12.dp)
)
.clickable(role = Role.Button, onClick = onClick)
Expand All @@ -92,7 +97,7 @@ fun JourneyCard(
Text(
text = timeToDeparture, style = KrailTheme.typography.titleMedium,
color = transportModeList.first().colorCode.hexToComposeColor(),
modifier = Modifier.padding(end = 8.dp)
modifier = Modifier.padding(end = 8.dp).align(Alignment.CenterVertically)
)
Row(
modifier = Modifier.align(Alignment.CenterVertically),
Expand Down Expand Up @@ -178,52 +183,7 @@ fun JourneyCard(
}
}

@PreviewLightDark
@Preview(fontScale = 2f)
@Composable
private fun PreviewJourneyCard() {
KrailTheme {
JourneyCard(
timeToDeparture = "in 5 mins",
originTime = "8:25am",
destinationTime = "8:40am",
totalTravelTime = "15 mins",
platformNumber = '1',
isWheelchairAccessible = true,
transportModeList = listOf(
TransportMode.Train(),
TransportMode.Bus()
).toImmutableList(),
onClick = {},
)
}
}

@Preview
@Preview(fontScale = 2f)
@Composable
private fun PreviewJourneyCardLongData() {
KrailTheme {
JourneyCard(
timeToDeparture = "in 1h 5mins",
originTime = "12:25am",
destinationTime = "12:40am",
totalTravelTime = "45h 15minutes",
platformNumber = 'A',
isWheelchairAccessible = true,
transportModeList = listOf(
TransportMode.Ferry(),
TransportMode.Bus(),
TransportMode.Train(),
TransportMode.Coach(),
TransportMode.Metro(),
).toImmutableList(),
onClick = {},
)
}
}


/*
@OptIn(ExperimentalLayoutApi::class)
@Composable
fun JourneyCard(
Expand Down Expand Up @@ -308,83 +268,54 @@ fun JourneyCard(
}
},
)
}
}*/


// region Previews
/**
@Preview

@PreviewLightDark
@Preview(fontScale = 2f)
@Composable
private fun JourneyCardTrainLongTextPreview() {
KrailTheme {
JourneyCard(
departureTimeText = "in 2h 3mins",
departureLocationText = "on Parramatta Station, Stand A",
originAndDestinationTimeText = "8:25am - 8:40am",
durationText = "23 mins",
transportModeIconRow = {
TransportModeInfo(
letter = 'T',
backgroundColor = "#005aa3".hexToComposeColor(),
badgeColor = "#005aa3".hexToComposeColor(),
badgeText = "T4",
)
SeparatorIcon(modifier = Modifier.align(Alignment.CenterVertically))
TransportModeInfo(
letter = 'T',
backgroundColor = "#005aa3".hexToComposeColor(),
badgeText = "T4",
)
SeparatorIcon(modifier = Modifier.align(Alignment.CenterVertically))
TransportModeInfo(
letter = 'T',
backgroundColor = "#005aa3".hexToComposeColor(),
badgeText = "T4",
)
SeparatorIcon(modifier = Modifier.align(Alignment.CenterVertically))
TransportModeInfo(
letter = 'T',
backgroundColor = "#005aa3".hexToComposeColor(),
badgeText = "T4",
)
SeparatorIcon(modifier = Modifier.align(Alignment.CenterVertically))
TransportModeInfo(
letter = 'T',
backgroundColor = "#005aa3".hexToComposeColor(),
badgeText = "T4",
)
},
onClick = {}
)
}
private fun PreviewJourneyCard() {
KrailTheme {
JourneyCard(
timeToDeparture = "in 5 mins",
originTime = "8:25am",
destinationTime = "8:40am",
totalTravelTime = "15 mins",
platformNumber = '1',
isWheelchairAccessible = true,
transportModeList = listOf(
TransportMode.Train(),
TransportMode.Bus()
).toImmutableList(),
onClick = {},
)
}
}

@ComponentPreviews
@Preview
@Preview(fontScale = 2f)
@Composable
private fun JourneyCardMultipleModesPreview() {
KrailTheme {
JourneyCard(
departureTimeText = "in 5 mins",
departureLocationText = "on Platform 1",
originAndDestinationTimeText = "8:25am - 8:40am",
durationText = "15 mins",
transportModeIconRow = {
TransportModeInfo(
letter = 'T',
backgroundColor = "#005aa3".hexToComposeColor(),
badgeText = "T4",
)
SeparatorIcon(modifier = Modifier.align(Alignment.CenterVertically))
TransportModeInfo(
letter = 'B',
backgroundColor = "#00B5EF".hexToComposeColor(),
badgeText = "700",
)
},
onClick = {},
)
}
private fun PreviewJourneyCardLongData() {
KrailTheme {
JourneyCard(
timeToDeparture = "in 1h 5mins",
originTime = "12:25am",
destinationTime = "12:40am",
totalTravelTime = "45h 15minutes",
platformNumber = 'A',
isWheelchairAccessible = true,
transportModeList = listOf(
TransportMode.Ferry(),
TransportMode.Bus(),
TransportMode.Train(),
TransportMode.Coach(),
TransportMode.Metro(),
).toImmutableList(),
onClick = {},
)
}
}

// endregion
*/
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,26 @@ import androidx.compose.animation.scaleIn
import androidx.compose.animation.shrinkOut
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.CutCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.res.vectorResource
import androidx.compose.ui.unit.dp
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.toImmutableList
import xyz.ksharma.krail.design.system.components.RoundIconButton
import xyz.ksharma.krail.design.system.components.SeparatorIcon
import xyz.ksharma.krail.design.system.components.Text
import xyz.ksharma.krail.design.system.components.TitleBar
import xyz.ksharma.krail.design.system.theme.KrailTheme
import xyz.ksharma.krail.trip_planner.ui.R
import xyz.ksharma.krail.trip_planner.ui.components.JourneyCard
import xyz.ksharma.krail.trip_planner.ui.components.JourneyDetailCard
import xyz.ksharma.krail.trip_planner.ui.components.TransportModeInfo
import xyz.ksharma.krail.trip_planner.ui.components.hexToComposeColor
import xyz.ksharma.krail.trip_planner.ui.state.TransportModeLine
import xyz.ksharma.krail.trip_planner.ui.state.timetable.TimeTableState
import xyz.ksharma.krail.trip_planner.ui.state.timetable.TimeTableUiEvent
Expand Down Expand Up @@ -87,7 +77,7 @@ fun TimeTableScreen(
exit = fadeOut() + shrinkOut(),
) {
JourneyDetailCard(
header = journey.timeText + journey.platformText?.let { " on ${journey.platformText}" } + " (${journey.travelTime})",
header = journey.timeText + journey.platformText.let { " on ${journey.platformText}" } + " (${journey.travelTime})",
journeyLegList = journey.legs,
onClick = {
onEvent(TimeTableUiEvent.JourneyCardClicked(journey.journeyId))
Expand All @@ -103,9 +93,10 @@ fun TimeTableScreen(
exit = fadeOut(),
) {
JourneyCardItem(
departureTimeText = journey.timeText,
departureLocationText = journey.platformText?.let { "on ${journey.platformText}" },
originDestinationTimeText = journey.originTime + " - " + journey.destinationTime,
timeToDeparture = journey.timeText,
departureLocationNumber = journey.platformText,
originTime = journey.originTime,
destinationTime = journey.destinationTime,
durationText = journey.travelTime,
transportModeLineList = journey.transportModeLines.map {
TransportModeLine(
Expand All @@ -130,36 +121,24 @@ fun TimeTableScreen(

@Composable
fun JourneyCardItem(
departureTimeText: String,
departureLocationText: String? = null,
originDestinationTimeText: String,
timeToDeparture: String,
departureLocationNumber: Char,
originTime: String,
durationText: String,
destinationTime: String,
transportModeLineList: ImmutableList<TransportModeLine>,
onClick: () -> Unit,
modifier: Modifier = Modifier,
) {
JourneyCard(
modifier = modifier,
departureTimeText = departureTimeText,
departureLocationText = departureLocationText,
originAndDestinationTimeText = originDestinationTimeText,
durationText = durationText,
transportModeIconRow = {
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(8.dp)
) {
transportModeLineList.forEachIndexed { index, line ->
TransportModeInfo(
letter = line.transportMode.name.first(),
backgroundColor = line.transportMode.colorCode.hexToComposeColor(),
badgeColor = line.lineColorCode?.hexToComposeColor(),
badgeText = line.lineName,
)
if (index < transportModeLineList.size - 1) SeparatorIcon()
}
}
},
timeToDeparture = timeToDeparture,
originTime = originTime,
destinationTime = destinationTime,
totalTravelTime = durationText,
platformNumber = departureLocationNumber,
isWheelchairAccessible = false,
transportModeList = transportModeLineList.map { it.transportMode }.toImmutableList(),
onClick = onClick,
modifier = modifier,
)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package xyz.ksharma.krail.trip_planner.ui.timetable.business

import androidx.compose.foundation.layout.size
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.toImmutableList
import timber.log.Timber
Expand Down Expand Up @@ -35,30 +34,26 @@ 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 = when (firstPublicTransportLeg?.transportation?.product?.productClass) {
// Train or Metro
1L, 2L -> firstPublicTransportLeg.stopSequence?.firstOrNull()?.disassembledName?.split(
","
)?.lastOrNull()

if (legs != null && originTimeUTC != null && arrivalTimeUTC != null && firstPublicTransportLeg != null && totalStops > 0) {
// Other Modes
9L, 5L, 4L, 7L -> firstPublicTransportLeg.stopSequence?.firstOrNull()?.disassembledName

else -> null
}?.trim()

if (legs != null && originTimeUTC != null && arrivalTimeUTC != null && firstPublicTransportLeg != null && totalStops > 0 && platformText != null) {
TimeTableState.JourneyCardInfo(
timeText = originTimeUTC.let {
Timber.d("originTime: $it :- ${calculateTimeDifferenceFromNow(utcDateString = it)}")
calculateTimeDifferenceFromNow(utcDateString = it).toGenericFormattedTimeString()
},

platformText = when (firstPublicTransportLeg.transportation?.product?.productClass) {
// Train or Metro
1L, 2L -> {
firstPublicTransportLeg.stopSequence?.firstOrNull()?.disassembledName?.split(
","
)
?.lastOrNull()
}

// Other Modes
9L, 5L, 4L, 7L -> {
firstPublicTransportLeg.stopSequence?.firstOrNull()?.disassembledName
}

else -> null
}?.trim(),
platformText = platformText.last(),

originTime = originTimeUTC.fromUTCToDisplayTimeString(),
originUtcDateTime = originTimeUTC,
Expand Down

0 comments on commit c01e6d1

Please sign in to comment.