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

79 lines
3.0 KiB
Kotlin
Raw Normal View History

2022-06-03 00:40:16 +05:30
package com.github.libretube.dialogs
import android.app.Dialog
2022-05-27 22:49:08 +05:30
import android.content.Context
import android.os.Bundle
import android.widget.ArrayAdapter
2022-05-30 19:13:55 +05:30
import android.widget.Toast
import androidx.fragment.app.DialogFragment
2022-06-03 00:40:16 +05:30
import com.github.libretube.R
2022-07-01 20:25:21 +05:30
import com.github.libretube.util.BackgroundMode
2022-06-26 14:25:05 +05:30
import com.github.libretube.util.PreferenceHelper
import com.google.android.material.dialog.MaterialAlertDialogBuilder
/**
* Dialog with different options for a selected video.
*
* Needs the [videoId] to load the content from the right video.
*/
2022-05-27 22:49:08 +05:30
class VideoOptionsDialog(private val videoId: String, context: Context) : DialogFragment() {
/**
* List that stores the different menu options. In the future could be add more options here.
*/
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
)
/**
* Dialog that returns a [MaterialAlertDialogBuilder] showing a menu of options.
*/
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return MaterialAlertDialogBuilder(requireContext())
.setNegativeButton(R.string.cancel) { dialog, _ ->
dialog.dismiss()
}
.setAdapter(
ArrayAdapter(
requireContext(),
R.layout.video_options_dialog_item,
2022-06-20 23:19:12 +05:30
optionsList
)
2022-06-20 23:19:12 +05:30
) { _, which ->
// 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
0 -> {
2022-06-03 00:40:16 +05:30
BackgroundMode.getInstance()
2022-06-30 19:32:55 +05:30
.playOnBackgroundMode(requireContext(), videoId)
}
2022-05-27 23:55:20 +05:30
// Add Video to Playlist Dialog
1 -> {
2022-06-26 14:25:05 +05:30
val token = PreferenceHelper.getToken(requireContext())
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-05-30 21:11:16 +05:30
2 -> {
2022-06-21 01:17:35 +05:30
val shareDialog = ShareDialog(videoId, false)
// using parentFragmentManager is important here
shareDialog.show(parentFragmentManager, "ShareDialog")
2022-05-30 21:11:16 +05:30
}
}
}
.show()
}
companion object {
const val TAG = "VideoOptionsDialog"
}
}