11package dev.typetype.android.core.ui.share
22
3+ import android.app.AlertDialog
4+ import android.content.Context
5+ import android.content.Intent
6+ import android.graphics.Color
7+ import android.graphics.drawable.ColorDrawable
8+ import android.view.Gravity
9+ import android.view.View
10+ import android.view.ViewGroup
11+ import android.widget.BaseAdapter
12+ import android.widget.ImageView
13+ import android.widget.LinearLayout
14+ import android.widget.TextView
315import androidx.compose.runtime.compositionLocalOf
16+ import dev.typetype.android.R
17+ import dev.typetype.android.domain.navigation.resolveIncomingVideoUrl
418import dev.typetype.android.domain.navigation.toPublicWatchParameter
519import java.net.URI
620import java.net.URLEncoder
@@ -19,6 +33,92 @@ fun buildShareUrl(serverBaseUrl: String?, videoUrl: String): String {
1933 return " $origin /watch?v=$encoded "
2034}
2135
36+ fun buildSourceShareUrl (videoUrl : String ): String =
37+ resolveIncomingVideoUrl(videoUrl) ? : videoUrl.trim()
38+
39+ fun showShareChooser (
40+ context : Context ,
41+ serverBaseUrl : String? ,
42+ videoUrl : String ,
43+ chooserTitle : String ,
44+ ) {
45+ val sourceUrl = buildSourceShareUrl(videoUrl)
46+ val typeTypeUrl = buildShareUrl(serverBaseUrl, videoUrl)
47+ val choices = buildList {
48+ add(
49+ ShareChoice (
50+ context.getString(R .string.video_menu_share_typetype),
51+ typeTypeUrl,
52+ android.R .drawable.ic_menu_share,
53+ ),
54+ )
55+ providerChoice(context, sourceUrl)?.takeIf { it.url != typeTypeUrl }?.let (::add)
56+ }
57+ AlertDialog .Builder (context)
58+ .setTitle(R .string.video_menu_share)
59+ .setAdapter(ShareChoiceAdapter (context, choices)) { _, which ->
60+ val intent = Intent (Intent .ACTION_SEND ).apply {
61+ type = " text/plain"
62+ putExtra(Intent .EXTRA_TEXT , choices[which].url)
63+ }
64+ context.startActivity(Intent .createChooser(intent, chooserTitle))
65+ }
66+ .show()
67+ }
68+
69+ private data class ShareChoice (val label : String , val url : String , val icon : Int )
70+
71+ private fun providerChoice (context : Context , sourceUrl : String ): ShareChoice ? {
72+ val provider = sourceUrl.lowercase()
73+ return when {
74+ " youtube.com" in provider || " youtu.be" in provider -> ShareChoice (
75+ context.getString(R .string.video_menu_share_source, " YouTube" ),
76+ sourceUrl,
77+ R .drawable.ic_service_youtube,
78+ )
79+ " nicovideo.jp" in provider || " nico.ms" in provider -> ShareChoice (
80+ context.getString(R .string.video_menu_share_source, " NicoNico" ),
81+ sourceUrl,
82+ R .drawable.ic_service_niconico,
83+ )
84+ " bilibili.com" in provider || " b23.tv" in provider -> ShareChoice (
85+ context.getString(R .string.video_menu_share_source, " BiliBili" ),
86+ sourceUrl,
87+ R .drawable.ic_service_bilibili,
88+ )
89+ else -> null
90+ }
91+ }
92+
93+ private class ShareChoiceAdapter (
94+ private val context : Context ,
95+ private val choices : List <ShareChoice >,
96+ ) : BaseAdapter() {
97+ override fun getCount (): Int = choices.size
98+ override fun getItem (position : Int ): ShareChoice = choices[position]
99+ override fun getItemId (position : Int ): Long = position.toLong()
100+
101+ override fun getView (position : Int , convertView : View ? , parent : ViewGroup ): View {
102+ val choice = getItem(position)
103+ val density = context.resources.displayMetrics.density
104+ val row = (convertView as ? LinearLayout ) ? : LinearLayout (context).apply {
105+ orientation = LinearLayout .HORIZONTAL
106+ gravity = Gravity .CENTER_VERTICAL
107+ background = ColorDrawable (Color .TRANSPARENT )
108+ val icon = ImageView (context)
109+ addView(icon, LinearLayout .LayoutParams ((28 * density).toInt(), (28 * density).toInt()))
110+ val label = TextView (context).apply {
111+ setTextAppearance(android.R .style.TextAppearance_Material_Body1 )
112+ }
113+ addView(label, LinearLayout .LayoutParams (0 , LinearLayout .LayoutParams .WRAP_CONTENT , 1f ))
114+ }
115+ row.setPadding((20 * density).toInt(), (12 * density).toInt(), (20 * density).toInt(), (12 * density).toInt())
116+ (row.getChildAt(0 ) as ImageView ).setImageResource(choice.icon)
117+ (row.getChildAt(1 ) as TextView ).text = choice.label
118+ return row
119+ }
120+ }
121+
22122fun buildImageUrl (serverBaseUrl : String? , imageUrl : String ): String {
23123 val source = imageUrl.trim().let { value ->
24124 if (value.startsWith(" httpss://" )) " https://${value.removePrefix(" httpss://" )} " else value
0 commit comments