Merge pull request #3012 from Isira-Seneviratne/WatchHistoryDao_suspend

Convert WatchHistoryDao methods to suspend functions.
This commit is contained in:
Bnyro 2023-02-10 16:46:27 +01:00 committed by GitHub
commit ce3edce7d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 37 additions and 34 deletions

View File

@ -8,11 +8,13 @@ import com.github.libretube.db.obj.WatchHistoryItem
import com.github.libretube.extensions.query import com.github.libretube.extensions.query
import com.github.libretube.extensions.toID import com.github.libretube.extensions.toID
import com.github.libretube.helpers.PreferenceHelper import com.github.libretube.helpers.PreferenceHelper
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
object DatabaseHelper { object DatabaseHelper {
private const val MAX_SEARCH_HISTORY_SIZE = 20 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( val watchHistoryItem = WatchHistoryItem(
videoId, videoId,
streams.title, streams.title,
@ -23,18 +25,16 @@ object DatabaseHelper {
streams.thumbnailUrl, streams.thumbnailUrl,
streams.duration streams.duration
) )
query { Database.watchHistoryDao().insertAll(listOf(watchHistoryItem))
Database.watchHistoryDao().insertAll(watchHistoryItem) val maxHistorySize = PreferenceHelper.getString(PreferenceKeys.WATCH_HISTORY_SIZE, "100")
val maxHistorySize = if (maxHistorySize == "unlimited") {
PreferenceHelper.getString(PreferenceKeys.WATCH_HISTORY_SIZE, "100") return@withContext
if (maxHistorySize == "unlimited") return@query }
// delete the first watch history entry if the limit is reached // delete the first watch history entry if the limit is reached
val watchHistory = Database.watchHistoryDao().getAll() val watchHistory = Database.watchHistoryDao().getAll()
if (watchHistory.size > maxHistorySize.toInt()) { if (watchHistory.size > maxHistorySize.toInt()) {
Database.watchHistoryDao() Database.watchHistoryDao().delete(watchHistory.first())
.delete(watchHistory.first())
}
} }
} }

View File

@ -10,17 +10,14 @@ import com.github.libretube.db.obj.WatchHistoryItem
@Dao @Dao
interface WatchHistoryDao { interface WatchHistoryDao {
@Query("SELECT * FROM watchHistoryItem") @Query("SELECT * FROM watchHistoryItem")
fun getAll(): List<WatchHistoryItem> suspend fun getAll(): List<WatchHistoryItem>
@Query("SELECT * FROM watchHistoryItem WHERE videoId LIKE :videoId LIMIT 1")
fun findById(videoId: String): WatchHistoryItem
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertAll(vararg watchHistoryItems: WatchHistoryItem) suspend fun insertAll(watchHistoryItems: List<WatchHistoryItem>)
@Delete @Delete
fun delete(watchHistoryItem: WatchHistoryItem) suspend fun delete(watchHistoryItem: WatchHistoryItem)
@Query("DELETE FROM watchHistoryItem") @Query("DELETE FROM watchHistoryItem")
fun deleteAll() suspend fun deleteAll()
} }

View File

@ -46,9 +46,7 @@ object BackupHelper {
JsonHelper.json.decodeFromStream<BackupFile>(it) JsonHelper.json.decodeFromStream<BackupFile>(it)
} ?: return } ?: return
Database.watchHistoryDao().insertAll( Database.watchHistoryDao().insertAll(backupFile.watchHistory.orEmpty())
*backupFile.watchHistory.orEmpty().toTypedArray()
)
Database.searchHistoryDao().insertAll( Database.searchHistoryDao().insertAll(
*backupFile.searchHistory.orEmpty().toTypedArray() *backupFile.searchHistory.orEmpty().toTypedArray()
) )

View File

