LibreTube/app/src/main/java/com/github/libretube/dialogs/ShareDialog.kt

49 lines
2.0 KiB
Kotlin
Raw Normal View History

2022-06-03 01:10:36 +05:30
package com.github.libretube.dialogs
2022-06-05 14:22:35 +05:30
import android.app.Dialog
2022-06-03 01:10:36 +05:30
import android.content.Intent
2022-06-05 14:22:35 +05:30
import android.os.Bundle
import androidx.fragment.app.DialogFragment
2022-06-03 01:10:36 +05:30
import androidx.preference.PreferenceManager
import com.github.libretube.R
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import java.net.URLEncoder
2022-06-05 14:22:35 +05:30
class ShareDialog(private val videoId: String) : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity?.let {
val sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(requireContext())
val instancePref = sharedPreferences.getString(
"instance",
"https://pipedapi.kavin.rocks"
)!!
val instance = "&instance=${URLEncoder.encode(instancePref, "UTF-8")}"
val shareOptions = arrayOf(
context?.getString(R.string.piped),
context?.getString(R.string.instance),
context?.getString(R.string.youtube)
)
MaterialAlertDialogBuilder(requireContext())
.setTitle(context?.getString(R.string.share))
.setItems(
shareOptions
) { _, id ->
val url = when (id) {
0 -> "https://piped.kavin.rocks/watch?v=$videoId"
1 -> "https://piped.kavin.rocks/watch?v=$videoId$instance"
2 -> "https://youtu.be/$videoId"
else -> "https://piped.kavin.rocks/watch?v=$videoId"
}
val intent = Intent()
intent.action = Intent.ACTION_SEND
intent.putExtra(Intent.EXTRA_TEXT, url)
intent.type = "text/plain"
2022-06-09 17:52:07 +05:30
context?.startActivity(Intent.createChooser(intent, context?.getString(R.string.shareTo)))
2022-06-05 14:22:35 +05:30
}
.show()
} ?: throw IllegalStateException("Activity cannot be null")
}
2022-06-03 01:10:36 +05:30
}