From fdc17d6dfd5753cebf4701eb72d9421c1288b16e Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Thu, 9 Feb 2023 05:48:28 +0530 Subject: [PATCH] Convert WatchHistoryDao methods to suspend functions. --- .../com/github/libretube/db/DatabaseHelper.kt | 24 +++++++++---------- .../libretube/db/dao/WatchHistoryDao.kt | 11 ++++----- .../github/libretube/helpers/BackupHelper.kt | 4 +--- .../ui/adapters/WatchHistoryAdapter.kt | 5 ++-- .../libretube/ui/fragments/PlayerFragment.kt | 3 ++- .../ui/fragments/WatchHistoryFragment.kt | 14 ++++++----- .../ui/preferences/HistorySettings.kt | 10 +++++--- 7 files changed, 37 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/com/github/libretube/db/DatabaseHelper.kt b/app/src/main/java/com/github/libretube/db/DatabaseHelper.kt index 35b58768a..4c67f461d 100644 --- a/app/src/main/java/com/github/libretube/db/DatabaseHelper.kt +++ b/app/src/main/java/com/github/libretube/db/DatabaseHelper.kt @@ -8,11 +8,13 @@ import com.github.libretube.db.obj.WatchHistoryItem import com.github.libretube.extensions.query import com.github.libretube.extensions.toID import com.github.libretube.helpers.PreferenceHelper +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext object DatabaseHelper { private const val MAX_SEARCH_HISTORY_SIZE = 20 - fun addToWatchHistory(videoId: String, streams: Streams) { + suspend fun addToWatchHistory(videoId: String, streams: Streams) = withContext(Dispatchers.IO) { val watchHistoryItem = WatchHistoryItem( videoId, streams.title, @@ -23,18 +25,16 @@ object DatabaseHelper { streams.thumbnailUrl, streams.duration ) - query { - Database.watchHistoryDao().insertAll(watchHistoryItem) - val maxHistorySize = - PreferenceHelper.getString(PreferenceKeys.WATCH_HISTORY_SIZE, "100") - if (maxHistorySize == "unlimited") return@query + Database.watchHistoryDao().insertAll(listOf(watchHistoryItem)) + val maxHistorySize = PreferenceHelper.getString(PreferenceKeys.WATCH_HISTORY_SIZE, "100") + if (maxHistorySize == "unlimited") { + return@withContext + } - // delete the first watch history entry if the limit is reached - val watchHistory = Database.watchHistoryDao().getAll() - if (watchHistory.size > maxHistorySize.toInt()) { - Database.watchHistoryDao() - .delete(watchHistory.first()) - } + // delete the first watch history entry if the limit is reached + val watchHistory = Database.watchHistoryDao().getAll() + if (watchHistory.size > maxHistorySize.toInt()) { + Database.watchHistoryDao().delete(watchHistory.first()) } } diff --git a/app/src/main/java/com/github/libretube/db/dao/WatchHistoryDao.kt b/app/src/main/java/com/github/libretube/db/dao/WatchHistoryDao.kt index dfa5b732a..c0c6828b5 100644 --- a/app/src/main/java/com/github/libretube/db/dao/WatchHistoryDao.kt +++ b/app/src/main/java/com/github/libretube/db/dao/WatchHistoryDao.kt @@ -10,17 +10,14 @@ import com.github.libretube.db.obj.WatchHistoryItem @Dao interface WatchHistoryDao { @Query("SELECT * FROM watchHistoryItem") - fun getAll(): List - - @Query("SELECT * FROM watchHistoryItem WHERE videoId LIKE :videoId LIMIT 1") - fun findById(videoId: String): WatchHistoryItem + suspend fun getAll(): List @Insert(onConflict = OnConflictStrategy.REPLACE) - fun insertAll(vararg watchHistoryItems: WatchHistoryItem) + suspend fun insertAll(watchHistoryItems: List) @Delete - fun delete(watchHistoryItem: WatchHistoryItem) + suspend fun delete(watchHistoryItem: WatchHistoryItem) @Query("DELETE FROM watchHistoryItem") - fun deleteAll() + suspend fun deleteAll() } diff --git a/app/src/main/java/com/github/libretube/helpers/BackupHelper.kt b/app/src/main/java/com/github/libretube/helpers/BackupHelper.kt index b520066ff..3bf590072 100644 --- a/app/src/main/java/com/github/libretube/helpers/BackupHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/BackupHelper.kt @@ -46,9 +46,7 @@ object BackupHelper { JsonHelper.json.decodeFromStream(it) } ?: return - Database.watchHistoryDao().insertAll( - *backupFile.watchHistory.orEmpty().toTypedArray() - ) + Database.watchHistoryDao().insertAll(backupFile.watchHistory.orEmpty()) Database.searchHistoryDao().insertAll( *backupFile.searchHistory.orEmpty().toTypedArray() ) diff --git a/app/src/main/java/com/github/libretube/ui/adapters/WatchHistoryAdapter.kt b/app/src/main/java/com/github/libretube/ui/adapters/WatchHistoryAdapter.kt index bed0f0b77..d18c10edb 100644 --- a/app/src/main/java/com/github/libretube/ui/adapters/WatchHistoryAdapter.kt +++ b/app/src/main/java/com/github/libretube/ui/adapters/WatchHistoryAdapter.kt @@ -7,7 +7,6 @@ import androidx.recyclerview.widget.RecyclerView import com.github.libretube.databinding.VideoRowBinding import com.github.libretube.db.DatabaseHolder import com.github.libretube.db.obj.WatchHistoryItem -import com.github.libretube.extensions.query import com.github.libretube.helpers.ImageHelper import com.github.libretube.helpers.NavigationHelper import com.github.libretube.ui.base.BaseActivity @@ -15,6 +14,8 @@ import com.github.libretube.ui.extensions.setFormattedDuration import com.github.libretube.ui.extensions.setWatchProgressLength import com.github.libretube.ui.sheets.VideoOptionsBottomSheet import com.github.libretube.ui.viewholders.WatchHistoryViewHolder +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.runBlocking class WatchHistoryAdapter( private val watchHistory: MutableList @@ -27,7 +28,7 @@ class WatchHistoryAdapter( fun removeFromWatchHistory(position: Int) { val history = watchHistory[position] - query { + runBlocking(Dispatchers.IO) { DatabaseHolder.Database.watchHistoryDao().delete(history) } watchHistory.removeAt(position) diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt index a9c9bb38d..9e62edbf8 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt @@ -121,6 +121,7 @@ import kotlin.math.abs import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import kotlinx.datetime.LocalDate import kotlinx.serialization.encodeToString import retrofit2.HttpException @@ -707,7 +708,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { streamItem.url?.toID()?.let { playNextVideo(it) } } - runOnUiThread { + withContext(Dispatchers.Main) { // hide the button to skip SponsorBlock segments manually binding.sbSkipBtn.visibility = View.GONE diff --git a/app/src/main/java/com/github/libretube/ui/fragments/WatchHistoryFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/WatchHistoryFragment.kt index f39eca543..9e2ad4d93 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/WatchHistoryFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/WatchHistoryFragment.kt @@ -9,6 +9,7 @@ import android.view.ViewGroup import androidx.core.os.postDelayed import androidx.core.view.updatePadding import androidx.fragment.app.activityViewModels +import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView @@ -16,9 +17,7 @@ import com.github.libretube.R import com.github.libretube.api.obj.StreamItem import com.github.libretube.databinding.FragmentWatchHistoryBinding import com.github.libretube.db.DatabaseHolder.Companion.Database -import com.github.libretube.extensions.awaitQuery import com.github.libretube.extensions.dpToPx -import com.github.libretube.extensions.query import com.github.libretube.helpers.NavigationHelper import com.github.libretube.helpers.ProxyHelper import com.github.libretube.ui.adapters.WatchHistoryAdapter @@ -26,6 +25,9 @@ import com.github.libretube.ui.base.BaseFragment import com.github.libretube.ui.models.PlayerViewModel import com.github.libretube.util.PlayingQueue import com.google.android.material.dialog.MaterialAlertDialogBuilder +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.runBlocking class WatchHistoryFragment : BaseFragment() { private lateinit var binding: FragmentWatchHistoryBinding @@ -51,9 +53,9 @@ class WatchHistoryFragment : BaseFragment() { ) } - val watchHistory = awaitQuery { - Database.watchHistoryDao().getAll() - }.reversed() + val watchHistory = runBlocking(Dispatchers.IO) { + Database.watchHistoryDao().getAll().reversed() + } if (watchHistory.isEmpty()) return @@ -69,7 +71,7 @@ class WatchHistoryFragment : BaseFragment() { .setPositiveButton(R.string.okay) { _, _ -> binding.historyScrollView.visibility = View.GONE binding.historyEmpty.visibility = View.VISIBLE - query { + lifecycleScope.launch(Dispatchers.IO) { Database.watchHistoryDao().deleteAll() } } diff --git a/app/src/main/java/com/github/libretube/ui/preferences/HistorySettings.kt b/app/src/main/java/com/github/libretube/ui/preferences/HistorySettings.kt index 7bda49e6c..4c8037c68 100644 --- a/app/src/main/java/com/github/libretube/ui/preferences/HistorySettings.kt +++ b/app/src/main/java/com/github/libretube/ui/preferences/HistorySettings.kt @@ -1,13 +1,15 @@ package com.github.libretube.ui.preferences import android.os.Bundle +import androidx.lifecycle.lifecycleScope import androidx.preference.Preference import com.github.libretube.R import com.github.libretube.constants.PreferenceKeys import com.github.libretube.db.DatabaseHolder.Companion.Database -import com.github.libretube.extensions.query import com.github.libretube.ui.base.BasePreferenceFragment import com.google.android.material.dialog.MaterialAlertDialogBuilder +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch class HistorySettings : BasePreferenceFragment() { override val titleResourceId: Int = R.string.history @@ -51,14 +53,16 @@ class HistorySettings : BasePreferenceFragment() { } } - private fun showClearDialog(title: Int, action: () -> Unit) { + private fun showClearDialog(title: Int, action: suspend () -> Unit) { MaterialAlertDialogBuilder(requireContext()) .setTitle(title) .setMessage(R.string.irreversible) .setNegativeButton(R.string.cancel, null) .setPositiveButton(R.string.okay) { _, _ -> // clear the selected preference preferences - query(action) + lifecycleScope.launch(Dispatchers.IO) { + action() + } } .show() }