mirror of
				https://github.com/recloudstream/cloudstream.git
				synced 2024-08-15 01:53:11 +00:00 
			
		
		
		
	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.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<String> = | ||||
| //                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 { | ||||
|  |  | |||
|  | @ -38,6 +38,7 @@ class PluginsViewModel : ViewModel() { | |||
|     var filteredPlugins: LiveData<PluginViewDataUpdate> = _filteredPlugins | ||||
| 
 | ||||
|     val tvTypes = mutableListOf<String>() | ||||
|     var languages = listOf<String>() | ||||
|     private var currentQuery: String? = null | ||||
| 
 | ||||
|     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> { | ||||
|         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 { | ||||
|  |  | |||
|  | @ -8,6 +8,11 @@ | |||
|         app:actionViewClass="android.widget.SearchView" | ||||
|         app:searchHintIcon="@drawable/search_icon" | ||||
|         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 | ||||
|         android:id="@+id/download_all" | ||||
|         android:icon="@drawable/netflix_download" | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue