2022-06-03 00:40:16 +05:30
|
|
|
package com.github.libretube.dialogs
|
2022-05-06 19:17:02 +05:30
|
|
|
|
|
|
|
import android.app.Dialog
|
2022-05-27 22:49:08 +05:30
|
|
|
import android.content.Context
|
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-06-03 00:40:16 +05:30
|
|
|
import com.github.libretube.R
|
2022-07-02 21:53:24 +05:30
|
|
|
import com.github.libretube.preferences.PreferenceHelper
|
2022-07-23 19:11:57 +05:30
|
|
|
import com.github.libretube.util.BackgroundHelper
|
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-07-19 01:12:50 +05:30
|
|
|
private val TAG = "VideoOptionsDialog"
|
|
|
|
|
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-06-20 23:19:12 +05:30
|
|
|
private val optionsList = 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-07-29 16:09:03 +05:30
|
|
|
.setNegativeButton(R.string.cancel, null)
|
2022-05-06 19:17:02 +05:30
|
|
|
.setAdapter(
|
|
|
|
ArrayAdapter(
|
|
|
|
requireContext(),
|
|
|
|
R.layout.video_options_dialog_item,
|
2022-06-20 23:19:12 +05:30
|
|
|
optionsList
|
2022-05-06 19:17:02 +05:30
|
|
|
)
|
2022-06-20 23:19:12 +05:30
|
|
|
) { _, which ->
|
2022-07-08 17:46:22 +05:30
|
|
|
when (optionsList[which]) {
|
2022-07-21 16:40:27 +05:30
|
|
|
// Start the background mode
|
2022-07-08 17:46:22 +05:30
|
|
|
context?.getString(R.string.playOnBackground) -> {
|
2022-07-23 19:11:57 +05:30
|
|
|
BackgroundHelper.playOnBackground(requireContext(), videoId)
|
2022-05-06 19:17:02 +05:30
|
|
|
}
|
2022-05-27 23:55:20 +05:30
|
|
|
// Add Video to Playlist Dialog
|
2022-07-08 17:46:22 +05:30
|
|
|
context?.getString(R.string.addToPlaylist) -> {
|
2022-07-17 20:49:55 +05:30
|
|
|
val token = PreferenceHelper.getToken()
|
2022-05-30 19:13:55 +05:30
|
|
|
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-07-08 17:46:22 +05:30
|
|
|
context?.getString(R.string.share) -> {
|
2022-06-21 01:17:35 +05:30
|
|
|
val shareDialog = ShareDialog(videoId, false)
|
2022-06-14 00:49:19 +05:30
|
|
|
// using parentFragmentManager is important here
|
|
|
|
shareDialog.show(parentFragmentManager, "ShareDialog")
|
2022-05-30 21:11:16 +05:30
|
|
|
}
|
2022-05-06 19:17:02 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
.show()
|
|
|
|
}
|
|
|
|
}
|