2022-05-06 19:17:02 +05:30
|
|
|
package com.github.libretube
|
|
|
|
|
|
|
|
import android.app.Dialog
|
2022-05-27 22:49:08 +05:30
|
|
|
import android.content.Context
|
2022-05-30 21:11:16 +05:30
|
|
|
import android.content.Intent
|
2022-05-06 19:17:02 +05:30
|
|
|
import android.os.Bundle
|
|
|
|
import android.widget.ArrayAdapter
|
2022-05-30 19:13:55 +05:30
|
|
|
import android.widget.Toast
|
2022-05-06 19:17:02 +05:30
|
|
|
import androidx.fragment.app.DialogFragment
|
2022-05-30 19:13:55 +05:30
|
|
|
import androidx.preference.PreferenceManager
|
2022-05-06 19:17:02 +05:30
|
|
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
|
|
|
|
2022-05-07 01:45:00 +05:30
|
|
|
/**
|
|
|
|
* Dialog with different options for a selected video.
|
|
|
|
*
|
2022-05-20 01:43:42 +05:30
|
|
|
* Needs the [videoId] to load the content from the right video.
|
2022-05-07 01:45:00 +05:30
|
|
|
*/
|
2022-05-27 22:49:08 +05:30
|
|
|
class VideoOptionsDialog(private val videoId: String, context: Context) : DialogFragment() {
|
2022-05-06 19:17:02 +05:30
|
|
|
/**
|
2022-05-07 01:45:00 +05:30
|
|
|
* List that stores the different menu options. In the future could be add more options here.
|
2022-05-06 19:17:02 +05:30
|
|
|
*/
|
2022-05-27 22:49:08 +05:30
|
|
|
private val list = listOf(
|
2022-05-27 23:55:20 +05:30
|
|
|
context.getString(R.string.playOnBackground),
|
2022-05-30 21:11:16 +05:30
|
|
|
context.getString(R.string.addToPlaylist),
|
|
|
|
context.getString(R.string.share)
|
2022-05-27 22:49:08 +05:30
|
|
|
)
|
2022-05-06 19:17:02 +05:30
|
|
|
|
2022-05-07 01:45:00 +05:30
|
|
|
/**
|
|
|
|
* Dialog that returns a [MaterialAlertDialogBuilder] showing a menu of options.
|
|
|
|
*/
|
2022-05-06 19:17:02 +05:30
|
|
|
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
|
|
|
return MaterialAlertDialogBuilder(requireContext())
|
2022-05-07 18:17:00 +05:30
|
|
|
.setNegativeButton(R.string.cancel) { dialog, _ ->
|
|
|
|
dialog.dismiss()
|
|
|
|
}
|
2022-05-06 19:17:02 +05:30
|
|
|
.setAdapter(
|
|
|
|
ArrayAdapter(
|
|
|
|
requireContext(),
|
|
|
|
R.layout.video_options_dialog_item,
|
|
|
|
list
|
|
|
|
)
|
2022-05-07 01:45:00 +05:30
|
|
|
) { dialog, which ->
|
2022-05-06 19:17:02 +05:30
|
|
|
// For now, this checks the position of the option with the position that is in the
|
|
|
|
// list. I don't like it, but we will do like this for now.
|
|
|
|
when (which) {
|
|
|
|
// This for example will be the "Background mode" option
|
2022-05-07 01:45:00 +05:30
|
|
|
0 -> {
|
2022-05-27 22:52:45 +05:30
|
|
|
BackgroundMode
|
|
|
|
.getInstance()
|
|
|
|
.playOnBackgroundMode(requireContext(), videoId, 0)
|
2022-05-06 19:17:02 +05:30
|
|
|
}
|
2022-05-27 23:55:20 +05:30
|
|
|
// Add Video to Playlist Dialog
|
|
|
|
1 -> {
|
2022-05-30 21:17:54 +05:30
|
|
|
val sharedPreferences = PreferenceManager
|
|
|
|
.getDefaultSharedPreferences(requireContext())
|
2022-05-30 19:13:55 +05:30
|
|
|
val token = sharedPreferences.getString("token", "")
|
|
|
|
if (token != "") {
|
|
|
|
val newFragment = AddtoPlaylistDialog()
|
2022-05-30 21:17:54 +05:30
|
|
|
val bundle = Bundle()
|
2022-05-30 19:13:55 +05:30
|
|
|
bundle.putString("videoId", videoId)
|
|
|
|
newFragment.arguments = bundle
|
|
|
|
newFragment.show(parentFragmentManager, "AddToPlaylist")
|
|
|
|
} else {
|
|
|
|
Toast.makeText(context, R.string.login_first, Toast.LENGTH_SHORT).show()
|
|
|
|
}
|
2022-05-27 23:55:20 +05:30
|
|
|
}
|
2022-05-30 21:11:16 +05:30
|
|
|
2 -> {
|
2022-05-30 21:17:54 +05:30
|
|
|
/* crashes
|
2022-05-30 21:11:16 +05:30
|
|
|
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(
|
|
|
|
getString(R.string.piped),
|
|
|
|
getString(R.string.instance),
|
|
|
|
getString(R.string.youtube)
|
|
|
|
)
|
|
|
|
MaterialAlertDialogBuilder(requireContext())
|
|
|
|
.setTitle(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"
|
|
|
|
}
|
|
|
|
dismiss()
|
|
|
|
val intent = Intent()
|
|
|
|
intent.action = Intent.ACTION_SEND
|
|
|
|
intent.putExtra(Intent.EXTRA_TEXT, url)
|
|
|
|
intent.type = "text/plain"
|
|
|
|
startActivity(Intent.createChooser(intent, "Share Url To:"))
|
|
|
|
}
|
|
|
|
.show()
|
|
|
|
*/
|
|
|
|
val intent = Intent()
|
|
|
|
intent.action = Intent.ACTION_SEND
|
2022-05-30 21:17:54 +05:30
|
|
|
intent.putExtra(
|
|
|
|
Intent.EXTRA_TEXT,
|
|
|
|
"https://piped.kavin.rocks/watch?v=$videoId"
|
|
|
|
)
|
2022-05-30 21:11:16 +05:30
|
|
|
intent.type = "text/plain"
|
|
|
|
startActivity(Intent.createChooser(intent, "Share Url To:"))
|
|
|
|
}
|
2022-05-06 19:17:02 +05:30
|
|
|
else -> {
|
2022-05-07 01:45:00 +05:30
|
|
|
dialog.dismiss()
|
2022-05-06 19:17:02 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.show()
|
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
const val TAG = "VideoOptionsDialog"
|
|
|
|
}
|
|
|
|
}
|