Skip to content

Commit

Permalink
Clean up filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawa committed Jul 23, 2024
1 parent 2f938c4 commit 8c0c5f1
Showing 1 changed file with 7 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.mullvad.mullvadvpn.relaylist

import co.touchlab.kermit.Logger
import net.mullvad.mullvadvpn.lib.model.GeoLocationId
import net.mullvad.mullvadvpn.lib.model.RelayItem

Expand All @@ -10,14 +9,16 @@ fun List<RelayItem.Location.Country>.findByGeoLocationId(geoLocationId: GeoLocat
fun List<RelayItem.Location.Country>.findByGeoLocationId(geoLocationId: GeoLocationId.City) =
flatMap { it.cities }.firstOrNull { it.id == geoLocationId }

fun List<RelayItem.Location.Country>.search(searchTerm: String): List<GeoLocationId> =
withDescendants().filter { it.name.contains(searchTerm, ignoreCase = true) }.map { it.id }

fun List<GeoLocationId>.expansionSet() = flatMap { it.ancestors() }.toSet()

fun List<RelayItem.Location.Country>.newFilterOnSearch(
searchTerm: String
): Pair<Set<GeoLocationId>, List<RelayItem.Location.Country>> {
val matchesIds =
withDescendants().filter { it.name.contains(searchTerm, ignoreCase = true) }.map { it.id }

val expansionSet = matchesIds.flatMap { it.ancestors() }.toSet()
Logger.d("Expansion Set: $expansionSet")
val matchesIds = search(searchTerm)
val expansionSet = matchesIds.expansionSet()

val filteredCountryList = mapNotNull { country ->
if (country.id in matchesIds) {
Expand Down

0 comments on commit 8c0c5f1

Please sign in to comment.