AquaStream/app/src/main/java/com/lagradost/cloudstream3/ui/settings/extensions/PluginsFragment.kt

212 lines
8.2 KiB
Kotlin
Raw Normal View History

2022-08-06 23:43:39 +00:00
package com.lagradost.cloudstream3.ui.settings.extensions
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.SearchView
2022-08-13 21:49:16 +00:00
import androidx.core.view.isVisible
2022-08-06 23:43:39 +00:00
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import com.lagradost.cloudstream3.APIHolder.getApiProviderLangSettings
import com.lagradost.cloudstream3.AllLanguagesName
2022-08-06 23:43:39 +00:00
import com.lagradost.cloudstream3.R
2022-10-28 01:51:27 +00:00
import com.lagradost.cloudstream3.TvType
import com.lagradost.cloudstream3.databinding.FragmentPluginsBinding
2022-08-07 21:11:13 +00:00
import com.lagradost.cloudstream3.mvvm.observe
2022-10-28 01:51:27 +00:00
import com.lagradost.cloudstream3.ui.home.HomeFragment.Companion.bindChips
import com.lagradost.cloudstream3.ui.result.FOCUS_SELF
import com.lagradost.cloudstream3.ui.result.setLinearListLayout
2022-08-21 20:13:53 +00:00
import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.isTvSettings
2022-08-07 21:11:13 +00:00
import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.setUpToolbar
import com.lagradost.cloudstream3.ui.settings.appLanguages
import com.lagradost.cloudstream3.utils.SingleSelectionHelper.showMultiDialog
import com.lagradost.cloudstream3.utils.SubtitleHelper
2022-08-21 20:13:53 +00:00
import com.lagradost.cloudstream3.utils.UIHelper.toPx
2022-08-06 23:43:39 +00:00
const val PLUGINS_BUNDLE_NAME = "name"
const val PLUGINS_BUNDLE_URL = "url"
2022-08-11 22:36:19 +00:00
const val PLUGINS_BUNDLE_LOCAL = "isLocal"
2022-08-06 23:43:39 +00:00
class PluginsFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
): View {
val localBinding = FragmentPluginsBinding.inflate(inflater, container, false)
binding = localBinding
return localBinding.root//inflater.inflate(R.layout.fragment_plugins, container, false)
}
override fun onDestroyView() {
binding = null
super.onDestroyView()
2022-08-06 23:43:39 +00:00
}
2022-08-07 21:11:13 +00:00
private val pluginViewModel: PluginsViewModel by activityViewModels()
var binding: FragmentPluginsBinding? = null
2022-08-06 23:43:39 +00:00
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// Since the ViewModel is getting reused the tvTypes must be cleared between uses
pluginViewModel.tvTypes.clear()
pluginViewModel.languages = listOf()
pluginViewModel.search(null)
// Filter by language set on preferred media
activity?.let {
2022-11-27 09:37:36 +00:00
val providerLangs = it.getApiProviderLangSettings().toList()
if (!providerLangs.contains(AllLanguagesName)) {
pluginViewModel.languages = mutableListOf("none") + providerLangs
//Log.i("DevDebug", "providerLang => ${pluginViewModel.languages.toJson()}")
}
}
2022-08-06 23:43:39 +00:00
val name = arguments?.getString(PLUGINS_BUNDLE_NAME)
val url = arguments?.getString(PLUGINS_BUNDLE_URL)
2022-08-11 22:36:19 +00:00
val isLocal = arguments?.getBoolean(PLUGINS_BUNDLE_LOCAL) == true
2022-08-07 21:11:13 +00:00
2022-08-07 23:03:54 +00:00
if (url == null || name == null) {
2022-08-06 23:43:39 +00:00
activity?.onBackPressed()
return
}
2022-08-07 23:03:54 +00:00
setUpToolbar(name)
binding?.settingsToolbar?.apply {
setOnMenuItemClickListener { menuItem ->
when (menuItem?.itemId) {
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 { languageCodes.indexOf(it) }
activity?.showMultiDialog(
languageNames,
selectedList,
getString(R.string.provider_lang_settings),
{}) { newList ->
pluginViewModel.languages = newList.map { languageCodes[it] }
pluginViewModel.updateFilteredPlugins()
2022-08-21 20:13:53 +00:00
}
}
else -> {}
}
return@setOnMenuItemClickListener true
2022-08-07 23:03:54 +00:00
}
val searchView =
menu?.findItem(R.id.search_button)?.actionView as? SearchView
2022-08-13 03:53:35 +00:00
// Don't go back if active query
setNavigationOnClickListener {
if (searchView?.isIconified == false) {
searchView.isIconified = true
} else {
activity?.onBackPressed()
}
2022-08-13 03:53:35 +00:00
}
searchView?.setOnQueryTextFocusChangeListener { _, hasFocus ->
if (!hasFocus) pluginViewModel.search(null)
}
2022-08-13 03:53:35 +00:00
searchView?.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
pluginViewModel.search(query)
return true
}
override fun onQueryTextChange(newText: String?): Boolean {
pluginViewModel.search(newText)
return true
}
})
}
2022-08-13 03:53:35 +00:00
// searchView?.onActionViewCollapsed = {
// pluginViewModel.search(null)
// }
// Because onActionViewCollapsed doesn't wanna work we need this workaround :(
binding?.pluginRecyclerView?.setLinearListLayout(
isHorizontal = false,
nextDown = FOCUS_SELF,
nextRight = FOCUS_SELF,
)
2022-08-13 03:53:35 +00:00
binding?.pluginRecyclerView?.adapter =
2022-08-07 21:11:13 +00:00
PluginAdapter {
2022-08-11 22:36:19 +00:00
pluginViewModel.handlePluginAction(activity, url, it, isLocal)
2022-08-06 23:43:39 +00:00
}
2022-08-07 21:11:13 +00:00
2022-08-28 23:52:15 +00:00
if (isTvSettings()) {
2022-08-21 20:13:53 +00:00
// Scrolling down does not reveal the whole RecyclerView on TV, add to bypass that.
binding?.pluginRecyclerView?.setPadding(0, 0, 0, 200.toPx)
2022-08-21 20:13:53 +00:00
}
observe(pluginViewModel.filteredPlugins) { (scrollToTop, list) ->
(binding?.pluginRecyclerView?.adapter as? PluginAdapter)?.updateList(list)
if (scrollToTop)
binding?.pluginRecyclerView?.scrollToPosition(0)
2022-08-06 23:43:39 +00:00
}
2022-08-07 21:11:13 +00:00
2022-08-11 22:36:19 +00:00
if (isLocal) {
2022-08-13 21:55:54 +00:00
// No download button and no categories on local
binding?.settingsToolbar?.menu?.findItem(R.id.download_all)?.isVisible = false
binding?.settingsToolbar?.menu?.findItem(R.id.lang_filter)?.isVisible = false
2022-08-11 22:36:19 +00:00
pluginViewModel.updatePluginListLocal()
binding?.tvtypesChipsScroll?.root?.isVisible = false
2022-08-11 22:36:19 +00:00
} else {
pluginViewModel.updatePluginList(context, url)
binding?.tvtypesChipsScroll?.root?.isVisible = true
2022-08-13 21:55:54 +00:00
bindChips(
binding?.tvtypesChipsScroll?.tvtypesChips,
emptyList(),
TvType.values().toList(),
callback = { list ->
pluginViewModel.tvTypes.clear()
pluginViewModel.tvTypes.addAll(list.map { it.name })
pluginViewModel.updateFilteredPlugins()
},
nextFocusDown = R.id.plugin_recycler_view,
nextFocusUp = null,
)
2022-08-13 21:49:16 +00:00
}
2022-08-06 23:43:39 +00:00
}
companion object {
2022-08-11 22:36:19 +00:00
fun newInstance(name: String, url: String, isLocal: Boolean): Bundle {
2022-08-06 23:43:39 +00:00
return Bundle().apply {
putString(PLUGINS_BUNDLE_NAME, name)
putString(PLUGINS_BUNDLE_URL, url)
2022-08-11 22:36:19 +00:00
putBoolean(PLUGINS_BUNDLE_LOCAL, isLocal)
2022-08-06 23:43:39 +00:00
}
}
2022-08-13 03:53:35 +00:00
// class RepoSearchView(context: Context) : android.widget.SearchView(context) {
2022-08-13 03:53:35 +00:00
// var onActionViewCollapsed = {}
//
// override fun onActionViewCollapsed() {
// onActionViewCollapsed()
// }
// }
2022-08-13 03:53:35 +00:00
2022-08-06 23:43:39 +00:00
}
}