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

186 lines
7.4 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-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
import com.lagradost.cloudstream3.R
2022-08-11 17:39:34 +00:00
import com.lagradost.cloudstream3.mvvm.Some
2022-08-06 23:43:39 +00:00
import com.lagradost.cloudstream3.mvvm.observe
import com.lagradost.cloudstream3.plugins.RepositoryManager
2022-08-11 17:39:34 +00:00
import com.lagradost.cloudstream3.ui.result.setText
2022-08-07 21:11:13 +00:00
import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.setUpToolbar
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
import kotlinx.android.synthetic.main.add_repo_input.*
import kotlinx.android.synthetic.main.add_repo_input.apply_btt
import kotlinx.android.synthetic.main.add_repo_input.cancel_btt
import kotlinx.android.synthetic.main.fragment_extensions.*
class ExtensionsFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
): View? {
return inflater.inflate(R.layout.fragment_extensions, container, false)
}
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()
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
2022-08-11 17:39:34 +00:00
repo_recycler_view?.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()
}
}
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()
2022-08-06 23:43:39 +00:00
}
2022-08-07 00:48:49 +00:00
})
observe(extensionViewModel.repositories) {
repo_recycler_view?.isVisible = it.isNotEmpty()
blank_repo_screen?.isVisible = it.isEmpty()
2022-08-11 17:39:34 +00:00
(repo_recycler_view?.adapter as? RepoAdapter)?.updateList(it)
}
observe(extensionViewModel.pluginStats) {
when (it) {
is Some.Success -> {
val value = it.value
plugin_storage_appbar?.isVisible = true
if (value.total == 0) {
plugin_download?.setLayoutWidth(1)
plugin_disabled?.setLayoutWidth(0)
plugin_not_downloaded?.setLayoutWidth(0)
} else {
plugin_download?.setLayoutWidth(value.downloaded)
plugin_disabled?.setLayoutWidth(value.disabled)
plugin_not_downloaded?.setLayoutWidth(value.notDownloaded)
}
plugin_not_downloaded_txt.setText(value.notDownloadedText)
plugin_disabled_txt.setText(value.disabledText)
plugin_download_txt.setText(value.downloadedText)
}
is Some.None -> {
plugin_storage_appbar?.isVisible = false
}
}
2022-08-06 23:43:39 +00:00
}
2022-08-11 22:36:19 +00:00
plugin_storage_appbar?.setOnClickListener {
findNavController().navigate(
R.id.navigation_settings_extensions_to_navigation_settings_plugins,
PluginsFragment.newInstance(
getString(R.string.extensions),
"",
true
)
)
}
2022-08-06 23:43:39 +00:00
add_repo_button?.setOnClickListener {
val builder =
AlertDialog.Builder(context ?: return@setOnClickListener, R.style.AlertDialogCustom)
.setView(R.layout.add_repo_input)
val dialog = builder.create()
dialog.show()
(activity?.getSystemService(Context.CLIPBOARD_SERVICE) as? ClipboardManager?)?.primaryClip?.getItemAt(
0
)?.text?.toString()?.let { copy ->
2022-08-11 17:41:54 +00:00
// Fix our own repo links and only paste the text if it's a link.
if (copy.startsWith("http")) {
val fixedUrl = if (copy.startsWith("https://cs.repo")) {
"https://" + copy.substringAfter("?")
} else {
copy
}
dialog.repo_url_input?.setText(fixedUrl)
}
2022-08-06 23:43:39 +00:00
}
// dialog.text2?.text = provider.name
dialog.apply_btt?.setOnClickListener secondListener@{
val name = dialog.repo_name_input?.text?.toString()
val url = dialog.repo_url_input?.text?.toString()
if (url.isNullOrBlank()) {
2022-08-06 23:43:39 +00:00
showToast(activity, R.string.error_invalid_data, Toast.LENGTH_SHORT)
return@secondListener
}
ioSafe {
val fixedName = if (!name.isNullOrBlank()) name
else RepositoryManager.parseRepository(url)?.name ?: "No name"
val newRepo = RepositoryData(fixedName, url)
2022-08-06 23:43:39 +00:00
RepositoryManager.addRepository(newRepo)
2022-08-11 17:39:34 +00:00
extensionViewModel.loadStats()
2022-08-06 23:43:39 +00:00
extensionViewModel.loadRepositories()
}
dialog.dismissSafe(activity)
}
dialog.cancel_btt?.setOnClickListener {
dialog.dismissSafe(activity)
}
}
2022-08-11 17:39:34 +00:00
extensionViewModel.loadStats()
2022-08-06 23:43:39 +00:00
extensionViewModel.loadRepositories()
}
}