Add new setting 'Preferred Media' to filter out media types (#203)

* Add new setting 'Preferred Media' to filter out media types. Closes #200

* Apply media filter to 'Random'

Co-authored-by: Osten <balt.758@gmail.com>
This commit is contained in:
Jace 2021-11-01 01:51:39 +08:00 committed by GitHub
parent bd8350f255
commit 0ebfe71759
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 72 additions and 5 deletions

View file

@ -14,6 +14,7 @@ import android.widget.TextView
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
import androidx.preference.PreferenceManager
import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.LinearSnapHelper import androidx.recyclerview.widget.LinearSnapHelper
@ -166,11 +167,24 @@ class HomeFragment : Fragment() {
} }
private val apiChangeClickListener = View.OnClickListener { view -> private val apiChangeClickListener = View.OnClickListener { view ->
val validAPIs = apis.filter { api -> api.hasMainPage }.toMutableList() val allApis = apis.filter { api -> api.hasMainPage }.toMutableList()
var validAPIs = allApis
val settingsManager = PreferenceManager.getDefaultSharedPreferences(context)
val currentPrefMedia = settingsManager.getInt(getString(R.string.preferred_media_settings), 0)
// Filter API depending on preferred media type
if (currentPrefMedia > 0) {
val listEnumAnime = listOf(TvType.Anime, TvType.AnimeMovie, TvType.ONA)
val listEnumMovieTv = listOf(TvType.Movie, TvType.TvSeries, TvType.Cartoon)
val mediaTypeList = if (currentPrefMedia==1) listEnumMovieTv else listEnumAnime
validAPIs = allApis.filter { api -> api.supportedTypes.any { it in mediaTypeList } }.toMutableList()
}
validAPIs.add(0, randomApi) validAPIs.add(0, randomApi)
validAPIs.add(0, noneApi) validAPIs.add(0, noneApi)
view.popupMenuNoIconsAndNoStringRes(validAPIs.mapIndexed { index, api -> Pair(index, api.name) }) { view.popupMenuNoIconsAndNoStringRes(validAPIs.mapIndexed { index, api -> Pair(index, api.name) }) {
homeViewModel.loadAndCancel(validAPIs[itemId].name) homeViewModel.loadAndCancel(validAPIs[itemId].name, currentPrefMedia)
} }
} }
@ -411,7 +425,7 @@ class HomeFragment : Fragment() {
val apiName = context?.getKey<String>(HOMEPAGE_API) val apiName = context?.getKey<String>(HOMEPAGE_API)
if (homeViewModel.apiName.value != apiName || apiName == null) { if (homeViewModel.apiName.value != apiName || apiName == null) {
//println("Caught home: " + homeViewModel.apiName.value + " at " + apiName) //println("Caught home: " + homeViewModel.apiName.value + " at " + apiName)
homeViewModel.loadAndCancel(apiName) homeViewModel.loadAndCancel(apiName, 0)
} }
} }
} }

View file

