refactor video options to bottom sheet

This commit is contained in:
Bnyro 2022-09-11 18:20:01 +02:00
parent 3647f1ed1c
commit 17df302c10
11 changed files with 122 additions and 132 deletions

View File

@ -6,7 +6,7 @@ import android.view.ViewGroup
import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.databinding.VideoRowBinding import com.github.libretube.databinding.VideoRowBinding
import com.github.libretube.dialogs.VideoOptionsDialog import com.github.libretube.sheets.VideoOptionsBottomSheet
import com.github.libretube.extensions.formatShort import com.github.libretube.extensions.formatShort
import com.github.libretube.extensions.setWatchProgressLength import com.github.libretube.extensions.setWatchProgressLength
import com.github.libretube.extensions.toID import com.github.libretube.extensions.toID
@ -51,8 +51,8 @@ class ChannelAdapter(
} }
val videoId = trending.url!!.toID() val videoId = trending.url!!.toID()
root.setOnLongClickListener { root.setOnLongClickListener {
VideoOptionsDialog(videoId) VideoOptionsBottomSheet(videoId)
.show(childFragmentManager, VideoOptionsDialog::class.java.name) .show(childFragmentManager, VideoOptionsBottomSheet::class.java.name)
true true
} }
watchProgress.setWatchProgressLength(videoId, trending.duration!!) watchProgress.setWatchProgressLength(videoId, trending.duration!!)

View File

@ -9,7 +9,7 @@ import androidx.fragment.app.FragmentManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.api.RetrofitInstance import com.github.libretube.api.RetrofitInstance
import com.github.libretube.databinding.PlaylistRowBinding import com.github.libretube.databinding.PlaylistRowBinding
import com.github.libretube.dialogs.VideoOptionsDialog import com.github.libretube.sheets.VideoOptionsBottomSheet
import com.github.libretube.extensions.TAG import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.setFormattedDuration import com.github.libretube.extensions.setFormattedDuration
import com.github.libretube.extensions.setWatchProgressLength import com.github.libretube.extensions.setWatchProgressLength
@ -60,8 +60,8 @@ class PlaylistAdapter(
} }
val videoId = streamItem.url!!.toID() val videoId = streamItem.url!!.toID()
root.setOnLongClickListener { root.setOnLongClickListener {
VideoOptionsDialog(videoId) VideoOptionsBottomSheet(videoId)
.show(childFragmentManager, VideoOptionsDialog::class.java.name) .show(childFragmentManager, VideoOptionsBottomSheet::class.java.name)
true true
} }

View File

@ -9,7 +9,7 @@ import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.api.RetrofitInstance import com.github.libretube.api.RetrofitInstance
import com.github.libretube.databinding.PlaylistsRowBinding import com.github.libretube.databinding.PlaylistsRowBinding
import com.github.libretube.dialogs.PlaylistOptionsDialog import com.github.libretube.sheets.PlaylistOptionsDialog
import com.github.libretube.extensions.TAG import com.github.libretube.extensions.TAG
import com.github.libretube.obj.PlaylistId import com.github.libretube.obj.PlaylistId
import com.github.libretube.obj.Playlists import com.github.libretube.obj.Playlists

View File

