From 6db616591ce9c9896b6e5518a069b3cfd46acae1 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Fri, 18 Nov 2022 18:42:59 +0100 Subject: [PATCH] cleanup database queries --- .../com/github/libretube/db/DatabaseHelper.kt | 32 ++----------------- .../libretube/db/dao/PlaylistBookmarkDao.kt | 3 ++ .../libretube/db/dao/WatchPositionDao.kt | 3 ++ .../libretube/services/BackgroundMode.kt | 8 +++-- .../ui/adapters/WatchHistoryAdapter.kt | 4 +-- .../libretube/ui/fragments/PlayerFragment.kt | 15 ++++----- .../ui/fragments/PlaylistFragment.kt | 4 +-- 7 files changed, 24 insertions(+), 45 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 c5c2a21c8..c6fdf259f 100644 --- a/app/src/main/java/com/github/libretube/db/DatabaseHelper.kt +++ b/app/src/main/java/com/github/libretube/db/DatabaseHelper.kt @@ -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()) } diff --git a/app/src/main/java/com/github/libretube/db/dao/PlaylistBookmarkDao.kt b/app/src/main/java/com/github/libretube/db/dao/PlaylistBookmarkDao.kt index 34df4e295..493c96470 100644 --- a/app/src/main/java/com/github/libretube/db/dao/PlaylistBookmarkDao.kt +++ b/app/src/main/java/com/github/libretube/db/dao/PlaylistBookmarkDao.kt @@ -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 diff --git a/app/src/main/java/com/github/libretube/db/dao/WatchPositionDao.kt b/app/src/main/java/com/github/libretube/db/dao/WatchPositionDao.kt index 6855eaab4..b08f6e2cf 100644 --- a/app/src/main/java/com/github/libretube/db/dao/WatchPositionDao.kt +++ b/app/src/main/java/com/github/libretube/db/dao/WatchPositionDao.kt @@ -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() } diff --git a/app/src/main/java/com/github/libretube/services/BackgroundMode.kt b/app/src/main/java/com/github/libretube/services/BackgroundMode.kt index ddf0249a5..412244175 100644 --- a/app/src/main/java/com/github/libretube/services/BackgroundMode.kt +++ b/app/src/main/java/com/github/libretube/services/BackgroundMode.kt @@ -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) } 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 7e2afd2cc..dfec905cb 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 @@ -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() { fun removeFromWatchHistory(position: Int) { - DatabaseHelper.removeFromWatchHistory(position) + DatabaseHolder.Database.watchHistoryDao().delete(watchHistory[position]) watchHistory.removeAt(position) notifyItemRemoved(position) notifyItemRangeChanged(position, itemCount) 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 fe2f78e92..b2c577392 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 @@ -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)) } } diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlaylistFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlaylistFragment.kt index c72388081..53649852f 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlaylistFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlaylistFragment.kt @@ -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(