Improve language list prioritization. (#3293)

Partially addresses #3277
This commit is contained in:
Levi Bard 2023-02-25 21:15:21 +01:00 committed by GitHub
parent 4a0251800d
commit f2b07196e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 128 additions and 56 deletions

View File

@ -90,7 +90,7 @@ import com.keylesspalace.tusky.settings.PrefKeys
import com.keylesspalace.tusky.util.APP_THEME_DEFAULT import com.keylesspalace.tusky.util.APP_THEME_DEFAULT
import com.keylesspalace.tusky.util.PickMediaFiles import com.keylesspalace.tusky.util.PickMediaFiles
import com.keylesspalace.tusky.util.afterTextChanged import com.keylesspalace.tusky.util.afterTextChanged
import com.keylesspalace.tusky.util.getInitialLanguage import com.keylesspalace.tusky.util.getInitialLanguages
import com.keylesspalace.tusky.util.getLocaleList import com.keylesspalace.tusky.util.getLocaleList
import com.keylesspalace.tusky.util.getMediaSize import com.keylesspalace.tusky.util.getMediaSize
import com.keylesspalace.tusky.util.hide import com.keylesspalace.tusky.util.hide
@ -267,7 +267,7 @@ class ComposeActivity :
binding.composeScheduleView.setDateTime(composeOptions?.scheduledAt) binding.composeScheduleView.setDateTime(composeOptions?.scheduledAt)
} }
setupLanguageSpinner(getInitialLanguage(composeOptions?.language, accountManager.activeAccount)) setupLanguageSpinner(getInitialLanguages(composeOptions?.language, accountManager.activeAccount))
setupComposeField(preferences, viewModel.startingText) setupComposeField(preferences, viewModel.startingText)
setupContentWarningField(composeOptions?.contentWarning) setupContentWarningField(composeOptions?.contentWarning)
setupPollView() setupPollView()
@ -543,7 +543,7 @@ class ComposeActivity :
) )
} }
private fun setupLanguageSpinner(initialLanguage: String) { private fun setupLanguageSpinner(initialLanguages: List<String>) {
binding.composePostLanguageButton.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { binding.composePostLanguageButton.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>, view: View?, position: Int, id: Long) { override fun onItemSelected(parent: AdapterView<*>, view: View?, position: Int, id: Long) {
viewModel.postLanguage = (parent.adapter.getItem(position) as Locale).modernLanguageCode viewModel.postLanguage = (parent.adapter.getItem(position) as Locale).modernLanguageCode
@ -554,7 +554,7 @@ class ComposeActivity :
} }
} }
binding.composePostLanguageButton.apply { binding.composePostLanguageButton.apply {
adapter = LocaleAdapter(context, android.R.layout.simple_spinner_dropdown_item, getLocaleList(initialLanguage)) adapter = LocaleAdapter(context, android.R.layout.simple_spinner_dropdown_item, getLocaleList(initialLanguages))
setSelection(0) setSelection(0)
} }
} }

View File

