Skip to content

Commit

Permalink
rearrange splitPerTrip
Browse files Browse the repository at this point in the history
  • Loading branch information
boringcactus committed Apr 10, 2024
1 parent f587480 commit 9a431f7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 52 deletions.
42 changes: 27 additions & 15 deletions iosApp/iosApp/Pages/StopDetails/StopDetailsFilteredRouteView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,47 @@ struct StopDetailsFilteredRouteView: View {
let now: Instant
@Binding var filter: StopDetailsFilter?

let tripData: [(PatternsByHeadsign, PatternsByHeadsign.Format)]
struct RowData {
let tripId: String
let headsign: String
let formatted: PatternsByHeadsign.Format

init?(trip: UpcomingTrip, route: Route, expectedDirection: Int32?, now: Instant) {
if trip.trip.directionId != expectedDirection {
return nil
}

tripId = trip.trip.id
headsign = trip.trip.headsign
formatted = PatternsByHeadsign(
route: route, headsign: headsign, patterns: [], upcomingTrips: [trip], alertsHere: nil
).format(now: now)

if !(formatted is PatternsByHeadsign.FormatSome) {
return nil
}
}
}

let rows: [RowData]

init(patternsByStop: PatternsByStop, now: Instant, filter: Binding<StopDetailsFilter?>) {
self.patternsByStop = patternsByStop
self.now = now
_filter = filter

let expectedDirection: Int32? = filter.wrappedValue?.directionId
let trips: [PatternsByHeadsign] = patternsByStop.splitPerTrip()
let tripsFormatted: [(PatternsByHeadsign, PatternsByHeadsign.Format)] = trips.map { ($0, $0.format(now: now)) }
tripData = tripsFormatted.filter {
let (patternsByHeadsign, formatted) = $0
let trip: UpcomingTrip? = patternsByHeadsign.upcomingTrips?.first
let tripDirectionId: Int32? = trip?.trip.directionId
let directionIdMatches = tripDirectionId == expectedDirection
let willDisplay = formatted is PatternsByHeadsign.FormatSome
return directionIdMatches && willDisplay
rows = patternsByStop.allUpcomingTrips().compactMap {
RowData(trip: $0, route: patternsByStop.route, expectedDirection: expectedDirection, now: now)
}
}

var body: some View {
Button(action: { filter = nil }, label: { Text("Clear Filter") })
List {
RoutePillSection(route: patternsByStop.route) {
ForEach(tripData, id: \.0.upcomingTrips?.first?.trip.id) { patternsByHeadsign, formatted in
HeadsignRowView(
headsign: patternsByHeadsign.headsign,
predictions: formatted
)
ForEach(rows, id: \.tripId) { row in
HeadsignRowView(headsign: row.headsign, predictions: row.formatted)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,6 @@ data class PatternsByHeadsign(
throw NoSuchElementException("Got directionId of empty PatternsByHeadsign")
}

/**
* Splits up this [PatternsByHeadsign] into a separate [PatternsByHeadsign] for each trip, for
* convenience when drawing each trip separately.
*/
fun splitPerTrip(): List<PatternsByHeadsign> =
upcomingTrips?.map { PatternsByHeadsign(route, headsign, patterns, listOf(it), alertsHere) }
?: emptyList()

override fun compareTo(other: PatternsByHeadsign): Int =
patterns.first().compareTo(other.patterns.first())

Expand Down Expand Up @@ -184,10 +176,8 @@ data class PatternsByStop(
@OptIn(ExperimentalTurfApi::class)
fun distanceFrom(position: Position) = distance(position, this.position)

fun splitPerTrip(): List<PatternsByHeadsign> =
this.patternsByHeadsign
.flatMap { it.splitPerTrip() }
.sortedBy { it.upcomingTrips?.firstOrNull() }
fun allUpcomingTrips(): List<UpcomingTrip> =
this.patternsByHeadsign.flatMap { it.upcomingTrips ?: emptyList() }.sorted()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,32 +75,12 @@ class PatternsByStopTest {

assertEquals(
listOf(
PatternsByHeadsign(
route,
"Ashmont",
listOf(routePatternAshmont),
listOf(upcomingTripAshmont1)
),
PatternsByHeadsign(
route,
"Braintree",
listOf(routePatternBraintree),
listOf(upcomingTripBraintree1)
),
PatternsByHeadsign(
route,
"Braintree",
listOf(routePatternBraintree),
listOf(upcomingTripBraintree2)
),
PatternsByHeadsign(
route,
"Ashmont",
listOf(routePatternAshmont),
listOf(upcomingTripAshmont2)
),
upcomingTripAshmont1,
upcomingTripBraintree1,
upcomingTripBraintree2,
upcomingTripAshmont2
),
patternsByStop.splitPerTrip()
patternsByStop.allUpcomingTrips()
)
}
}

0 comments on commit 9a431f7

Please sign in to comment.