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..8b933af 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,15 @@ 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("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,22 +64,65 @@ 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(embedImageContainer) + val chatListItemEmbedImage = cardView.findViewById(chatListItemEmbedImage) + val playButton = cardView.findViewById(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( + """ + + + + + + """, + "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(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 @@ -150,7 +196,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) }