Skip to content

Commit df0e562

Browse files
committed
fix: playing queue doesn't work in download playlists fragment
1 parent 49d14f6 commit df0e562

6 files changed

Lines changed: 84 additions & 39 deletions

File tree

app/src/main/java/com/github/libretube/helpers/BackgroundHelper.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ object BackgroundHelper {
7878
fun playOnBackgroundOffline(
7979
context: Context,
8080
videoId: String?,
81+
playlistId: String?,
8182
downloadTab: DownloadTab,
8283
shuffle: Boolean = false
8384
) {
@@ -86,6 +87,7 @@ object BackgroundHelper {
8687

8788
val arguments = bundleOf(
8889
IntentData.videoId to videoId,
90+
IntentData.playlistId to playlistId,
8991
IntentData.shuffle to shuffle,
9092
IntentData.downloadTab to downloadTab,
9193
IntentData.noInternet to noInternet,

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

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ open class OfflinePlayerService : AbstractPlayerService() {
4848
private var downloadWithItems: DownloadWithItems? = null
4949
private lateinit var downloadTab: DownloadTab
5050
private var shuffle: Boolean = false
51+
private var playlistId: String? = null
5152

5253
private val scope = CoroutineScope(Dispatchers.Main)
5354

@@ -77,13 +78,18 @@ open class OfflinePlayerService : AbstractPlayerService() {
7778
shuffle = args.getBoolean(IntentData.shuffle, false)
7879
noInternetService = args.getBoolean(IntentData.noInternet, false)
7980
isAudioOnlyPlayer = args.getBoolean(IntentData.audioOnly, false)
81+
playlistId = args.getString(IntentData.playlistId)
8082

8183
PlayingQueue.clear()
8284

8385
this.videoId = if (shuffle) {
8486
runBlocking(Dispatchers.IO) {
85-
Database.downloadDao().getAll().filterByTab(downloadTab).randomOrNull()
86-
}?.download?.videoId
87+
if (downloadTab == DownloadTab.PLAYLIST) {
88+
Database.downloadDao().getDownloadPlaylistById(playlistId!!).downloadVideos.randomOrNull()
89+
} else {
90+
Database.downloadDao().getAll().filterByTab(downloadTab).randomOrNull()?.download
91+
}
92+
}?.videoId
8793
} else {
8894
args.getString(IntentData.videoId)
8995
} ?: return
@@ -191,16 +197,24 @@ open class OfflinePlayerService : AbstractPlayerService() {
191197
}
192198

193199
private suspend fun fillQueue() {
194-
val downloads = withContext(Dispatchers.IO) {
195-
Database.downloadDao().getAll()
196-
}
197-
.filterByTab(downloadTab)
198-
.filter { it.download.videoId != videoId }
199-
.toMutableList()
200+
if (downloadTab == DownloadTab.PLAYLIST) {
201+
val videos = withContext(Dispatchers.IO) {
202+
Database.downloadDao().getDownloadPlaylistById(playlistId!!)
203+
}.downloadVideos
204+
205+
PlayingQueue.setStreams(videos.map { it.toStreamItem() })
206+
} else {
207+
val downloads = withContext(Dispatchers.IO) {
208+
Database.downloadDao().getAll()
209+
}
210+
.filterByTab(downloadTab)
211+
.filter { it.download.videoId != videoId }
212+
.toMutableList()
200213

201-
if (shuffle) downloads.shuffle()
214+
if (shuffle) downloads.shuffle()
202215

203-
PlayingQueue.add(*downloads.map { it.download.toStreamItem() }.toTypedArray())
216+
PlayingQueue.add(*downloads.map { it.download.toStreamItem() }.toTypedArray())
217+
}
204218
}
205219

206220
private fun playNextVideo(videoId: String? = null) {

app/src/main/java/com/github/libretube/ui/activities/OfflinePlayerActivity.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,18 @@ class OfflinePlayerActivity : BaseActivity() {
142142

143143
super.onCreate(savedInstanceState)
144144

145-
videoId = intent?.getStringExtra(IntentData.videoId)!!
145+
val videoId = intent?.getStringExtra(IntentData.videoId)
146+
val playlistId = intent?.getStringExtra(IntentData.playlistId)
147+
val shuffle = intent?.getBooleanExtra(IntentData.shuffle, false)
146148

147149
binding = ActivityOfflinePlayerBinding.inflate(layoutInflater)
148150
setContentView(binding.root)
149151

150152
val arguments = bundleOf(
151153
IntentData.downloadTab to DownloadTab.VIDEO,
152154
IntentData.videoId to videoId,
155+
IntentData.playlistId to playlistId,
156+
IntentData.shuffle to shuffle,
153157
IntentData.audioOnly to false
154158
)
155159
BackgroundHelper.startMediaService(this, OfflinePlayerService::class.java, arguments) {

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

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import kotlin.io.path.fileSize
4141
class DownloadsAdapter(
4242
private val context: Context,
4343
private val downloadTab: DownloadTab,
44+
private val playlistId: String?,
4445
private val toggleDownload: (DownloadWithItems) -> Boolean
4546
) : ListAdapter<DownloadWithItems, DownloadsViewHolder>(DiffUtilItemCallback()) {
4647
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DownloadsViewHolder {
@@ -111,17 +112,27 @@ class DownloadsAdapter(
111112
}
112113

113114
root.setOnClickListener {
114-
if (downloadTab == DownloadTab.VIDEO) {
115-
val intent = Intent(root.context, OfflinePlayerActivity::class.java)
116-
intent.putExtra(IntentData.videoId, download.videoId)
117-
root.context.startActivity(intent)
118-
} else {
119-
BackgroundHelper.playOnBackgroundOffline(
120-
root.context,
121-
download.videoId,
122-
downloadTab
123-
)
124-
NavigationHelper.openAudioPlayerFragment(root.context, offlinePlayer = true)
115+
when (downloadTab) {
116+
DownloadTab.VIDEO -> {
117+
val intent = Intent(root.context, OfflinePlayerActivity::class.java)
118+
.putExtra(IntentData.videoId, download.videoId)
119+
root.context.startActivity(intent)
120+
}
121+
DownloadTab.AUDIO -> {
122+
BackgroundHelper.playOnBackgroundOffline(
123+
root.context,
124+
download.videoId,
125+
playlistId,
126+
downloadTab
127+
)
128+
NavigationHelper.openAudioPlayerFragment(root.context, offlinePlayer = true)
129+
}
130+
DownloadTab.PLAYLIST -> {
131+
val intent = Intent(root.context, OfflinePlayerActivity::class.java)
132+
.putExtra(IntentData.videoId, download.videoId)
133+
.putExtra(IntentData.playlistId, playlistId)
134+
root.context.startActivity(intent)
135+
}
125136
}
126137
}
127138

@@ -142,6 +153,7 @@ class DownloadsAdapter(
142153
.apply {
143154
arguments = bundleOf(
144155
IntentData.streamItem to download.toStreamItem(),
156+
IntentData.playlistId to playlistId,
145157
IntentData.downloadTab to downloadTab
146158
)
147159
}

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

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import com.github.libretube.helpers.PreferenceHelper
4646
import com.github.libretube.obj.DownloadStatus
4747
import com.github.libretube.receivers.DownloadReceiver
4848
import com.github.libretube.services.DownloadService
49+
import com.github.libretube.ui.activities.OfflinePlayerActivity
4950
import com.github.libretube.ui.adapters.DownloadPlaylistAdapter
5051
import com.github.libretube.ui.adapters.DownloadsAdapter
5152
import com.github.libretube.ui.base.DynamicLayoutManagerFragment
@@ -118,7 +119,7 @@ class DownloadsFragmentAdapter(fragment: Fragment) : FragmentStateAdapter(fragme
118119
}
119120

120121
return DownloadsFragmentPage().apply {
121-
arguments = bundleOf(IntentData.currentPosition to DownloadTab.entries[position])
122+
arguments = bundleOf(IntentData.downloadTab to DownloadTab.entries[position])
122123
}
123124
}
124125
}
@@ -135,7 +136,7 @@ class DownloadsFragmentPage : DynamicLayoutManagerFragment(R.layout.fragment_dow
135136
private val downloadReceiver = DownloadReceiver()
136137

137138
// Either downloadTab or downloadPlaylistId are set, never both at the same time!
138-
private var downloadTab: DownloadTab? = null
139+
private lateinit var downloadTab: DownloadTab
139140
private var downloadPlaylistId: String? = null
140141

141142
private var selectedSortType
@@ -171,11 +172,11 @@ class DownloadsFragmentPage : DynamicLayoutManagerFragment(R.layout.fragment_dow
171172
override fun onCreate(savedInstanceState: Bundle?) {
172173
super.onCreate(savedInstanceState)
173174

174-
this.downloadTab = requireArguments().serializable(IntentData.currentPosition)
175+
this.downloadTab = requireArguments().serializable(IntentData.downloadTab)!!
175176
this.downloadPlaylistId = requireArguments().getString(IntentData.playlistId)
176177

177-
if (downloadPlaylistId == null && downloadTab == null)
178-
throw IllegalArgumentException("either downloadTab or downloadPlaylistId must be set")
178+
if (downloadTab == DownloadTab.PLAYLIST && downloadPlaylistId == null)
179+
throw IllegalArgumentException("downloadTab unspecified or missing playlist id")
179180
}
180181

181182
override fun setLayoutManagers(gridItems: Int) {
@@ -185,7 +186,7 @@ class DownloadsFragmentPage : DynamicLayoutManagerFragment(R.layout.fragment_dow
185186
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
186187
_binding = FragmentDownloadContentBinding.bind(view)
187188
super.onViewCreated(view, savedInstanceState)
188-
adapter = DownloadsAdapter(requireContext(), downloadTab ?: DownloadTab.VIDEO) {
189+
adapter = DownloadsAdapter(requireContext(), downloadTab, downloadPlaylistId) {
189190
var isDownloading = false
190191
val ids = it.downloadItems
191192
.filter { item -> item.path.fileSize() < item.downloadSize }
@@ -229,7 +230,7 @@ class DownloadsFragmentPage : DynamicLayoutManagerFragment(R.layout.fragment_dow
229230
val downloads = withContext(Dispatchers.IO) {
230231
Database.downloadDao().getAll()
231232
}.let { downloads ->
232-
if (downloadTab != null) downloads.filterByTab(downloadTab!!)
233+
if (downloadTab != DownloadTab.PLAYLIST) downloads.filterByTab(downloadTab)
233234
else downloads.filter { playlistItems.orEmpty().contains(it.download.videoId) }
234235
}
235236

@@ -268,14 +269,22 @@ class DownloadsFragmentPage : DynamicLayoutManagerFragment(R.layout.fragment_dow
268269
}
269270

270271
binding.shuffleAll.setOnClickListener {
271-
BackgroundHelper.playOnBackgroundOffline(
272-
requireContext(),
273-
null,
274-
downloadTab ?: DownloadTab.VIDEO,
275-
shuffle = true
276-
)
272+
if (downloadTab == DownloadTab.AUDIO) {
273+
BackgroundHelper.playOnBackgroundOffline(
274+
requireContext(),
275+
videoId = null,
276+
playlistId = null,
277+
downloadTab,
278+
shuffle = true
279+
)
277280

278-
NavigationHelper.openAudioPlayerFragment(requireContext(), offlinePlayer = true)
281+
NavigationHelper.openAudioPlayerFragment(requireContext(), offlinePlayer = true)
282+
} else {
283+
val intent = Intent(context, OfflinePlayerActivity::class.java)
284+
.putExtra(IntentData.playlistId, downloadPlaylistId)
285+
.putExtra(IntentData.shuffle, true)
286+
requireContext().startActivity(intent)
287+
}
279288
}
280289

281290
playerViewModel.isMiniPlayerVisible.observe(viewLifecycleOwner) { isMiniPlayerVisible ->
@@ -305,7 +314,7 @@ class DownloadsFragmentPage : DynamicLayoutManagerFragment(R.layout.fragment_dow
305314
binding.downloadsEmpty.isVisible = isEmpty
306315
binding.downloadsContainer.isGone = isEmpty
307316
binding.deleteAll.isGone = isEmpty
308-
binding.shuffleAll.isGone = isEmpty || downloadTab != DownloadTab.AUDIO
317+
binding.shuffleAll.isGone = isEmpty
309318
}
310319

311320
private fun showDeleteAllDialog(context: Context, adapter: DownloadsAdapter) {
@@ -438,7 +447,10 @@ class PlaylistDownloadsFragmentPage : Fragment(R.layout.fragment_download_conten
438447
childFragmentManager.commit {
439448
replace<DownloadsFragmentPage>(
440449
binding.fragment.id,
441-
args = bundleOf(IntentData.playlistId to playlist.downloadPlaylist.playlistId)
450+
args = bundleOf(
451+
IntentData.downloadTab to DownloadTab.PLAYLIST,
452+
IntentData.playlistId to playlist.downloadPlaylist.playlistId
453+
)
442454
)
443455
}
444456
backPressedCallback.isEnabled = true

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class DownloadOptionsBottomSheet : BaseBottomSheet() {
2525
val streamItem = arguments?.parcelable<StreamItem>(IntentData.streamItem)!!
2626
val videoId = streamItem.url!!.toID()
2727
val downloadTab = arguments?.serializable<DownloadTab>(IntentData.downloadTab)!!
28+
val playlistId = arguments?.getString(IntentData.playlistId)
2829

2930
val options = mutableListOf(
3031
R.string.playOnBackground,
@@ -46,7 +47,7 @@ class DownloadOptionsBottomSheet : BaseBottomSheet() {
4647
setSimpleItems(options.map { getString(it) }) { which ->
4748
when (options[which]) {
4849
R.string.playOnBackground -> {
49-
BackgroundHelper.playOnBackgroundOffline(requireContext(), videoId, downloadTab)
50+
BackgroundHelper.playOnBackgroundOffline(requireContext(), videoId, playlistId, downloadTab)
5051
}
5152

5253
R.string.go_to_video -> {

0 commit comments

Comments
 (0)