refactor: improve performance when starting background shuffle mode

This commit is contained in:
Bnyro 2024-10-16 11:04:39 +02:00
parent 21d4e03509
commit bff39a2740
2 changed files with 5 additions and 2 deletions

View File

@ -11,6 +11,7 @@ import com.github.libretube.db.obj.Download
import com.github.libretube.db.obj.DownloadChapter
import com.github.libretube.db.obj.DownloadItem
import com.github.libretube.db.obj.DownloadWithItems
import com.github.libretube.enums.FileType
@Dao
interface DownloadDao {
@ -22,6 +23,9 @@ interface DownloadDao {
@Query("SELECT * FROM download WHERE videoId = :videoId")
suspend fun findById(videoId: String): DownloadWithItems
@Query("SELECT videoId FROM downloadItem WHERE type = :fileType ORDER BY RANDOM() LIMIT 1")
suspend fun getRandomVideoIdByFileType(fileType: FileType): String?
@Query("SELECT * FROM downloaditem WHERE id = :id")
suspend fun findDownloadItemById(id: Int): DownloadItem?

View File

@ -42,8 +42,7 @@ class OfflinePlayerService : AbstractPlayerService() {
videoId = if (shuffle) {
runBlocking(Dispatchers.IO) {
Database.downloadDao().getAll().filterByTab(downloadTab)
.randomOrNull()?.download?.videoId
Database.downloadDao().getRandomVideoIdByFileType(FileType.AUDIO)
}
} else {
intent.getStringExtra(IntentData.videoId)