From 30e33ba5c78d8224cd6aaa354b5e13a2c20065ef Mon Sep 17 00:00:00 2001 From: Enova Date: Fri, 14 Jun 2024 21:13:31 -0500 Subject: [PATCH 1/3] Enable embeds of any other video embed type. Also cleanup code a bit. --- .../betterplatformembeds/PlayableEmbeds.kt | 54 ++++++++++++++++--- 1 file changed, 47 insertions(+), 7 deletions(-) diff --git a/PlayableEmbeds/src/main/kotlin/dev/vendicated/aliucordplugins/betterplatformembeds/PlayableEmbeds.kt b/PlayableEmbeds/src/main/kotlin/dev/vendicated/aliucordplugins/betterplatformembeds/PlayableEmbeds.kt index 06f3e8c..2f6599a 100644 --- a/PlayableEmbeds/src/main/kotlin/dev/vendicated/aliucordplugins/betterplatformembeds/PlayableEmbeds.kt +++ b/PlayableEmbeds/src/main/kotlin/dev/vendicated/aliucordplugins/betterplatformembeds/PlayableEmbeds.kt @@ -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 @@ -50,10 +48,10 @@ class PlayableEmbeds : Plugin() { "(?:embed/|v/|watch\\?v=|watch\\?.+&v=|shorts/))((\\w|-){11})"+ "(?:(?:\\?|&)(?:star)?t=(\\d+))?(?:\\S+)?") - override fun start(_context: Context) { + override fun start(context: Context) { patcher.after("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 @@ -61,14 +59,56 @@ class PlayableEmbeds : Plugin() { 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(Utils.getResId("embed_image_container", "id")) + val chatListItemEmbedImage = cardView.findViewById(Utils.getResId("chat_list_item_embed_image", "id")) + val playButton = cardView.findViewById(Utils.getResId("chat_list_item_embed_image_icons", "id")) + playButton.visibility = View.GONE + chatListItemEmbedImage.visibility = View.GONE + val posterUrl = embed.image?.url + + 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 { + loadData( + """ + + + + + + """, + "text/html", + "UTF-8" + ) + } + } + private fun addYoutubeEmbed(layout: ViewGroup, url: String) { val ctx = layout.context From c51888819b019ac0a152a5f219b955bdda3d49fa Mon Sep 17 00:00:00 2001 From: Enova Date: Sun, 16 Jun 2024 23:08:01 -0500 Subject: [PATCH 2/3] Cache resource ids --- .../betterplatformembeds/PlayableEmbeds.kt | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/PlayableEmbeds/src/main/kotlin/dev/vendicated/aliucordplugins/betterplatformembeds/PlayableEmbeds.kt b/PlayableEmbeds/src/main/kotlin/dev/vendicated/aliucordplugins/betterplatformembeds/PlayableEmbeds.kt index 2f6599a..359d682 100644 --- a/PlayableEmbeds/src/main/kotlin/dev/vendicated/aliucordplugins/betterplatformembeds/PlayableEmbeds.kt +++ b/PlayableEmbeds/src/main/kotlin/dev/vendicated/aliucordplugins/betterplatformembeds/PlayableEmbeds.kt @@ -48,6 +48,11 @@ class PlayableEmbeds : Plugin() { "(?:embed/|v/|watch\\?v=|watch\\?.+&v=|shorts/))((\\w|-){11})"+ "(?:(?:\\?|&)(?:star)?t=(\\d+))?(?:\\S+)?") + 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("configureUI", WidgetChatListAdapterItemEmbed.Model::class.java) { val model = it.args[0] as WidgetChatListAdapterItemEmbed.Model @@ -73,9 +78,9 @@ class PlayableEmbeds : Plugin() { val videoUrl = embed.video?.url ?: return - val cardView = layout.findViewById(Utils.getResId("embed_image_container", "id")) - val chatListItemEmbedImage = cardView.findViewById(Utils.getResId("chat_list_item_embed_image", "id")) - val playButton = cardView.findViewById(Utils.getResId("chat_list_item_embed_image_icons", "id")) + val cardView = layout.findViewById(embedImageContainer) + val chatListItemEmbedImage = cardView.findViewById(chatListItemEmbedImage) + val playButton = cardView.findViewById(chatListItemEmbedImageIcons) playButton.visibility = View.GONE chatListItemEmbedImage.visibility = View.GONE val posterUrl = embed.image?.url @@ -114,9 +119,9 @@ class PlayableEmbeds : Plugin() { val (_, videoId, _, timestamp) = youtubeUrlRe.find(url, 0).groupValues - val cardView = layout.findViewById(Utils.getResId("embed_image_container", "id")) - val chatListItemEmbedImage = cardView.findViewById(Utils.getResId("chat_list_item_embed_image", "id")) - val playButton = cardView.findViewById(Utils.getResId("chat_list_item_embed_image_icons", "id")) + val cardView = layout.findViewById(embedImageContainer) + val chatListItemEmbedImage = cardView.findViewById(chatListItemEmbedImage) + val playButton = cardView.findViewById(chatListItemEmbedImageIcons) playButton.visibility = View.GONE chatListItemEmbedImage.visibility = View.GONE @@ -190,7 +195,7 @@ class PlayableEmbeds : Plugin() { @SuppressLint("SetJavaScriptEnabled") settings.javaScriptEnabled = true - val cardView = layout.findViewById(Utils.getResId("chat_list_item_embed_container_card", "id")) + val cardView = layout.findViewById(chatListItemEmbedContainerCard) cardView.addView(this) } From 472b04fdcd2572160684a7e77a3f370b8ef96645 Mon Sep 17 00:00:00 2001 From: Enova Date: Sun, 16 Jun 2024 23:27:33 -0500 Subject: [PATCH 3/3] Get actual poster url and don't preload if it's found --- .../aliucordplugins/betterplatformembeds/PlayableEmbeds.kt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/PlayableEmbeds/src/main/kotlin/dev/vendicated/aliucordplugins/betterplatformembeds/PlayableEmbeds.kt b/PlayableEmbeds/src/main/kotlin/dev/vendicated/aliucordplugins/betterplatformembeds/PlayableEmbeds.kt index 359d682..8b933af 100644 --- a/PlayableEmbeds/src/main/kotlin/dev/vendicated/aliucordplugins/betterplatformembeds/PlayableEmbeds.kt +++ b/PlayableEmbeds/src/main/kotlin/dev/vendicated/aliucordplugins/betterplatformembeds/PlayableEmbeds.kt @@ -83,7 +83,7 @@ class PlayableEmbeds : Plugin() { val playButton = cardView.findViewById(chatListItemEmbedImageIcons) playButton.visibility = View.GONE chatListItemEmbedImage.visibility = View.GONE - val posterUrl = embed.image?.url + val posterUrl = embed.thumbnail?.proxyUrl val webView = ScrollableWebView(ctx).apply { id = widgetId @@ -98,11 +98,12 @@ class PlayableEmbeds : Plugin() { webviewMap[webView] = embed.url webView.run { + val needsPreload = if (posterUrl != null) "none" else "metadata" loadData( """ -