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 {
|
||||
@Transaction
|
||||
@Query("SELECT * FROM download")
|
||||
fun getAll(): List<DownloadWithItems>
|
||||
suspend fun getAll(): List<DownloadWithItems>
|
||||
|
||||
@Transaction
|
||||
@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")
|
||||
fun findDownloadItemById(id: Int): DownloadItem
|
||||
|
||||
@Query("SELECT * FROM downloadItem WHERE path = :path")
|
||||
fun findDownloadItemByFilePath(path: String): DownloadItem
|
||||
suspend fun findDownloadItemById(id: Int): DownloadItem
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.IGNORE)
|
||||
fun insertDownload(download: Download)
|
||||
suspend fun insertDownload(download: Download)
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun insertDownloadItem(downloadItem: DownloadItem): Long
|
||||
suspend fun insertDownloadItem(downloadItem: DownloadItem): Long
|
||||
|
||||
@Update(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun updateDownload(download: Download)
|
||||
|
||||
@Update(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun updateDownloadItem(downloadItem: DownloadItem)
|
||||
suspend fun updateDownloadItem(downloadItem: DownloadItem)
|
||||
|
||||
@Transaction
|
||||
@Delete
|
||||
fun deleteDownload(download: Download)
|
||||
|
||||
@Delete
|
||||
fun deleteDownloadItem(downloadItem: DownloadItem)
|
||||
|
||||
@Query("DELETE FROM downloadItem WHERE videoId = :videoId")
|
||||
fun deleteDownloadItemsByVideoId(videoId: String)
|
||||
suspend fun deleteDownload(download: Download)
|
||||
}
|
||||
|
@ -23,10 +23,8 @@ import com.github.libretube.db.DatabaseHolder.Database
|
||||
import com.github.libretube.db.obj.Download
|
||||
import com.github.libretube.db.obj.DownloadItem
|
||||
import com.github.libretube.enums.FileType
|
||||
import com.github.libretube.extensions.awaitQuery
|
||||
import com.github.libretube.extensions.formatAsFileSize
|
||||
import com.github.libretube.extensions.getContentLength
|
||||
import com.github.libretube.extensions.query
|
||||
import com.github.libretube.extensions.toDownloadItems
|
||||
import com.github.libretube.extensions.toastFromMainThread
|
||||
import com.github.libretube.helpers.DownloadHelper
|
||||
@ -101,18 +99,15 @@ class DownloadService : LifecycleService() {
|
||||
|
||||
val thumbnailTargetFile = getDownloadFile(DownloadHelper.THUMBNAIL_DIR, fileName)
|
||||
|
||||
awaitQuery {
|
||||
Database.downloadDao().insertDownload(
|
||||
Download(
|
||||
videoId = videoId,
|
||||
title = streams.title,
|
||||
thumbnailPath = thumbnailTargetFile.absolutePath,
|
||||
description = streams.description,
|
||||
uploadDate = streams.uploadDate.toString(),
|
||||
uploader = streams.uploader
|
||||
val download = Download(
|
||||
videoId,
|
||||
streams.title,
|
||||
streams.description,
|
||||
streams.uploader,
|
||||
streams.uploadDate.toString(),
|
||||
thumbnailTargetFile.absolutePath
|
||||
)
|
||||
)
|
||||
}
|
||||
Database.downloadDao().insertDownload(download)
|
||||
ImageHelper.downloadImage(
|
||||
this@DownloadService,
|
||||
streams.thumbnailUrl,
|
||||
@ -142,7 +137,7 @@ class DownloadService : LifecycleService() {
|
||||
* for the requested file.
|
||||
*/
|
||||
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.VIDEO -> getDownloadFile(DownloadHelper.VIDEO_DIR, item.fileName)
|
||||
FileType.SUBTITLE -> getDownloadFile(DownloadHelper.SUBTITLE_DIR, item.fileName)
|
||||
@ -150,11 +145,8 @@ class DownloadService : LifecycleService() {
|
||||
file.createNewFile()
|
||||
item.path = file.absolutePath
|
||||
|
||||
item.id = awaitQuery {
|
||||
Database.downloadDao().insertDownloadItem(item)
|
||||
}.toInt()
|
||||
|
||||
lifecycleScope.launch(coroutineContext) {
|
||||
item.id = Database.downloadDao().insertDownloadItem(item).toInt()
|
||||
downloadFile(item)
|
||||
}
|
||||
}
|
||||
@ -174,11 +166,9 @@ class DownloadService : LifecycleService() {
|
||||
url.getContentLength().let { size ->
|
||||
if (size > 0 && size != item.downloadSize) {
|
||||
item.downloadSize = size
|
||||
query {
|
||||
Database.downloadDao().updateDownloadItem(item)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
// Set start range where last downloading was held.
|
||||
@ -305,11 +295,8 @@ class DownloadService : LifecycleService() {
|
||||
return
|
||||
}
|
||||
|
||||
val downloadItem = awaitQuery {
|
||||
Database.downloadDao().findDownloadItemById(id)
|
||||
}
|
||||
lifecycleScope.launch(coroutineContext) {
|
||||
downloadFile(downloadItem)
|
||||
downloadFile(Database.downloadDao().findDownloadItemById(id))
|
||||
}
|
||||
}
|
||||
|
||||
@ -340,10 +327,8 @@ class DownloadService : LifecycleService() {
|
||||
stream?.find { it.format == item.format && it.quality == item.quality }?.let {
|
||||
item.url = it.url
|
||||
}
|
||||
query {
|
||||
Database.downloadDao().updateDownloadItem(item)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether the file downloading or not.
|
||||
|
@ -9,12 +9,12 @@ import android.os.Bundle
|
||||
import android.text.format.DateUtils
|
||||
import android.view.View
|
||||
import androidx.activity.viewModels
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import com.github.libretube.constants.IntentData
|
||||
import com.github.libretube.databinding.ActivityOfflinePlayerBinding
|
||||
import com.github.libretube.databinding.ExoStyledPlayerControlViewBinding
|
||||
import com.github.libretube.db.DatabaseHolder.Database
|
||||
import com.github.libretube.enums.FileType
|
||||
import com.github.libretube.extensions.awaitQuery
|
||||
import com.github.libretube.extensions.updateParameters
|
||||
import com.github.libretube.helpers.PlayerHelper
|
||||
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.util.MimeTypes
|
||||
import java.io.File
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
class OfflinePlayerActivity : BaseActivity() {
|
||||
private lateinit var binding: ActivityOfflinePlayerBinding
|
||||
@ -108,7 +111,8 @@ class OfflinePlayerActivity : BaseActivity() {
|
||||
}
|
||||
|
||||
private fun playVideo() {
|
||||
val downloadFiles = awaitQuery {
|
||||
lifecycleScope.launch {
|
||||
val downloadFiles = withContext(Dispatchers.IO) {
|
||||
Database.downloadDao().findById(videoId).downloadItems
|
||||
}
|
||||
|
||||
@ -130,6 +134,7 @@ class OfflinePlayerActivity : BaseActivity() {
|
||||
player.prepare()
|
||||
player.play()
|
||||
}
|
||||
}
|
||||
|
||||
private fun setMediaSource(videoUri: Uri?, audioUri: Uri?, subtitleUri: Uri?) {
|
||||
val subtitle = subtitleUri?.let {
|
||||
|
@ -13,12 +13,13 @@ import com.github.libretube.databinding.DownloadedMediaRowBinding
|
||||
import com.github.libretube.db.DatabaseHolder
|
||||
import com.github.libretube.db.obj.DownloadWithItems
|
||||
import com.github.libretube.extensions.formatAsFileSize
|
||||
import com.github.libretube.extensions.query
|
||||
import com.github.libretube.helpers.ImageHelper
|
||||
import com.github.libretube.ui.activities.OfflinePlayerActivity
|
||||
import com.github.libretube.ui.viewholders.DownloadsViewHolder
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||
import java.io.File
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.runBlocking
|
||||
|
||||
class DownloadsAdapter(
|
||||
private val context: Context,
|
||||
@ -105,7 +106,7 @@ class DownloadsAdapter(
|
||||
}
|
||||
}
|
||||
|
||||
query {
|
||||
runBlocking(Dispatchers.IO) {
|
||||
DatabaseHolder.Database.downloadDao().deleteDownload(download)
|
||||
}
|
||||
downloads.removeAt(position)
|
||||
|
@ -16,7 +16,6 @@ import com.github.libretube.R
|
||||
import com.github.libretube.databinding.FragmentDownloadsBinding
|
||||
import com.github.libretube.db.DatabaseHolder.Database
|
||||
import com.github.libretube.db.obj.DownloadWithItems
|
||||
import com.github.libretube.extensions.awaitQuery
|
||||
import com.github.libretube.extensions.formatAsFileSize
|
||||
import com.github.libretube.helpers.DownloadHelper
|
||||
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.viewholders.DownloadsViewHolder
|
||||
import java.io.File
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.flow.collectLatest
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.runBlocking
|
||||
|
||||
class DownloadsFragment : BaseFragment() {
|
||||
private lateinit var binding: FragmentDownloadsBinding
|
||||
@ -69,9 +70,10 @@ class DownloadsFragment : BaseFragment() {
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
|
||||
awaitQuery {
|
||||
downloads.addAll(Database.downloadDao().getAll())
|
||||
val dbDownloads = runBlocking(Dispatchers.IO) {
|
||||
Database.downloadDao().getAll()
|
||||
}
|
||||
downloads.addAll(dbDownloads)
|
||||
if (downloads.isEmpty()) return
|
||||
|
||||
binding.downloadsEmpty.visibility = View.GONE
|
||||
|
Loading…
Reference in New Issue
Block a user