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

View file

@ -5,19 +5,15 @@ import android.os.Bundle
import android.view.View
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.PreferenceManager
import com.lagradost.cloudstream3.APIHolder
import com.lagradost.cloudstream3.*
import com.lagradost.cloudstream3.APIHolder.getApiDubstatusSettings
import com.lagradost.cloudstream3.APIHolder.getApiProviderLangSettings
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.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.USER_SELECTED_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
@ -100,25 +96,26 @@ class SettingsLang : PreferenceFragmentCompat() {
}
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 names = enumValues<TvType>().sorted().map { it.name }
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 =
settingsManager.getInt(getString(R.string.prefer_media_type_key), 0)
activity?.showBottomDialog(
prefNames.toList(),
prefValues.indexOf(currentPrefMedia),
activity?.showMultiDialog(
names,
currentList,
getString(R.string.preferred_media_settings),
true,
{}) {
settingsManager.edit()
.putInt(getString(R.string.prefer_media_type_key), prefValues[it])
.apply()
{}) { selectedList ->
settingsManager.edit().putStringSet(
this.getString(R.string.prefer_media_type_key),
selectedList.map { it.toString() }.toMutableSet()
).apply()
removeKey(USER_SELECTED_HOMEPAGE_API)
// (context ?: AcraApplication.context)?.let { ctx -> app.initClient(ctx) }
//(context ?: AcraApplication.context)?.let { ctx -> app.initClient(ctx) }
}
return@setOnPreferenceClickListener true
}

View file

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

View file

@ -29,19 +29,6 @@
<item>4</item>
</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">
<item>@string/resolution_and_title</item>
<item>@string/title</item>