@ -10,6 +10,7 @@ import com.lagradost.cloudstream3.APIHolder.getApiFromNameNull
import com.lagradost.cloudstream3.HomePageResponse import com.lagradost.cloudstream3.HomePageResponse
import com.lagradost.cloudstream3.MainAPI import com.lagradost.cloudstream3.MainAPI
import com.lagradost.cloudstream3.SearchResponse import com.lagradost.cloudstream3.SearchResponse
import com.lagradost.cloudstream3.TvType
import com.lagradost.cloudstream3.mvvm.Resource import com.lagradost.cloudstream3.mvvm.Resource
import com.lagradost.cloudstream3.ui.APIRepository import com.lagradost.cloudstream3.ui.APIRepository
import com.lagradost.cloudstream3.ui.APIRepository.Companion.noneApi import com.lagradost.cloudstream3.ui.APIRepository.Companion.noneApi
@ -148,12 +149,21 @@ class HomeViewModel : ViewModel() {
} }
} }
fun loadAndCancel(preferredApiName: String?) = viewModelScope.launch { fun loadAndCancel(preferredApiName: String?, currentPrefMedia: Int) = viewModelScope.launch {
val api = getApiFromNameNull(preferredApiName) val api = getApiFromNameNull(preferredApiName)
if (preferredApiName == noneApi.name) if (preferredApiName == noneApi.name)
loadAndCancel(noneApi) loadAndCancel(noneApi)
else if(preferredApiName == randomApi.name || api == null) { else if(preferredApiName == randomApi.name || api == null) {
loadAndCancel(apis.filter { it.hasMainPage }.random()) val allApis = apis.filter { api -> api.hasMainPage }.toMutableList()
var validAPIs = allApis
if (currentPrefMedia > 0) {
val listEnumAnime = listOf(TvType.Anime, TvType.AnimeMovie, TvType.ONA)
val listEnumMovieTv = listOf(TvType.Movie, TvType.TvSeries, TvType.Cartoon)
val mediaTypeList = if (currentPrefMedia==1) listEnumMovieTv else listEnumAnime
validAPIs = allApis.filter { api -> api.supportedTypes.any { it in mediaTypeList } }.toMutableList()
}
loadAndCancel(validAPIs.random())
} else { } else {
loadAndCancel(api) loadAndCancel(api)
} }

View file

@ -82,6 +82,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
val providerLangPreference = findPreference<Preference>(getString(R.string.provider_lang_key))!! val providerLangPreference = findPreference<Preference>(getString(R.string.provider_lang_key))!!
val allLayoutPreference = findPreference<Preference>(getString(R.string.app_layout_key))!! val allLayoutPreference = findPreference<Preference>(getString(R.string.app_layout_key))!!
val colorPrimaryPreference = findPreference<Preference>(getString(R.string.primary_color_key))!! val colorPrimaryPreference = findPreference<Preference>(getString(R.string.primary_color_key))!!
val preferedMediaTypePreference = findPreference<Preference>(getString(R.string.prefer_media_type_key))!!
val appThemePreference = findPreference<Preference>(getString(R.string.app_theme_key))!! val appThemePreference = findPreference<Preference>(getString(R.string.app_theme_key))!!
legalPreference.setOnPreferenceClickListener { legalPreference.setOnPreferenceClickListener {
@ -163,6 +164,30 @@ class SettingsFragment : PreferenceFragmentCompat() {
return@setOnPreferenceClickListener true return@setOnPreferenceClickListener true
} }
if (preferedMediaTypePreference != null) {
preferedMediaTypePreference.setOnPreferenceClickListener {
val prefNames = resources.getStringArray(R.array.media_type_pref)
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 { allLayoutPreference.setOnPreferenceClickListener {
val prefNames = resources.getStringArray(R.array.app_layout) val prefNames = resources.getStringArray(R.array.app_layout)
val prefValues = resources.getIntArray(R.array.app_layout_values) val prefValues = resources.getIntArray(R.array.app_layout_values)

View file

@ -46,6 +46,17 @@
<item>4</item> <item>4</item>
</array> </array>
<array name="media_type_pref">
<item>All</item>
<item>Movies and TV</item>
<item>Anime</item>
</array>
<array name="media_type_pref_values">
<item>0</item>
<item>1</item>
<item>2</item>
</array>
<array name="episode_long_click_options"> <array name="episode_long_click_options">
<item>@string/episode_action_chomecast_episode</item> <item>@string/episode_action_chomecast_episode</item>
<item>@string/episode_action_chomecast_mirror</item> <item>@string/episode_action_chomecast_mirror</item>

View file

@ -26,6 +26,7 @@
<string name="dns_key" translatable="false">dns_key</string> <string name="dns_key" translatable="false">dns_key</string>
<string name="app_layout_key" translatable="false">app_layout_key</string> <string name="app_layout_key" translatable="false">app_layout_key</string>
<string name="primary_color_key" translatable="false">primary_color_key</string> <string name="primary_color_key" translatable="false">primary_color_key</string>
<string name="prefer_media_type_key" translatable="false">prefer_media_type_key</string>
<string name="app_theme_key" translatable="false">app_theme_key</string> <string name="app_theme_key" translatable="false">app_theme_key</string>
<!-- FORMAT MIGHT TRANSLATE, WILL CAUSE CRASH IF APPLIED WRONG --> <!-- FORMAT MIGHT TRANSLATE, WILL CAUSE CRASH IF APPLIED WRONG -->
@ -297,6 +298,7 @@
<string name="general">General</string> <string name="general">General</string>
<string name="provider_lang_settings">Provider Languages</string> <string name="provider_lang_settings">Provider Languages</string>
<string name="app_layout">App Layout</string> <string name="app_layout">App Layout</string>
<string name="preferred_media_settings">Preferred Media</string>
<string name="automatic">Auto</string> <string name="automatic">Auto</string>
<string name="tv_layout">Tv Layout</string> <string name="tv_layout">Tv Layout</string>

View file

@ -83,6 +83,11 @@
android:title="@string/app_language" android:title="@string/app_language"
android:icon="@drawable/ic_baseline_language_24"> android:icon="@drawable/ic_baseline_language_24">
</Preference> </Preference>
<Preference
android:key="@string/prefer_media_type_key"
android:title="@string/preferred_media_settings"
android:icon="@drawable/ic_baseline_play_arrow_24">
</Preference>
<Preference <Preference
android:key="@string/display_sub_key" android:key="@string/display_sub_key"
android:title="@string/display_subbed_dubbed_settings" android:title="@string/display_subbed_dubbed_settings"