fix: only add downloads of same type (audio/video) to queue

This commit is contained in:
Bnyro 2024-10-06 16:12:52 +02:00
parent 9b68e4faea
commit 56330118a6
9 changed files with 39 additions and 29 deletions

View File

@ -49,4 +49,5 @@ object IntentData {
const val nextPage = "nextPage" const val nextPage = "nextPage"
const val videoInfo = "videoInfo" const val videoInfo = "videoInfo"
const val offlinePlayer = "offlinePlayer" const val offlinePlayer = "offlinePlayer"
const val downloadTab = "downloadTab"
} }

View File

@ -2,6 +2,8 @@ package com.github.libretube.db.obj
import androidx.room.Embedded import androidx.room.Embedded
import androidx.room.Relation import androidx.room.Relation
import com.github.libretube.enums.FileType
import com.github.libretube.ui.fragments.DownloadTab
data class DownloadWithItems( data class DownloadWithItems(
@Embedded val download: Download, @Embedded val download: Download,
@ -16,3 +18,15 @@ data class DownloadWithItems(
) )
val downloadChapters: List<DownloadChapter> = emptyList() val downloadChapters: List<DownloadChapter> = emptyList()
) )
fun List<DownloadWithItems>.filterByTab(tab: DownloadTab) = filter { dl ->
when (tab) {
DownloadTab.AUDIO -> {
dl.downloadItems.any { it.type == FileType.AUDIO } && dl.downloadItems.none { it.type == FileType.VIDEO }
}
DownloadTab.VIDEO -> {
dl.downloadItems.any { it.type == FileType.VIDEO }
}
}
}

View File

@ -10,6 +10,7 @@ import com.github.libretube.constants.IntentData
import com.github.libretube.parcelable.PlayerData import com.github.libretube.parcelable.PlayerData
import com.github.libretube.services.OfflinePlayerService import com.github.libretube.services.OfflinePlayerService
import com.github.libretube.services.OnlinePlayerService import com.github.libretube.services.OnlinePlayerService
import com.github.libretube.ui.fragments.DownloadTab
import com.github.libretube.ui.fragments.PlayerFragment import com.github.libretube.ui.fragments.PlayerFragment
/** /**
@ -75,11 +76,12 @@ object BackgroundHelper {
* @param context the current context * @param context the current context
* @param videoId the videoId of the video or null if all available downloads should be shuffled * @param videoId the videoId of the video or null if all available downloads should be shuffled
*/ */
fun playOnBackgroundOffline(context: Context, videoId: String?) { fun playOnBackgroundOffline(context: Context, videoId: String?, downloadTab: DownloadTab) {
stopBackgroundPlay(context) stopBackgroundPlay(context)
val playerIntent = Intent(context, OfflinePlayerService::class.java) val playerIntent = Intent(context, OfflinePlayerService::class.java)
.putExtra(IntentData.videoId, videoId) .putExtra(IntentData.videoId, videoId)
.putExtra(IntentData.downloadTab, downloadTab)
ContextCompat.startForegroundService(context, playerIntent) ContextCompat.startForegroundService(context, playerIntent)
} }

View File

