Skip to content

Commit

Permalink
Add JourneyCardClicked event (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
ksharma-xyz authored Oct 15, 2024
1 parent a0db335 commit bb0476f
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ package xyz.ksharma.krail.trip_planner.ui.state.timetable

sealed interface TimeTableUiEvent {
data class LoadTimeTable(val fromStopId: String, val toStopId: String) : TimeTableUiEvent
data class JourneyCardClicked(val journeyCardInfo: TimeTableState.JourneyCardInfo) :
TimeTableUiEvent
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package xyz.ksharma.krail.trip_planner.ui.components

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowRow
Expand All @@ -11,6 +12,7 @@ import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import xyz.ksharma.krail.design.system.LocalTextColor
Expand All @@ -30,10 +32,11 @@ fun JourneyCard(
durationText: String,
backgroundColor: Color = KrailTheme.colors.tertiaryContainer.copy(alpha = 0.8f),
transportModeIconRow: @Composable RowScope.() -> Unit,
onClick: () -> Unit,
modifier: Modifier = Modifier,
) {
BasicJourneyCard(
modifier = modifier,
modifier = modifier.clickable(role = Role.Button, onClick = onClick),
backgroundColor = backgroundColor,
content = {
FlowRow(
Expand Down Expand Up @@ -119,7 +122,6 @@ private fun JourneyCardTrainLongTextPreview() {
originAndDestinationTimeText = "8:25am - 8:40am",
durationText = "23 mins",
transportModeIconRow = {

TransportModeInfo(
letter = 'T',
backgroundColor = "#005aa3".hexToComposeColor(),
Expand Down Expand Up @@ -150,6 +152,7 @@ private fun JourneyCardTrainLongTextPreview() {
badgeText = "T4",
)
},
onClick = {}
)
}
}
Expand All @@ -176,6 +179,7 @@ private fun JourneyCardMultipleModesPreview() {
badgeText = "700",
)
},
onClick = {},
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ internal fun NavGraphBuilder.timeTableDestination(navController: NavHostControll
// Subscribe to the isActive state flow - for updating the TimeText periodically.
val isActive by viewModel.isActive.collectAsStateWithLifecycle()

TimeTableScreen(timeTableState)
TimeTableScreen(timeTableState = timeTableState, onEvent = { viewModel.onEvent(it) })
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ 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

@Composable
fun TimeTableScreen(
timeTableState: TimeTableState,
onEvent: (TimeTableUiEvent) -> Unit,
modifier: Modifier = Modifier,
) {
LazyColumn(
Expand Down Expand Up @@ -60,6 +62,9 @@ fun TimeTableScreen(
lineName = it.lineName,
)
}.toImmutableList(),
onClick = {
onEvent(TimeTableUiEvent.JourneyCardClicked(journey))
},
modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp),
)
}
Expand All @@ -78,6 +83,7 @@ fun JourneyCardItem(
originDestinationTimeText: String,
durationText: String,
transportModeLineList: ImmutableList<TransportModeLine>,
onClick: () -> Unit,
modifier: Modifier = Modifier,
) {
JourneyCard(
Expand All @@ -100,6 +106,7 @@ fun JourneyCardItem(
if (index < transportModeLineList.size - 1) SeparatorIcon()
}
}
}
},
onClick = onClick,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,14 @@ class TimeTableViewModel @Inject constructor(
fun onEvent(event: TimeTableUiEvent) {
when (event) {
is TimeTableUiEvent.LoadTimeTable -> onLoadTimeTable(event.fromStopId, event.toStopId)
is TimeTableUiEvent.JourneyCardClicked -> onJourneyCardClicked(event.journeyCardInfo)
}
}

private fun onJourneyCardClicked(journeyCardInfo: TimeTableState.JourneyCardInfo) {
Timber.d("Journey Card Clicked: $journeyCardInfo")
}

private fun onLoadTimeTable(fromStopId: String?, toStopId: String?) {
Timber.d("loadTimeTable API Call- fromStopItem: $fromStopId, toStopItem: $toStopId")

Expand All @@ -63,18 +68,12 @@ class TimeTableViewModel @Inject constructor(
require(!(fromStopId.isNullOrEmpty() || toStopId.isNullOrEmpty())) { "Invalid Stop Ids" }
tripRepository.trip(originStopId = fromStopId, destinationStopId = toStopId)
.onSuccess { response ->

// TODO -
// 1. Create UI Model
// 2. Update UI State

updateUiState {
copy(
isLoading = false,
journeyList = response.buildJourneyList() ?: persistentListOf()
)
}

response.logForUnderstandingData()
}.onFailure {
Timber.e("Error while fetching trip: $it")
Expand Down

0 comments on commit bb0476f

Please sign in to comment.