cloudstream/app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsLang.kt

185 lines
7.6 KiB
Kotlin

package com.lagradost.cloudstream3.ui.settings
import android.os.Bundle
import android.view.View
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.PreferenceManager
import com.lagradost.cloudstream3.*
import com.lagradost.cloudstream3.APIHolder.getApiDubstatusSettings
import com.lagradost.cloudstream3.APIHolder.getApiProviderLangSettings
import com.lagradost.cloudstream3.mvvm.logError
import com.lagradost.cloudstream3.network.initClient
import com.lagradost.cloudstream3.ui.APIRepository
import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.getPref
import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.setUpToolbar
import com.lagradost.cloudstream3.utils.HOMEPAGE_API
import com.lagradost.cloudstream3.utils.SingleSelectionHelper.showBottomDialog
import com.lagradost.cloudstream3.utils.SingleSelectionHelper.showDialog
import com.lagradost.cloudstream3.utils.SingleSelectionHelper.showMultiDialog
import com.lagradost.cloudstream3.utils.SubtitleHelper
import com.lagradost.cloudstream3.utils.UIHelper.hideKeyboard
class SettingsLang : PreferenceFragmentCompat() {
// idk, if you find a way of automating this it would be great
// https://www.iemoji.com/view/emoji/1794/flags/antarctica
// Emoji Character Encoding Data --> C/C++/Java Src
// https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes leave blank for auto
private val languages = arrayListOf(
Triple("", "Spanish", "es"),
Triple("", "English", "en"),
Triple("", "Viet Nam", "vi"),
Triple("", "Dutch", "nl"),
Triple("", "French", "fr"),
Triple("", "Greek", "el"),
Triple("", "Swedish", "sv"),
Triple("", "Tagalog", "tl"),
Triple("", "Polish", "pl"),
Triple("", "Hindi", "hi"),
Triple("", "Malayalam", "ml"),
Triple("", "Norsk", "no"),
Triple("", "German", "de"),
Triple("", "Arabic", "ar"),
Triple("", "Turkish", "tr"),
Triple("", "Macedonian", "mk"),
Triple("\uD83C\uDDE7\uD83C\uDDF7", "Portuguese (Brazil)", "pt"),
Triple("", "Romanian", "ro"),
Triple("", "Italian", "it"),
Triple("", "Chinese", "zh"),
Triple("", "Indonesian", "id"),
Triple("", "Czech", "cs"),
).sortedBy { it.second } //ye, we go alphabetical, so ppl don't put their lang on top
private fun getCurrentLocale(): String {
val res = requireContext().resources
// Change locale settings in the app.
// val dm = res.displayMetrics
val conf = res.configuration
return conf?.locale?.language ?: "en"
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setUpToolbar(R.string.category_preferred_media_and_lang)
}
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
hideKeyboard()
setPreferencesFromResource(R.xml.settings_media_lang, rootKey)
val settingsManager = PreferenceManager.getDefaultSharedPreferences(requireContext())
getPref(R.string.display_sub_key)?.setOnPreferenceClickListener {
activity?.getApiDubstatusSettings()?.let { current ->
val dublist = DubStatus.values()
val names = dublist.map { it.name }
val currentList = ArrayList<Int>()
for (i in current) {
currentList.add(dublist.indexOf(i))
}
activity?.showMultiDialog(
names,
currentList,
getString(R.string.display_subbed_dubbed_settings),
{}) { selectedList ->
APIRepository.dubStatusActive = selectedList.map { dublist[it] }.toHashSet()
settingsManager.edit().putStringSet(
this.getString(R.string.display_sub_key),
selectedList.map { names[it] }.toMutableSet()
).apply()
}
}
return@setOnPreferenceClickListener true
}
getPref(R.string.prefer_media_type_key)?.setOnPreferenceClickListener {
val prefNames = resources.getStringArray(R.array.media_type_pref)
val prefValues = resources.getIntArray(R.array.media_type_pref_values)
val currentPrefMedia =
settingsManager.getInt(getString(R.string.prefer_media_type_key), 0)
activity?.showBottomDialog(
prefNames.toList(),
prefValues.indexOf(currentPrefMedia),
getString(R.string.preferred_media_settings),
true,
{}) {
settingsManager.edit()
.putInt(getString(R.string.prefer_media_type_key), prefValues[it])
.apply()
AcraApplication.removeKey(HOMEPAGE_API)
(context ?: AcraApplication.context)?.let { ctx -> app.initClient(ctx) }
}
return@setOnPreferenceClickListener true
}
getPref(R.string.locale_key)?.setOnPreferenceClickListener {
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 { (emoji, name, iso) ->
val flag = emoji.ifBlank { SubtitleHelper.getFlagFromIso(iso) ?: "ERROR" }
"$flag $name"
}
val index = languageCodes.indexOf(current)
activity?.showDialog(
languageNames, index, getString(R.string.app_language), true, { }
) { languageIndex ->
try {
val code = languageCodes[languageIndex]
CommonActivity.setLocale(activity, code)
settingsManager.edit().putString(getString(R.string.locale_key), code).apply()
activity?.recreate()
} catch (e: Exception) {
logError(e)
}
}
return@setOnPreferenceClickListener true
}
getPref(R.string.provider_lang_key)?.setOnPreferenceClickListener {
activity?.getApiProviderLangSettings()?.let { current ->
val allLangs = HashSet<String>()
for (api in APIHolder.apis) {
allLangs.add(api.lang)
}
val currentList = ArrayList<Int>()
for (i in current) {
currentList.add(allLangs.indexOf(i))
}
val names = allLangs.map {
val emoji = SubtitleHelper.getFlagFromIso(it)
val name = SubtitleHelper.fromTwoLettersToLanguage(it)
val fullName = "$emoji $name"
Pair(it, fullName)
}
activity?.showMultiDialog(
names.map { it.second },
currentList,
getString(R.string.provider_lang_settings),
{}) { selectedList ->
settingsManager.edit().putStringSet(
this.getString(R.string.provider_lang_key),
selectedList.map { names[it].first }.toMutableSet()
).apply()
//APIRepository.providersActive = it.context.getApiSettings()
}
}
return@setOnPreferenceClickListener true
}
}
}