Update SettingsFragment.kt

This commit is contained in:
LagradOst 2021-11-04 11:22:06 +01:00 committed by GitHub
parent 8e9c11db4b
commit f0c34f2075
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -240,210 +240,221 @@ class SettingsFragment : PreferenceFragmentCompat() {
// pref = visual path // pref = visual path
settingsManager.edit().putString(getString(R.string.download_path_key), dirs[it]).apply() settingsManager.edit().putString(getString(R.string.download_path_key), dirs[it]).apply()
settingsManager.edit().putString(getString(R.string.download_path_pref), dirs[it]).apply() settingsManager.edit().putString(getString(R.string.download_path_pref), dirs[it]).apply()
}
}
if (preferedMediaTypePreference != null) { if (preferedMediaTypePreference != null) {
preferedMediaTypePreference.setOnPreferenceClickListener { preferedMediaTypePreference.setOnPreferenceClickListener {
val prefNames = resources.getStringArray(R.array.media_type_pref) val prefNames = resources.getStringArray(R.array.media_type_pref)
val prefValues = resources.getIntArray(R.array.media_type_pref_values) val prefValues = resources.getIntArray(R.array.media_type_pref_values)
val settingsManager = PreferenceManager.getDefaultSharedPreferences(context)
val currentPrefMedia =
settingsManager.getInt(getString(R.string.preferred_media_settings), 0)
context?.showBottomDialog(
prefNames.toList(),
prefValues.indexOf(currentPrefMedia),
getString(R.string.preferred_media_settings),
true,
{}) {
settingsManager.edit()
.putInt(getString(R.string.preferred_media_settings), prefValues[it])
.apply()
context?.initRequestClient()
}
return@setOnPreferenceClickListener true
}
}
allLayoutPreference.setOnPreferenceClickListener {
val prefNames = resources.getStringArray(R.array.app_layout)
val prefValues = resources.getIntArray(R.array.app_layout_values)
val settingsManager = PreferenceManager.getDefaultSharedPreferences(context) val settingsManager = PreferenceManager.getDefaultSharedPreferences(context)
val currentPrefMedia = val currentLayout =
settingsManager.getInt(getString(R.string.preferred_media_settings), 0) settingsManager.getInt(getString(R.string.app_layout_key), -1)
context?.showBottomDialog( context?.showBottomDialog(
prefNames.toList(), prefNames.toList(),
prefValues.indexOf(currentPrefMedia), prefValues.indexOf(currentLayout),
getString(R.string.preferred_media_settings), getString(R.string.app_layout),
true, true,
{}) { {}) {
settingsManager.edit() try {
.putInt(getString(R.string.preferred_media_settings), prefValues[it]) settingsManager.edit().putInt(getString(R.string.app_layout_key), prefValues[it])
.apply()
activity?.recreate()
} catch (e: Exception) {
logError(e)
}
}
return@setOnPreferenceClickListener true
}
colorPrimaryPreference.setOnPreferenceClickListener {
val prefNames = resources.getStringArray(R.array.themes_overlay_names)
val prefValues = resources.getStringArray(R.array.themes_overlay_names_values)
val settingsManager = PreferenceManager.getDefaultSharedPreferences(context)
val currentLayout =
settingsManager.getString(getString(R.string.primary_color_key), prefValues.first())
context?.showBottomDialog(
prefNames.toList(),
prefValues.indexOf(currentLayout),
getString(R.string.primary_color_settings),
true,
{}) {
try {
settingsManager.edit().putString(getString(R.string.primary_color_key), prefValues[it])
.apply()
activity?.recreate()
} catch (e: Exception) {
logError(e)
}
}
return@setOnPreferenceClickListener true
}
appThemePreference.setOnPreferenceClickListener {
val prefNames = resources.getStringArray(R.array.themes_names)
val prefValues = resources.getStringArray(R.array.themes_names_values)
val settingsManager = PreferenceManager.getDefaultSharedPreferences(context)
val currentLayout =
settingsManager.getString(getString(R.string.app_theme_key), prefValues.first())
context?.showBottomDialog(
prefNames.toList(),
prefValues.indexOf(currentLayout),
getString(R.string.app_theme_settings),
true,
{}) {
try {
settingsManager.edit().putString(getString(R.string.app_theme_key), prefValues[it])
.apply()
activity?.recreate()
} catch (e: Exception) {
logError(e)
}
}
return@setOnPreferenceClickListener true
}
watchQualityPreference.setOnPreferenceClickListener {
val prefNames = resources.getStringArray(R.array.quality_pref)
val prefValues = resources.getIntArray(R.array.quality_pref_values)
val settingsManager = PreferenceManager.getDefaultSharedPreferences(context)
val currentQuality =
settingsManager.getInt(
getString(R.string.watch_quality_pref),
Qualities.values().last().value
)
context?.showBottomDialog(
prefNames.toList(),
prefValues.indexOf(currentQuality),
getString(R.string.watch_quality_pref),
true,
{}) {
settingsManager.edit().putInt(getString(R.string.watch_quality_pref), prefValues[it])
.apply() .apply()
}
return@setOnPreferenceClickListener true
}
dnsPreference.setOnPreferenceClickListener {
val prefNames = resources.getStringArray(R.array.dns_pref)
val prefValues = resources.getIntArray(R.array.dns_pref_values)
val settingsManager = PreferenceManager.getDefaultSharedPreferences(context)
val currentDns =
settingsManager.getInt(getString(R.string.dns_pref), 0)
context?.showBottomDialog(
prefNames.toList(),
prefValues.indexOf(currentDns),
getString(R.string.dns_pref),
true,
{}) {
settingsManager.edit().putInt(getString(R.string.dns_pref), prefValues[it]).apply()
context?.initRequestClient() context?.initRequestClient()
} }
return@setOnPreferenceClickListener true return@setOnPreferenceClickListener true
} }
}
allLayoutPreference.setOnPreferenceClickListener { try {
val prefNames = resources.getStringArray(R.array.app_layout) val settingsManager = PreferenceManager.getDefaultSharedPreferences(context)
val prefValues = resources.getIntArray(R.array.app_layout_values)
val settingsManager = PreferenceManager.getDefaultSharedPreferences(context)
val currentLayout = beneneCount = settingsManager.getInt(getString(R.string.benene_count), 0)
settingsManager.getInt(getString(R.string.app_layout_key), -1)
context?.showBottomDialog( benenePreference.summary =
prefNames.toList(), if (beneneCount <= 0) getString(R.string.benene_count_text_none) else getString(R.string.benene_count_text).format(
prefValues.indexOf(currentLayout), beneneCount
getString(R.string.app_layout), )
true, benenePreference.setOnPreferenceClickListener {
{}) { try {
try { beneneCount++
settingsManager.edit().putInt(getString(R.string.app_layout_key), prefValues[it]).apply() settingsManager.edit().putInt(getString(R.string.benene_count), beneneCount).apply()
activity?.recreate() it.summary = getString(R.string.benene_count_text).format(beneneCount)
} catch (e : Exception) { } catch (e: Exception) {
logError(e) logError(e)
}
return@setOnPreferenceClickListener true
} }
} catch (e: Exception) {
e.printStackTrace()
} }
return@setOnPreferenceClickListener true
}
colorPrimaryPreference.setOnPreferenceClickListener { updatePreference.setOnPreferenceClickListener {
val prefNames = resources.getStringArray(R.array.themes_overlay_names) thread {
val prefValues = resources.getStringArray(R.array.themes_overlay_names_values) if (!requireActivity().runAutoUpdate(false)) {
val settingsManager = PreferenceManager.getDefaultSharedPreferences(context) activity?.runOnUiThread {
showToast(activity, R.string.no_update_found, Toast.LENGTH_SHORT)
val currentLayout = settingsManager.getString( getString(R.string.primary_color_key),prefValues.first()) }
context?.showBottomDialog(
prefNames.toList(),
prefValues.indexOf(currentLayout),
getString(R.string.primary_color_settings),
true,
{}) {
try {
settingsManager.edit().putString(getString(R.string.primary_color_key), prefValues[it]).apply()
activity?.recreate()
} catch (e : Exception) {
logError(e)
}
}
return@setOnPreferenceClickListener true
}
appThemePreference.setOnPreferenceClickListener {
val prefNames = resources.getStringArray(R.array.themes_names)
val prefValues = resources.getStringArray(R.array.themes_names_values)
val settingsManager = PreferenceManager.getDefaultSharedPreferences(context)
val currentLayout = settingsManager.getString( getString(R.string.app_theme_key),prefValues.first())
context?.showBottomDialog(
prefNames.toList(),
prefValues.indexOf(currentLayout),
getString(R.string.app_theme_settings),
true,
{}) {
try {
settingsManager.edit().putString(getString(R.string.app_theme_key), prefValues[it]).apply()
activity?.recreate()
} catch (e : Exception) {
logError(e)
}
}
return@setOnPreferenceClickListener true
}
watchQualityPreference.setOnPreferenceClickListener {
val prefNames = resources.getStringArray(R.array.quality_pref)
val prefValues = resources.getIntArray(R.array.quality_pref_values)
val settingsManager = PreferenceManager.getDefaultSharedPreferences(context)
val currentQuality =
settingsManager.getInt(getString(R.string.watch_quality_pref), Qualities.values().last().value)
context?.showBottomDialog(
prefNames.toList(),
prefValues.indexOf(currentQuality),
getString(R.string.watch_quality_pref),
true,
{}) {
settingsManager.edit().putInt(getString(R.string.watch_quality_pref), prefValues[it]).apply()
}
return@setOnPreferenceClickListener true
}
dnsPreference.setOnPreferenceClickListener {
val prefNames = resources.getStringArray(R.array.dns_pref)
val prefValues = resources.getIntArray(R.array.dns_pref_values)
val settingsManager = PreferenceManager.getDefaultSharedPreferences(context)
val currentDns =
settingsManager.getInt(getString(R.string.dns_pref), 0)
context?.showBottomDialog(
prefNames.toList(),
prefValues.indexOf(currentDns),
getString(R.string.dns_pref),
true,
{}) {
settingsManager.edit().putInt(getString(R.string.dns_pref), prefValues[it]).apply()
context?.initRequestClient()
}
return@setOnPreferenceClickListener true
}
try {
val settingsManager = PreferenceManager.getDefaultSharedPreferences(context)
beneneCount = settingsManager.getInt(getString(R.string.benene_count), 0)
benenePreference.summary =
if (beneneCount <= 0) getString(R.string.benene_count_text_none) else getString(R.string.benene_count_text).format(
beneneCount
)
benenePreference.setOnPreferenceClickListener {
try {
beneneCount++
settingsManager.edit().putInt(getString(R.string.benene_count), beneneCount).apply()
it.summary = getString(R.string.benene_count_text).format(beneneCount)
} catch (e: Exception) {
logError(e)
}
return@setOnPreferenceClickListener true
}
} catch (e: Exception) {
e.printStackTrace()
}
updatePreference.setOnPreferenceClickListener {
thread {
if (!requireActivity().runAutoUpdate(false)) {
activity?.runOnUiThread {
showToast(activity, R.string.no_update_found, Toast.LENGTH_SHORT)
} }
} }
return@setOnPreferenceClickListener true
}
localePreference.setOnPreferenceClickListener { pref ->
val tempLangs = languages.toMutableList()
if (beneneCount > 100) {
tempLangs.add(Triple("\uD83E\uDD8D", "mmmm... monke", "mo"))
}
val current = getCurrentLocale()
val languageCodes = tempLangs.map { it.third }
val languageNames = tempLangs.map { "${it.first} ${it.second}" }
val index = languageCodes.indexOf(current)
pref?.context?.showDialog(
languageNames, index, getString(R.string.app_language), true, { }
) { languageIndex ->
try {
val code = languageCodes[languageIndex]
setLocale(activity, code)
val settingsManager = PreferenceManager.getDefaultSharedPreferences(pref.context)
settingsManager.edit().putString(getString(R.string.locale_key), code).apply()
activity?.recreate()
} catch (e: Exception) {
logError(e)
}
}
return@setOnPreferenceClickListener true
} }
return@setOnPreferenceClickListener true
} }
localePreference.setOnPreferenceClickListener { pref -> private fun getCurrentLocale(): String {
val tempLangs = languages.toMutableList() val res = context!!.resources
if (beneneCount > 100) { // Change locale settings in the app.
tempLangs.add(Triple("\uD83E\uDD8D", "mmmm... monke", "mo")) // val dm = res.displayMetrics
} val conf = res.configuration
val current = getCurrentLocale() return conf?.locale?.language ?: "en"
val languageCodes = tempLangs.map { it.third } }
val languageNames = tempLangs.map { "${it.first} ${it.second}" }
val index = languageCodes.indexOf(current) override fun onPreferenceTreeClick(preference: Preference?): Boolean {
pref?.context?.showDialog( if (preference != null) {
languageNames, index, getString(R.string.app_language), true, { } if (preference.key == getString(R.string.subtitle_settings_key)) {
) { languageIndex -> SubtitlesFragment.push(activity, false)
try {
val code = languageCodes[languageIndex]
setLocale(activity, code)
val settingsManager = PreferenceManager.getDefaultSharedPreferences(pref.context)
settingsManager.edit().putString(getString(R.string.locale_key), code).apply()
activity?.recreate()
} catch (e: Exception) {
logError(e)
} }
} }
return@setOnPreferenceClickListener true return super.onPreferenceTreeClick(preference)
} }
} }
private fun getCurrentLocale(): String {
val res = context!!.resources
// Change locale settings in the app.
// val dm = res.displayMetrics
val conf = res.configuration
return conf?.locale?.language ?: "en"
}
override fun onPreferenceTreeClick(preference: Preference?): Boolean {
if (preference != null) {
if (preference.key == getString(R.string.subtitle_settings_key)) {
SubtitlesFragment.push(activity, false)
}
}
return super.onPreferenceTreeClick(preference)
}
}