|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2023-2026 NewPipe contributors <https://newpipe.net> |
| 3 | + * SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +package org.schabi.newpipe.util |
| 7 | + |
| 8 | +import android.content.Context |
| 9 | +import android.view.View |
| 10 | +import android.view.View.OnLongClickListener |
| 11 | +import android.widget.Toast |
| 12 | +import androidx.appcompat.app.AppCompatActivity |
| 13 | +import androidx.preference.PreferenceManager |
| 14 | +import org.schabi.newpipe.R |
| 15 | +import org.schabi.newpipe.databinding.PlaylistControlBinding |
| 16 | +import org.schabi.newpipe.fragments.list.playlist.PlaylistControlViewHolder |
| 17 | +import org.schabi.newpipe.player.PlayerType |
| 18 | + |
| 19 | +/** |
| 20 | + * Utility class for play buttons and their respective click listeners. |
| 21 | + */ |
| 22 | +object PlayButtonHelper { |
| 23 | + /** |
| 24 | + * Initialize [OnClickListener][View.OnClickListener] |
| 25 | + * and [OnLongClickListener][OnLongClickListener] for playlist control |
| 26 | + * buttons defined in [R.layout.playlist_control]. |
| 27 | + * |
| 28 | + * @param activity The activity to use for the [Toast][Toast]. |
| 29 | + * @param playlistControlBinding The binding of the |
| 30 | + * [playlist control layout][R.layout.playlist_control]. |
| 31 | + * @param fragment The fragment to get the play queue from. |
| 32 | + */ |
| 33 | + @JvmStatic |
| 34 | + fun initPlaylistControlClickListener( |
| 35 | + activity: AppCompatActivity, |
| 36 | + playlistControlBinding: PlaylistControlBinding, |
| 37 | + fragment: PlaylistControlViewHolder |
| 38 | + ) { |
| 39 | + // click listener |
| 40 | + playlistControlBinding.playlistCtrlPlayAllButton.setOnClickListener { |
| 41 | + NavigationHelper.playOnMainPlayer(activity, fragment.getPlayQueue()) |
| 42 | + showHoldToAppendToastIfNeeded(activity) |
| 43 | + } |
| 44 | + playlistControlBinding.playlistCtrlPlayPopupButton.setOnClickListener { |
| 45 | + NavigationHelper.playOnPopupPlayer(activity, fragment.getPlayQueue(), false) |
| 46 | + showHoldToAppendToastIfNeeded(activity) |
| 47 | + } |
| 48 | + playlistControlBinding.playlistCtrlPlayBgButton.setOnClickListener { |
| 49 | + NavigationHelper.playOnBackgroundPlayer(activity, fragment.getPlayQueue(), false) |
| 50 | + showHoldToAppendToastIfNeeded(activity) |
| 51 | + } |
| 52 | + |
| 53 | + // long click listener |
| 54 | + playlistControlBinding.playlistCtrlPlayAllButton.setOnLongClickListener { |
| 55 | + NavigationHelper.enqueueOnPlayer(activity, fragment.getPlayQueue(), PlayerType.MAIN) |
| 56 | + true |
| 57 | + } |
| 58 | + playlistControlBinding.playlistCtrlPlayPopupButton.setOnLongClickListener { |
| 59 | + NavigationHelper.enqueueOnPlayer(activity, fragment.getPlayQueue(), PlayerType.POPUP) |
| 60 | + true |
| 61 | + } |
| 62 | + playlistControlBinding.playlistCtrlPlayBgButton.setOnLongClickListener { |
| 63 | + NavigationHelper.enqueueOnPlayer(activity, fragment.getPlayQueue(), PlayerType.AUDIO) |
| 64 | + true |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Show the "hold to append" toast if the corresponding preference is enabled. |
| 70 | + * |
| 71 | + * @param context The context to show the toast. |
| 72 | + */ |
| 73 | + private fun showHoldToAppendToastIfNeeded(context: Context) { |
| 74 | + if (shouldShowHoldToAppendTip(context)) { |
| 75 | + Toast.makeText(context, R.string.hold_to_append, Toast.LENGTH_SHORT).show() |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Check if the "hold to append" toast should be shown. |
| 81 | + * |
| 82 | + * |
| 83 | + * |
| 84 | + * The tip is shown if the corresponding preference is enabled. |
| 85 | + * This is the default behaviour. |
| 86 | + * |
| 87 | + * |
| 88 | + * @param context The context to get the preference. |
| 89 | + * @return `true` if the tip should be shown, `false` otherwise. |
| 90 | + */ |
| 91 | + @JvmStatic |
| 92 | + fun shouldShowHoldToAppendTip(context: Context): Boolean { |
| 93 | + return PreferenceManager.getDefaultSharedPreferences(context) |
| 94 | + .getBoolean(context.getString(R.string.show_hold_to_append_key), true) |
| 95 | + } |
| 96 | +} |
0 commit comments