From 0e1dd01274584252c7a3e38fc10bf6a042d21925 Mon Sep 17 00:00:00 2001 From: Jonatan Rhodin Date: Tue, 20 Aug 2024 10:20:34 +0200 Subject: [PATCH] Fixes based on feedback --- android/app/src/main/AndroidManifest.xml | 2 +- .../LocaleChangedBroadcastReceiver.kt | 2 +- .../relaylist/RelayListExtensions.kt | 3 +++ .../repository/RelayListRepository.kt | 15 +++++-------- .../mullvadvpn/util/StringExtensions.kt | 3 --- .../RelayLocationTranslationRepository.kt | 22 ++++++++++--------- 6 files changed, 23 insertions(+), 24 deletions(-) rename android/app/src/main/kotlin/net/mullvad/mullvadvpn/{broadcastreceiver => receiver}/LocaleChangedBroadcastReceiver.kt (92%) diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 2144f1f94542..28d6a17ed1f6 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -106,7 +106,7 @@ - diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/broadcastreceiver/LocaleChangedBroadcastReceiver.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/receiver/LocaleChangedBroadcastReceiver.kt similarity index 92% rename from android/app/src/main/kotlin/net/mullvad/mullvadvpn/broadcastreceiver/LocaleChangedBroadcastReceiver.kt rename to android/app/src/main/kotlin/net/mullvad/mullvadvpn/receiver/LocaleChangedBroadcastReceiver.kt index 28199f8d500a..8969ffd14c16 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/broadcastreceiver/LocaleChangedBroadcastReceiver.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/receiver/LocaleChangedBroadcastReceiver.kt @@ -1,4 +1,4 @@ -package net.mullvad.mullvadvpn.broadcastreceiver +package net.mullvad.mullvadvpn.receiver import android.content.BroadcastReceiver import android.content.Context diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/relaylist/RelayListExtensions.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/relaylist/RelayListExtensions.kt index ea017339f6c2..6d308e3b267f 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/relaylist/RelayListExtensions.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/relaylist/RelayListExtensions.kt @@ -55,3 +55,6 @@ fun List.getRelayItemsByCodes( ): List = this.filter { codes.contains(it.id) } + this.flatMap { it.descendants() }.filter { codes.contains(it.id) } + +fun List.sortedByName() = + this.sortedWith(compareBy(String.CASE_INSENSITIVE_ORDER) { it.name }) diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/RelayListRepository.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/RelayListRepository.kt index fbb1d70ad6f8..69a35c406ba0 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/RelayListRepository.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/RelayListRepository.kt @@ -25,7 +25,7 @@ import net.mullvad.mullvadvpn.lib.model.cities import net.mullvad.mullvadvpn.lib.model.name import net.mullvad.mullvadvpn.lib.shared.RelayLocationTranslationRepository import net.mullvad.mullvadvpn.relaylist.findByGeoLocationId -import net.mullvad.mullvadvpn.util.sortedByName +import net.mullvad.mullvadvpn.relaylist.sortedByName class RelayListRepository( private val managementService: ManagementService, @@ -36,11 +36,11 @@ class RelayListRepository( combine(managementService.relayCountries, translationRepository.translations) { countries, translations -> - countries.translateRelay(translations) + countries.translateRelays(translations) } .stateIn(CoroutineScope(dispatcher), SharingStarted.WhileSubscribed(), emptyList()) - private fun List.translateRelay( + private fun List.translateRelays( translations: Map ): List { if (translations.isEmpty()) { @@ -49,19 +49,16 @@ class RelayListRepository( return Every.list() .modify(this) { - it.copy { + it.copy { RelayItem.Location.Country.name set translations.getOrDefault(it.name, it.name) RelayItem.Location.Country.cities.every(Every.list()).name transform { cityName -> translations.getOrDefault(cityName, cityName) } - RelayItem.Location.Country.cities transform - { cities -> - cities.sortedByName { it.name } - } + RelayItem.Location.Country.cities transform { cities -> cities.sortedByName() } } } - .sortedByName { it.name } + .sortedByName() } val wireguardEndpointData: StateFlow = diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/util/StringExtensions.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/util/StringExtensions.kt index ea1adf5e3537..adc64ec41e31 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/util/StringExtensions.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/util/StringExtensions.kt @@ -14,6 +14,3 @@ fun String.removeHtmlTags(): String = Html.fromHtml(this, HtmlCompat.FROM_HTML_MODE_LEGACY).toString() fun List.trimAll() = map { it.trim() } - -fun List.sortedByName(comparator: (T) -> String) = - this.sortedWith(compareBy(String.CASE_INSENSITIVE_ORDER, comparator)) diff --git a/android/lib/shared/src/main/kotlin/net/mullvad/mullvadvpn/lib/shared/RelayLocationTranslationRepository.kt b/android/lib/shared/src/main/kotlin/net/mullvad/mullvadvpn/lib/shared/RelayLocationTranslationRepository.kt index 2a2fc7b8faef..edaa0c1ebc41 100644 --- a/android/lib/shared/src/main/kotlin/net/mullvad/mullvadvpn/lib/shared/RelayLocationTranslationRepository.kt +++ b/android/lib/shared/src/main/kotlin/net/mullvad/mullvadvpn/lib/shared/RelayLocationTranslationRepository.kt @@ -12,6 +12,7 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.MainScope import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.filterNotNull import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.withContext @@ -26,29 +27,30 @@ class RelayLocationTranslationRepository( ) { val translations: StateFlow = localeRepository.currentLocale + .filterNotNull() .map { loadTranslations(it) } .stateIn(externalScope, SharingStarted.Eagerly, emptyMap()) - private suspend fun loadTranslations(locale: Locale?): Translations = + private suspend fun loadTranslations(locale: Locale): Translations = withContext(dispatcher) { - Logger.d("Updating translations based $locale") - if (locale == null || locale.language == DEFAULT_LANGUAGE) emptyMap() + Logger.d("Updating translations based on $locale") + if (locale.language == DEFAULT_LANGUAGE) emptyMap() else { // Load current translations val xml = context.resources.getXml(R.xml.relay_locations) - loadRelayTranslation(xml) + xml.loadRelayTranslation() } } - private fun loadRelayTranslation(xml: XmlResourceParser): Map { + private fun XmlResourceParser.loadRelayTranslation(): Map { val translation = mutableMapOf() - while (xml.eventType != XmlResourceParser.END_DOCUMENT) { - if (xml.eventType == XmlResourceParser.START_TAG && xml.name == "string") { - val key = xml.getAttributeValue(null, "name") - val value = xml.nextText() + while (this.eventType != XmlResourceParser.END_DOCUMENT) { + if (this.eventType == XmlResourceParser.START_TAG && this.name == "string") { + val key = this.getAttributeValue(null, "name") + val value = this.nextText() translation[key] = value } - xml.next() + this.next() } return translation.toMap() }