@ -49,7 +49,7 @@ import com.keylesspalace.tusky.settings.makePreferenceScreen
import com.keylesspalace.tusky.settings.preference import com.keylesspalace.tusky.settings.preference
import com.keylesspalace.tusky.settings.preferenceCategory import com.keylesspalace.tusky.settings.preferenceCategory
import com.keylesspalace.tusky.settings.switchPreference import com.keylesspalace.tusky.settings.switchPreference
import com.keylesspalace.tusky.util.getInitialLanguage import com.keylesspalace.tusky.util.getInitialLanguages
import com.keylesspalace.tusky.util.getLocaleList import com.keylesspalace.tusky.util.getLocaleList
import com.keylesspalace.tusky.util.getTuskyDisplayName import com.keylesspalace.tusky.util.getTuskyDisplayName
import com.keylesspalace.tusky.util.makeIcon import com.keylesspalace.tusky.util.makeIcon
@ -197,7 +197,7 @@ class AccountPreferencesFragment : PreferenceFragmentCompat(), Injectable {
} }
listPreference { listPreference {
val locales = getLocaleList(getInitialLanguage(null, accountManager.activeAccount)) val locales = getLocaleList(getInitialLanguages(null, accountManager.activeAccount))
setTitle(R.string.pref_default_post_language) setTitle(R.string.pref_default_post_language)
// Explicitly add "System default" to the start of the list // Explicitly add "System default" to the start of the list
entries = ( entries = (

View File

@ -23,67 +23,57 @@ import java.util.Locale
private const val TAG: String = "LocaleUtils" private const val TAG: String = "LocaleUtils"
private fun mergeLocaleListCompat(list: MutableList<Locale>, localeListCompat: LocaleListCompat) { private fun LocaleListCompat.toList(): List<Locale> {
for (index in 0 until localeListCompat.size()) { val list = mutableListOf<Locale>()
val locale = localeListCompat[index] for (index in 0 until this.size()) {
if (locale != null && list.none { locale.language == it.language }) { this[index]?.let { list.add(it) }
list.add(locale)
}
} }
return list
} }
// Ensure that the locale whose code matches the given language is first in the list // Ensure that the locale whose code matches the given language is first in the list
private fun ensureLanguageIsFirst(locales: MutableList<Locale>, language: String) { private fun ensureLanguagesAreFirst(locales: MutableList<Locale>, languages: List<String>) {
var currentLocaleIndex = locales.indexOfFirst { it.language == language } for (language in languages.reversed()) {
if (currentLocaleIndex < 0) { // Iterate prioritized languages in reverse to retain the order once bubbled to the top
// Recheck against modern language codes var currentLocaleIndex = locales.indexOfFirst { it.language == language }
// This should only happen when replying or when the per-account post language is set
// to a modern code
currentLocaleIndex = locales.indexOfFirst { it.modernLanguageCode == language }
if (currentLocaleIndex < 0) { if (currentLocaleIndex < 0) {
// This can happen when: // Recheck against modern language codes
// - Your per-account posting language is set to one android doesn't know (e.g. toki pona) // This should only happen when replying or when the per-account post language is set
// - Replying to a post in a language android doesn't know // to a modern code
locales.add(0, Locale(language)) currentLocaleIndex = locales.indexOfFirst { it.modernLanguageCode == language }
Log.w(TAG, "Attempting to use unknown language tag '$language'")
return
}
}
if (currentLocaleIndex > 0) { if (currentLocaleIndex < 0) {
// Move preselected locale to the top // This can happen when:
locales.add(0, locales.removeAt(currentLocaleIndex)) // - Your per-account posting language is set to one android doesn't know (e.g. toki pona)
// - Replying to a post in a language android doesn't know
locales.add(0, Locale(language))
Log.w(TAG, "Attempting to use unknown language tag '$language'")
continue
}
}
if (currentLocaleIndex > 0) {
// Move preselected locale to the top
locales.add(0, locales.removeAt(currentLocaleIndex))
}
} }
} }
fun getInitialLanguage(language: String? = null, activeAccount: AccountEntity? = null): String { fun getInitialLanguages(language: String? = null, activeAccount: AccountEntity? = null): List<String> {
return if (language.isNullOrEmpty()) { val selected = listOfNotNull(language, activeAccount?.defaultPostLanguage)
// Account-specific language set on the server val system = AppCompatDelegate.getApplicationLocales().toList() +
if (activeAccount?.defaultPostLanguage?.isNotEmpty() == true) { LocaleListCompat.getDefault().toList()
activeAccount.defaultPostLanguage
} else { return (selected + system.map { it.language }).distinct().filter { it.isNotEmpty() }
// Setting the application ui preference sets the default locale
AppCompatDelegate.getApplicationLocales()[0]?.language
?: Locale.getDefault().language
}
} else {
language
}
} }
fun getLocaleList(initialLanguage: String): List<Locale> { fun getLocaleList(initialLanguages: List<String>): List<Locale> {
val locales = mutableListOf<Locale>() val locales = Locale.getAvailableLocales().filter {
mergeLocaleListCompat(locales, AppCompatDelegate.getApplicationLocales()) // configured app languages first
mergeLocaleListCompat(locales, LocaleListCompat.getDefault()) // then configured system languages
locales.addAll( // finally, other languages
// Only "base" languages, "en" but not "en_DK" // Only "base" languages, "en" but not "en_DK"
Locale.getAvailableLocales().filter { it.country.isNullOrEmpty() &&
it.country.isNullOrEmpty() && it.script.isNullOrEmpty() &&
it.script.isNullOrEmpty() && it.variant.isNullOrEmpty()
it.variant.isNullOrEmpty() }.sortedBy { it.displayName }.toMutableList()
}.sortedBy { it.displayName } ensureLanguagesAreFirst(locales, initialLanguages)
)
ensureLanguageIsFirst(locales, initialLanguage)
return locales return locales
} }

View File

@ -0,0 +1,82 @@
package com.keylesspalace.tusky.util
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.os.LocaleListCompat
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.keylesspalace.tusky.db.AccountEntity
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito
import org.robolectric.annotation.Config
@Config(sdk = [28])
@RunWith(AndroidJUnit4::class)
class LocaleUtilsTest {
@Test
fun initialLanguagesContainReplySelectedAppAndSystem() {
val expectedLanguages = arrayOf<String?>("yi", "tok", "da", "fr", "sv", "kab")
val languages = getMockedInitialLanguages(expectedLanguages)
Assert.assertArrayEquals(expectedLanguages, languages.subList(0, expectedLanguages.size).toTypedArray())
}
@Test
fun whenReplyLanguageIsNull_DefaultLanguageIsFirst() {
val defaultLanguage = "tok"
val languages = getMockedInitialLanguages(arrayOf(null, defaultLanguage, "da", "fr", "sv", "kab"))
Assert.assertEquals(defaultLanguage, languages[0])
}
@Test
fun initialLanguagesAreDistinct() {
val defaultLanguage = "da"
val languages = getMockedInitialLanguages(arrayOf(defaultLanguage, defaultLanguage, "fr", defaultLanguage, "kab", defaultLanguage))
Assert.assertEquals(1, languages.count { it == defaultLanguage })
}
@Test
fun initialLanguageDeduplicationDoesNotReorder() {
val defaultLanguage = "da"
Assert.assertEquals(
defaultLanguage,
getMockedInitialLanguages(arrayOf(defaultLanguage, defaultLanguage, "fr", defaultLanguage, "kab", defaultLanguage))[0]
)
Assert.assertEquals(
defaultLanguage,
getMockedInitialLanguages(arrayOf(null, defaultLanguage, "fr", defaultLanguage, "kab", defaultLanguage))[0]
)
}
@Test
fun emptyInitialLanguagesAreDropped() {
val languages = getMockedInitialLanguages(arrayOf("", "", "fr", "", "kab", ""))
Assert.assertFalse(languages.any { it.isEmpty() })
}
private fun getMockedInitialLanguages(configuredLanguages: Array<String?>): List<String> {
val appLanguages = LocaleListCompat.forLanguageTags(configuredLanguages.slice(2 until 4).joinToString(","))
val systemLanguages = LocaleListCompat.forLanguageTags(configuredLanguages.slice(4 until configuredLanguages.size).joinToString(","))
Mockito.mockStatic(AppCompatDelegate::class.java).use { appCompatDelegate ->
appCompatDelegate.`when`<LocaleListCompat> { AppCompatDelegate.getApplicationLocales() }.thenReturn(appLanguages)
Mockito.mockStatic(LocaleListCompat::class.java).use { localeListCompat ->
localeListCompat.`when`<LocaleListCompat> { LocaleListCompat.getDefault() }.thenReturn(systemLanguages)
return getInitialLanguages(
configuredLanguages[0],
AccountEntity(
id = 0,
domain = "foo.bar",
accessToken = "",
clientId = null,
clientSecret = null,
isActive = true,
defaultPostLanguage = configuredLanguages[1] ?: "",
)
)
}
}
}
}