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

81 lines
3.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
2022-06-05 14:22:35 +05:30
class ShareDialog(private val videoId: String) : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity?.let {
2022-06-18 14:44:46 +05:30
var shareOptions = arrayOf(
getString(R.string.piped),
getString(R.string.youtube)
2022-06-05 14:22:35 +05:30
)
2022-06-18 14:44:46 +05:30
val instanceUrl = getCustomInstanceFrontendUrl()
// add instanceUrl option if custom instance frontend url available
if (instanceUrl != "") shareOptions += getString(R.string.instance)
2022-06-05 14:22:35 +05:30
MaterialAlertDialogBuilder(requireContext())
.setTitle(context?.getString(R.string.share))
.setItems(
shareOptions
) { _, id ->
val url = when (id) {
0 -> "https://piped.kavin.rocks/watch?v=$videoId"
2022-06-18 14:44:46 +05:30
1 -> "https://youtu.be/$videoId"
2 -> "$instanceUrl/watch?v=$videoId" // only available for custom instances
2022-06-05 14:22:35 +05:30
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:54:26 +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-18 14:44:46 +05:30
// get the frontend url if it's a custom instance
private fun getCustomInstanceFrontendUrl(): String {
val sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(requireContext())
val instancePref = sharedPreferences.getString(
"selectInstance",
"https://pipedapi.kavin.rocks"
)
// get the api urls of the other custom instances
var customInstancesUrls = try {
sharedPreferences
.getStringSet("custom_instances_url", HashSet())!!.toList()
} catch (e: Exception) {
emptyList()
}
// get the frontend urls of the other custom instances
var customInstancesFrontendUrls = try {
sharedPreferences
.getStringSet("custom_instances_url", HashSet())!!.toList()
} catch (e: Exception) {
emptyList()
}
// return the custom instance frontend url if available
return if (customInstancesUrls.contains(instancePref)) {
val index = customInstancesUrls.indexOf(instancePref)
return customInstancesFrontendUrls[index]
} else {
""
}
}
2022-06-03 01:10:36 +05:30
}