Move extensions enabled languages on top (closes #5694) (#5998)

This commit is contained in:
Andreas 2021-10-01 15:15:04 +02:00 committed by GitHub
parent 09f0ac866f
commit c6d4e4c15f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -11,7 +11,6 @@ import android.view.MenuItem
import android.view.View import android.view.View
import androidx.appcompat.view.ContextThemeWrapper import androidx.appcompat.view.ContextThemeWrapper
import androidx.core.os.bundleOf import androidx.core.os.bundleOf
import androidx.preference.Preference
import androidx.preference.PreferenceGroupAdapter import androidx.preference.PreferenceGroupAdapter
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import androidx.preference.PreferenceScreen import androidx.preference.PreferenceScreen
@ -103,22 +102,54 @@ class ExtensionDetailsController(bundle: Bundle? = null) :
val isMultiLangSingleSource = isMultiSource && extension.sources.map { it.name }.distinct().size == 1 val isMultiLangSingleSource = isMultiSource && extension.sources.map { it.name }.distinct().size == 1
with(screen) { with(screen) {
extension.sources if (isMultiSource && isMultiLangSingleSource.not()) {
multiLanguagePreference(context, extension.sources)
} else {
singleLanguagePreference(context, extension.sources)
}
}
return PreferenceGroupAdapter(screen)
}
private fun PreferenceScreen.singleLanguagePreference(context: Context, sources: List<Source>) {
sources
.map { source -> LocaleHelper.getSourceDisplayName(source.lang, context) to source }
.sortedWith(compareBy({ (_, source) -> !source.isEnabled() }, { (lang, _) -> lang.lowercase() }))
.forEach { (lang, source) ->
val preferenceBlock = {
sourceSwitchPreference(source, LocaleHelper.getSourceDisplayName(lang, context))
}
preferenceBlock()
}
}
private fun PreferenceScreen.multiLanguagePreference(context: Context, sources: List<Source>) {
sources
.groupBy { (it as CatalogueSource).lang } .groupBy { (it as CatalogueSource).lang }
.toSortedMap(compareBy { LocaleHelper.getSourceDisplayName(it, context) }) .toSortedMap(compareBy { LocaleHelper.getSourceDisplayName(it, context) })
.forEach { .forEach { entry ->
val preferenceBlock = { val preferenceBlock = {
it.value entry.value
.sortedWith(compareBy({ !it.isEnabled() }, { it.name.lowercase() })) .sortedWith(compareBy({ source -> !source.isEnabled() }, { source -> source.name.lowercase() }))
.forEach { source -> .forEach { source ->
val sourcePrefs = mutableListOf<Preference>() sourceSwitchPreference(source, source.toString())
}
}
preferenceCategory {
title = LocaleHelper.getSourceDisplayName(entry.key, context)
preferenceBlock()
}
}
}
private fun PreferenceScreen.sourceSwitchPreference(source: Source, name: String) {
val block: (@DSL SwitchPreferenceCompat).() -> Unit = { val block: (@DSL SwitchPreferenceCompat).() -> Unit = {
key = source.getPreferenceKey() key = source.getPreferenceKey()
title = when { title = name
isMultiSource && !isMultiLangSingleSource -> source.toString()
else -> LocaleHelper.getSourceDisplayName(it.key, context)
}
isPersistent = false isPersistent = false
isChecked = source.isEnabled() isChecked = source.isEnabled()
@ -133,7 +164,6 @@ class ExtensionDetailsController(bundle: Bundle? = null) :
.onEach { .onEach {
val enabled = source.isEnabled() val enabled = source.isEnabled()
isChecked = enabled isChecked = enabled
sourcePrefs.forEach { pref -> pref.isVisible = enabled }
} }
.launchIn(viewScope) .launchIn(viewScope)
} }
@ -152,22 +182,6 @@ class ExtensionDetailsController(bundle: Bundle? = null) :
switchPreference(block) switchPreference(block)
} }
} }
}
if (isMultiSource && !isMultiLangSingleSource) {
preferenceCategory {
title = LocaleHelper.getSourceDisplayName(it.key, context)
preferenceBlock()
}
} else {
preferenceBlock()
}
}
}
return PreferenceGroupAdapter(screen)
}
override fun onDestroyView(view: View) { override fun onDestroyView(view: View) {
preferenceScreen = null preferenceScreen = null