@ -10,8 +10,8 @@ import com.github.libretube.api.SubscriptionHelper
import com.github.libretube.databinding.ChannelRowBinding import com.github.libretube.databinding.ChannelRowBinding
import com.github.libretube.databinding.PlaylistSearchRowBinding import com.github.libretube.databinding.PlaylistSearchRowBinding
import com.github.libretube.databinding.VideoRowBinding import com.github.libretube.databinding.VideoRowBinding
import com.github.libretube.dialogs.PlaylistOptionsDialog import com.github.libretube.sheets.PlaylistOptionsDialog
import com.github.libretube.dialogs.VideoOptionsDialog import com.github.libretube.sheets.VideoOptionsBottomSheet
import com.github.libretube.extensions.formatShort import com.github.libretube.extensions.formatShort
import com.github.libretube.extensions.setFormattedDuration import com.github.libretube.extensions.setFormattedDuration
import com.github.libretube.extensions.setWatchProgressLength import com.github.libretube.extensions.setWatchProgressLength
@ -99,8 +99,8 @@ class SearchAdapter(
} }
val videoId = item.url!!.toID() val videoId = item.url!!.toID()
root.setOnLongClickListener { root.setOnLongClickListener {
VideoOptionsDialog(videoId) VideoOptionsBottomSheet(videoId)
.show(childFragmentManager, VideoOptionsDialog::class.java.name) .show(childFragmentManager, VideoOptionsBottomSheet::class.java.name)
true true
} }
channelImage.setOnClickListener { channelImage.setOnClickListener {

View File

@ -6,7 +6,7 @@ import android.view.ViewGroup
import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.databinding.TrendingRowBinding import com.github.libretube.databinding.TrendingRowBinding
import com.github.libretube.dialogs.VideoOptionsDialog import com.github.libretube.sheets.VideoOptionsBottomSheet
import com.github.libretube.extensions.formatShort import com.github.libretube.extensions.formatShort
import com.github.libretube.extensions.setFormattedDuration import com.github.libretube.extensions.setFormattedDuration
import com.github.libretube.extensions.setWatchProgressLength import com.github.libretube.extensions.setWatchProgressLength
@ -63,8 +63,8 @@ class TrendingAdapter(
} }
val videoId = trending.url!!.toID() val videoId = trending.url!!.toID()
root.setOnLongClickListener { root.setOnLongClickListener {
VideoOptionsDialog(videoId) VideoOptionsBottomSheet(videoId)
.show(childFragmentManager, VideoOptionsDialog::class.java.name) .show(childFragmentManager, VideoOptionsBottomSheet::class.java.name)
true true
} }
watchProgress.setWatchProgressLength(videoId, trending.duration!!) watchProgress.setWatchProgressLength(videoId, trending.duration!!)

View File

@ -7,7 +7,7 @@ import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.databinding.WatchHistoryRowBinding import com.github.libretube.databinding.WatchHistoryRowBinding
import com.github.libretube.db.DatabaseHelper import com.github.libretube.db.DatabaseHelper
import com.github.libretube.db.obj.WatchHistoryItem import com.github.libretube.db.obj.WatchHistoryItem
import com.github.libretube.dialogs.VideoOptionsDialog import com.github.libretube.sheets.VideoOptionsBottomSheet
import com.github.libretube.extensions.setFormattedDuration import com.github.libretube.extensions.setFormattedDuration
import com.github.libretube.extensions.setWatchProgressLength import com.github.libretube.extensions.setWatchProgressLength
import com.github.libretube.util.ImageHelper import com.github.libretube.util.ImageHelper
@ -53,8 +53,8 @@ class WatchHistoryAdapter(
NavigationHelper.navigateVideo(root.context, video.videoId) NavigationHelper.navigateVideo(root.context, video.videoId)
} }
root.setOnLongClickListener { root.setOnLongClickListener {
VideoOptionsDialog(video.videoId) VideoOptionsBottomSheet(video.videoId)
.show(childFragmentManager, VideoOptionsDialog::class.java.name) .show(childFragmentManager, VideoOptionsBottomSheet::class.java.name)
true true
} }

View File

@ -1,112 +0,0 @@
package com.github.libretube.dialogs
import android.app.Dialog
import android.app.NotificationManager
import android.content.Context
import android.os.Build
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.Toast
import androidx.fragment.app.DialogFragment
import com.github.libretube.Globals
import com.github.libretube.R
import com.github.libretube.constants.IntentData
import com.github.libretube.constants.PLAYER_NOTIFICATION_ID
import com.github.libretube.util.BackgroundHelper
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.
*/
class VideoOptionsDialog(
private val videoId: String
) : DialogFragment() {
/**
* Dialog that returns a [MaterialAlertDialogBuilder] showing a menu of options.
*/
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
/**
* List that stores the different menu options. In the future could be add more options here.
*/
val optionsList = mutableListOf(
context?.getString(R.string.playOnBackground),
context?.getString(R.string.addToPlaylist),
context?.getString(R.string.download),
context?.getString(R.string.share)
)
// remove the add to playlist option if not logged in
if (PreferenceHelper.getToken() == "") {
optionsList.remove(
context?.getString(R.string.addToPlaylist)
)
}
/**
* Check whether the player is running by observing the notification
*/
try {
val notificationManager =
context?.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
notificationManager.activeNotifications.forEach {
if (it.id == PLAYER_NOTIFICATION_ID) {
optionsList += context?.getString(R.string.add_to_queue)
}
}
}
} catch (e: Exception) {
e.printStackTrace()
}
return MaterialAlertDialogBuilder(requireContext())
.setNegativeButton(R.string.cancel, null)
.setAdapter(
ArrayAdapter(
requireContext(),
R.layout.video_options_dialog_item,
optionsList
)
) { _, which ->
when (optionsList[which]) {
// Start the background mode
context?.getString(R.string.playOnBackground) -> {
BackgroundHelper.playOnBackground(requireContext(), videoId)
}
// Add Video to Playlist Dialog
context?.getString(R.string.addToPlaylist) -> {
val token = PreferenceHelper.getToken()
if (token != "") {
val newFragment = AddToPlaylistDialog()
val bundle = Bundle()
bundle.putString(IntentData.videoId, videoId)
newFragment.arguments = bundle
newFragment.show(
parentFragmentManager,
AddToPlaylistDialog::class.java.name
)
} else {
Toast.makeText(context, R.string.login_first, Toast.LENGTH_SHORT).show()
}
}
context?.getString(R.string.download) -> {
val downloadDialog = DownloadDialog(videoId)
downloadDialog.show(parentFragmentManager, DownloadDialog::class.java.name)
}
context?.getString(R.string.share) -> {
val shareDialog = ShareDialog(videoId, false)
// using parentFragmentManager is important here
shareDialog.show(parentFragmentManager, ShareDialog::class.java.name)
}
context?.getString(R.string.add_to_queue) -> {
Globals.playingQueue += videoId
}
}
}
.show()
}
}

