Skip to content

Commit 09a306e

Browse files
authored
Merge pull request #7348 from Bnyro/master
feat: support for adding downloads to playback queue
2 parents ad66ea7 + f262210 commit 09a306e

File tree

8 files changed

+50
-19
lines changed

8 files changed

+50
-19
lines changed

app/src/main/java/com/github/libretube/constants/IntentData.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ object IntentData {
3636
const val url = "url"
3737
const val videoStats = "videoStats"
3838
const val bitmapUrl = "bitmapUrl"
39-
const val isCurrentlyPlaying = "isCurrentlyPlaying"
4039
const val isSubscribed = "isSubscribed"
4140
const val sortOptions = "sortOptions"
4241
const val hideWatched = "hideWatched"

app/src/main/java/com/github/libretube/services/AbstractPlayerService.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import com.github.libretube.ui.activities.MainActivity
3737
import com.github.libretube.util.NowPlayingNotification
3838
import com.github.libretube.util.PauseableTimer
3939
import com.github.libretube.util.PlayingQueue
40+
import com.github.libretube.util.PlayingQueueMode
4041
import com.google.common.util.concurrent.ListenableFuture
4142
import kotlinx.coroutines.CoroutineScope
4243
import kotlinx.coroutines.Dispatchers
@@ -100,6 +101,7 @@ abstract class AbstractPlayerService : MediaLibraryService(), MediaLibrarySessio
100101
Player.STATE_ENDED -> {
101102
saveWatchPosition()
102103
}
104+
103105
Player.STATE_READY -> {
104106
isTransitioning = false
105107
}
@@ -115,6 +117,9 @@ abstract class AbstractPlayerService : MediaLibraryService(), MediaLibrarySessio
115117
): ListenableFuture<SessionResult> {
116118
when (customCommand.customAction) {
117119
START_SERVICE_ACTION -> {
120+
PlayingQueue.queueMode =
121+
if (isOfflinePlayer) PlayingQueueMode.OFFLINE else PlayingQueueMode.ONLINE
122+
118123
CoroutineScope(Dispatchers.IO).launch {
119124
onServiceCreated(args)
120125
withContext(Dispatchers.Main) {
@@ -262,8 +267,9 @@ abstract class AbstractPlayerService : MediaLibraryService(), MediaLibrarySessio
262267
abstract val isOfflinePlayer: Boolean
263268
var isAudioOnlyPlayer: Boolean = false
264269

265-
val watchPositionsEnabled get() =
266-
(PlayerHelper.watchPositionsAudio && isAudioOnlyPlayer) || (PlayerHelper.watchPositionsVideo && !isAudioOnlyPlayer)
270+
val watchPositionsEnabled
271+
get() =
272+
(PlayerHelper.watchPositionsAudio && isAudioOnlyPlayer) || (PlayerHelper.watchPositionsVideo && !isAudioOnlyPlayer)
267273

268274
override fun onGetSession(controllerInfo: MediaSession.ControllerInfo): MediaLibrarySession? =
269275
mediaLibrarySession

app/src/main/java/com/github/libretube/ui/adapters/DownloadsAdapter.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,7 @@ class DownloadsAdapter(
137137
DownloadOptionsBottomSheet()
138138
.apply {
139139
arguments = bundleOf(
140-
IntentData.videoId to download.videoId,
141-
IntentData.channelName to download.uploader,
140+
IntentData.streamItem to download.toStreamItem(),
142141
IntentData.downloadTab to downloadTab
143142
)
144143
}

app/src/main/java/com/github/libretube/ui/fragments/AudioPlayerFragment.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -471,10 +471,7 @@ class AudioPlayerFragment : Fragment(R.layout.fragment_audio_player), AudioPlaye
471471
val current = PlayingQueue.getCurrent() ?: return
472472
VideoOptionsBottomSheet()
473473
.apply {
474-
arguments = bundleOf(
475-
IntentData.streamItem to current,
476-
IntentData.isCurrentlyPlaying to true
477-
)
474+
arguments = bundleOf(IntentData.streamItem to current)
478475
}
479476
.show(childFragmentManager)
480477
}

app/src/main/java/com/github/libretube/ui/fragments/WatchHistoryFragment.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ class WatchHistoryFragment : DynamicLayoutManagerFragment(R.layout.fragment_watc
6060
}
6161

6262
binding.watchHistoryRecView.setOnDismissListener { position ->
63-
val item = viewModel.filteredWatchHistory.value?.getOrNull(position) ?: return@setOnDismissListener
63+
val item = viewModel.filteredWatchHistory.value?.getOrNull(position)
64+
?: return@setOnDismissListener
6465
viewModel.removeFromHistory(item)
6566
}
6667

app/src/main/java/com/github/libretube/ui/sheets/DownloadOptionsBottomSheet.kt

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,43 @@ import android.os.Bundle
44
import androidx.core.os.bundleOf
55
import androidx.fragment.app.setFragmentResult
66
import com.github.libretube.R
7+
import com.github.libretube.api.obj.StreamItem
78
import com.github.libretube.constants.IntentData
89
import com.github.libretube.enums.ShareObjectType
10+
import com.github.libretube.extensions.parcelable
911
import com.github.libretube.extensions.serializable
12+
import com.github.libretube.extensions.toID
1013
import com.github.libretube.helpers.BackgroundHelper
1114
import com.github.libretube.helpers.ContextHelper
1215
import com.github.libretube.helpers.NavigationHelper
1316
import com.github.libretube.obj.ShareData
14-
import com.github.libretube.ui.activities.DownloadActivity
1517
import com.github.libretube.ui.activities.NoInternetActivity
1618
import com.github.libretube.ui.dialogs.ShareDialog
1719
import com.github.libretube.ui.fragments.DownloadTab
20+
import com.github.libretube.util.PlayingQueue
21+
import com.github.libretube.util.PlayingQueueMode
1822

1923
class DownloadOptionsBottomSheet : BaseBottomSheet() {
2024
override fun onCreate(savedInstanceState: Bundle?) {
21-
val videoId = arguments?.getString(IntentData.videoId)!!
25+
val streamItem = arguments?.parcelable<StreamItem>(IntentData.streamItem)!!
26+
val videoId = streamItem.url!!.toID()
2227
val downloadTab = arguments?.serializable<DownloadTab>(IntentData.downloadTab)!!
2328

2429
val options = mutableListOf(
2530
R.string.playOnBackground,
26-
R.string.go_to_video,
2731
R.string.share,
2832
R.string.delete
2933
)
3034

3135
// can't navigate to video while in offline activity
32-
if (ContextHelper.tryUnwrapActivity<NoInternetActivity>(requireContext()) != null) {
33-
options.remove(R.string.go_to_video)
36+
if (ContextHelper.tryUnwrapActivity<NoInternetActivity>(requireContext()) == null) {
37+
options += R.string.go_to_video
38+
}
39+
40+
val isSelectedVideoCurrentlyPlaying = PlayingQueue.getCurrent()?.url?.toID() == videoId
41+
if (!isSelectedVideoCurrentlyPlaying && PlayingQueue.isNotEmpty() && PlayingQueue.queueMode == PlayingQueueMode.OFFLINE) {
42+
options += R.string.play_next
43+
options += R.string.add_to_queue
3444
}
3545

3646
setSimpleItems(options.map { getString(it) }) { which ->
@@ -59,6 +69,14 @@ class DownloadOptionsBottomSheet : BaseBottomSheet() {
5969
setFragmentResult(DELETE_DOWNLOAD_REQUEST_KEY, bundleOf())
6070
dialog?.dismiss()
6171
}
72+
73+
R.string.play_next -> {
74+
PlayingQueue.addAsNext(streamItem)
75+
}
76+
77+
R.string.add_to_queue -> {
78+
PlayingQueue.add(streamItem)
79+
}
6280
}
6381
}
6482

app/src/main/java/com/github/libretube/ui/sheets/VideoOptionsBottomSheet.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import com.github.libretube.ui.dialogs.AddToPlaylistDialog
2424
import com.github.libretube.ui.dialogs.ShareDialog
2525
import com.github.libretube.ui.fragments.SubscriptionsFragment
2626
import com.github.libretube.util.PlayingQueue
27+
import com.github.libretube.util.PlayingQueueMode
2728
import kotlinx.coroutines.Dispatchers
2829
import kotlinx.coroutines.runBlocking
2930
import kotlinx.coroutines.withContext
@@ -35,18 +36,17 @@ import kotlinx.coroutines.withContext
3536
*/
3637
class VideoOptionsBottomSheet : BaseBottomSheet() {
3738
private lateinit var streamItem: StreamItem
38-
private var isCurrentlyPlaying = false
3939

4040
override fun onCreate(savedInstanceState: Bundle?) {
4141
streamItem = arguments?.parcelable(IntentData.streamItem)!!
42-
isCurrentlyPlaying = arguments?.getBoolean(IntentData.isCurrentlyPlaying) ?: false
4342

4443
val videoId = streamItem.url?.toID() ?: return
4544

4645
setTitle(streamItem.title)
4746

4847
val optionsList = mutableListOf<Int>()
49-
if (!isCurrentlyPlaying) {
48+
// these options are only available for other videos than the currently playing one
49+
if (PlayingQueue.getCurrent()?.url?.toID() != videoId) {
5050
optionsList += getOptionsForNotActivePlayback(videoId)
5151
}
5252

@@ -136,7 +136,7 @@ class VideoOptionsBottomSheet : BaseBottomSheet() {
136136
val optionsList = mutableListOf(R.string.playOnBackground)
137137

138138
// Check whether the player is running and add queue options
139-
if (PlayingQueue.isNotEmpty()) {
139+
if (PlayingQueue.isNotEmpty() && PlayingQueue.queueMode == PlayingQueueMode.ONLINE) {
140140
optionsList += R.string.play_next
141141
optionsList += R.string.add_to_queue
142142
}

app/src/main/java/com/github/libretube/util/PlayingQueue.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ object PlayingQueue {
1818

1919
private val queueJobs = mutableListOf<Job>()
2020

21+
/**
22+
* Current use case of the queue. Do NOT add any offline videos while the [queueMode] is online
23+
* or vice versa.
24+
*/
25+
var queueMode: PlayingQueueMode = PlayingQueueMode.ONLINE
26+
2127
// wrapper around PlayerHelper#repeatMode for compatibility
2228
var repeatMode: Int
2329
get() = PlayerHelper.repeatMode
@@ -222,3 +228,8 @@ object PlayingQueue {
222228
add(*streams.filter { !it.isLive }.toTypedArray(), skipExisting = true)
223229
}
224230
}
231+
232+
enum class PlayingQueueMode {
233+
ONLINE,
234+
OFFLINE
235+
}

0 commit comments

Comments
 (0)