forked from recloudstream/cloudstream
add a way to filter providers by language
This commit is contained in:
parent
78bd0452c0
commit
7aad801a6e
3 changed files with 52 additions and 3 deletions
|
@ -6,10 +6,20 @@ import android.widget.SearchView
|
||||||
import androidx.core.view.isVisible
|
import androidx.core.view.isVisible
|
||||||
import androidx.fragment.app.Fragment
|
import androidx.fragment.app.Fragment
|
||||||
import androidx.fragment.app.activityViewModels
|
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.R
|
||||||
|
import com.lagradost.cloudstream3.mvvm.logError
|
||||||
import com.lagradost.cloudstream3.mvvm.observe
|
import com.lagradost.cloudstream3.mvvm.observe
|
||||||
import com.lagradost.cloudstream3.ui.home.HomeFragment.Companion.getPairList
|
import com.lagradost.cloudstream3.ui.home.HomeFragment.Companion.getPairList
|
||||||
import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.setUpToolbar
|
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.*
|
import kotlinx.android.synthetic.main.fragment_plugins.*
|
||||||
|
|
||||||
const val PLUGINS_BUNDLE_NAME = "name"
|
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
|
// Since the ViewModel is getting reused the tvTypes must be cleared between uses
|
||||||
pluginViewModel.tvTypes.clear()
|
pluginViewModel.tvTypes.clear()
|
||||||
|
pluginViewModel.languages = listOf()
|
||||||
pluginViewModel.search(null)
|
pluginViewModel.search(null)
|
||||||
|
|
||||||
val name = arguments?.getString(PLUGINS_BUNDLE_NAME)
|
val name = arguments?.getString(PLUGINS_BUNDLE_NAME)
|
||||||
|
@ -50,6 +61,24 @@ class PluginsFragment : Fragment() {
|
||||||
R.id.download_all -> {
|
R.id.download_all -> {
|
||||||
PluginsViewModel.downloadAll(activity, url, pluginViewModel)
|
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 -> {}
|
else -> {}
|
||||||
}
|
}
|
||||||
return@setOnMenuItemClickListener true
|
return@setOnMenuItemClickListener true
|
||||||
|
@ -104,6 +133,7 @@ class PluginsFragment : Fragment() {
|
||||||
if (isLocal) {
|
if (isLocal) {
|
||||||
// No download button and no categories on local
|
// No download button and no categories on local
|
||||||
settings_toolbar?.menu?.findItem(R.id.download_all)?.isVisible = false
|
settings_toolbar?.menu?.findItem(R.id.download_all)?.isVisible = false
|
||||||
|
settings_toolbar?.menu?.findItem(R.id.lang_filter)?.isVisible = false
|
||||||
pluginViewModel.updatePluginListLocal()
|
pluginViewModel.updatePluginListLocal()
|
||||||
tv_types_scroll_view?.isVisible = false
|
tv_types_scroll_view?.isVisible = false
|
||||||
} else {
|
} else {
|
||||||
|
@ -123,11 +153,14 @@ class PluginsFragment : Fragment() {
|
||||||
home_select_others
|
home_select_others
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// val supportedTypes: Array<String> =
|
||||||
|
// pluginViewModel.filteredPlugins.value!!.second.flatMap { it -> it.plugin.second.tvTypes ?: listOf("Other") }.distinct().toTypedArray()
|
||||||
|
|
||||||
// Copy pasted code
|
// Copy pasted code
|
||||||
for ((button, validTypes) in pairList) {
|
for ((button, validTypes) in pairList) {
|
||||||
val validTypesMapped = validTypes.map { it.name }
|
val validTypesMapped = validTypes.map { it.name }
|
||||||
val isValid =
|
val isValid = true
|
||||||
true //validAPIs.any { api -> validTypes.any { api.supportedTypes.contains(it) } }
|
//validTypes.any { it -> supportedTypes.contains(it.name) }
|
||||||
button?.isVisible = isValid
|
button?.isVisible = isValid
|
||||||
if (isValid) {
|
if (isValid) {
|
||||||
fun buttonContains(): Boolean {
|
fun buttonContains(): Boolean {
|
||||||
|
|
|
@ -38,6 +38,7 @@ class PluginsViewModel : ViewModel() {
|
||||||
var filteredPlugins: LiveData<PluginViewDataUpdate> = _filteredPlugins
|
var filteredPlugins: LiveData<PluginViewDataUpdate> = _filteredPlugins
|
||||||
|
|
||||||
val tvTypes = mutableListOf<String>()
|
val tvTypes = mutableListOf<String>()
|
||||||
|
var languages = listOf<String>()
|
||||||
private var currentQuery: String? = null
|
private var currentQuery: String? = null
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
@ -187,6 +188,16 @@ class PluginsViewModel : ViewModel() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun List<PluginViewData>.filterLang(): List<PluginViewData> {
|
||||||
|
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<PluginViewData>.sortByQuery(query: String?): List<PluginViewData> {
|
private fun List<PluginViewData>.sortByQuery(query: String?): List<PluginViewData> {
|
||||||
return if (query == null) {
|
return if (query == null) {
|
||||||
// Return list to base state if no query
|
// Return list to base state if no query
|
||||||
|
@ -197,7 +208,7 @@ class PluginsViewModel : ViewModel() {
|
||||||
}
|
}
|
||||||
|
|
||||||
fun updateFilteredPlugins() {
|
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 {
|
fun updatePluginList(repositoryUrl: String) = viewModelScope.launchSafe {
|
||||||
|
|
|
@ -8,6 +8,11 @@
|
||||||
app:actionViewClass="android.widget.SearchView"
|
app:actionViewClass="android.widget.SearchView"
|
||||||
app:searchHintIcon="@drawable/search_icon"
|
app:searchHintIcon="@drawable/search_icon"
|
||||||
app:showAsAction="ifRoom" />
|
app:showAsAction="ifRoom" />
|
||||||
|
<item
|
||||||
|
android:id="@+id/lang_filter"
|
||||||
|
android:icon="@drawable/ic_baseline_language_24"
|
||||||
|
android:title="@string/provider_lang_settings"
|
||||||
|
app:showAsAction="collapseActionView|ifRoom" />
|
||||||
<item
|
<item
|
||||||
android:id="@+id/download_all"
|
android:id="@+id/download_all"
|
||||||
android:icon="@drawable/netflix_download"
|
android:icon="@drawable/netflix_download"
|
||||||
|
|
Loading…
Reference in a new issue