@ -7,7 +7,6 @@ import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.databinding.VideoRowBinding import com.github.libretube.databinding.VideoRowBinding
import com.github.libretube.db.DatabaseHolder import com.github.libretube.db.DatabaseHolder
import com.github.libretube.db.obj.WatchHistoryItem import com.github.libretube.db.obj.WatchHistoryItem
import com.github.libretube.extensions.query
import com.github.libretube.helpers.ImageHelper import com.github.libretube.helpers.ImageHelper
import com.github.libretube.helpers.NavigationHelper import com.github.libretube.helpers.NavigationHelper
import com.github.libretube.ui.base.BaseActivity 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.extensions.setWatchProgressLength
import com.github.libretube.ui.sheets.VideoOptionsBottomSheet import com.github.libretube.ui.sheets.VideoOptionsBottomSheet
import com.github.libretube.ui.viewholders.WatchHistoryViewHolder import com.github.libretube.ui.viewholders.WatchHistoryViewHolder
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
class WatchHistoryAdapter( class WatchHistoryAdapter(
private val watchHistory: MutableList<WatchHistoryItem> private val watchHistory: MutableList<WatchHistoryItem>
@ -27,7 +28,7 @@ class WatchHistoryAdapter(
fun removeFromWatchHistory(position: Int) { fun removeFromWatchHistory(position: Int) {
val history = watchHistory[position] val history = watchHistory[position]
query { runBlocking(Dispatchers.IO) {
DatabaseHolder.Database.watchHistoryDao().delete(history) DatabaseHolder.Database.watchHistoryDao().delete(history)
} }
watchHistory.removeAt(position) watchHistory.removeAt(position)

View File

@ -121,6 +121,7 @@ import kotlin.math.abs
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.datetime.LocalDate import kotlinx.datetime.LocalDate
import kotlinx.serialization.encodeToString import kotlinx.serialization.encodeToString
import retrofit2.HttpException import retrofit2.HttpException
@ -707,7 +708,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
streamItem.url?.toID()?.let { playNextVideo(it) } streamItem.url?.toID()?.let { playNextVideo(it) }
} }
runOnUiThread { withContext(Dispatchers.Main) {
// hide the button to skip SponsorBlock segments manually // hide the button to skip SponsorBlock segments manually
binding.sbSkipBtn.visibility = View.GONE binding.sbSkipBtn.visibility = View.GONE

View File

@ -9,6 +9,7 @@ import android.view.ViewGroup
import androidx.core.os.postDelayed import androidx.core.os.postDelayed
import androidx.core.view.updatePadding import androidx.core.view.updatePadding
import androidx.fragment.app.activityViewModels import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView 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.api.obj.StreamItem
import com.github.libretube.databinding.FragmentWatchHistoryBinding import com.github.libretube.databinding.FragmentWatchHistoryBinding
import com.github.libretube.db.DatabaseHolder.Companion.Database 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.dpToPx
import com.github.libretube.extensions.query
import com.github.libretube.helpers.NavigationHelper import com.github.libretube.helpers.NavigationHelper
import com.github.libretube.helpers.ProxyHelper import com.github.libretube.helpers.ProxyHelper
import com.github.libretube.ui.adapters.WatchHistoryAdapter 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.ui.models.PlayerViewModel
import com.github.libretube.util.PlayingQueue import com.github.libretube.util.PlayingQueue
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
class WatchHistoryFragment : BaseFragment() { class WatchHistoryFragment : BaseFragment() {
private lateinit var binding: FragmentWatchHistoryBinding private lateinit var binding: FragmentWatchHistoryBinding
@ -51,9 +53,9 @@ class WatchHistoryFragment : BaseFragment() {
) )
} }
val watchHistory = awaitQuery { val watchHistory = runBlocking(Dispatchers.IO) {
Database.watchHistoryDao().getAll() Database.watchHistoryDao().getAll().reversed()
}.reversed() }
if (watchHistory.isEmpty()) return if (watchHistory.isEmpty()) return
@ -69,7 +71,7 @@ class WatchHistoryFragment : BaseFragment() {
.setPositiveButton(R.string.okay) { _, _ -> .setPositiveButton(R.string.okay) { _, _ ->
binding.historyScrollView.visibility = View.GONE binding.historyScrollView.visibility = View.GONE
binding.historyEmpty.visibility = View.VISIBLE binding.historyEmpty.visibility = View.VISIBLE
query { lifecycleScope.launch(Dispatchers.IO) {
Database.watchHistoryDao().deleteAll() Database.watchHistoryDao().deleteAll()
} }
} }

View File

@ -1,13 +1,15 @@
package com.github.libretube.ui.preferences package com.github.libretube.ui.preferences
import android.os.Bundle import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import androidx.preference.Preference import androidx.preference.Preference
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.constants.PreferenceKeys import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.db.DatabaseHolder.Companion.Database import com.github.libretube.db.DatabaseHolder.Companion.Database
import com.github.libretube.extensions.query
import com.github.libretube.ui.base.BasePreferenceFragment import com.github.libretube.ui.base.BasePreferenceFragment
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
class HistorySettings : BasePreferenceFragment() { class HistorySettings : BasePreferenceFragment() {
override val titleResourceId: Int = R.string.history 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()) MaterialAlertDialogBuilder(requireContext())
.setTitle(title) .setTitle(title)
.setMessage(R.string.irreversible) .setMessage(R.string.irreversible)
.setNegativeButton(R.string.cancel, null) .setNegativeButton(R.string.cancel, null)
.setPositiveButton(R.string.okay) { _, _ -> .setPositiveButton(R.string.okay) { _, _ ->
// clear the selected preference preferences // clear the selected preference preferences
query(action) lifecycleScope.launch(Dispatchers.IO) {
action()
}
} }
.show() .show()
} }