Merge pull request #1901 from Bnyro/master

cleanup database queries
This commit is contained in:
Bnyro 2022-11-18 18:44:09 +01:00 committed by GitHub
commit 7233f81b3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 24 additions and 45 deletions

View File

@ -5,12 +5,13 @@ import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.db.DatabaseHolder.Companion.Database
import com.github.libretube.db.obj.SearchHistoryItem
import com.github.libretube.db.obj.WatchHistoryItem
import com.github.libretube.db.obj.WatchPosition
import com.github.libretube.extensions.query
import com.github.libretube.extensions.toID
import com.github.libretube.util.PreferenceHelper
object DatabaseHelper {
private const val MAX_SEARCH_HISTORY_SIZE = 20
fun addToWatchHistory(videoId: String, streams: Streams) {
val watchHistoryItem = WatchHistoryItem(
videoId,
@ -37,40 +38,13 @@ object DatabaseHelper {
}
}
fun removeFromWatchHistory(index: Int) {
query {
Database.watchHistoryDao().delete(
Database.watchHistoryDao().getAll()[index]
)
}
}
fun saveWatchPosition(videoId: String, position: Long) {
val watchPosition = WatchPosition(
videoId,
position
)
query {
Database.watchPositionDao().insertAll(watchPosition)
}
}
fun removeWatchPosition(videoId: String) {
query {
Database.watchPositionDao().findById(videoId)?.let {
Database.watchPositionDao().delete(it)
}
}
}
fun addToSearchHistory(searchHistoryItem: SearchHistoryItem) {
query {
Database.searchHistoryDao().insertAll(searchHistoryItem)
val maxHistorySize = 20
// delete the first watch history entry if the limit is reached
val searchHistory = Database.searchHistoryDao().getAll()
if (searchHistory.size > maxHistorySize) {
if (searchHistory.size > MAX_SEARCH_HISTORY_SIZE) {
Database.searchHistoryDao()
.delete(searchHistory.first())
}

View File

@ -21,6 +21,9 @@ interface PlaylistBookmarkDao {
@Delete
fun delete(playlistBookmark: PlaylistBookmark)
@Query("DELETE FROM playlistBookmark WHERE playlistId = :playlistId")
fun deleteById(playlistId: String)
@Query("SELECT EXISTS(SELECT * FROM playlistBookmark WHERE playlistId= :playlistId)")
fun includes(playlistId: String): Boolean

View File

@ -21,6 +21,9 @@ interface WatchPositionDao {
@Delete
fun delete(watchPosition: WatchPosition)
@Query("DELETE FROM watchHistoryItem WHERE videoId = :videoId")
fun deleteById(videoId: String)
@Query("DELETE FROM watchPosition")
fun deleteAll()
}

View File

@ -21,9 +21,11 @@ import com.github.libretube.constants.BACKGROUND_CHANNEL_ID
import com.github.libretube.constants.IntentData
import com.github.libretube.constants.PLAYER_NOTIFICATION_ID
import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.db.DatabaseHelper
import com.github.libretube.db.DatabaseHolder
import com.github.libretube.db.DatabaseHolder.Companion.Database
import com.github.libretube.db.obj.WatchPosition
import com.github.libretube.extensions.awaitQuery
import com.github.libretube.extensions.query
import com.github.libretube.extensions.toID
import com.github.libretube.extensions.toStreamItem
import com.github.libretube.util.NowPlayingNotification
@ -138,7 +140,9 @@ class BackgroundMode : Service() {
private fun updateWatchPosition() {
player?.currentPosition?.let {
DatabaseHelper.saveWatchPosition(videoId, it)
query {
Database.watchPositionDao().insertAll(WatchPosition(videoId, it))
}
}
handler.postDelayed(this::updateWatchPosition, 500)
}

View File

@ -5,7 +5,7 @@ import android.view.ViewGroup
import androidx.fragment.app.FragmentManager
import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.databinding.WatchHistoryRowBinding
import com.github.libretube.db.DatabaseHelper
import com.github.libretube.db.DatabaseHolder
import com.github.libretube.db.obj.WatchHistoryItem
import com.github.libretube.ui.extensions.setFormattedDuration
import com.github.libretube.ui.extensions.setWatchProgressLength
@ -21,7 +21,7 @@ class WatchHistoryAdapter(
RecyclerView.Adapter<WatchHistoryViewHolder>() {
fun removeFromWatchHistory(position: Int) {
DatabaseHelper.removeFromWatchHistory(position)
DatabaseHolder.Database.watchHistoryDao().delete(watchHistory[position])
watchHistory.removeAt(position)
notifyItemRemoved(position)
notifyItemRangeChanged(position, itemCount)

View File

@ -51,6 +51,7 @@ import com.github.libretube.databinding.ExoStyledPlayerControlViewBinding
import com.github.libretube.databinding.FragmentPlayerBinding
import com.github.libretube.db.DatabaseHelper
import com.github.libretube.db.DatabaseHolder.Companion.Database
import com.github.libretube.db.obj.WatchPosition
import com.github.libretube.enums.ShareObjectType
import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.awaitQuery
@ -519,13 +520,12 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
// save the watch position if video isn't finished and option enabled
private fun saveWatchPosition() {
if (PlayerHelper.watchPositionsEnabled && exoPlayer.currentPosition != exoPlayer.duration) {
DatabaseHelper.saveWatchPosition(
videoId!!,
exoPlayer.currentPosition
)
query {
Database.watchPositionDao().insertAll(WatchPosition(videoId!!, exoPlayer.currentPosition))
}
} else if (PlayerHelper.watchPositionsEnabled) {
// delete watch position if video has ended
DatabaseHelper.removeWatchPosition(videoId!!)
Database.watchPositionDao().deleteById(videoId!!)
}
}
@ -846,10 +846,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
// save the watch position when paused
if (playbackState == PlaybackState.STATE_PAUSED) {
query {
DatabaseHelper.saveWatchPosition(
videoId!!,
exoPlayer.currentPosition
)
Database.watchPositionDao().insertAll(WatchPosition(videoId!!, exoPlayer.currentPosition))
}
}

View File

@ -137,9 +137,7 @@ class PlaylistFragment : BaseFragment() {
updateBookmarkRes()
query {
if (!isBookmarked) {
DatabaseHolder.Database.playlistBookmarkDao().delete(
DatabaseHolder.Database.playlistBookmarkDao().findById(playlistId!!)
)
DatabaseHolder.Database.playlistBookmarkDao().deleteById(playlistId!!)
} else {
DatabaseHolder.Database.playlistBookmarkDao().insertAll(
PlaylistBookmark(