only allow voting on installed extensions

This commit is contained in:
Cloudburst 2022-09-10 19:21:01 +02:00
parent 9431cde2f9
commit 74e3af0dbe
7 changed files with 71 additions and 10 deletions

View file

@ -339,9 +339,7 @@ object PluginManager {
}
plugins[filePath] = pluginInstance
classLoaders[loader] = pluginInstance
if (data.url != null) { // TODO: make this cleaner
urlPlugins[data.url] = pluginInstance
}
urlPlugins[data.url ?: filePath] = pluginInstance
pluginInstance.load(activity)
Log.i(TAG, "Loaded plugin ${data.internalName} successfully")
currentlyLoading = null
@ -382,6 +380,7 @@ object PluginManager {
classLoaders.values.removeIf { v -> v == plugin }
plugins.remove(absolutePath)
urlPlugins.values.removeIf { v -> v == plugin }
}
/**

View file

@ -1,11 +1,15 @@
package com.lagradost.cloudstream3.plugins
import android.util.Log
import android.widget.Toast
import com.lagradost.cloudstream3.AcraApplication.Companion.context
import com.lagradost.cloudstream3.AcraApplication.Companion.getKey
import com.lagradost.cloudstream3.AcraApplication.Companion.setKey
import com.lagradost.cloudstream3.R
import java.security.MessageDigest
import com.lagradost.cloudstream3.app
import com.lagradost.cloudstream3.utils.Coroutines.ioSafe
import com.lagradost.cloudstream3.utils.Coroutines.main
object VotingApi { // please do not cheat the votes lol
private const val LOGKEY = "VotingApi"
@ -33,10 +37,13 @@ object VotingApi { // please do not cheat the votes lol
}
fun SitePlugin.getVoteType(): VoteType {
if (repositoryUrl == null) return VoteType.NONE
return getVoteType(url)
}
fun SitePlugin.canVote(): Boolean {
return canVote(this.url)
}
// Plugin url to Int
private val votesCache = mutableMapOf<String, Int>()
@ -62,7 +69,18 @@ object VotingApi { // please do not cheat the votes lol
app.get(url)
}
fun canVote(pluginUrl: String): Boolean {
if (!PluginManager.urlPlugins.contains(pluginUrl)) return false
return true
}
suspend fun vote(pluginUrl: String, requestType: VoteType): Int {
if (!canVote(pluginUrl)) {
main {
Toast.makeText(context, R.string.extension_install_first, Toast.LENGTH_SHORT).show()
}
return getVotes(pluginUrl)
}
val savedType: VoteType = getKey("cs3-votes/${transformUrl(pluginUrl)}") ?: VoteType.NONE
var newType: VoteType = requestType
var changeValue = 0

View file

@ -183,7 +183,6 @@ class PluginAdapter(
}"
)
}
}
} else {
itemView.action_settings?.isVisible = false
@ -217,10 +216,8 @@ class PluginAdapter(
itemView.lang_icon.text = "${getFlagFromIso(metadata.language)} ${fromTwoLettersToLanguage(metadata.language)}"
}
if (isLocal) {
itemView.ext_votes?.isVisible = false
} else {
itemView.ext_votes?.isVisible = false
itemView.ext_votes?.isVisible = false
if (!isLocal) {
ioSafe {
metadata.getVotes().main {
itemView.ext_votes?.setText(txt(R.string.extension_rating, prettyCount(it)))

View file

@ -11,6 +11,8 @@ import com.lagradost.cloudstream3.utils.UIHelper.setImage
import com.lagradost.cloudstream3.utils.UIHelper.toPx
import kotlinx.android.synthetic.main.fragment_plugin_details.*
import android.text.format.Formatter.formatFileSize
import android.util.Log
import androidx.core.view.isVisible
import com.lagradost.cloudstream3.plugins.VotingApi
import com.lagradost.cloudstream3.plugins.VotingApi.getVoteType
import com.lagradost.cloudstream3.plugins.VotingApi.getVotes
@ -19,9 +21,11 @@ import com.lagradost.cloudstream3.utils.Coroutines.ioSafe
import com.lagradost.cloudstream3.utils.Coroutines.main
import com.lagradost.cloudstream3.utils.UIHelper.colorFromAttribute
import com.lagradost.cloudstream3.AcraApplication.Companion.openBrowser
import com.lagradost.cloudstream3.utils.SubtitleHelper
import com.lagradost.cloudstream3.plugins.PluginManager
import com.lagradost.cloudstream3.plugins.VotingApi.canVote
import com.lagradost.cloudstream3.utils.SubtitleHelper.fromTwoLettersToLanguage
import com.lagradost.cloudstream3.utils.SubtitleHelper.getFlagFromIso
import kotlinx.android.synthetic.main.repository_item.view.*
class PluginDetailsFragment(val data: PluginViewData) : BottomSheetDialogFragment() {
@ -80,7 +84,35 @@ class PluginDetailsFragment(val data: PluginViewData) : BottomSheetDialogFragmen
if (metadata.repositoryUrl != null) {
openBrowser(metadata.repositoryUrl)
}
}
if (!metadata.canVote()) {
downvote.alpha = .6f
upvote.alpha = .6f
}
if (data.isDownloaded) {
// On local plugins page the filepath is provided instead of url.
val plugin = PluginManager.urlPlugins[metadata.url] ?: PluginManager.plugins[metadata.url]
if (plugin?.openSettings != null && context != null) {
action_settings?.isVisible = true
action_settings.setOnClickListener {
try {
plugin.openSettings!!.invoke(requireContext())
} catch (e: Throwable) {
Log.e(
"PluginAdapter",
"Failed to open ${metadata.name} settings: ${
Log.getStackTraceString(e)
}"
)
}
}
} else {
action_settings?.isVisible = false
}
} else {
action_settings?.isVisible = false
}
upvote.setOnClickListener {

View file

@ -43,11 +43,24 @@
android:textStyle="normal"
tools:text="Hello world" />
<ImageView
android:id="@+id/action_settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="16dp"
android:background="?attr/selectableItemBackgroundBorderless"
android:contentDescription="@string/title_settings"
android:visibility="gone"
app:srcCompat="@drawable/ic_baseline_tune_24"
tools:visibility="visible" />
<ImageView
android:id="@+id/github_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|end"
android:layout_marginStart="16dp"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="@drawable/ic_github_logo" />
</LinearLayout>

View file

@ -454,4 +454,5 @@
<string name="extension_authors">Autorzy</string>
<string name="extension_types">Wspierane</string>
<string name="extension_language">Język</string>
<string name="extension_install_first">Najpierw zainstaluj rozszerzenie</string>
</resources>

View file

@ -624,6 +624,7 @@
<string name="extension_authors">Authors</string>
<string name="extension_types">Supported</string>
<string name="extension_language">Language</string>
<string name="extension_install_first">Install the extension first</string>
<string name="hls_playlist">HLS Playlist</string>
</resources>