From 17df302c10385f4f0ec72b88790fa7e25ef289a9 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sun, 11 Sep 2022 18:20:01 +0200 Subject: [PATCH] refactor video options to bottom sheet --- .../libretube/adapters/ChannelAdapter.kt | 6 +- .../libretube/adapters/PlaylistAdapter.kt | 6 +- .../libretube/adapters/PlaylistsAdapter.kt | 2 +- .../libretube/adapters/SearchAdapter.kt | 8 +- .../libretube/adapters/TrendingAdapter.kt | 6 +- .../libretube/adapters/WatchHistoryAdapter.kt | 6 +- .../libretube/dialogs/VideoOptionsDialog.kt | 112 ------------------ .../libretube/fragments/PlaylistFragment.kt | 2 +- .../PlaylistOptionsDialog.kt | 3 +- .../sheets/VideoOptionsBottomSheet.kt | 101 ++++++++++++++++ .../com/github/libretube/views/BottomSheet.kt | 2 +- 11 files changed, 122 insertions(+), 132 deletions(-) delete mode 100644 app/src/main/java/com/github/libretube/dialogs/VideoOptionsDialog.kt rename app/src/main/java/com/github/libretube/{dialogs => sheets}/PlaylistOptionsDialog.kt (98%) create mode 100644 app/src/main/java/com/github/libretube/sheets/VideoOptionsBottomSheet.kt diff --git a/app/src/main/java/com/github/libretube/adapters/ChannelAdapter.kt b/app/src/main/java/com/github/libretube/adapters/ChannelAdapter.kt index dcefa6541..c09a7ba1a 100644 --- a/app/src/main/java/com/github/libretube/adapters/ChannelAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/ChannelAdapter.kt @@ -6,7 +6,7 @@ import android.view.ViewGroup import androidx.fragment.app.FragmentManager import androidx.recyclerview.widget.RecyclerView 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.setWatchProgressLength import com.github.libretube.extensions.toID @@ -51,8 +51,8 @@ class ChannelAdapter( } val videoId = trending.url!!.toID() root.setOnLongClickListener { - VideoOptionsDialog(videoId) - .show(childFragmentManager, VideoOptionsDialog::class.java.name) + VideoOptionsBottomSheet(videoId) + .show(childFragmentManager, VideoOptionsBottomSheet::class.java.name) true } watchProgress.setWatchProgressLength(videoId, trending.duration!!) diff --git a/app/src/main/java/com/github/libretube/adapters/PlaylistAdapter.kt b/app/src/main/java/com/github/libretube/adapters/PlaylistAdapter.kt index 04385574c..9a84c4ce1 100644 --- a/app/src/main/java/com/github/libretube/adapters/PlaylistAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/PlaylistAdapter.kt @@ -9,7 +9,7 @@ import androidx.fragment.app.FragmentManager import androidx.recyclerview.widget.RecyclerView import com.github.libretube.api.RetrofitInstance 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.setFormattedDuration import com.github.libretube.extensions.setWatchProgressLength @@ -60,8 +60,8 @@ class PlaylistAdapter( } val videoId = streamItem.url!!.toID() root.setOnLongClickListener { - VideoOptionsDialog(videoId) - .show(childFragmentManager, VideoOptionsDialog::class.java.name) + VideoOptionsBottomSheet(videoId) + .show(childFragmentManager, VideoOptionsBottomSheet::class.java.name) true } diff --git a/app/src/main/java/com/github/libretube/adapters/PlaylistsAdapter.kt b/app/src/main/java/com/github/libretube/adapters/PlaylistsAdapter.kt index 0e80b7a0e..588974b10 100644 --- a/app/src/main/java/com/github/libretube/adapters/PlaylistsAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/PlaylistsAdapter.kt @@ -9,7 +9,7 @@ import androidx.recyclerview.widget.RecyclerView import com.github.libretube.R import com.github.libretube.api.RetrofitInstance 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.obj.PlaylistId import com.github.libretube.obj.Playlists diff --git a/app/src/main/java/com/github/libretube/adapters/SearchAdapter.kt b/app/src/main/java/com/github/libretube/adapters/SearchAdapter.kt index b36d3fd39..802d4e541 100644 --- a/app/src/main/java/com/github/libretube/adapters/SearchAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/SearchAdapter.kt @@ -10,8 +10,8 @@ import com.github.libretube.api.SubscriptionHelper import com.github.libretube.databinding.ChannelRowBinding import com.github.libretube.databinding.PlaylistSearchRowBinding import com.github.libretube.databinding.VideoRowBinding -import com.github.libretube.dialogs.PlaylistOptionsDialog -import com.github.libretube.dialogs.VideoOptionsDialog +import com.github.libretube.sheets.PlaylistOptionsDialog +import com.github.libretube.sheets.VideoOptionsBottomSheet import com.github.libretube.extensions.formatShort import com.github.libretube.extensions.setFormattedDuration import com.github.libretube.extensions.setWatchProgressLength @@ -99,8 +99,8 @@ class SearchAdapter( } val videoId = item.url!!.toID() root.setOnLongClickListener { - VideoOptionsDialog(videoId) - .show(childFragmentManager, VideoOptionsDialog::class.java.name) + VideoOptionsBottomSheet(videoId) + .show(childFragmentManager, VideoOptionsBottomSheet::class.java.name) true } channelImage.setOnClickListener { diff --git a/app/src/main/java/com/github/libretube/adapters/TrendingAdapter.kt b/app/src/main/java/com/github/libretube/adapters/TrendingAdapter.kt index 322a8ca1d..89f0e7e63 100644 --- a/app/src/main/java/com/github/libretube/adapters/TrendingAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/TrendingAdapter.kt @@ -6,7 +6,7 @@ import android.view.ViewGroup import androidx.fragment.app.FragmentManager import androidx.recyclerview.widget.RecyclerView 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.setFormattedDuration import com.github.libretube.extensions.setWatchProgressLength @@ -63,8 +63,8 @@ class TrendingAdapter( } val videoId = trending.url!!.toID() root.setOnLongClickListener { - VideoOptionsDialog(videoId) - .show(childFragmentManager, VideoOptionsDialog::class.java.name) + VideoOptionsBottomSheet(videoId) + .show(childFragmentManager, VideoOptionsBottomSheet::class.java.name) true } watchProgress.setWatchProgressLength(videoId, trending.duration!!) diff --git a/app/src/main/java/com/github/libretube/adapters/WatchHistoryAdapter.kt b/app/src/main/java/com/github/libretube/adapters/WatchHistoryAdapter.kt index 15751aa2a..9b88ddf3d 100644 --- a/app/src/main/java/com/github/libretube/adapters/WatchHistoryAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/WatchHistoryAdapter.kt @@ -7,7 +7,7 @@ import androidx.recyclerview.widget.RecyclerView import com.github.libretube.databinding.WatchHistoryRowBinding import com.github.libretube.db.DatabaseHelper 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.setWatchProgressLength import com.github.libretube.util.ImageHelper @@ -53,8 +53,8 @@ class WatchHistoryAdapter( NavigationHelper.navigateVideo(root.context, video.videoId) } root.setOnLongClickListener { - VideoOptionsDialog(video.videoId) - .show(childFragmentManager, VideoOptionsDialog::class.java.name) + VideoOptionsBottomSheet(video.videoId) + .show(childFragmentManager, VideoOptionsBottomSheet::class.java.name) true } diff --git a/app/src/main/java/com/github/libretube/dialogs/VideoOptionsDialog.kt b/app/src/main/java/com/github/libretube/dialogs/VideoOptionsDialog.kt deleted file mode 100644 index 25cf42b52..000000000 --- a/app/src/main/java/com/github/libretube/dialogs/VideoOptionsDialog.kt +++ /dev/null @@ -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() - } -} diff --git a/app/src/main/java/com/github/libretube/fragments/PlaylistFragment.kt b/app/src/main/java/com/github/libretube/fragments/PlaylistFragment.kt index 79b3a20a5..db41d5e68 100644 --- a/app/src/main/java/com/github/libretube/fragments/PlaylistFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/PlaylistFragment.kt @@ -14,7 +14,7 @@ import com.github.libretube.adapters.PlaylistAdapter import com.github.libretube.api.RetrofitInstance import com.github.libretube.constants.IntentData 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.TAG import com.github.libretube.extensions.toID diff --git a/app/src/main/java/com/github/libretube/dialogs/PlaylistOptionsDialog.kt b/app/src/main/java/com/github/libretube/sheets/PlaylistOptionsDialog.kt similarity index 98% rename from app/src/main/java/com/github/libretube/dialogs/PlaylistOptionsDialog.kt rename to app/src/main/java/com/github/libretube/sheets/PlaylistOptionsDialog.kt index 177554f88..e92c60ab6 100644 --- a/app/src/main/java/com/github/libretube/dialogs/PlaylistOptionsDialog.kt +++ b/app/src/main/java/com/github/libretube/sheets/PlaylistOptionsDialog.kt @@ -1,4 +1,4 @@ -package com.github.libretube.dialogs +package com.github.libretube.sheets import android.app.Dialog import android.os.Bundle @@ -10,6 +10,7 @@ import androidx.fragment.app.DialogFragment import com.github.libretube.R import com.github.libretube.api.RetrofitInstance import com.github.libretube.databinding.DialogTextPreferenceBinding +import com.github.libretube.dialogs.ShareDialog import com.github.libretube.extensions.TAG import com.github.libretube.extensions.toID import com.github.libretube.obj.PlaylistId diff --git a/app/src/main/java/com/github/libretube/sheets/VideoOptionsBottomSheet.kt b/app/src/main/java/com/github/libretube/sheets/VideoOptionsBottomSheet.kt new file mode 100644 index 000000000..7a0042674 --- /dev/null +++ b/app/src/main/java/com/github/libretube/sheets/VideoOptionsBottomSheet.kt @@ -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) + } +} diff --git a/app/src/main/java/com/github/libretube/views/BottomSheet.kt b/app/src/main/java/com/github/libretube/views/BottomSheet.kt index bed191da0..9ac6ef127 100644 --- a/app/src/main/java/com/github/libretube/views/BottomSheet.kt +++ b/app/src/main/java/com/github/libretube/views/BottomSheet.kt @@ -12,7 +12,7 @@ import com.google.android.material.bottomsheet.BottomSheetBehavior import com.google.android.material.bottomsheet.BottomSheetDialog import com.google.android.material.bottomsheet.BottomSheetDialogFragment -class BottomSheet : BottomSheetDialogFragment() { +open class BottomSheet : BottomSheetDialogFragment() { private lateinit var items: List private lateinit var listener: (index: Int) -> Unit private lateinit var binding: BottomSheetBinding