From 34660bcd485bcf2c48f840a4d3757d7d9b600711 Mon Sep 17 00:00:00 2001
From: Blatzar <46196380+Blatzar@users.noreply.github.com>
Date: Sun, 7 Aug 2022 02:48:49 +0200
Subject: [PATCH] Added loading repositories from url
---
app/src/main/AndroidManifest.xml | 1 +
.../lagradost/cloudstream3/MainActivity.kt | 25 ++++++++++++++++++-
.../cloudstream3/plugins/PluginManager.kt | 2 +-
.../cloudstream3/plugins/RepositoryManager.kt | 25 +++++++++++++------
.../settings/extensions/ExtensionsFragment.kt | 25 ++++++++++++-------
.../ui/settings/extensions/PluginAdapter.kt | 2 --
.../ui/settings/extensions/PluginsFragment.kt | 1 -
.../ui/settings/extensions/RepoAdapter.kt | 10 ++++++--
8 files changed, 68 insertions(+), 23 deletions(-)
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index dfeae5af..24b65174 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -178,6 +178,7 @@
+
diff --git a/app/src/main/java/com/lagradost/cloudstream3/MainActivity.kt b/app/src/main/java/com/lagradost/cloudstream3/MainActivity.kt
index 9b73f2f2..49504efd 100644
--- a/app/src/main/java/com/lagradost/cloudstream3/MainActivity.kt
+++ b/app/src/main/java/com/lagradost/cloudstream3/MainActivity.kt
@@ -10,6 +10,7 @@ import android.view.KeyEvent
import android.view.Menu
import android.view.MenuItem
import android.view.WindowManager
+import android.widget.Toast
import androidx.annotation.IdRes
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isVisible
@@ -79,6 +80,9 @@ import java.io.File
import kotlin.concurrent.thread
import kotlin.reflect.KClass
import com.lagradost.cloudstream3.plugins.PluginManager
+import com.lagradost.cloudstream3.plugins.RepositoryManager
+import com.lagradost.cloudstream3.ui.settings.extensions.RepositoryData
+import com.lagradost.cloudstream3.utils.Coroutines.main
const val VLC_PACKAGE = "org.videolan.vlc"
@@ -320,7 +324,26 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener {
val str = intent.dataString
loadCache()
if (str != null) {
- if (str.contains(appString)) {
+ if (str.startsWith("https://cs.repo")) {
+ val realUrl = "https://" + str.substringAfter("?")
+ println("Repository url: $realUrl")
+ ioSafe {
+ val repo = RepositoryManager.parseRepository(realUrl) ?: return@ioSafe
+ RepositoryManager.addRepository(
+ RepositoryData(
+ repo.name,
+ realUrl
+ )
+ )
+ main {
+ showToast(
+ this,
+ this.getString(R.string.player_loaded_subtitles, repo.name),
+ Toast.LENGTH_LONG
+ )
+ }
+ }
+ } else if (str.contains(appString)) {
for (api in OAuth2Apis) {
if (str.contains("/${api.redirectUrl}")) {
ioSafe {
diff --git a/app/src/main/java/com/lagradost/cloudstream3/plugins/PluginManager.kt b/app/src/main/java/com/lagradost/cloudstream3/plugins/PluginManager.kt
index a1e6da60..9aed690a 100644
--- a/app/src/main/java/com/lagradost/cloudstream3/plugins/PluginManager.kt
+++ b/app/src/main/java/com/lagradost/cloudstream3/plugins/PluginManager.kt
@@ -85,7 +85,7 @@ object PluginManager {
var loadedLocalPlugins = false
private val gson = Gson()
- fun maybeLoadPlugin(context: Context, file: File) {
+ private fun maybeLoadPlugin(context: Context, file: File) {
val name = file.name
if (file.extension == "zip" || file.extension == "cs3") {
loadPlugin(context, file, PluginData(name, null, false, file.absolutePath))
diff --git a/app/src/main/java/com/lagradost/cloudstream3/plugins/RepositoryManager.kt b/app/src/main/java/com/lagradost/cloudstream3/plugins/RepositoryManager.kt
index cff52871..5bc38f39 100644
--- a/app/src/main/java/com/lagradost/cloudstream3/plugins/RepositoryManager.kt
+++ b/app/src/main/java/com/lagradost/cloudstream3/plugins/RepositoryManager.kt
@@ -44,19 +44,19 @@ data class SitePlugin(
object RepositoryManager {
const val ONLINE_PLUGINS_FOLDER = "Extensions"
- private suspend fun parseRepository(url: String): Repository? {
+ suspend fun parseRepository(url: String): Repository? {
return suspendSafeApiCall {
// Take manifestVersion and such into account later
app.get(url).parsedSafe()
}
}
- private suspend fun parsePlugins(pluginUrls: String): ArrayList? {
+ private suspend fun parsePlugins(pluginUrls: String): List {
// Take manifestVersion and such into account later
val response = app.get(pluginUrls)
// Normal parsed function not working?
// return response.parsedSafe()
- return tryParseJson>(response.text)
+ return tryParseJson>(response.text)?.toList() ?: emptyList()
}
suspend fun getRepoPlugins(repositoryUrl: String): List? {
@@ -85,10 +85,21 @@ object RepositoryManager {
// Don't want to read before we write in another thread
private val repoLock = Mutex()
suspend fun addRepository(repository: RepositoryData) {
- repoLock.withLock {
- val currentRepos = getKey>(REPOSITORIES_KEY) ?: emptyList()
- setKey(REPOSITORIES_KEY, currentRepos + repository)
- }
+ repoLock.withLock {
+ val currentRepos = getKey>(REPOSITORIES_KEY) ?: emptyArray()
+ // No duplicates
+ if (currentRepos.any { it.url == repository.url }) return
+ setKey(REPOSITORIES_KEY, currentRepos + repository)
+ }
+ }
+
+ suspend fun removeRepository(repository: RepositoryData) {
+ repoLock.withLock {
+ val currentRepos = getKey>(REPOSITORIES_KEY) ?: emptyArray()
+ // No duplicates
+ val newRepos = currentRepos.filter { it.url != repository.url }
+ setKey(REPOSITORIES_KEY, newRepos)
+ }
}
private fun write(stream: InputStream, output: OutputStream) {
diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/settings/extensions/ExtensionsFragment.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/settings/extensions/ExtensionsFragment.kt
index d91a2a44..b7f00e73 100644
--- a/app/src/main/java/com/lagradost/cloudstream3/ui/settings/extensions/ExtensionsFragment.kt
+++ b/app/src/main/java/com/lagradost/cloudstream3/ui/settings/extensions/ExtensionsFragment.kt
@@ -39,16 +39,23 @@ class ExtensionsFragment : Fragment() {
super.onViewCreated(view, savedInstanceState)
context?.fixPaddingStatusbar(extensions_root)
- observe(extensionViewModel.repositories) {
- // Kinda cheap to do this instead of updates
- repo_recycler_view?.adapter = RepoAdapter(it) {
- findNavController().navigate(
- R.id.navigation_settings_extensions_to_navigation_settings_plugins,
- Bundle().apply {
- putString(PLUGINS_BUNDLE_NAME, it.name)
- putString(PLUGINS_BUNDLE_URL, it.url)
- })
+ repo_recycler_view?.adapter = RepoAdapter(emptyArray(), {
+ findNavController().navigate(
+ R.id.navigation_settings_extensions_to_navigation_settings_plugins,
+ Bundle().apply {
+ putString(PLUGINS_BUNDLE_NAME, it.name)
+ putString(PLUGINS_BUNDLE_URL, it.url)
+ })
+ }, { repo ->
+ ioSafe {
+ RepositoryManager.removeRepository(repo)
+ extensionViewModel.loadRepositories()
}
+ })
+
+ observe(extensionViewModel.repositories) {
+ (repo_recycler_view?.adapter as? RepoAdapter)?.repositories = it
+ (repo_recycler_view?.adapter as? RepoAdapter)?.notifyDataSetChanged()
}
add_repo_button?.setOnClickListener {
diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/settings/extensions/PluginAdapter.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/settings/extensions/PluginAdapter.kt
index 2a862498..61ad2601 100644
--- a/app/src/main/java/com/lagradost/cloudstream3/ui/settings/extensions/PluginAdapter.kt
+++ b/app/src/main/java/com/lagradost/cloudstream3/ui/settings/extensions/PluginAdapter.kt
@@ -46,8 +46,6 @@ class PluginAdapter(
plugin: SitePlugin
) {
val isDownloaded = storedPlugins.any { it.url == plugin.url }
-println("ISOWNLOADED $isDownloaded ${storedPlugins.map { it.url }} ||||| ${plugin.url}")
-
val drawableInt = if (isDownloaded)
R.drawable.ic_baseline_delete_outline_24
diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/settings/extensions/PluginsFragment.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/settings/extensions/PluginsFragment.kt
index 452fd4b0..2367b515 100644
--- a/app/src/main/java/com/lagradost/cloudstream3/ui/settings/extensions/PluginsFragment.kt
+++ b/app/src/main/java/com/lagradost/cloudstream3/ui/settings/extensions/PluginsFragment.kt
@@ -43,7 +43,6 @@ class PluginsFragment : Fragment() {
ioSafe {
val plugins = extensionViewModel.getPlugins(url)
- println("GET PLUGINS $plugins")
main {
repo_recycler_view?.adapter = PluginAdapter(plugins) { plugin, isDownloaded ->
ioSafe {
diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/settings/extensions/RepoAdapter.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/settings/extensions/RepoAdapter.kt
index 92c2e4d1..f71aec81 100644
--- a/app/src/main/java/com/lagradost/cloudstream3/ui/settings/extensions/RepoAdapter.kt
+++ b/app/src/main/java/com/lagradost/cloudstream3/ui/settings/extensions/RepoAdapter.kt
@@ -9,8 +9,9 @@ import com.lagradost.cloudstream3.ui.settings.AccountClickCallback
import kotlinx.android.synthetic.main.repository_item.view.*
class RepoAdapter(
- private val repositories: Array,
- val clickCallback: (RepositoryData) -> Unit
+ var repositories: Array,
+ val clickCallback: RepoAdapter.(RepositoryData) -> Unit,
+ val imageClickCallback: RepoAdapter.(RepositoryData) -> Unit
) :
RecyclerView.Adapter() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
@@ -36,6 +37,11 @@ class RepoAdapter(
fun bind(
repositoryData: RepositoryData
) {
+ itemView.action_button?.setImageResource(R.drawable.ic_baseline_delete_outline_24)
+ itemView.action_button?.setOnClickListener {
+ imageClickCallback(repositoryData)
+ }
+
itemView.setOnClickListener {
clickCallback(repositoryData)
}