Skip to content

Commit

Permalink
Rename RelayItem code
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawa committed Jul 23, 2024
1 parent d285960 commit 2f938c4
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ private val DUMMY_RELAY_2 =
private val DUMMY_RELAY_CITY_1 =
RelayItem.Location.City(
name = "Relay City 1",
id = GeoLocationId.City(countryCode = GeoLocationId.Country("RCo1"), cityCode = "RCi1"),
id = GeoLocationId.City(country = GeoLocationId.Country("RCo1"), code = "RCi1"),
relays = listOf(DUMMY_RELAY_1),
expanded = false
)
private val DUMMY_RELAY_CITY_2 =
RelayItem.Location.City(
name = "Relay City 2",
id = GeoLocationId.City(countryCode = GeoLocationId.Country("RCo2"), cityCode = "RCi2"),
id = GeoLocationId.City(country = GeoLocationId.Country("RCo2"), code = "RCi2"),
relays = listOf(DUMMY_RELAY_2),
expanded = false
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private fun generateRelayItemRelay(
id =
GeoLocationId.Hostname(
city = cityCode,
hostname = hostName,
code = hostName,
),
active = active,
provider = Provider(ProviderId("Provider"), Ownership.MullvadOwned),
Expand All @@ -69,6 +69,6 @@ private fun String.generateCityCode(countryCode: GeoLocationId.Country) =
GeoLocationId.City(countryCode, take(CITY_CODE_LENGTH).lowercase())

private fun generateHostname(city: GeoLocationId.City, index: Int) =
"${city.countryCode.countryCode}-${city.cityCode}-wg-${index+1}"
"${city.country.code}-${city.code}-wg-${index+1}"

private const val CITY_CODE_LENGTH = 3
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private fun PreviewEditCustomListScreen() {
GeoLocationId.Hostname(
GeoLocationId.City(
GeoLocationId.Country("country"),
cityCode = "city"
code = "city"
),
"hostname",
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fun List<RelayItem.Location.Country>.newFilterOnSearch(
val matchesIds =
withDescendants().filter { it.name.contains(searchTerm, ignoreCase = true) }.map { it.id }

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

val filteredCountryList = mapNotNull { country ->
Expand All @@ -40,7 +40,7 @@ fun List<RelayItem.Location.Country>.newFilterOnSearch(
return expansionSet to filteredCountryList
}

private fun GeoLocationId.parents(): List<GeoLocationId> =
private fun GeoLocationId.ancestors(): List<GeoLocationId> =
when (this) {
is GeoLocationId.City -> listOf(country)
is GeoLocationId.Country -> emptyList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ class SelectLocationViewModel(
val item = relayListRepository.selectedLocation.value.getOrNull()
return when (item) {
is CustomListId -> setOf()
is GeoLocationId.City -> setOf(item.countryCode.countryCode)
is GeoLocationId.City -> setOf(item.country.code)
is GeoLocationId.Country -> setOf()
is GeoLocationId.Hostname -> setOf(item.country.countryCode, item.city.cityCode)
is GeoLocationId.Hostname -> setOf(item.country.code, item.city.code)
null -> setOf()
}
}
Expand Down Expand Up @@ -281,9 +281,7 @@ class SelectLocationViewModel(
(parent?.value ?: "") +
when (this) {
is CustomListId -> value
is GeoLocationId.City -> cityCode
is GeoLocationId.Country -> countryCode
is GeoLocationId.Hostname -> hostname
is GeoLocationId -> code
}

fun selectRelay(relayItem: RelayItem) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class CustomListActionUseCaseTest {
val action = CustomListAction.Delete(id = customListId)
val expectedResult =
Deleted(undo = action.not(name = name, locations = listOf(location))).right()
every { mockLocation.countryCode } returns location.countryCode
every { mockLocation.code } returns location.code
coEvery { mockCustomListsRepository.deleteCustomList(id = customListId) } returns
Unit.right()
coEvery { mockCustomListsRepository.getCustomListById(customListId) } returns
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@ internal fun GeoLocationId.fromDomain(): ManagementInterface.GeographicLocationC
ManagementInterface.GeographicLocationConstraint.newBuilder()
.apply {
when (val id = this@fromDomain) {
is GeoLocationId.Country -> setCountry(id.countryCode)
is GeoLocationId.City -> setCountry(id.countryCode.countryCode).setCity(id.cityCode)
is GeoLocationId.Country -> setCountry(id.code)
is GeoLocationId.City -> setCountry(id.country.code).setCity(id.code)
is GeoLocationId.Hostname ->
setCountry(id.country.countryCode)
.setCity(id.city.cityCode)
.setHostname(id.hostname)
setCountry(id.country.code)
.setCity(id.city.code)
.setHostname(id.code)
}
}
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ sealed interface RelayItem {
val provider: Provider,
override val active: Boolean,
) : Location {
override val name: String = id.hostname
override val name: String = id.code
override val hasChildren: Boolean = false

companion object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package net.mullvad.mullvadvpn.lib.model

import android.os.Parcelable
import arrow.optics.optics
import kotlinx.parcelize.IgnoredOnParcel
import kotlinx.parcelize.Parcelize

@optics
Expand All @@ -21,28 +22,29 @@ value class CustomListId(val value: String) : RelayItemId, Parcelable {
sealed interface GeoLocationId : RelayItemId, Parcelable {
@optics
@Parcelize
data class Country(val countryCode: String) : GeoLocationId {
data class Country(override val code: String) : GeoLocationId {
companion object
}

@optics
@Parcelize
data class City(val countryCode: Country, val cityCode: String) : GeoLocationId {
data class City(override val country: Country, override val code: String) : GeoLocationId {
companion object
}

@optics
@Parcelize
data class Hostname(val city: City, val hostname: String) : GeoLocationId {
data class Hostname(val city: City, override val code: String) : GeoLocationId {
companion object
}

val code: String
val country: Country
get() =
when (this) {
is Country -> this
is City -> this.countryCode
is Hostname -> this.city.countryCode
is City -> this.country
is Hostname -> this.city.country
}

companion object
Expand Down

0 comments on commit 2f938c4

Please sign in to comment.