Make searching always respect your preferred media setting

This commit is contained in:
Blatzar 2022-10-28 22:29:32 +02:00
parent fd7cf51f57
commit 65b5efb848

View file

@ -117,18 +117,36 @@ class SearchFragment : Fragment() {
var selectedSearchTypes = mutableListOf<TvType>() var selectedSearchTypes = mutableListOf<TvType>()
var selectedApis = mutableSetOf<String>() var selectedApis = mutableSetOf<String>()
/**
* Will filter all providers by preferred media and selectedSearchTypes.
* If that results in no available providers then only filter
* providers by preferred media
**/
fun search(query: String?) { fun search(query: String?) {
if (query == null) return if (query == null) return
context?.getApiSettings()?.let { settings ->
context?.let { ctx ->
val default = enumValues<TvType>().sorted().filter { it != TvType.NSFW }
.map { it.ordinal.toString() }.toSet()
val preferredTypes = PreferenceManager.getDefaultSharedPreferences(ctx)
.getStringSet(this.getString(R.string.prefer_media_type_key), default)
?.mapNotNull { it.toIntOrNull() ?: return@mapNotNull null } ?: default
val settings = ctx.getApiSettings()
val notFilteredBySelectedTypes = selectedApis.filter { name ->
settings.contains(name)
}.map { name ->
name to getApiFromNameNull(name)?.supportedTypes
}.filter { (_, types) ->
types?.any { preferredTypes.contains(it.ordinal) } == true
}
searchViewModel.searchAndCancel( searchViewModel.searchAndCancel(
query = query, query = query,
providersActive = selectedApis.filter { name -> providersActive = notFilteredBySelectedTypes.filter { (_, types) ->
settings.contains(name) && getApiFromNameNull(name)?.supportedTypes?.any { types?.any { selectedSearchTypes.contains(it) } == true
selectedSearchTypes.contains( }.ifEmpty { notFilteredBySelectedTypes }.map { it.first }.toSet()
it
)
} == true
}.toSet()
) )
} }
} }