Skip to content

Commit

Permalink
Fixes based on feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Pururun committed Aug 20, 2024
1 parent 9734f04 commit 0e1dd01
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 24 deletions.
2 changes: 1 addition & 1 deletion android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
<meta-data android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
<receiver android:name=".broadcastreceiver.LocaleChangedBroadcastReceiver"
<receiver android:name=".receiver.LocaleChangedBroadcastReceiver"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.LOCALE_CHANGED" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.mullvad.mullvadvpn.broadcastreceiver
package net.mullvad.mullvadvpn.receiver

import android.content.BroadcastReceiver
import android.content.Context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,6 @@ fun List<RelayItem.Location.Country>.getRelayItemsByCodes(
): List<RelayItem.Location> =
this.filter { codes.contains(it.id) } +
this.flatMap { it.descendants() }.filter { codes.contains(it.id) }

fun <T : RelayItem> List<T>.sortedByName() =
this.sortedWith(compareBy(String.CASE_INSENSITIVE_ORDER) { it.name })
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<RelayItem.Location.Country>.translateRelay(
private fun List<RelayItem.Location.Country>.translateRelays(
translations: Map<String, String>
): List<RelayItem.Location.Country> {
if (translations.isEmpty()) {
Expand All @@ -49,19 +49,16 @@ class RelayListRepository(

return Every.list<RelayItem.Location.Country>()
.modify(this) {
it.copy<RelayItem.Location.Country> {
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<WireguardEndpointData> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,3 @@ fun String.removeHtmlTags(): String =
Html.fromHtml(this, HtmlCompat.FROM_HTML_MODE_LEGACY).toString()

fun List<String>.trimAll() = map { it.trim() }

fun <T> List<T>.sortedByName(comparator: (T) -> String) =
this.sortedWith(compareBy(String.CASE_INSENSITIVE_ORDER, comparator))
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,29 +27,30 @@ class RelayLocationTranslationRepository(
) {
val translations: StateFlow<Translations> =
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<String, String> {
private fun XmlResourceParser.loadRelayTranslation(): Map<String, String> {
val translation = mutableMapOf<String, String>()
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()
}
Expand Down

0 comments on commit 0e1dd01

Please sign in to comment.