Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable embeds of any other video embed type #81

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ import com.aliucord.Utils
import com.aliucord.annotations.AliucordPlugin
import com.aliucord.entities.Plugin
import com.aliucord.patcher.*
import com.aliucord.wrappers.embeds.MessageEmbedWrapper.Companion.rawProvider
import com.aliucord.wrappers.embeds.MessageEmbedWrapper.Companion.url
import com.aliucord.wrappers.embeds.ProviderWrapper.Companion.name
import com.aliucord.wrappers.embeds.MessageEmbedWrapper
import com.discord.widgets.chat.list.adapter.WidgetChatListAdapterItemEmbed
import com.facebook.drawee.view.SimpleDraweeView
import com.google.android.material.card.MaterialCardView
Expand All @@ -50,33 +48,81 @@ class PlayableEmbeds : Plugin() {
"(?:embed/|v/|watch\\?v=|watch\\?.+&v=|shorts/))((\\w|-){11})"+
"(?:(?:\\?|&)(?:star)?t=(\\d+))?(?:\\S+)?")

override fun start(_context: Context) {
private val embedImageContainer = Utils.getResId("embed_image_container", "id")
private val chatListItemEmbedImage = Utils.getResId("chat_list_item_embed_image", "id")
private val chatListItemEmbedImageIcons = Utils.getResId("chat_list_item_embed_image_icons", "id")
private val chatListItemEmbedContainerCard = Utils.getResId("chat_list_item_embed_container_card", "id")

override fun start(context: Context) {
patcher.after<WidgetChatListAdapterItemEmbed>("configureUI", WidgetChatListAdapterItemEmbed.Model::class.java) {
val model = it.args[0] as WidgetChatListAdapterItemEmbed.Model
val embed = model.embedEntry.embed
val embed = MessageEmbedWrapper(model.embedEntry.embed)
val holder = it.thisObject as WidgetChatListAdapterItemEmbed
val layout = holder.itemView as ConstraintLayout

layout.findViewById<WebView?>(widgetId)?.let { v ->
if (webviewMap[v] == embed.url) return@after
(v.parent as ViewGroup).removeView(v)
}
val url = embed.url ?: return@after;
when (embed.rawProvider?.name) {
val url = embed.url ?: return@after
when (embed.provider?.name) {
"YouTube" -> addYoutubeEmbed(layout, url)
"Spotify" -> addSpotifyEmbed(layout, url)
else -> addDefaultEmbed(layout, embed)
}
}
}

private fun addDefaultEmbed(layout: ViewGroup, embed: MessageEmbedWrapper) {
val ctx = layout.context

val videoUrl = embed.video?.url ?: return

val cardView = layout.findViewById<CardView>(embedImageContainer)
val chatListItemEmbedImage = cardView.findViewById<SimpleDraweeView>(chatListItemEmbedImage)
val playButton = cardView.findViewById<View>(chatListItemEmbedImageIcons)
playButton.visibility = View.GONE
chatListItemEmbedImage.visibility = View.GONE
val posterUrl = embed.thumbnail?.proxyUrl

val webView = ScrollableWebView(ctx).apply {
id = widgetId
setBackgroundColor(Color.TRANSPARENT)
layoutParams = LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT)

@SuppressLint("SetJavaScriptEnabled")
settings.javaScriptEnabled = false

cardView.addView(this)
}

webviewMap[webView] = embed.url
webView.run {
val needsPreload = if (posterUrl != null) "none" else "metadata"
loadData(
"""
<html>
<body style="margin: 0; padding: 0;">
<video poster="$posterUrl" preload="$needsPreload" autoplay="false" controls style="width: 100%">
<source src="$videoUrl" type="video/mp4">
</video>
</body>
</html>
""",
"text/html",
"UTF-8"
)
}
}

private fun addYoutubeEmbed(layout: ViewGroup, url: String) {
val ctx = layout.context

val (_, videoId, _, timestamp) = youtubeUrlRe.find(url, 0).groupValues

val cardView = layout.findViewById<CardView>(Utils.getResId("embed_image_container", "id"))
val chatListItemEmbedImage = cardView.findViewById<SimpleDraweeView>(Utils.getResId("chat_list_item_embed_image", "id"))
val playButton = cardView.findViewById<View>(Utils.getResId("chat_list_item_embed_image_icons", "id"))
val cardView = layout.findViewById<CardView>(embedImageContainer)
val chatListItemEmbedImage = cardView.findViewById<SimpleDraweeView>(chatListItemEmbedImage)
val playButton = cardView.findViewById<View>(chatListItemEmbedImageIcons)
playButton.visibility = View.GONE
chatListItemEmbedImage.visibility = View.GONE

Expand Down Expand Up @@ -150,7 +196,7 @@ class PlayableEmbeds : Plugin() {
@SuppressLint("SetJavaScriptEnabled")
settings.javaScriptEnabled = true

val cardView = layout.findViewById<MaterialCardView>(Utils.getResId("chat_list_item_embed_container_card", "id"))
val cardView = layout.findViewById<MaterialCardView>(chatListItemEmbedContainerCard)
cardView.addView(this)
}

Expand Down