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

177 lines
6.7 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.R
2022-10-28 01:51:27 +00:00
import com.lagradost.cloudstream3.TvType
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
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-07 23:03:54 +00:00
import kotlinx.android.synthetic.main.fragment_plugins.*
2022-10-28 01:51:27 +00:00
import kotlinx.android.synthetic.main.tvtypes_chips.*
import kotlinx.android.synthetic.main.tvtypes_chips_scroll.*
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? {
2022-08-07 23:03:54 +00:00
return inflater.inflate(R.layout.fragment_plugins, container, false)
2022-08-06 23:43:39 +00:00
}
2022-08-07 21:11:13 +00:00
private val pluginViewModel: PluginsViewModel by activityViewModels()
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)
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)
2022-08-06 23:43:39 +00:00
2022-08-07 23:03:54 +00:00
settings_toolbar?.setOnMenuItemClickListener { menuItem ->
when (menuItem?.itemId) {
R.id.download_all -> {
PluginsViewModel.downloadAll(activity, url, pluginViewModel)
2022-08-07 23:03:54 +00:00
}
R.id.lang_filter -> {
val tempLangs = appLanguages.toMutableList()
val languageCodes = mutableListOf("none") + tempLangs.map { (_, _, iso) -> iso }
2022-08-21 20:13:53 +00:00
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 ->
2022-08-21 20:13:53 +00:00
pluginViewModel.languages = newList.map { it -> languageCodes[it] }
pluginViewModel.updateFilteredPlugins()
}
}
2022-08-07 23:03:54 +00:00
else -> {}
}
return@setOnMenuItemClickListener true
}
2022-08-13 03:53:35 +00:00
val searchView =
settings_toolbar?.menu?.findItem(R.id.search_button)?.actionView as? SearchView
2022-08-13 03:53:35 +00:00
// Don't go back if active query
settings_toolbar?.setNavigationOnClickListener {
if (searchView?.isIconified == false) {
2022-08-13 21:49:16 +00:00
searchView.isIconified = true
2022-08-13 03:53:35 +00:00
} else {
activity?.onBackPressed()
}
}
// searchView?.onActionViewCollapsed = {
// pluginViewModel.search(null)
// }
// Because onActionViewCollapsed doesn't wanna work we need this workaround :(
searchView?.setOnQueryTextFocusChangeListener { _, hasFocus ->
if (!hasFocus) pluginViewModel.search(null)
}
searchView?.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
2022-08-13 03:53:35 +00:00
override fun onQueryTextSubmit(query: String?): Boolean {
pluginViewModel.search(query)
return true
}
override fun onQueryTextChange(newText: String?): Boolean {
pluginViewModel.search(newText)
return true
}
})
2022-08-07 23:03:54 +00:00
plugin_recycler_view?.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.
plugin_recycler_view?.setPadding(0, 0, 0, 200.toPx)
}
observe(pluginViewModel.filteredPlugins) { (scrollToTop, list) ->
(plugin_recycler_view?.adapter as? PluginAdapter?)?.updateList(list)
if (scrollToTop)
plugin_recycler_view?.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
2022-08-13 03:53:35 +00:00
settings_toolbar?.menu?.findItem(R.id.download_all)?.isVisible = false
settings_toolbar?.menu?.findItem(R.id.lang_filter)?.isVisible = false
2022-08-11 22:36:19 +00:00
pluginViewModel.updatePluginListLocal()
2022-08-13 21:55:54 +00:00
tv_types_scroll_view?.isVisible = false
2022-08-11 22:36:19 +00:00
} else {
pluginViewModel.updatePluginList(context, url)
2022-08-13 21:55:54 +00:00
tv_types_scroll_view?.isVisible = true
2022-10-28 01:51:27 +00:00
bindChips(home_select_group, emptyList(), TvType.values().toList()) { list ->
pluginViewModel.tvTypes.clear()
pluginViewModel.tvTypes.addAll(list.map { it.name })
pluginViewModel.updateFilteredPlugins()
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
}
}