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

Fix GB location UI #4016

Merged
merged 1 commit into from
Dec 16, 2023
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 @@ -18,17 +18,30 @@ package com.duckduckgo.networkprotection.impl.settings.geoswitching

import java.util.*

private const val REGIONAL_INDICATOR_OFFSET = 0x1F1A5

/*
* The regional indicators go from 0x1F1E6 (A) to 0x1F1FF (Z).
* This is the A regional indicator value minus 65 decimal so
* that we can just add this to the A-Z char
*/
internal fun getEmojiForCountryCode(countryCode: String): String {
return when (countryCode) {
"uk" -> "🇬🇧"
"fr" -> "🇫🇷"
"ca" -> "🇨🇦"
"us" -> "🇺🇸"
"de" -> "🇩🇪"
"es" -> "🇪🇸"
"nl" -> "🇳🇱"
else -> "🏳️"
@Suppress("NAME_SHADOWING")
val countryCode = countryCode.uppercase()

if (countryCode.isBlank()) return "🏳️"
if (countryCode.length > 2) return "🏳️"
val (first, second) = countryCode.toCharArray().run {
this[0] to this[1]
}
if (first < 'A' || first > 'Z') return "🏳️"
if (second < 'A' || second > 'Z') return "🏳️"

return countryCode.uppercase()
.split("")
.filter { it.isNotBlank() }
.map { it.codePointAt(0) + REGIONAL_INDICATOR_OFFSET }
.joinToString("") { String(Character.toChars(it)) }
}

internal fun getDisplayableCountry(countryCode: String): String {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.duckduckgo.networkprotection.impl.settings.geoswitching

import org.junit.Assert.assertEquals
import org.junit.Test

class CountryUtilsTest {

@Test
fun testEmojiForCountryCode() {
assertEquals("🇬🇧", getEmojiForCountryCode("gb"))
assertEquals("🇬🇧", getEmojiForCountryCode("GB"))

assertEquals("🇫🇷", getEmojiForCountryCode("fr"))
assertEquals("🇫🇷", getEmojiForCountryCode("FR"))

assertEquals("🇨🇦", getEmojiForCountryCode("ca"))
assertEquals("🇨🇦", getEmojiForCountryCode("CA"))

assertEquals("🇺🇸", getEmojiForCountryCode("us"))
assertEquals("🇺🇸", getEmojiForCountryCode("US"))

assertEquals("🇩🇪", getEmojiForCountryCode("de"))
assertEquals("🇩🇪", getEmojiForCountryCode("DE"))

assertEquals("🇪🇸", getEmojiForCountryCode("es"))
assertEquals("🇪🇸", getEmojiForCountryCode("ES"))

assertEquals("🇳🇱", getEmojiForCountryCode("nl"))
assertEquals("🇳🇱", getEmojiForCountryCode("NL"))

assertEquals("🏳️", getEmojiForCountryCode(""))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class FakeNetpEgressServersProvider : NetpEgressServersProvider {
cities = listOf("El Segundo", "Chicago", "Atlanta", "Newark"),
),
ServerLocation(
countryCode = "uk",
countryCode = "gb",
countryName = "UK",
cities = emptyList(),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class NetpGeoSwitchingViewModelTest {
assertEquals(
it.items[4],
CountryItem(
countryCode = "uk",
countryCode = "gb",
countryEmoji = "🇬🇧",
countryName = "UK",
cities = emptyList(),
Expand Down
Loading