refactor: improve bookmarks performance and reliability

This commit is contained in:
Bnyro 2024-08-23 13:40:11 +02:00
parent a6e7576f34
commit 0ee013240c
3 changed files with 9 additions and 3 deletions

View File

@ -12,6 +12,9 @@ interface PlaylistBookmarkDao {
@Query("SELECT * FROM playlistBookmark")
suspend fun getAll(): List<PlaylistBookmark>
@Query("SELECT * FROM playlistBookmark WHERE playlistId = :playlistId LIMIT 1")
suspend fun findById(playlistId: String): PlaylistBookmark?
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insert(bookmark: PlaylistBookmark)

View File

@ -29,6 +29,7 @@ import com.github.libretube.extensions.ceilHalf
import com.github.libretube.extensions.dpToPx
import com.github.libretube.helpers.NavBarHelper
import com.github.libretube.helpers.PreferenceHelper
import com.github.libretube.helpers.ProxyHelper
import com.github.libretube.ui.adapters.PlaylistBookmarkAdapter
import com.github.libretube.ui.adapters.PlaylistsAdapter
import com.github.libretube.ui.base.DynamicLayoutManagerFragment
@ -140,7 +141,9 @@ class LibraryFragment : DynamicLayoutManagerFragment() {
private fun initBookmarks() {
lifecycleScope.launch {
val bookmarks = withContext(Dispatchers.IO) {
DatabaseHolder.Database.playlistBookmarkDao().getAll()
DatabaseHolder.Database.playlistBookmarkDao().getAll().map {
it.copy(thumbnailUrl = ProxyHelper.rewriteUrl(it.thumbnailUrl))
}
}
val binding = _binding ?: return@launch

View File

@ -290,8 +290,8 @@ class PlaylistFragment : DynamicLayoutManagerFragment() {
if (!isBookmarked) return
withContext(Dispatchers.IO) {
// update the playlist thumbnail and title if bookmarked
val playlistBookmark = DatabaseHolder.Database.playlistBookmarkDao().getAll()
.firstOrNull { it.playlistId == playlistId } ?: return@withContext
val playlistBookmark = DatabaseHolder.Database.playlistBookmarkDao().findById(playlistId)
?: return@withContext
if (playlistBookmark.thumbnailUrl != playlist.thumbnailUrl ||
playlistBookmark.playlistName != playlist.name ||
playlistBookmark.videos != playlist.videos