@ -1,7 +1,6 @@
package com.github.libretube.services package com.github.libretube.services
import android.content.Intent import android.content.Intent
import android.util.Log
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.media3.common.MediaItem import androidx.media3.common.MediaItem
import androidx.media3.common.Player import androidx.media3.common.Player
@ -11,11 +10,14 @@ import com.github.libretube.constants.IntentData
import com.github.libretube.db.DatabaseHolder.Database import com.github.libretube.db.DatabaseHolder.Database
import com.github.libretube.db.obj.DownloadChapter import com.github.libretube.db.obj.DownloadChapter
import com.github.libretube.db.obj.DownloadWithItems import com.github.libretube.db.obj.DownloadWithItems
import com.github.libretube.db.obj.filterByTab
import com.github.libretube.enums.FileType import com.github.libretube.enums.FileType
import com.github.libretube.extensions.serializableExtra
import com.github.libretube.extensions.toAndroidUri import com.github.libretube.extensions.toAndroidUri
import com.github.libretube.extensions.toID import com.github.libretube.extensions.toID
import com.github.libretube.helpers.PlayerHelper import com.github.libretube.helpers.PlayerHelper
import com.github.libretube.obj.PlayerNotificationData import com.github.libretube.obj.PlayerNotificationData
import com.github.libretube.ui.fragments.DownloadTab
import com.github.libretube.util.PlayingQueue import com.github.libretube.util.PlayingQueue
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -28,9 +30,11 @@ import kotlin.io.path.exists
@UnstableApi @UnstableApi
class OfflinePlayerService : AbstractPlayerService() { class OfflinePlayerService : AbstractPlayerService() {
private var downloadWithItems: DownloadWithItems? = null private var downloadWithItems: DownloadWithItems? = null
private lateinit var downloadTab: DownloadTab
override suspend fun onServiceCreated(intent: Intent) { override suspend fun onServiceCreated(intent: Intent) {
videoId = intent.getStringExtra(IntentData.videoId) ?: return videoId = intent.getStringExtra(IntentData.videoId) ?: return
downloadTab = intent.serializableExtra(IntentData.downloadTab)!!
PlayingQueue.clear() PlayingQueue.clear()
@ -88,7 +92,7 @@ class OfflinePlayerService : AbstractPlayerService() {
private suspend fun fillQueue() { private suspend fun fillQueue() {
val downloads = withContext(Dispatchers.IO) { val downloads = withContext(Dispatchers.IO) {
Database.downloadDao().getAll() Database.downloadDao().getAll()
} }.filterByTab(downloadTab)
PlayingQueue.insertRelatedStreams(downloads.map { it.download.toStreamItem() }) PlayingQueue.insertRelatedStreams(downloads.map { it.download.toStreamItem() })
} }

View File

@ -32,6 +32,7 @@ import com.github.libretube.databinding.ActivityOfflinePlayerBinding
import com.github.libretube.databinding.ExoStyledPlayerControlViewBinding import com.github.libretube.databinding.ExoStyledPlayerControlViewBinding
import com.github.libretube.db.DatabaseHolder.Database import com.github.libretube.db.DatabaseHolder.Database
import com.github.libretube.db.obj.DownloadChapter import com.github.libretube.db.obj.DownloadChapter
import com.github.libretube.db.obj.filterByTab
import com.github.libretube.enums.FileType import com.github.libretube.enums.FileType
import com.github.libretube.enums.PlayerEvent import com.github.libretube.enums.PlayerEvent
import com.github.libretube.extensions.serializableExtra import com.github.libretube.extensions.serializableExtra
@ -41,6 +42,7 @@ import com.github.libretube.helpers.PlayerHelper
import com.github.libretube.helpers.WindowHelper import com.github.libretube.helpers.WindowHelper
import com.github.libretube.obj.PlayerNotificationData import com.github.libretube.obj.PlayerNotificationData
import com.github.libretube.ui.base.BaseActivity import com.github.libretube.ui.base.BaseActivity
import com.github.libretube.ui.fragments.DownloadTab
import com.github.libretube.ui.interfaces.TimeFrameReceiver import com.github.libretube.ui.interfaces.TimeFrameReceiver
import com.github.libretube.ui.listeners.SeekbarPreviewListener import com.github.libretube.ui.listeners.SeekbarPreviewListener
import com.github.libretube.ui.models.ChaptersViewModel import com.github.libretube.ui.models.ChaptersViewModel
@ -324,7 +326,7 @@ class OfflinePlayerActivity : BaseActivity() {
private suspend fun fillQueue() { private suspend fun fillQueue() {
val downloads = withContext(Dispatchers.IO) { val downloads = withContext(Dispatchers.IO) {
Database.downloadDao().getAll() Database.downloadDao().getAll()
} }.filterByTab(DownloadTab.VIDEO)
PlayingQueue.insertRelatedStreams(downloads.map { it.download.toStreamItem() }) PlayingQueue.insertRelatedStreams(downloads.map { it.download.toStreamItem() })
} }

View File

@ -3,13 +3,10 @@ package com.github.libretube.ui.adapters
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.os.Handler
import android.os.Looper
import android.text.format.DateUtils import android.text.format.DateUtils
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.ViewGroup import android.view.ViewGroup
import androidx.core.os.bundleOf import androidx.core.os.bundleOf
import androidx.core.os.postDelayed
import androidx.core.view.isGone import androidx.core.view.isGone
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
@ -42,8 +39,6 @@ class DownloadsAdapter(
private val downloads: MutableList<DownloadWithItems>, private val downloads: MutableList<DownloadWithItems>,
private val toggleDownload: (DownloadWithItems) -> Boolean private val toggleDownload: (DownloadWithItems) -> Boolean
) : RecyclerView.Adapter<DownloadsViewHolder>() { ) : RecyclerView.Adapter<DownloadsViewHolder>() {
private val handler = Handler(Looper.getMainLooper())
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DownloadsViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DownloadsViewHolder {
val binding = DownloadedMediaRowBinding.inflate( val binding = DownloadedMediaRowBinding.inflate(
LayoutInflater.from(parent.context), LayoutInflater.from(parent.context),
@ -112,7 +107,7 @@ class DownloadsAdapter(
intent.putExtra(IntentData.videoId, download.videoId) intent.putExtra(IntentData.videoId, download.videoId)
root.context.startActivity(intent) root.context.startActivity(intent)
} else { } else {
BackgroundHelper.playOnBackgroundOffline(root.context, download.videoId) BackgroundHelper.playOnBackgroundOffline(root.context, download.videoId, downloadTab)
NavigationHelper.startAudioPlayer(root.context, offlinePlayer = true) NavigationHelper.startAudioPlayer(root.context, offlinePlayer = true)
} }
} }
@ -130,7 +125,8 @@ class DownloadsAdapter(
.apply { .apply {
arguments = bundleOf( arguments = bundleOf(
IntentData.videoId to download.videoId, IntentData.videoId to download.videoId,
IntentData.channelName to download.uploader IntentData.channelName to download.uploader,
IntentData.downloadTab to downloadTab
) )
} }
.show(fragmentManager) .show(fragmentManager)

View File

@ -10,7 +10,6 @@ import android.os.Handler
import android.os.IBinder import android.os.IBinder
import android.os.Looper import android.os.Looper
import android.text.format.DateUtils import android.text.format.DateUtils
import android.util.Log
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
@ -99,7 +98,6 @@ class AudioPlayerFragment : Fragment(), AudioPlayerOptions {
val serviceClass = val serviceClass =
if (isOffline) OfflinePlayerService::class.java else OnlinePlayerService::class.java if (isOffline) OfflinePlayerService::class.java else OnlinePlayerService::class.java
Log.e("class", serviceClass.name.toString())
Intent(activity, serviceClass).also { intent -> Intent(activity, serviceClass).also { intent ->
activity?.bindService(intent, connection, 0) activity?.bindService(intent, connection, 0)
} }

View File

@ -27,7 +27,7 @@ import com.github.libretube.databinding.FragmentDownloadContentBinding
import com.github.libretube.databinding.FragmentDownloadsBinding import com.github.libretube.databinding.FragmentDownloadsBinding
import com.github.libretube.db.DatabaseHolder.Database import com.github.libretube.db.DatabaseHolder.Database
import com.github.libretube.db.obj.DownloadWithItems import com.github.libretube.db.obj.DownloadWithItems
import com.github.libretube.enums.FileType import com.github.libretube.db.obj.filterByTab
import com.github.libretube.extensions.ceilHalf import com.github.libretube.extensions.ceilHalf
import com.github.libretube.extensions.formatAsFileSize import com.github.libretube.extensions.formatAsFileSize
import com.github.libretube.extensions.serializable import com.github.libretube.extensions.serializable
@ -111,7 +111,7 @@ class DownloadsFragmentPage : DynamicLayoutManagerFragment() {
private var binder: DownloadService.LocalBinder? = null private var binder: DownloadService.LocalBinder? = null
private val downloads = mutableListOf<DownloadWithItems>() private val downloads = mutableListOf<DownloadWithItems>()
private val downloadReceiver = DownloadReceiver() private val downloadReceiver = DownloadReceiver()
private lateinit var downloadTabSelf: DownloadTab private lateinit var downloadTab: DownloadTab
private val serviceConnection = object : ServiceConnection { private val serviceConnection = object : ServiceConnection {
var isBound = false var isBound = false
@ -137,7 +137,7 @@ class DownloadsFragmentPage : DynamicLayoutManagerFragment() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
this.downloadTabSelf = requireArguments().serializable(IntentData.currentPosition)!! this.downloadTab = requireArguments().serializable(IntentData.currentPosition)!!
} }
override fun onCreateView( override fun onCreateView(
@ -181,23 +181,13 @@ class DownloadsFragmentPage : DynamicLayoutManagerFragment() {
} }
downloads.clear() downloads.clear()
downloads.addAll(dbDownloads.filter { dl -> downloads.addAll(dbDownloads.filterByTab(downloadTab))
when (downloadTabSelf) {
DownloadTab.AUDIO -> {
dl.downloadItems.any { it.type == FileType.AUDIO } && dl.downloadItems.none { it.type == FileType.VIDEO }
}
DownloadTab.VIDEO -> {
dl.downloadItems.any { it.type == FileType.VIDEO }
}
}
})
if (downloads.isEmpty()) return@launch if (downloads.isEmpty()) return@launch
sortDownloadList(selectedSortType) sortDownloadList(selectedSortType)
adapter = DownloadsAdapter(requireContext(), downloadTabSelf, downloads) { adapter = DownloadsAdapter(requireContext(), downloadTab, downloads) {
var isDownloading = false var isDownloading = false
val ids = it.downloadItems val ids = it.downloadItems
.filter { item -> item.path.fileSize() < item.downloadSize } .filter { item -> item.path.fileSize() < item.downloadSize }

View File

@ -6,14 +6,17 @@ import androidx.fragment.app.setFragmentResult
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.constants.IntentData import com.github.libretube.constants.IntentData
import com.github.libretube.enums.ShareObjectType import com.github.libretube.enums.ShareObjectType
import com.github.libretube.extensions.serializable
import com.github.libretube.helpers.BackgroundHelper import com.github.libretube.helpers.BackgroundHelper
import com.github.libretube.helpers.NavigationHelper import com.github.libretube.helpers.NavigationHelper
import com.github.libretube.obj.ShareData import com.github.libretube.obj.ShareData
import com.github.libretube.ui.dialogs.ShareDialog import com.github.libretube.ui.dialogs.ShareDialog
import com.github.libretube.ui.fragments.DownloadTab
class DownloadOptionsBottomSheet : BaseBottomSheet() { class DownloadOptionsBottomSheet : BaseBottomSheet() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
val videoId = arguments?.getString(IntentData.videoId)!! val videoId = arguments?.getString(IntentData.videoId)!!
val downloadTab = arguments?.serializable<DownloadTab>(IntentData.downloadTab)!!
val options = listOf( val options = listOf(
R.string.playOnBackground, R.string.playOnBackground,
@ -25,7 +28,7 @@ class DownloadOptionsBottomSheet : BaseBottomSheet() {
setSimpleItems(options.map { getString(it) }) { which -> setSimpleItems(options.map { getString(it) }) { which ->
when (options[which]) { when (options[which]) {
R.string.playOnBackground -> { R.string.playOnBackground -> {
BackgroundHelper.playOnBackgroundOffline(requireContext(), videoId) BackgroundHelper.playOnBackgroundOffline(requireContext(), videoId, downloadTab)
} }
R.string.go_to_video -> { R.string.go_to_video -> {