[Additional feature] Also apply quality filter to HomePage list

This commit is contained in:
Jace 2022-08-15 15:57:09 +08:00 committed by Cloudburst
parent dce6f06368
commit ed9ae3d0e0
2 changed files with 39 additions and 14 deletions

View file

@ -256,19 +256,41 @@ object APIHolder {
fun Context.filterSearchResultByFilmQuality(data: List<SearchResponse>): List<SearchResponse> { fun Context.filterSearchResultByFilmQuality(data: List<SearchResponse>): List<SearchResponse> {
// Filter results omitting entries with certain quality // Filter results omitting entries with certain quality
if (data.isEmpty()) { if (data.isNotEmpty()) {
return data val filteredSearchQuality = PreferenceManager.getDefaultSharedPreferences(this)
?.getStringSet(getString(R.string.pref_filter_search_quality_key), setOf())
?.mapNotNull { entry ->
entry.toIntOrNull() ?: return@mapNotNull null
} ?: listOf()
if (filteredSearchQuality.isNotEmpty()) {
return data.filter { item ->
val searchQualVal = item.quality?.ordinal ?: -1
//Log.i("filterSearch", "QuickSearch item => ${item.toJson()}")
!filteredSearchQuality.contains(searchQualVal)
}
}
} }
val filteredSearchQuality = PreferenceManager.getDefaultSharedPreferences(this) return data
?.getStringSet(getString(R.string.pref_filter_search_quality_key), setOf()) }
?.mapNotNull { entry ->
entry.toIntOrNull() ?: return@mapNotNull null fun Context.filterHomePageListByFilmQuality(data: HomePageList): HomePageList {
} ?: listOf() // Filter results omitting entries with certain quality
if (filteredSearchQuality.isNotEmpty()) { if (data.list.isNotEmpty()) {
return data.filter { item -> val filteredSearchQuality = PreferenceManager.getDefaultSharedPreferences(this)
val searchQualVal = item.quality?.ordinal ?: -1 ?.getStringSet(getString(R.string.pref_filter_search_quality_key), setOf())
//Log.i("filterSearch", "QuickSearch item => ${item.toJson()}") ?.mapNotNull { entry ->
!filteredSearchQuality.contains(searchQualVal) entry.toIntOrNull() ?: return@mapNotNull null
} ?: listOf()
if (filteredSearchQuality.isNotEmpty()) {
return HomePageList(
name = data.name,
isHorizontalImages = data.isHorizontalImages,
list = data.list.filter { item ->
val searchQualVal = item.quality?.ordinal ?: -1
//Log.i("filterSearch", "QuickSearch item => ${item.toJson()}")
!filteredSearchQuality.contains(searchQualVal)
}
)
} }
} }
return data return data

View file

@ -5,7 +5,9 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import com.lagradost.cloudstream3.APIHolder.apis import com.lagradost.cloudstream3.APIHolder.apis
import com.lagradost.cloudstream3.APIHolder.filterHomePageListByFilmQuality
import com.lagradost.cloudstream3.APIHolder.filterProviderByPreferredMedia import com.lagradost.cloudstream3.APIHolder.filterProviderByPreferredMedia
import com.lagradost.cloudstream3.APIHolder.filterSearchResultByFilmQuality
import com.lagradost.cloudstream3.APIHolder.getApiFromNameNull import com.lagradost.cloudstream3.APIHolder.getApiFromNameNull
import com.lagradost.cloudstream3.AcraApplication.Companion.context import com.lagradost.cloudstream3.AcraApplication.Companion.context
import com.lagradost.cloudstream3.AcraApplication.Companion.getKey import com.lagradost.cloudstream3.AcraApplication.Companion.getKey
@ -225,8 +227,9 @@ class HomeViewModel : ViewModel() {
expandable.clear() expandable.clear()
data.value.forEach { home -> data.value.forEach { home ->
home?.items?.forEach { list -> home?.items?.forEach { list ->
val filteredList = context?.filterHomePageListByFilmQuality(list) ?: list
expandable[list.name] = expandable[list.name] =
ExpandableHomepageList(list, 1, home.hasNext) ExpandableHomepageList(filteredList, 1, home.hasNext)
} }
} }
_page.postValue(Resource.Success(expandable)) _page.postValue(Resource.Success(expandable))
@ -241,7 +244,7 @@ class HomeViewModel : ViewModel() {
.toList() .toList()
if (currentList.isNotEmpty()) { if (currentList.isNotEmpty()) {
val randomItems = currentList.shuffled() val randomItems = context?.filterSearchResultByFilmQuality(currentList.shuffled()) ?: currentList.shuffled()
_randomItems.postValue(randomItems) _randomItems.postValue(randomItems)
} }