Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI: Add stop filtering and prioritization for search results #641

Merged
merged 8 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class SearchStopViewModelTest {
}
}

/*
/* This test is not valid anymore, as we're not calling API.
@Test
fun `GIVEN search query WHEN SearchTextChanged is triggered and api is success THEN uiState is updated with results`() =
runTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import xyz.ksharma.krail.core.analytics.Analytics
import xyz.ksharma.krail.core.analytics.AnalyticsScreen
import xyz.ksharma.krail.core.analytics.event.AnalyticsEvent
import xyz.ksharma.krail.core.analytics.event.trackScreenViewEvent
import xyz.ksharma.krail.core.log.log
import xyz.ksharma.krail.sandook.Sandook
import xyz.ksharma.krail.sandook.SelectProductClassesForStop
import xyz.ksharma.krail.trip.planner.network.api.service.TripPlanningService
import xyz.ksharma.krail.trip.planner.ui.state.TransportMode
import xyz.ksharma.krail.trip.planner.ui.state.TransportModeSortOrder
import xyz.ksharma.krail.trip.planner.ui.state.searchstop.SearchStopState
import xyz.ksharma.krail.trip.planner.ui.state.searchstop.SearchStopUiEvent

Expand Down Expand Up @@ -64,16 +64,18 @@ class SearchStopViewModel(
log("results: $results")*/

val resultsDb: List<SelectProductClassesForStop> =
sandook.selectStops(
stopName = query,
excludeProductClassList = emptyList(),
).take(50)
resultsDb.forEach {
log("resultsDb [$query]: ${it.stopName}")
}
val stopResults = resultsDb.map {
it.toStopResult()
}
sandook.selectStops(stopName = query, excludeProductClassList = listOf())

val stopResults = resultsDb
.map { it.toStopResult() }
.let {
filterProductClasses(
stopResults = it,
excludedProductClasses = listOf(TransportMode.Ferry().productClass).toImmutableList()
)
}
.let(::prioritiseStops)
.take(50)

updateUiState { displayData(stopResults) }
}.getOrElse {
Expand All @@ -84,6 +86,45 @@ class SearchStopViewModel(
}
}

// TODO - move to another file and add UT for it. Inject and use.
private fun prioritiseStops(stopResults: List<SearchStopState.StopResult>): List<SearchStopState.StopResult> {
val sortedTransportModes = TransportMode.sortedValues(TransportModeSortOrder.PRIORITY)
val transportModePriorityMap = sortedTransportModes.mapIndexed { index, transportMode ->
transportMode.productClass to index
}.toMap()

// TODO - these should come from Firebase config and have only these hardcoded as fallback.
val highPriorityStopIds = listOf(
"200060",
"200070",
"200080",
"206010",
"2150106",
"200017",
"200039",
"201016",
"201039",
"201080",
"200066",
"200030",
"200046",
"200050",

)

return stopResults.sortedWith(compareBy(
{ stopResult ->
if (stopResult.stopId in highPriorityStopIds) 0 else 1
},
{ stopResult ->
stopResult.transportModeType.minOfOrNull {
transportModePriorityMap[it.productClass] ?: Int.MAX_VALUE
} ?: Int.MAX_VALUE
},
{ it.stopName }
))
}

private fun SearchStopState.displayData(stopsResult: List<SearchStopState.StopResult>) = copy(
stops = stopsResult.toImmutableList(),
isLoading = false,
Expand All @@ -104,6 +145,17 @@ class SearchStopViewModel(
}
}

fun filterProductClasses(
stopResults: List<SearchStopState.StopResult>,
excludedProductClasses: List<Int>,
): List<SearchStopState.StopResult> {
return stopResults.filter { stopResult ->
val productClasses = stopResult.transportModeType.map { it.productClass }
productClasses.any { it !in excludedProductClasses }
}
}

