From 7aad801a6e8f993bc508bb5c213ef11007fa780e Mon Sep 17 00:00:00 2001 From: C10udburst <18114966+C10udburst@users.noreply.github.com> Date: Sun, 21 Aug 2022 10:52:44 +0200 Subject: [PATCH] add a way to filter providers by language --- .../ui/settings/extensions/PluginsFragment.kt | 37 ++++++++++++++++++- .../settings/extensions/PluginsViewModel.kt | 13 ++++++- app/src/main/res/menu/repository.xml | 5 +++ 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/settings/extensions/PluginsFragment.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/settings/extensions/PluginsFragment.kt index 27a43d4d..3dffef85 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/settings/extensions/PluginsFragment.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/settings/extensions/PluginsFragment.kt @@ -6,10 +6,20 @@ import android.widget.SearchView import androidx.core.view.isVisible import androidx.fragment.app.Fragment import androidx.fragment.app.activityViewModels +import androidx.lifecycle.map +import com.lagradost.cloudstream3.AcraApplication +import com.lagradost.cloudstream3.CommonActivity import com.lagradost.cloudstream3.R +import com.lagradost.cloudstream3.mvvm.logError import com.lagradost.cloudstream3.mvvm.observe import com.lagradost.cloudstream3.ui.home.HomeFragment.Companion.getPairList import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.setUpToolbar +import com.lagradost.cloudstream3.ui.settings.appLanguages +import com.lagradost.cloudstream3.ui.settings.getCurrentLocale +import com.lagradost.cloudstream3.utils.SingleSelectionHelper.showDialog +import com.lagradost.cloudstream3.utils.SingleSelectionHelper.showMultiDialog +import com.lagradost.cloudstream3.utils.SubtitleHelper +import com.lagradost.cloudstream3.utils.USER_SELECTED_HOMEPAGE_API import kotlinx.android.synthetic.main.fragment_plugins.* const val PLUGINS_BUNDLE_NAME = "name" @@ -32,6 +42,7 @@ class PluginsFragment : Fragment() { // Since the ViewModel is getting reused the tvTypes must be cleared between uses pluginViewModel.tvTypes.clear() + pluginViewModel.languages = listOf() pluginViewModel.search(null) val name = arguments?.getString(PLUGINS_BUNDLE_NAME) @@ -50,6 +61,24 @@ class PluginsFragment : Fragment() { R.id.download_all -> { PluginsViewModel.downloadAll(activity, url, pluginViewModel) } + R.id.lang_filter -> { + val tempLangs = appLanguages.toMutableList() + val languageCodes = mutableListOf("none") + tempLangs.map { (_, _, iso) -> iso } + val languageNames = mutableListOf(getString(R.string.no_data)) + tempLangs.map { (emoji, name, iso) -> + val flag = emoji.ifBlank { SubtitleHelper.getFlagFromIso(iso) ?: "ERROR" } + "$flag $name" + } + val selectedList = pluginViewModel.languages.map { it -> languageCodes.indexOf(it) } + + activity?.showMultiDialog( + languageNames, + selectedList, + getString(R.string.provider_lang_settings), + {}) { newList -> + pluginViewModel.languages = newList.map { it -> languageCodes[it] } + pluginViewModel.updateFilteredPlugins() + } + } else -> {} } return@setOnMenuItemClickListener true @@ -104,6 +133,7 @@ class PluginsFragment : Fragment() { if (isLocal) { // No download button and no categories on local settings_toolbar?.menu?.findItem(R.id.download_all)?.isVisible = false + settings_toolbar?.menu?.findItem(R.id.lang_filter)?.isVisible = false pluginViewModel.updatePluginListLocal() tv_types_scroll_view?.isVisible = false } else { @@ -123,11 +153,14 @@ class PluginsFragment : Fragment() { home_select_others ) +// val supportedTypes: Array = +// pluginViewModel.filteredPlugins.value!!.second.flatMap { it -> it.plugin.second.tvTypes ?: listOf("Other") }.distinct().toTypedArray() + // Copy pasted code for ((button, validTypes) in pairList) { val validTypesMapped = validTypes.map { it.name } - val isValid = - true //validAPIs.any { api -> validTypes.any { api.supportedTypes.contains(it) } } + val isValid = true + //validTypes.any { it -> supportedTypes.contains(it.name) } button?.isVisible = isValid if (isValid) { fun buttonContains(): Boolean { diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/settings/extensions/PluginsViewModel.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/settings/extensions/PluginsViewModel.kt index 146f0f6d..a8e95254 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/settings/extensions/PluginsViewModel.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/settings/extensions/PluginsViewModel.kt @@ -38,6 +38,7 @@ class PluginsViewModel : ViewModel() { var filteredPlugins: LiveData = _filteredPlugins val tvTypes = mutableListOf() + var languages = listOf() private var currentQuery: String? = null companion object { @@ -187,6 +188,16 @@ class PluginsViewModel : ViewModel() { } } + private fun List.filterLang(): List { + if (languages.isEmpty()) return this + return this.filter { + if (it.plugin.second.language == null) { + return@filter languages.contains("none") + } + languages.contains(it.plugin.second.language) + } + } + private fun List.sortByQuery(query: String?): List { return if (query == null) { // Return list to base state if no query @@ -197,7 +208,7 @@ class PluginsViewModel : ViewModel() { } fun updateFilteredPlugins() { - _filteredPlugins.postValue(false to plugins.filterTvTypes().sortByQuery(currentQuery)) + _filteredPlugins.postValue(false to plugins.filterTvTypes().filterLang().sortByQuery(currentQuery)) } fun updatePluginList(repositoryUrl: String) = viewModelScope.launchSafe { diff --git a/app/src/main/res/menu/repository.xml b/app/src/main/res/menu/repository.xml index 4e192291..d44c9764 100644 --- a/app/src/main/res/menu/repository.xml +++ b/app/src/main/res/menu/repository.xml @@ -8,6 +8,11 @@ app:actionViewClass="android.widget.SearchView" app:searchHintIcon="@drawable/search_icon" app:showAsAction="ifRoom" /> +