[Feature] Refactor Preferred Media to allow multi-selection and support for all TvType.

This commit is contained in:
Jace 2022-08-20 08:36:24 +08:00 committed by Cloudburst
parent 1e56468bd9
commit a49f4668e4
4 changed files with 64 additions and 75 deletions

View file

@ -227,26 +227,19 @@ object APIHolder {
} }
fun Context.filterProviderByPreferredMedia(hasHomePageIsRequired: Boolean = true): List<MainAPI> { fun Context.filterProviderByPreferredMedia(hasHomePageIsRequired: Boolean = true): List<MainAPI> {
val settingsManager = PreferenceManager.getDefaultSharedPreferences(this) val default = enumValues<TvType>().sorted().filter { it != TvType.NSFW }.map { it.ordinal }
val currentPrefMedia = val defaultSet = default.map { it.toString() }.toSet()
settingsManager.getInt(this.getString(R.string.prefer_media_type_key), 0) val currentPrefMedia = PreferenceManager.getDefaultSharedPreferences(this)
.getStringSet(this.getString(R.string.prefer_media_type_key), defaultSet)
?.mapNotNull { it.toIntOrNull() ?: return@mapNotNull null }?: default
val langs = this.getApiProviderLangSettings() val langs = this.getApiProviderLangSettings()
val allApis = apis.filter { langs.contains(it.lang) } val allApis = apis.filter { langs.contains(it.lang) }
.filter { api -> api.hasMainPage || !hasHomePageIsRequired } .filter { api -> api.hasMainPage || !hasHomePageIsRequired }
return if (currentPrefMedia < 1) { return if (currentPrefMedia.isEmpty()) {
allApis allApis
} else { } else {
// Filter API depending on preferred media type // Filter API depending on preferred media type
val listEnumAnime = listOf(TvType.Anime, TvType.AnimeMovie, TvType.OVA) allApis.filter { api -> api.supportedTypes.any { currentPrefMedia.contains(it.ordinal) } }
val listEnumMovieTv =
listOf(TvType.Movie, TvType.TvSeries, TvType.Cartoon, TvType.AsianDrama)
val listEnumDoc = listOf(TvType.Documentary)
val mediaTypeList = when (currentPrefMedia) {
2 -> listEnumAnime
3 -> listEnumDoc
else -> listEnumMovieTv
}
allApis.filter { api -> api.supportedTypes.any { it in mediaTypeList } }
} }
} }
@ -586,19 +579,19 @@ enum class DubStatus(val id: Int) {
Subbed(0), Subbed(0),
} }
enum class TvType { enum class TvType(value: Int?) {
Movie, Movie(1),
AnimeMovie, AnimeMovie(2),
TvSeries, TvSeries(3),
Cartoon, Cartoon(4),
Anime, Anime(5),
OVA, OVA(6),
Torrent, Torrent(7),
Documentary, Documentary(8),
AsianDrama, AsianDrama(9),
Live, Live(10),
NSFW, NSFW(11),
Others Others(12)
} }
// IN CASE OF FUTURE ANIME MOVIE OR SMTH // IN CASE OF FUTURE ANIME MOVIE OR SMTH

View file

@ -5,19 +5,15 @@ import android.os.Bundle
import android.view.View import android.view.View
import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceFragmentCompat
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import com.lagradost.cloudstream3.APIHolder import com.lagradost.cloudstream3.*
import com.lagradost.cloudstream3.APIHolder.getApiDubstatusSettings import com.lagradost.cloudstream3.APIHolder.getApiDubstatusSettings
import com.lagradost.cloudstream3.APIHolder.getApiProviderLangSettings import com.lagradost.cloudstream3.APIHolder.getApiProviderLangSettings
import com.lagradost.cloudstream3.AcraApplication.Companion.removeKey import com.lagradost.cloudstream3.AcraApplication.Companion.removeKey
import com.lagradost.cloudstream3.CommonActivity
import com.lagradost.cloudstream3.DubStatus
import com.lagradost.cloudstream3.R
import com.lagradost.cloudstream3.mvvm.logError import com.lagradost.cloudstream3.mvvm.logError
import com.lagradost.cloudstream3.ui.APIRepository import com.lagradost.cloudstream3.ui.APIRepository
import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.getPref import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.getPref
import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.setUpToolbar import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.setUpToolbar
import com.lagradost.cloudstream3.utils.USER_SELECTED_HOMEPAGE_API import com.lagradost.cloudstream3.utils.USER_SELECTED_HOMEPAGE_API
import com.lagradost.cloudstream3.utils.SingleSelectionHelper.showBottomDialog
import com.lagradost.cloudstream3.utils.SingleSelectionHelper.showDialog import com.lagradost.cloudstream3.utils.SingleSelectionHelper.showDialog
import com.lagradost.cloudstream3.utils.SingleSelectionHelper.showMultiDialog import com.lagradost.cloudstream3.utils.SingleSelectionHelper.showMultiDialog
import com.lagradost.cloudstream3.utils.SubtitleHelper import com.lagradost.cloudstream3.utils.SubtitleHelper
@ -100,25 +96,26 @@ class SettingsLang : PreferenceFragmentCompat() {
} }
getPref(R.string.prefer_media_type_key)?.setOnPreferenceClickListener { getPref(R.string.prefer_media_type_key)?.setOnPreferenceClickListener {
val prefNames = resources.getStringArray(R.array.media_type_pref) val names = enumValues<TvType>().sorted().map { it.name }
val prefValues = resources.getIntArray(R.array.media_type_pref_values) val default = enumValues<TvType>().sorted().filter { it != TvType.NSFW }.map { it.ordinal }
val defaultSet = default.map { it.toString() }.toSet()
val currentList = settingsManager.getStringSet(getString(R.string.prefer_media_type_key), defaultSet)?.map {
it.toInt()
} ?: default
val currentPrefMedia = activity?.showMultiDialog(
settingsManager.getInt(getString(R.string.prefer_media_type_key), 0) names,
currentList,
activity?.showBottomDialog(
prefNames.toList(),
prefValues.indexOf(currentPrefMedia),
getString(R.string.preferred_media_settings), getString(R.string.preferred_media_settings),
true, {}) { selectedList ->
{}) { settingsManager.edit().putStringSet(
settingsManager.edit() this.getString(R.string.prefer_media_type_key),
.putInt(getString(R.string.prefer_media_type_key), prefValues[it]) selectedList.map { it.toString() }.toMutableSet()
.apply() ).apply()
removeKey(USER_SELECTED_HOMEPAGE_API) removeKey(USER_SELECTED_HOMEPAGE_API)
// (context ?: AcraApplication.context)?.let { ctx -> app.initClient(ctx) } //(context ?: AcraApplication.context)?.let { ctx -> app.initClient(ctx) }
} }
return@setOnPreferenceClickListener true return@setOnPreferenceClickListener true
} }