/// TODO - move to mapper:
private fun SelectProductClassesForStop.toStopResult() = SearchStopState.StopResult(
stopId = stopId,
stopName = stopName,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package xyz.ksharma.krail.trip.planner.ui.searchstop

import kotlinx.collections.immutable.toImmutableList
import kotlinx.coroutines.test.runTest
import xyz.ksharma.krail.trip.planner.ui.state.TransportMode
import xyz.ksharma.krail.trip.planner.ui.state.searchstop.SearchStopState
import kotlin.test.Test
import kotlin.test.assertEquals

class StopFilterByProductClassTest {

@Test
fun `should return stops excluding given product classes`() = runTest {
// Given
val testCases = listOf(
TestCase(
excludedClasses = listOf(1),
expectedStopIds = listOf("10101101", "10101105", "12349", "12356")
),
TestCase(
excludedClasses = listOf(1, 2),
expectedStopIds = listOf("12349", "12356")
),
TestCase(
excludedClasses = listOf(5),
expectedStopIds = listOf("10101101", "10101100", "10101105", "12349")
),
// All product classes are excluded
TestCase(
excludedClasses = listOf(1, 2, 5),
expectedStopIds = listOf()
)
)

testCases.forEach { testCase ->
// When
val actualResults = filterProductClasses(
stopResults = stopResults,
excludedProductClasses = testCase.excludedClasses
)

val actualStopIds = actualResults.map { it.stopId }

// Then
assertEquals(testCase.expectedStopIds.sorted(), actualStopIds.sorted())
}
}

private data class TestCase(
val excludedClasses: List<Int>,
val expectedStopIds: List<String>,
)

companion object {
private val stopResults = listOf(
SearchStopState.StopResult(
stopName = "Town Hall Station",
stopId = "10101101",
transportModeType = listOf(
TransportMode.Train(),
TransportMode.Metro()
).toImmutableList(),
),
SearchStopState.StopResult(
stopName = "Wynyard Station",
stopId = "10101100",
transportModeType = listOf(TransportMode.Train()).toImmutableList(),
),
SearchStopState.StopResult(
stopName = "Metro Only Station",
stopId = "10101105",
transportModeType = listOf(TransportMode.Metro()).toImmutableList(),
),
SearchStopState.StopResult(
stopName = "Schofields Station",
stopId = "12349",
transportModeType = listOf(
TransportMode.Bus(),
TransportMode.Train()
).toImmutableList(),
),
SearchStopState.StopResult(
stopName = "103 ABC Rd, Hallway",
stopId = "12356",
transportModeType = listOf(TransportMode.Bus()).toImmutableList(),
),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,8 @@ internal class RealSandook(factory: SandookDriverFactory) : Sandook {
excludeProductClassList: List<Int>,
): List<SelectProductClassesForStop> {
return nswStopsQueries.selectProductClassesForStop(
stopName,
stopName,
productClass = excludeProductClassList.map { it.toLong() },
stopId = stopName,
stopName = stopName,
).executeAsList()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ interface Sandook {
*/
fun selectStops(
stopName: String,
excludeProductClassList: List<Int> = emptyList(),
excludeProductClassList: List<Int>,
): List<SelectProductClassesForStop>

// endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,10 @@ selectProductClassesForStop:
SELECT s.*,
COALESCE(GROUP_CONCAT(p.productClass), '') AS productClasses
FROM NswStops AS s
LEFT JOIN NswStopProductClass AS p ON s.stopId = p.stopId
LEFT JOIN NswStopProductClass AS p
ON s.stopId = p.stopId
WHERE (
s.stopId = ? -- Exact match scenario
OR s.stopName LIKE '%' || ? || '%' -- Partial match scenario
)
AND s.stopId NOT IN (
SELECT stopId
FROM NswStopProductClass
WHERE productClass IN ?
s.stopId = :stopId -- Use named parameter :stopId
OR s.stopName LIKE '%' || :stopName || '%' -- Use named parameter :stopName
)
GROUP BY s.stopId;