183 lines
7.4 KiB
Kotlin
183 lines
7.4 KiB
Kotlin
package com.lagradost.cloudstream3.ui.settings
|
|
|
|
import android.os.Build
|
|
import android.os.Bundle
|
|
import android.view.View
|
|
import androidx.preference.PreferenceFragmentCompat
|
|
import androidx.preference.PreferenceManager
|
|
import com.lagradost.cloudstream3.R
|
|
import com.lagradost.cloudstream3.SearchQuality
|
|
import com.lagradost.cloudstream3.mvvm.logError
|
|
import com.lagradost.cloudstream3.ui.search.SearchResultBuilder
|
|
import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.getPref
|
|
import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.setPaddingBottom
|
|
import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.setUpToolbar
|
|
import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.updateTv
|
|
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.UIHelper.hideKeyboard
|
|
|
|
class SettingsUI : PreferenceFragmentCompat() {
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
super.onViewCreated(view, savedInstanceState)
|
|
setUpToolbar(R.string.category_ui)
|
|
setPaddingBottom()
|
|
}
|
|
|
|
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
|
hideKeyboard()
|
|
setPreferencesFromResource(R.xml.settins_ui, rootKey)
|
|
val settingsManager = PreferenceManager.getDefaultSharedPreferences(requireContext())
|
|
|
|
getPref(R.string.poster_ui_key)?.setOnPreferenceClickListener {
|
|
val prefNames = resources.getStringArray(R.array.poster_ui_options)
|
|
val keys = resources.getStringArray(R.array.poster_ui_options_values)
|
|
val prefValues = keys.map {
|
|
settingsManager.getBoolean(it, true)
|
|
}.mapIndexedNotNull { index, b ->
|
|
if (b) {
|
|
index
|
|
} else null
|
|
}
|
|
|
|
activity?.showMultiDialog(
|
|
prefNames.toList(),
|
|
prefValues,
|
|
getString(R.string.poster_ui_settings),
|
|
{}) { list ->
|
|
val edit = settingsManager.edit()
|
|
for ((i, key) in keys.withIndex()) {
|
|
edit.putBoolean(key, list.contains(i))
|
|
}
|
|
edit.apply()
|
|
SearchResultBuilder.updateCache(it.context)
|
|
}
|
|
|
|
return@setOnPreferenceClickListener true
|
|
}
|
|
|
|
getPref(R.string.app_layout_key)?.setOnPreferenceClickListener {
|
|
val prefNames = resources.getStringArray(R.array.app_layout)
|
|
val prefValues = resources.getIntArray(R.array.app_layout_values)
|
|
|
|
val currentLayout =
|
|
settingsManager.getInt(getString(R.string.app_layout_key), -1)
|
|
|
|
activity?.showBottomDialog(
|
|
prefNames.toList(),
|
|
prefValues.indexOf(currentLayout),
|
|
getString(R.string.app_layout),
|
|
true,
|
|
{}) {
|
|
try {
|
|
settingsManager.edit()
|
|
.putInt(getString(R.string.app_layout_key), prefValues[it])
|
|
.apply()
|
|
context?.updateTv()
|
|
activity?.recreate()
|
|
} catch (e: Exception) {
|
|
logError(e)
|
|
}
|
|
}
|
|
return@setOnPreferenceClickListener true
|
|
}
|
|
|
|
getPref(R.string.app_theme_key)?.setOnPreferenceClickListener {
|
|
val prefNames = resources.getStringArray(R.array.themes_names).toMutableList()
|
|
val prefValues = resources.getStringArray(R.array.themes_names_values).toMutableList()
|
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) { // remove monet on android 11 and less
|
|
val toRemove = prefValues
|
|
.mapIndexed { idx, s -> if (s.startsWith("Monet")) idx else null }
|
|
.filterNotNull()
|
|
var offset = 0
|
|
toRemove.forEach { idx ->
|
|
prefNames.removeAt(idx - offset)
|
|
prefValues.removeAt(idx - offset)
|
|
offset += 1
|
|
}
|
|
}
|
|
|
|
val currentLayout =
|
|
settingsManager.getString(getString(R.string.app_theme_key), prefValues.first())
|
|
|
|
activity?.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
|
|
}
|
|
getPref(R.string.primary_color_key)?.setOnPreferenceClickListener {
|
|
val prefNames = resources.getStringArray(R.array.themes_overlay_names).toMutableList()
|
|
val prefValues = resources.getStringArray(R.array.themes_overlay_names_values).toMutableList()
|
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) { // remove monet on android 11 and less
|
|
val toRemove = prefValues
|
|
.mapIndexed { idx, s -> if (s.startsWith("Monet")) idx else null }
|
|
.filterNotNull()
|
|
var offset = 0
|
|
toRemove.forEach { idx ->
|
|
prefNames.removeAt(idx - offset)
|
|
prefValues.removeAt(idx - offset)
|
|
offset += 1
|
|
}
|
|
}
|
|
|
|
val currentLayout =
|
|
settingsManager.getString(getString(R.string.primary_color_key), prefValues.first())
|
|
|
|
activity?.showDialog(
|
|
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
|
|
}
|
|
|
|
getPref(R.string.pref_filter_search_quality_key)?.setOnPreferenceClickListener {
|
|
val names = enumValues<SearchQuality>().sorted().map { it.name }
|
|
val currentList = settingsManager.getStringSet(
|
|
getString(R.string.pref_filter_search_quality_key),
|
|
setOf()
|
|
)?.map {
|
|
it.toInt()
|
|
} ?: listOf()
|
|
|
|
activity?.showMultiDialog(
|
|
names,
|
|
currentList,
|
|
getString(R.string.pref_filter_search_quality),
|
|
{}) { selectedList ->
|
|
settingsManager.edit().putStringSet(
|
|
this.getString(R.string.pref_filter_search_quality_key),
|
|
selectedList.map { it.toString() }.toMutableSet()
|
|
).apply()
|
|
}
|
|
|
|
return@setOnPreferenceClickListener true
|
|
}
|
|
|
|
}
|
|
} |