mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 14:20:30 +05:30
Merge pull request #3082 from Isira-Seneviratne/DownloadDao_suspend
Convert DownloadDao methods to suspend functions.
This commit is contained in:
commit
2ae5ebf637
@ -15,37 +15,25 @@ import com.github.libretube.db.obj.DownloadWithItems
|
|||||||
interface DownloadDao {
|
interface DownloadDao {
|
||||||
@Transaction
|
@Transaction
|
||||||
@Query("SELECT * FROM download")
|
@Query("SELECT * FROM download")
|
||||||
fun getAll(): List<DownloadWithItems>
|
suspend fun getAll(): List<DownloadWithItems>
|
||||||
|
|
||||||
@Transaction
|
@Transaction
|
||||||
@Query("SELECT * FROM download WHERE videoId = :videoId")
|
@Query("SELECT * FROM download WHERE videoId = :videoId")
|
||||||
fun findById(videoId: String): DownloadWithItems
|
suspend fun findById(videoId: String): DownloadWithItems
|
||||||
|
|
||||||
@Query("SELECT * FROM downloaditem WHERE id = :id")
|
@Query("SELECT * FROM downloaditem WHERE id = :id")
|
||||||
fun findDownloadItemById(id: Int): DownloadItem
|
suspend fun findDownloadItemById(id: Int): DownloadItem
|
||||||
|
|
||||||
@Query("SELECT * FROM downloadItem WHERE path = :path")
|
|
||||||
fun findDownloadItemByFilePath(path: String): DownloadItem
|
|
||||||
|
|
||||||
@Insert(onConflict = OnConflictStrategy.IGNORE)
|
@Insert(onConflict = OnConflictStrategy.IGNORE)
|
||||||
fun insertDownload(download: Download)
|
suspend fun insertDownload(download: Download)
|
||||||
|
|
||||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||||
fun insertDownloadItem(downloadItem: DownloadItem): Long
|
suspend fun insertDownloadItem(downloadItem: DownloadItem): Long
|
||||||
|
|
||||||
@Update(onConflict = OnConflictStrategy.REPLACE)
|
@Update(onConflict = OnConflictStrategy.REPLACE)
|
||||||
fun updateDownload(download: Download)
|
suspend fun updateDownloadItem(downloadItem: DownloadItem)
|
||||||
|
|
||||||
@Update(onConflict = OnConflictStrategy.REPLACE)
|
|
||||||
fun updateDownloadItem(downloadItem: DownloadItem)
|
|
||||||
|
|
||||||
@Transaction
|
@Transaction
|
||||||
@Delete
|
@Delete
|
||||||
fun deleteDownload(download: Download)
|
suspend fun deleteDownload(download: Download)
|
||||||
|
|
||||||
@Delete
|
|
||||||
fun deleteDownloadItem(downloadItem: DownloadItem)
|
|
||||||
|
|
||||||
@Query("DELETE FROM downloadItem WHERE videoId = :videoId")
|
|
||||||
fun deleteDownloadItemsByVideoId(videoId: String)
|
|
||||||
}
|
}
|
||||||
|
@ -23,10 +23,8 @@ import com.github.libretube.db.DatabaseHolder.Database
|
|||||||
import com.github.libretube.db.obj.Download
|
import com.github.libretube.db.obj.Download
|
||||||
import com.github.libretube.db.obj.DownloadItem
|
import com.github.libretube.db.obj.DownloadItem
|
||||||
import com.github.libretube.enums.FileType
|
import com.github.libretube.enums.FileType
|
||||||
import com.github.libretube.extensions.awaitQuery
|
|
||||||
import com.github.libretube.extensions.formatAsFileSize
|
import com.github.libretube.extensions.formatAsFileSize
|
||||||
import com.github.libretube.extensions.getContentLength
|
import com.github.libretube.extensions.getContentLength
|
||||||
import com.github.libretube.extensions.query
|
|
||||||
import com.github.libretube.extensions.toDownloadItems
|
import com.github.libretube.extensions.toDownloadItems
|
||||||
import com.github.libretube.extensions.toastFromMainThread
|
import com.github.libretube.extensions.toastFromMainThread
|
||||||
import com.github.libretube.helpers.DownloadHelper
|
import com.github.libretube.helpers.DownloadHelper
|
||||||
@ -101,18 +99,15 @@ class DownloadService : LifecycleService() {
|
|||||||
|
|
||||||
val thumbnailTargetFile = getDownloadFile(DownloadHelper.THUMBNAIL_DIR, fileName)
|
val thumbnailTargetFile = getDownloadFile(DownloadHelper.THUMBNAIL_DIR, fileName)
|
||||||
|
|
||||||
awaitQuery {
|
val download = Download(
|
||||||
Database.downloadDao().insertDownload(
|
videoId,
|
||||||
Download(
|
streams.title,
|
||||||
videoId = videoId,
|
streams.description,
|
||||||
title = streams.title,
|
streams.uploader,
|
||||||
thumbnailPath = thumbnailTargetFile.absolutePath,
|
streams.uploadDate.toString(),
|
||||||
description = streams.description,
|
thumbnailTargetFile.absolutePath
|
||||||
uploadDate = streams.uploadDate.toString(),
|
)
|
||||||
uploader = streams.uploader
|
Database.downloadDao().insertDownload(download)
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
ImageHelper.downloadImage(
|
ImageHelper.downloadImage(
|
||||||
this@DownloadService,
|
this@DownloadService,
|
||||||
streams.thumbnailUrl,
|
streams.thumbnailUrl,
|
||||||
@ -142,7 +137,7 @@ class DownloadService : LifecycleService() {
|
|||||||
* for the requested file.
|
* for the requested file.
|
||||||
*/
|
*/
|
||||||
private fun start(item: DownloadItem) {
|
private fun start(item: DownloadItem) {
|
||||||
val file: File = when (item.type) {
|
val file = when (item.type) {
|
||||||
FileType.AUDIO -> getDownloadFile(DownloadHelper.AUDIO_DIR, item.fileName)
|
FileType.AUDIO -> getDownloadFile(DownloadHelper.AUDIO_DIR, item.fileName)
|
||||||
FileType.VIDEO -> getDownloadFile(DownloadHelper.VIDEO_DIR, item.fileName)
|
FileType.VIDEO -> getDownloadFile(DownloadHelper.VIDEO_DIR, item.fileName)
|
||||||
FileType.SUBTITLE -> getDownloadFile(DownloadHelper.SUBTITLE_DIR, item.fileName)
|
FileType.SUBTITLE -> getDownloadFile(DownloadHelper.SUBTITLE_DIR, item.fileName)
|
||||||
@ -150,11 +145,8 @@ class DownloadService : LifecycleService() {
|
|||||||
file.createNewFile()
|
file.createNewFile()
|
||||||
item.path = file.absolutePath
|
item.path = file.absolutePath
|
||||||
|
|
||||||
item.id = awaitQuery {
|
|
||||||
Database.downloadDao().insertDownloadItem(item)
|
|
||||||
}.toInt()
|
|
||||||
|
|
||||||
lifecycleScope.launch(coroutineContext) {
|
lifecycleScope.launch(coroutineContext) {
|
||||||
|
item.id = Database.downloadDao().insertDownloadItem(item).toInt()
|
||||||
downloadFile(item)
|
downloadFile(item)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -174,9 +166,7 @@ class DownloadService : LifecycleService() {
|
|||||||
url.getContentLength().let { size ->
|
url.getContentLength().let { size ->
|
||||||
if (size > 0 && size != item.downloadSize) {
|
if (size > 0 && size != item.downloadSize) {
|
||||||
item.downloadSize = size
|
item.downloadSize = size
|
||||||
query {
|
Database.downloadDao().updateDownloadItem(item)
|
||||||
Database.downloadDao().updateDownloadItem(item)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -305,11 +295,8 @@ class DownloadService : LifecycleService() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
val downloadItem = awaitQuery {
|
|
||||||
Database.downloadDao().findDownloadItemById(id)
|
|
||||||
}
|
|
||||||
lifecycleScope.launch(coroutineContext) {
|
lifecycleScope.launch(coroutineContext) {
|
||||||
downloadFile(downloadItem)
|
downloadFile(Database.downloadDao().findDownloadItemById(id))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -340,9 +327,7 @@ class DownloadService : LifecycleService() {
|
|||||||
stream?.find { it.format == item.format && it.quality == item.quality }?.let {
|
stream?.find { it.format == item.format && it.quality == item.quality }?.let {
|
||||||
item.url = it.url
|
item.url = it.url
|
||||||
}
|
}
|
||||||
query {
|
Database.downloadDao().updateDownloadItem(item)
|
||||||
Database.downloadDao().updateDownloadItem(item)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -9,12 +9,12 @@ import android.os.Bundle
|
|||||||
import android.text.format.DateUtils
|
import android.text.format.DateUtils
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import androidx.activity.viewModels
|
import androidx.activity.viewModels
|
||||||
|
import androidx.lifecycle.lifecycleScope
|
||||||
import com.github.libretube.constants.IntentData
|
import com.github.libretube.constants.IntentData
|
||||||
import com.github.libretube.databinding.ActivityOfflinePlayerBinding
|
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.enums.FileType
|
import com.github.libretube.enums.FileType
|
||||||
import com.github.libretube.extensions.awaitQuery
|
|
||||||
import com.github.libretube.extensions.updateParameters
|
import com.github.libretube.extensions.updateParameters
|
||||||
import com.github.libretube.helpers.PlayerHelper
|
import com.github.libretube.helpers.PlayerHelper
|
||||||
import com.github.libretube.helpers.PlayerHelper.loadPlaybackParams
|
import com.github.libretube.helpers.PlayerHelper.loadPlaybackParams
|
||||||
@ -34,6 +34,9 @@ import com.google.android.exoplayer2.ui.StyledPlayerView
|
|||||||
import com.google.android.exoplayer2.upstream.FileDataSource
|
import com.google.android.exoplayer2.upstream.FileDataSource
|
||||||
import com.google.android.exoplayer2.util.MimeTypes
|
import com.google.android.exoplayer2.util.MimeTypes
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
|
||||||
class OfflinePlayerActivity : BaseActivity() {
|
class OfflinePlayerActivity : BaseActivity() {
|
||||||
private lateinit var binding: ActivityOfflinePlayerBinding
|
private lateinit var binding: ActivityOfflinePlayerBinding
|
||||||
@ -108,27 +111,29 @@ class OfflinePlayerActivity : BaseActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun playVideo() {
|
private fun playVideo() {
|
||||||
val downloadFiles = awaitQuery {
|
lifecycleScope.launch {
|
||||||
Database.downloadDao().findById(videoId).downloadItems
|
val downloadFiles = withContext(Dispatchers.IO) {
|
||||||
|
Database.downloadDao().findById(videoId).downloadItems
|
||||||
|
}
|
||||||
|
|
||||||
|
val video = downloadFiles.firstOrNull { it.type == FileType.VIDEO }
|
||||||
|
val audio = downloadFiles.firstOrNull { it.type == FileType.AUDIO }
|
||||||
|
val subtitle = downloadFiles.firstOrNull { it.type == FileType.SUBTITLE }
|
||||||
|
|
||||||
|
val videoUri = video?.path?.let { File(it).toUri() }
|
||||||
|
val audioUri = audio?.path?.let { File(it).toUri() }
|
||||||
|
val subtitleUri = subtitle?.path?.let { File(it).toUri() }
|
||||||
|
|
||||||
|
setMediaSource(videoUri, audioUri, subtitleUri)
|
||||||
|
|
||||||
|
trackSelector.updateParameters {
|
||||||
|
setPreferredTextRoleFlags(C.ROLE_FLAG_CAPTION)
|
||||||
|
setPreferredTextLanguage("en")
|
||||||
|
}
|
||||||
|
|
||||||
|
player.prepare()
|
||||||
|
player.play()
|
||||||
}
|
}
|
||||||
|
|
||||||
val video = downloadFiles.firstOrNull { it.type == FileType.VIDEO }
|
|
||||||
val audio = downloadFiles.firstOrNull { it.type == FileType.AUDIO }
|
|
||||||
val subtitle = downloadFiles.firstOrNull { it.type == FileType.SUBTITLE }
|
|
||||||
|
|
||||||
val videoUri = video?.path?.let { File(it).toUri() }
|
|
||||||
val audioUri = audio?.path?.let { File(it).toUri() }
|
|
||||||
val subtitleUri = subtitle?.path?.let { File(it).toUri() }
|
|
||||||
|
|
||||||
setMediaSource(videoUri, audioUri, subtitleUri)
|
|
||||||
|
|
||||||
trackSelector.updateParameters {
|
|
||||||
setPreferredTextRoleFlags(C.ROLE_FLAG_CAPTION)
|
|
||||||
setPreferredTextLanguage("en")
|
|
||||||
}
|
|
||||||
|
|
||||||
player.prepare()
|
|
||||||
player.play()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setMediaSource(videoUri: Uri?, audioUri: Uri?, subtitleUri: Uri?) {
|
private fun setMediaSource(videoUri: Uri?, audioUri: Uri?, subtitleUri: Uri?) {
|
||||||
|
@ -13,12 +13,13 @@ import com.github.libretube.databinding.DownloadedMediaRowBinding
|
|||||||
import com.github.libretube.db.DatabaseHolder
|
import com.github.libretube.db.DatabaseHolder
|
||||||
import com.github.libretube.db.obj.DownloadWithItems
|
import com.github.libretube.db.obj.DownloadWithItems
|
||||||
import com.github.libretube.extensions.formatAsFileSize
|
import com.github.libretube.extensions.formatAsFileSize
|
||||||
import com.github.libretube.extensions.query
|
|
||||||
import com.github.libretube.helpers.ImageHelper
|
import com.github.libretube.helpers.ImageHelper
|
||||||
import com.github.libretube.ui.activities.OfflinePlayerActivity
|
import com.github.libretube.ui.activities.OfflinePlayerActivity
|
||||||
import com.github.libretube.ui.viewholders.DownloadsViewHolder
|
import com.github.libretube.ui.viewholders.DownloadsViewHolder
|
||||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.runBlocking
|
||||||
|
|
||||||
class DownloadsAdapter(
|
class DownloadsAdapter(
|
||||||
private val context: Context,
|
private val context: Context,
|
||||||
@ -105,7 +106,7 @@ class DownloadsAdapter(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
query {
|
runBlocking(Dispatchers.IO) {
|
||||||
DatabaseHolder.Database.downloadDao().deleteDownload(download)
|
DatabaseHolder.Database.downloadDao().deleteDownload(download)
|
||||||
}
|
}
|
||||||
downloads.removeAt(position)
|
downloads.removeAt(position)
|
||||||
|
@ -16,7 +16,6 @@ import com.github.libretube.R
|
|||||||
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.extensions.awaitQuery
|
|
||||||
import com.github.libretube.extensions.formatAsFileSize
|
import com.github.libretube.extensions.formatAsFileSize
|
||||||
import com.github.libretube.helpers.DownloadHelper
|
import com.github.libretube.helpers.DownloadHelper
|
||||||
import com.github.libretube.obj.DownloadStatus
|
import com.github.libretube.obj.DownloadStatus
|
||||||
@ -26,9 +25,11 @@ import com.github.libretube.ui.adapters.DownloadsAdapter
|
|||||||
import com.github.libretube.ui.base.BaseFragment
|
import com.github.libretube.ui.base.BaseFragment
|
||||||
import com.github.libretube.ui.viewholders.DownloadsViewHolder
|
import com.github.libretube.ui.viewholders.DownloadsViewHolder
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.Job
|
import kotlinx.coroutines.Job
|
||||||
import kotlinx.coroutines.flow.collectLatest
|
import kotlinx.coroutines.flow.collectLatest
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.runBlocking
|
||||||
|
|
||||||
class DownloadsFragment : BaseFragment() {
|
class DownloadsFragment : BaseFragment() {
|
||||||
private lateinit var binding: FragmentDownloadsBinding
|
private lateinit var binding: FragmentDownloadsBinding
|
||||||
@ -69,9 +70,10 @@ class DownloadsFragment : BaseFragment() {
|
|||||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||||
super.onViewCreated(view, savedInstanceState)
|
super.onViewCreated(view, savedInstanceState)
|
||||||
|
|
||||||
awaitQuery {
|
val dbDownloads = runBlocking(Dispatchers.IO) {
|
||||||
downloads.addAll(Database.downloadDao().getAll())
|
Database.downloadDao().getAll()
|
||||||
}
|
}
|
||||||
|
downloads.addAll(dbDownloads)
|
||||||
if (downloads.isEmpty()) return
|
if (downloads.isEmpty()) return
|
||||||
|
|
||||||
binding.downloadsEmpty.visibility = View.GONE
|
binding.downloadsEmpty.visibility = View.GONE
|
||||||
|
Loading…
Reference in New Issue
Block a user