diff --git a/app/src/main/java/com/github/libretube/db/dao/DownloadDao.kt b/app/src/main/java/com/github/libretube/db/dao/DownloadDao.kt index 1bd6023f4..98ce341f2 100644 --- a/app/src/main/java/com/github/libretube/db/dao/DownloadDao.kt +++ b/app/src/main/java/com/github/libretube/db/dao/DownloadDao.kt @@ -15,37 +15,25 @@ import com.github.libretube.db.obj.DownloadWithItems interface DownloadDao { @Transaction @Query("SELECT * FROM download") - fun getAll(): List + suspend fun getAll(): List @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) } diff --git a/app/src/main/java/com/github/libretube/services/DownloadService.kt b/app/src/main/java/com/github/libretube/services/DownloadService.kt index e3f9aa072..f3faf70ac 100644 --- a/app/src/main/java/com/github/libretube/services/DownloadService.kt +++ b/app/src/main/java/com/github/libretube/services/DownloadService.kt @@ -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,9 +166,7 @@ class DownloadService : LifecycleService() { url.getContentLength().let { size -> if (size > 0 && size != item.downloadSize) { item.downloadSize = size - query { - Database.downloadDao().updateDownloadItem(item) - } + Database.downloadDao().updateDownloadItem(item) } } @@ -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,9 +327,7 @@ class DownloadService : LifecycleService() { stream?.find { it.format == item.format && it.quality == item.quality }?.let { item.url = it.url } - query { - Database.downloadDao().updateDownloadItem(item) - } + Database.downloadDao().updateDownloadItem(item) } /** diff --git a/app/src/main/java/com/github/libretube/ui/activities/OfflinePlayerActivity.kt b/app/src/main/java/com/github/libretube/ui/activities/OfflinePlayerActivity.kt index 9000bddec..950de3674 100644 --- a/app/src/main/java/com/github/libretube/ui/activities/OfflinePlayerActivity.kt +++ b/app/src/main/java/com/github/libretube/ui/activities/OfflinePlayerActivity.kt @@ -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,27 +111,29 @@ class OfflinePlayerActivity : BaseActivity() { } private fun playVideo() { - val downloadFiles = awaitQuery { - Database.downloadDao().findById(videoId).downloadItems + lifecycleScope.launch { + 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?) { diff --git a/app/src/main/java/com/github/libretube/ui/adapters/DownloadsAdapter.kt b/app/src/main/java/com/github/libretube/ui/adapters/DownloadsAdapter.kt index cba95ab55..af418becf 100644 --- a/app/src/main/java/com/github/libretube/ui/adapters/DownloadsAdapter.kt +++ b/app/src/main/java/com/github/libretube/ui/adapters/DownloadsAdapter.kt @@ -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) diff --git a/app/src/main/java/com/github/libretube/ui/fragments/DownloadsFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/DownloadsFragment.kt index 5c59dc8ea..94302b411 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/DownloadsFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/DownloadsFragment.kt @@ -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