Skip to content

Commit

Permalink
horror show
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawa committed Jul 23, 2024
1 parent b2a346a commit b74015d
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 174 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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 @@ -9,123 +10,43 @@ fun List<RelayItem.Location.Country>.findByGeoLocationId(geoLocationId: GeoLocat
fun List<RelayItem.Location.Country>.findByGeoLocationId(geoLocationId: GeoLocationId.City) =
flatMap { it.cities }.firstOrNull { it.id == geoLocationId }

/**
* Filter and expand the list based on search terms If a country is matched, that country and all
* its children are added to the list, but the country is not expanded If a city is matched, its
* parent country is added and expanded if needed and its children are added, but the city is not
* expanded If a relay is matched, its parents are added and expanded and itself is also added.
*/
@Suppress("NestedBlockDepth")
// fun List<RelayItem.Location.Country>.filterOnSearchTerm(
// searchTerm: String,
// selectedItem: RelayItemId?
// ): List<RelayItem.Location.Country> {
// return if (searchTerm.length >= MIN_SEARCH_LENGTH) {
// val filteredCountries = mutableMapOf<GeoLocationId.Country, RelayItem.Location.Country>()
// this.forEach { relayCountry ->
// val cities = mutableListOf<RelayItem.Location.City>()
//
// // Try to match the search term with a country
// // If we match a country, add that country and all cities and relays in that country
// // Do not currently expand the country or any city
// if (relayCountry.name.contains(other = searchTerm, ignoreCase = true)) {
// cities.addAll(relayCountry.cities.map { city -> city.copy(expanded = false) })
// filteredCountries[relayCountry.id] =
// relayCountry.copy(expanded = false, cities = cities)
// }
//
// // Go through and try to match the search term with every city
// relayCountry.cities.forEach { relayCity ->
// val relays = mutableListOf<RelayItem.Location.Relay>()
// // If we match and we already added the country to the filtered list just expand
// the
// // country.
// // If the country is not currently in the filtered list, add it and expand it.
// // Finally if the city has not already been added to the filtered list, add it,
// but
// // do not expand it yet.
// if (relayCity.name.contains(other = searchTerm, ignoreCase = true)) {
// val value = filteredCountries[relayCountry.id]
// if (value != null) {
// filteredCountries[relayCountry.id] = value.copy(expanded = true)
// } else {
// filteredCountries[relayCountry.id] =
// relayCountry.copy(expanded = true, cities = cities)
// }
// if (cities.none { city -> city.id == relayCity.id }) {
// cities.add(relayCity.copy(expanded = false))
// }
// }
//
// // Go through and try to match the search term with every relay
// relayCity.relays.forEach { relay ->
// // If we match a relay, check if the county the relay is in already is added,
// if
// // so expand, if not add and expand the country.
// // Check if the city that the relay is in is already added to the filtered
// list,
// // if so expand it, if not add it to the filtered list and expand it.
// // Finally add the relay to the list.
// if (relay.name.contains(other = searchTerm, ignoreCase = true)) {
// val value = filteredCountries[relayCountry.id]
// if (value != null) {
// filteredCountries[relayCountry.id] = value.copy(expanded = true)
// } else {
// filteredCountries[relayCountry.id] =
// relayCountry.copy(expanded = true, cities = cities)
// }
// val cityIndex = cities.indexOfFirst { it.id == relayCity.id }
//
// // No city found
// if (cityIndex < 0) {
// cities.add(relayCity.copy(expanded = true, relays = relays))
// } else {
// // Update found city as expanded
// cities[cityIndex] = cities[cityIndex].copy(expanded = true)
// }
//
// relays.add(relay.copy())
// }
// }
// }
// }
// filteredCountries.values.sortedBy { it.name }
// } else {
// this.expandItemForSelection(selectedItem)
// }
// }
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 }

/// ** Expand the parent(s), if any, for the current selected item */
// private fun List<RelayItem.Location.Country>.expandItemForSelection(
// selectedItem: RelayItemId?
// ): List<RelayItem.Location.Country> {
// selectedItem ?: return this
// return when (selectedItem) {
// is CustomListId,
// is GeoLocationId.Country -> this
// is GeoLocationId.City ->
// map { if (it.id == selectedItem.country) it.copy(expanded = true) else it }
// is GeoLocationId.Hostname -> {
// map { country ->
// if (country.id == selectedItem.country) {
// country.copy(
// expanded = true,
// cities =
// country.cities.map { city ->
// if (city.id == selectedItem.city) {
// city.copy(expanded = true)
// } else {
// city
// }
// },
// )
// } else {
// country
// }
// }
// }
// }
// }
val expansionSet = matchesIds.flatMap { it.parents() }.toSet()
Logger.d("Expansion Set: $expansionSet")

val filteredCountryList = filter { it.id in expansionSet || it.id in matchesIds }
.map {
it.copy(
cities =
it.cities
.filter {
it.id in expansionSet ||
it.id in matchesIds ||
it.id.country in matchesIds
}
.map {
it.copy(
relays =
it.relays.filter {
it.id in expansionSet ||
it.id in matchesIds ||
it.id.city in matchesIds ||
it.id.country in matchesIds
})
})
}
return expansionSet to filteredCountryList
}

private fun GeoLocationId.parents(): List<GeoLocationId> =
when (this) {
is GeoLocationId.City -> listOf(country)
is GeoLocationId.Country -> emptyList()
is GeoLocationId.Hostname -> listOf(country, city)
}

fun List<RelayItem.Location.Country>.getRelayItemsByCodes(
codes: List<GeoLocationId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ inline fun <T1, T2, T3, T4, T5, T6, T7, R> combine(
args[3] as T4,
args[4] as T5,
args[5] as T6,
args[6] as T7
)
args[6] as T7)
}
}

Expand All @@ -75,11 +74,40 @@ inline fun <T1, T2, T3, T4, T5, T6, T7, T8, R> combine(
args[4] as T5,
args[5] as T6,
args[6] as T7,
args[7] as T8
)
args[7] as T8)
}
}

inline fun <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, R> combine(
flow: Flow<T1>,
flow2: Flow<T2>,
flow3: Flow<T3>,
flow4: Flow<T4>,
flow5: Flow<T5>,
flow6: Flow<T6>,
flow7: Flow<T7>,
flow8: Flow<T8>,
flow9: Flow<T9>,
flow10: Flow<T10>,
crossinline transform: suspend (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10) -> R
): Flow<R> {
return kotlinx.coroutines.flow.combine(
flow, flow2, flow3, flow4, flow5, flow6, flow7, flow8, flow9, flow10) { args: Array<*> ->
@Suppress("UNCHECKED_CAST")
transform(
args[0] as T1,
args[1] as T2,
args[2] as T3,
args[3] as T4,
args[4] as T5,
args[5] as T6,
args[6] as T7,
args[7] as T8,
args[8] as T9,
args[9] as T10)
}
}

fun <T> Deferred<T>.getOrDefault(default: T) =
try {
getCompleted()
Expand Down
Loading

0 comments on commit b74015d

Please sign in to comment.