Convert WatchHistoryDao methods to suspend functions.

This commit is contained in:
Isira Seneviratne 2023-02-09 05:48:28 +05:30
parent dca36fa95a
commit fdc17d6dfd
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.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())
}
Database.watchHistoryDao().delete(watchHistory.first())
}
}

View File

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

View File

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

View File

@ -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

View File

@ -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()
}
}

View File

@ -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()
}