View file

@ -6,10 +6,12 @@ import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.AbsListView import android.widget.AbsListView
import android.widget.ArrayAdapter import android.widget.ArrayAdapter
import androidx.core.util.forEach
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController import androidx.navigation.fragment.findNavController
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import com.lagradost.cloudstream3.R import com.lagradost.cloudstream3.R
import com.lagradost.cloudstream3.TvType
import com.lagradost.cloudstream3.utils.DataStore.removeKey import com.lagradost.cloudstream3.utils.DataStore.removeKey
import com.lagradost.cloudstream3.utils.USER_SELECTED_HOMEPAGE_API import com.lagradost.cloudstream3.utils.USER_SELECTED_HOMEPAGE_API
import com.lagradost.cloudstream3.utils.UIHelper.fixPaddingStatusbar import com.lagradost.cloudstream3.utils.UIHelper.fixPaddingStatusbar
@ -35,24 +37,34 @@ class SetupFragmentMedia : Fragment() {
val arrayAdapter = val arrayAdapter =
ArrayAdapter<String>(this, R.layout.sort_bottom_single_choice) ArrayAdapter<String>(this, R.layout.sort_bottom_single_choice)
val currentPrefMedia = val names = enumValues<TvType>().sorted().map { it.name }
settingsManager.getInt(getString(R.string.prefer_media_type_key), 0) val selected = mutableListOf<Int>()
val prefNames = resources.getStringArray(R.array.media_type_pref) arrayAdapter.addAll(names)
val prefValues = resources.getIntArray(R.array.media_type_pref_values) listview1?.let {
it.adapter = arrayAdapter
it.choiceMode = AbsListView.CHOICE_MODE_MULTIPLE
arrayAdapter.addAll(prefNames.toList()) it.setOnItemClickListener { _, _, _, _ ->
listview1?.adapter = arrayAdapter it.checkedItemPositions?.forEach { key, value ->
listview1?.choiceMode = AbsListView.CHOICE_MODE_SINGLE if (value) {
listview1?.setItemChecked(currentPrefMedia, true) selected.add(key)
} else {
selected.remove(key)
}
}
val prefValues = selected.mapNotNull { pos ->
val item = it.getItemAtPosition(pos)?.toString() ?: return@mapNotNull null
val itemVal = TvType.valueOf(item)
itemVal.ordinal.toString()
}.toSet()
settingsManager.edit()
.putStringSet(getString(R.string.prefer_media_type_key), prefValues)
.apply()
listview1?.setOnItemClickListener { _, _, position, _ -> // Regenerate set homepage
settingsManager.edit() removeKey(USER_SELECTED_HOMEPAGE_API)
.putInt(getString(R.string.prefer_media_type_key), prefValues[position]) }
.apply()
// Regenerate set homepage
removeKey(USER_SELECTED_HOMEPAGE_API)
} }
next_btt?.setOnClickListener { next_btt?.setOnClickListener {

View file

@ -29,19 +29,6 @@
<item>4</item> <item>4</item>
</array> </array>
<array name="media_type_pref">
<item>All</item>
<item>Movies and TV</item>
<item>Anime</item>
<item>Documentary</item>
</array>
<array name="media_type_pref_values">
<item>0</item>
<item>1</item>
<item>2</item>
<item>3</item>
</array>
<array name="limit_title_rez_pref_names"> <array name="limit_title_rez_pref_names">
<item>@string/resolution_and_title</item> <item>@string/resolution_and_title</item>
<item>@string/title</item> <item>@string/title</item>