Skip to content

Commit

Permalink
fix: Move stop list changes into splitForTarget
Browse files Browse the repository at this point in the history
This makes it easier to make the changes for the future first stop
behavior when we need to do things more complicated than checking if
the vehicle exists, and ensures it can never break the existing trip
details page.
  • Loading branch information
EmmaSimon committed Dec 19, 2024
1 parent 5fb50b1 commit 174c214
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 7 deletions.
3 changes: 2 additions & 1 deletion iosApp/iosApp/Pages/StopDetails/TripStops.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ struct TripStops: View {
stops.splitForTarget(
targetStopId: targetId,
targetStopSequence: Int32(stopSequence),
globalData: global
globalData: global,
combinedStopDetails: true
)
} else { nil }
}
Expand Down
3 changes: 2 additions & 1 deletion iosApp/iosApp/Pages/TripDetails/TripDetailsPage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ struct TripDetailsPage: View {
if let target, let stopSequence = target.stopSequence, let splitStops = stops.splitForTarget(
targetStopId: target.stopId,
targetStopSequence: Int32(stopSequence),
globalData: globalResponse
globalData: globalResponse,
combinedStopDetails: false
) {
TripDetailsStopListSplitView(
splitStops: splitStops,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ struct TripDetailsStopListSplitView: View {

return TripDetailsStopListSplitView(
splitStops: .init(
firstStop: nil,
collapsedStops: [entry(stop1, 10, pred1)],
targetStop: entry(stop2, 20, pred2),
followingStops: [entry(stop3, 30, pred3)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ constructor(val stops: List<Entry>, val startTerminalEntry: Entry? = null) {
fun splitForTarget(
targetStopId: String,
targetStopSequence: Int,
globalData: GlobalResponse
globalData: GlobalResponse,
// TODO: Remove this once the feature flag is removed
combinedStopDetails: Boolean = false
): TargetSplit? {
var targetStopIndex =
stops.indexOfFirst {
Expand All @@ -54,15 +56,26 @@ constructor(val stops: List<Entry>, val startTerminalEntry: Entry? = null) {
return null
}

val collapsedStops = stops.subList(fromIndex = 0, toIndex = targetStopIndex)
var firstStop: Entry? = null
var collapsedStops = stops.subList(fromIndex = 0, toIndex = targetStopIndex)
val firstCollapsed = collapsedStops.firstOrNull()
if (
combinedStopDetails &&
firstCollapsed == startTerminalEntry &&
startTerminalEntry?.vehicle == null
) {
collapsedStops = collapsedStops.drop(1)
firstStop = firstCollapsed
}
val targetStop = stops[targetStopIndex]
val followingStops =
stops.subList(fromIndex = targetStopIndex + 1, toIndex = stops.lastIndex + 1)

return TargetSplit(collapsedStops, targetStop, followingStops)
return TargetSplit(firstStop, collapsedStops, targetStop, followingStops)
}

data class TargetSplit(
val firstStop: Entry? = null,
val collapsedStops: List<Entry>,
val targetStop: Entry,
val followingStops: List<Entry>
Expand Down Expand Up @@ -206,8 +219,7 @@ constructor(val stops: List<Entry>, val startTerminalEntry: Entry? = null) {
vehicle.currentStatus == Vehicle.CurrentStatus.StoppedAt)
}
}
.mapNotNull { getEntry(it.value) }
.dropWhile { it == startTerminalEntry && it.vehicle == null },
.mapNotNull { getEntry(it.value) },
startTerminalEntry
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,26 @@ class TripDetailsStopListTest {
)
}

@Test
fun `splitForTarget removes first stop from collapsed when no vehicle exists`() = test {
val list = stopListOf(
entry("A", 10),
entry("B", 20),
entry("C", 30),
entry("D", 40)
)

assertEquals(
TripDetailsStopList.TargetSplit(
firstStop = entry("A", 10),
collapsedStops = listOf(entry("B", 20)),
targetStop = entry("C", 30),
followingStops = listOf(entry("D", 40)),
),
list.splitForTarget("C", 30, globalData(), true)
)
}

@Test
fun `splitForTarget returns null if target not found`() = test {
val list = stopListOf(entry("A", 10), entry("B", 20), entry("C", 30))
Expand Down

0 comments on commit 174c214

Please sign in to comment.