View File

@ -14,7 +14,7 @@ import com.github.libretube.adapters.PlaylistAdapter
import com.github.libretube.api.RetrofitInstance import com.github.libretube.api.RetrofitInstance
import com.github.libretube.constants.IntentData import com.github.libretube.constants.IntentData
import com.github.libretube.databinding.FragmentPlaylistBinding import com.github.libretube.databinding.FragmentPlaylistBinding
import com.github.libretube.dialogs.PlaylistOptionsDialog import com.github.libretube.sheets.PlaylistOptionsDialog
import com.github.libretube.extensions.BaseFragment import com.github.libretube.extensions.BaseFragment
import com.github.libretube.extensions.TAG import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.toID import com.github.libretube.extensions.toID

View File

@ -1,4 +1,4 @@
package com.github.libretube.dialogs package com.github.libretube.sheets
import android.app.Dialog import android.app.Dialog
import android.os.Bundle import android.os.Bundle
@ -10,6 +10,7 @@ import androidx.fragment.app.DialogFragment
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.api.RetrofitInstance import com.github.libretube.api.RetrofitInstance
import com.github.libretube.databinding.DialogTextPreferenceBinding import com.github.libretube.databinding.DialogTextPreferenceBinding
import com.github.libretube.dialogs.ShareDialog
import com.github.libretube.extensions.TAG import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.toID import com.github.libretube.extensions.toID
import com.github.libretube.obj.PlaylistId import com.github.libretube.obj.PlaylistId

View File

@ -0,0 +1,101 @@
package com.github.libretube.sheets
import android.app.NotificationManager
import android.content.Context
import android.os.Build
import android.os.Bundle
import android.widget.Toast
import com.github.libretube.Globals
import com.github.libretube.R
import com.github.libretube.constants.IntentData
import com.github.libretube.constants.PLAYER_NOTIFICATION_ID
import com.github.libretube.dialogs.AddToPlaylistDialog
import com.github.libretube.dialogs.DownloadDialog
import com.github.libretube.dialogs.ShareDialog
import com.github.libretube.util.BackgroundHelper
import com.github.libretube.util.PreferenceHelper
import com.github.libretube.views.BottomSheet
/**
* Dialog with different options for a selected video.
*
* Needs the [videoId] to load the content from the right video.
*/
class VideoOptionsBottomSheet(
private val videoId: String
) : BottomSheet() {
override fun onCreate(savedInstanceState: Bundle?) {
// List that stores the different menu options. In the future could be add more options here.
val optionsList = mutableListOf(
context?.getString(R.string.playOnBackground)!!,
context?.getString(R.string.addToPlaylist)!!,
context?.getString(R.string.download)!!,
context?.getString(R.string.share)!!
)
// remove the add to playlist option if not logged in
if (PreferenceHelper.getToken() == "") {
optionsList.remove(
context?.getString(R.string.addToPlaylist)
)
}
/**
* Check whether the player is running by observing the notification
*/
try {
val notificationManager =
context?.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
notificationManager.activeNotifications.forEach {
if (it.id == PLAYER_NOTIFICATION_ID) {
optionsList += context?.getString(R.string.add_to_queue)!!
}
}
}
} catch (e: Exception) {
e.printStackTrace()
}
setSimpleItems(optionsList) { which ->
when (optionsList[which]) {
// Start the background mode
context?.getString(R.string.playOnBackground) -> {
BackgroundHelper.playOnBackground(requireContext(), videoId)
}
// Add Video to Playlist Dialog
context?.getString(R.string.addToPlaylist) -> {
val token = PreferenceHelper.getToken()
if (token != "") {
val newFragment = AddToPlaylistDialog()
val bundle = Bundle()
bundle.putString(IntentData.videoId, videoId)
newFragment.arguments = bundle
newFragment.show(
parentFragmentManager,
AddToPlaylistDialog::class.java.name
)
} else {
Toast.makeText(context, R.string.login_first, Toast.LENGTH_SHORT).show()
}
}
context?.getString(R.string.download) -> {
val downloadDialog = DownloadDialog(videoId)
downloadDialog.show(parentFragmentManager, DownloadDialog::class.java.name)
}
context?.getString(R.string.share) -> {
val shareDialog = ShareDialog(videoId, false)
// using parentFragmentManager is important here
shareDialog.show(parentFragmentManager, ShareDialog::class.java.name)
}
context?.getString(R.string.add_to_queue) -> {
Globals.playingQueue += videoId
}
}
}
super.onCreate(savedInstanceState)
}
}

View File

@ -12,7 +12,7 @@ import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment import com.google.android.material.bottomsheet.BottomSheetDialogFragment
class BottomSheet : BottomSheetDialogFragment() { open class BottomSheet : BottomSheetDialogFragment() {
private lateinit var items: List<BottomSheetItem> private lateinit var items: List<BottomSheetItem>
private lateinit var listener: (index: Int) -> Unit private lateinit var listener: (index: Int) -> Unit
private lateinit var binding: BottomSheetBinding private lateinit var binding: BottomSheetBinding