Skip to content

Commit 780e6a4

Browse files
author
Yevhen Babiichuk (DustDFG)
committed
Convert newpipe/util/PlayButtonHelper to kotlin
1 parent 13186c0 commit 780e6a4

2 files changed

Lines changed: 96 additions & 94 deletions

File tree

app/src/main/java/org/schabi/newpipe/util/PlayButtonHelper.java

Lines changed: 0 additions & 94 deletions
This file was deleted.
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

Comments
 (0)