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

258 lines
10 KiB
Kotlin
Raw Normal View History

2022-08-06 23:43:39 +00:00
package com.lagradost.cloudstream3.ui.settings.extensions
import android.content.ClipboardManager
import android.content.Context
2022-08-08 18:14:57 +00:00
import android.content.DialogInterface
2022-08-06 23:43:39 +00:00
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
2022-08-11 17:39:34 +00:00
import android.widget.LinearLayout
2022-08-06 23:43:39 +00:00
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
2022-08-28 23:52:15 +00:00
import androidx.core.view.isGone
2022-08-11 17:39:34 +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 androidx.navigation.fragment.findNavController
import com.lagradost.cloudstream3.CommonActivity.showToast
2022-08-15 15:03:06 +00:00
import com.lagradost.cloudstream3.MainActivity.Companion.afterRepositoryLoadedEvent
2022-08-06 23:43:39 +00:00
import com.lagradost.cloudstream3.R
2023-07-14 00:28:49 +00:00
import com.lagradost.cloudstream3.databinding.AddRepoInputBinding
import com.lagradost.cloudstream3.databinding.FragmentExtensionsBinding
2022-08-06 23:43:39 +00:00
import com.lagradost.cloudstream3.mvvm.observe
import com.lagradost.cloudstream3.mvvm.observeNullable
2022-08-06 23:43:39 +00:00
import com.lagradost.cloudstream3.plugins.RepositoryManager
import com.lagradost.cloudstream3.ui.result.FOCUS_SELF
import com.lagradost.cloudstream3.ui.result.setLinearListLayout
2022-08-11 17:39:34 +00:00
import com.lagradost.cloudstream3.ui.result.setText
2022-08-28 23:52:15 +00:00
import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.isTrueTvSettings
2022-08-07 21:11:13 +00:00
import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.setUpToolbar
import com.lagradost.cloudstream3.utils.AppUtils.downloadAllPluginsDialog
import com.lagradost.cloudstream3.utils.AppUtils.setDefaultFocus
2022-08-06 23:43:39 +00:00
import com.lagradost.cloudstream3.utils.Coroutines.ioSafe
2022-08-08 18:14:57 +00:00
import com.lagradost.cloudstream3.utils.Coroutines.main
2022-08-06 23:43:39 +00:00
import com.lagradost.cloudstream3.utils.UIHelper.dismissSafe
2022-08-28 23:52:15 +00:00
import com.lagradost.cloudstream3.widget.LinearRecycleViewLayoutManager
2022-08-15 01:31:33 +00:00
2022-08-06 23:43:39 +00:00
class ExtensionsFragment : Fragment() {
2023-07-14 00:28:49 +00:00
var binding: FragmentExtensionsBinding? = null
override fun onDestroyView() {
binding = null
super.onDestroyView()
}
2022-08-06 23:43:39 +00:00
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
2023-07-14 00:28:49 +00:00
): View {
val localBinding = FragmentExtensionsBinding.inflate(inflater, container, false)
binding = localBinding
return localBinding.root//inflater.inflate(R.layout.fragment_extensions, container, false)
2022-08-06 23:43:39 +00:00
}
2022-08-11 17:39:34 +00:00
private fun View.setLayoutWidth(weight: Int) {
val param = LinearLayout.LayoutParams(
0,
LinearLayout.LayoutParams.MATCH_PARENT,
weight.toFloat()
)
this.layoutParams = param
}
2022-08-06 23:43:39 +00:00
private val extensionViewModel: ExtensionsViewModel by activityViewModels()
2022-08-15 15:03:06 +00:00
override fun onResume() {
super.onResume()
afterRepositoryLoadedEvent += ::reloadRepositories
}
override fun onStop() {
super.onStop()
afterRepositoryLoadedEvent -= ::reloadRepositories
}
private fun reloadRepositories(success: Boolean = true) {
extensionViewModel.loadStats()
extensionViewModel.loadRepositories()
}
2022-08-06 23:43:39 +00:00
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
2022-08-07 21:11:13 +00:00
//context?.fixPaddingStatusbar(extensions_root)
setUpToolbar(R.string.extensions)
2022-08-06 23:43:39 +00:00
binding?.repoRecyclerView?.setLinearListLayout(
isHorizontal = false,
nextUp = R.id.settings_toolbar, //FOCUS_SELF, // back has no id so we cant :pensive:
nextDown = R.id.plugin_storage_appbar,
nextRight = FOCUS_SELF,
nextLeft = R.id.nav_rail_view
)
2023-07-14 00:28:49 +00:00
binding?.repoRecyclerView?.adapter = RepoAdapter(false, {
2022-08-07 00:48:49 +00:00
findNavController().navigate(
R.id.navigation_settings_extensions_to_navigation_settings_plugins,
2022-08-11 22:36:19 +00:00
PluginsFragment.newInstance(
it.name,
it.url,
false
)
)
2022-08-07 00:48:49 +00:00
}, { repo ->
2022-08-08 18:14:57 +00:00
// Prompt user before deleting repo
main {
val builder = AlertDialog.Builder(context ?: view.context)
val dialogClickListener =
DialogInterface.OnClickListener { _, which ->
when (which) {
DialogInterface.BUTTON_POSITIVE -> {
ioSafe {
RepositoryManager.removeRepository(view.context, repo)
2022-08-11 17:39:34 +00:00
extensionViewModel.loadStats()
2022-08-08 18:14:57 +00:00
extensionViewModel.loadRepositories()
}
}
2022-08-08 18:14:57 +00:00
DialogInterface.BUTTON_NEGATIVE -> {}
}
}
builder.setTitle(R.string.delete_repository)
.setMessage(
context?.getString(R.string.delete_repository_plugins)
)
.setPositiveButton(R.string.delete, dialogClickListener)
.setNegativeButton(R.string.cancel, dialogClickListener)
.show().setDefaultFocus()
2022-08-06 23:43:39 +00:00
}
2022-08-07 00:48:49 +00:00
})
observe(extensionViewModel.repositories) {
2023-07-14 00:28:49 +00:00
binding?.repoRecyclerView?.isVisible = it.isNotEmpty()
binding?.blankRepoScreen?.isVisible = it.isEmpty()
(binding?.repoRecyclerView?.adapter as? RepoAdapter)?.updateList(it)
2022-08-11 17:39:34 +00:00
}
/*binding?.repoRecyclerView?.apply {
2022-08-28 23:52:15 +00:00
context?.let { ctx ->
layoutManager = LinearRecycleViewLayoutManager(ctx, nextFocusUpId, nextFocusDownId)
}
}*/
2022-08-28 23:52:15 +00:00
// list_repositories?.setOnClickListener {
// // Open webview on tv if browser fails
// val isTv = isTvSettings()
// openBrowser(PUBLIC_REPOSITORIES_LIST, isTv, this)
//
// // Set clipboard on TV because the browser might not exist or work properly
// if (isTv) {
// val serviceClipboard =
// (activity?.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager?)
// ?: return@setOnClickListener
// val clip = ClipData.newPlainText("Repository url", PUBLIC_REPOSITORIES_LIST)
// serviceClipboard.setPrimaryClip(clip)
// }
// }
2022-08-15 01:31:33 +00:00
observeNullable(extensionViewModel.pluginStats) { value ->
2023-07-14 00:28:49 +00:00
binding?.apply {
if (value == null) {
pluginStorageAppbar.isVisible = false
2023-07-14 00:28:49 +00:00
return@observeNullable
}
2023-07-14 00:28:49 +00:00
pluginStorageAppbar.isVisible = true
if (value.total == 0) {
pluginDownload.setLayoutWidth(1)
pluginDisabled.setLayoutWidth(0)
pluginNotDownloaded.setLayoutWidth(0)
} else {
pluginDownload.setLayoutWidth(value.downloaded)
pluginDisabled.setLayoutWidth(value.disabled)
pluginNotDownloaded.setLayoutWidth(value.notDownloaded)
}
pluginNotDownloadedTxt.setText(value.notDownloadedText)
pluginDisabledTxt.setText(value.disabledText)
pluginDownloadTxt.setText(value.downloadedText)
2022-08-11 17:39:34 +00:00
}
2022-08-06 23:43:39 +00:00
}
2023-07-14 00:28:49 +00:00
binding?.pluginStorageAppbar?.setOnClickListener {
2022-08-11 22:36:19 +00:00
findNavController().navigate(
R.id.navigation_settings_extensions_to_navigation_settings_plugins,
PluginsFragment.newInstance(
getString(R.string.extensions),
"",
true
)
)
}
2022-08-28 23:52:15 +00:00
val addRepositoryClick = View.OnClickListener {
2023-07-14 00:28:49 +00:00
val ctx = context ?: return@OnClickListener
val binding = AddRepoInputBinding.inflate(LayoutInflater.from(ctx), null, false)
2022-08-06 23:43:39 +00:00
val builder =
2023-07-14 00:28:49 +00:00
AlertDialog.Builder(ctx, R.style.AlertDialogCustom)
.setView(binding.root)
2022-08-06 23:43:39 +00:00
val dialog = builder.create()
dialog.show()
(activity?.getSystemService(Context.CLIPBOARD_SERVICE) as? ClipboardManager?)?.primaryClip?.getItemAt(
0
)?.text?.toString()?.let { copy ->
2023-07-14 00:28:49 +00:00
binding.repoUrlInput.setText(copy)
2022-08-06 23:43:39 +00:00
}
2022-08-15 15:03:06 +00:00
// dialog.list_repositories?.setOnClickListener {
// // Open webview on tv if browser fails
// openBrowser(PUBLIC_REPOSITORIES_LIST, isTvSettings(), this)
// dialog.dismissSafe()
// }
2022-08-06 23:43:39 +00:00
// dialog.text2?.text = provider.name
2023-07-14 00:28:49 +00:00
binding.applyBtt.setOnClickListener secondListener@{
val name = binding.repoNameInput.text?.toString()
2022-08-06 23:43:39 +00:00
ioSafe {
2023-07-14 00:28:49 +00:00
val url = binding.repoUrlInput.text?.toString()
?.let { it1 -> RepositoryManager.parseRepoUrl(it1) }
if (url.isNullOrBlank()) {
2022-11-05 18:54:04 +00:00
main {
showToast(R.string.error_invalid_data, Toast.LENGTH_SHORT)
2022-11-05 18:54:04 +00:00
}
} else {
val fixedName = if (!name.isNullOrBlank()) name
else RepositoryManager.parseRepository(url)?.name ?: "No name"
val newRepo = RepositoryData(fixedName, url)
RepositoryManager.addRepository(newRepo)
extensionViewModel.loadStats()
extensionViewModel.loadRepositories()
this@ExtensionsFragment.activity?.downloadAllPluginsDialog(url, fixedName)
}
2022-08-06 23:43:39 +00:00
}
dialog.dismissSafe(activity)
}
2023-07-14 00:28:49 +00:00
binding.cancelBtt.setOnClickListener {
2022-08-06 23:43:39 +00:00
dialog.dismissSafe(activity)
}
}
2022-08-28 23:52:15 +00:00
val isTv = isTrueTvSettings()
2023-07-14 00:28:49 +00:00
binding?.apply {
addRepoButton.isGone = isTv
addRepoButtonImageviewHolder.isVisible = isTv
2022-10-13 20:58:18 +00:00
2023-07-14 00:28:49 +00:00
// Band-aid for Fire TV
pluginStorageAppbar.isFocusableInTouchMode = isTv
addRepoButtonImageview.isFocusableInTouchMode = isTv
2022-08-28 23:52:15 +00:00
2023-07-14 00:28:49 +00:00
addRepoButton.setOnClickListener(addRepositoryClick)
addRepoButtonImageview.setOnClickListener(addRepositoryClick)
}
2022-08-15 15:03:06 +00:00
reloadRepositories()
2022-08-06 23:43:39 +00:00
}
}