Convert PlaylistBookmarkDao methods to suspend functions.

This commit is contained in:
Isira Seneviratne 2023-02-22 03:58:04 +05:30
parent 015e389227
commit 675340a190
8 changed files with 61 additions and 75 deletions

View File

@ -1,7 +1,6 @@
package com.github.libretube.db.dao
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
@ -11,26 +10,20 @@ import com.github.libretube.db.obj.PlaylistBookmark
@Dao
interface PlaylistBookmarkDao {
@Query("SELECT * FROM playlistBookmark")
fun getAll(): List<PlaylistBookmark>
@Query("SELECT * FROM playlistBookmark WHERE playlistId LIKE :playlistId LIMIT 1")
fun findById(playlistId: String): PlaylistBookmark
suspend fun getAll(): List<PlaylistBookmark>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertAll(vararg bookmarks: PlaylistBookmark)
@Delete
fun delete(playlistBookmark: PlaylistBookmark)
suspend fun insertAll(bookmarks: List<PlaylistBookmark>)
@Update
fun update(playlistBookmark: PlaylistBookmark)
suspend fun update(playlistBookmark: PlaylistBookmark)
@Query("DELETE FROM playlistBookmark WHERE playlistId = :playlistId")
fun deleteById(playlistId: String)
suspend fun deleteById(playlistId: String)
@Query("SELECT EXISTS(SELECT * FROM playlistBookmark WHERE playlistId= :playlistId)")
fun includes(playlistId: String): Boolean
suspend fun includes(playlistId: String): Boolean
@Query("DELETE FROM playlistBookmark")
fun deleteAll()
suspend fun deleteAll()
}

View File

@ -1,13 +0,0 @@
package com.github.libretube.extensions
fun <T> awaitQuery(
query: () -> T
): T {
var x: T? = null
val thread = Thread {
x = query.invoke()
}
thread.start()
thread.join()
return x!!
}

View File

@ -51,11 +51,9 @@ object BackupHelper {
Database.watchPositionDao().insertAll(backupFile.watchPositions.orEmpty())
Database.localSubscriptionDao().insertAll(backupFile.localSubscriptions.orEmpty())
Database.customInstanceDao().insertAll(backupFile.customInstances.orEmpty())
Database.playlistBookmarkDao().insertAll(
*backupFile.playlistBookmarks.orEmpty().toTypedArray()
)
Database.playlistBookmarkDao().insertAll(backupFile.playlistBookmarks.orEmpty())
backupFile.localPlaylists.orEmpty().forEach {
backupFile.localPlaylists?.forEach {
Database.localPlaylistsDao().createPlaylist(it.playlist)
val playlistId = Database.localPlaylistsDao().getAll().last().playlist.id
it.videos.forEach {

View File

@ -11,11 +11,13 @@ import com.github.libretube.databinding.PlaylistsRowBinding
import com.github.libretube.db.DatabaseHolder
import com.github.libretube.db.obj.PlaylistBookmark
import com.github.libretube.enums.PlaylistType
import com.github.libretube.extensions.query
import com.github.libretube.helpers.ImageHelper
import com.github.libretube.helpers.NavigationHelper
import com.github.libretube.ui.sheets.PlaylistOptionsBottomSheet
import com.github.libretube.ui.viewholders.PlaylistBookmarkViewHolder
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
class PlaylistBookmarkAdapter(
private val bookmarks: List<PlaylistBookmark>,
@ -80,13 +82,12 @@ class PlaylistBookmarkAdapter(
deletePlaylist.setImageResource(
if (isBookmarked) R.drawable.ic_bookmark else R.drawable.ic_bookmark_outlined
)
query {
CoroutineScope(Dispatchers.IO).launch {
if (!isBookmarked) {
DatabaseHolder.Database.playlistBookmarkDao()
.deleteById(bookmark.playlistId)
} else {
DatabaseHolder.Database.playlistBookmarkDao()
.insertAll(bookmark)
DatabaseHolder.Database.playlistBookmarkDao().insertAll(listOf(bookmark))
}
}
}

View File

@ -5,6 +5,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
@ -16,7 +17,6 @@ import com.github.libretube.api.SubscriptionHelper
import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.databinding.FragmentHomeBinding
import com.github.libretube.db.DatabaseHolder
import com.github.libretube.extensions.awaitQuery
import com.github.libretube.extensions.launchWhenCreatedIO
import com.github.libretube.helpers.LocaleHelper
import com.github.libretube.helpers.PreferenceHelper
@ -26,6 +26,7 @@ import com.github.libretube.ui.adapters.VideosAdapter
import com.github.libretube.ui.base.BaseFragment
import com.github.libretube.ui.models.SubscriptionsViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class HomeFragment : BaseFragment() {
@ -123,21 +124,22 @@ class HomeFragment : BaseFragment() {
}
private fun loadBookmarks() {
val bookmarkedPlaylists = awaitQuery {
DatabaseHolder.Database.playlistBookmarkDao().getAll()
}.takeIf { it.isNotEmpty() } ?: return
runOnUiThread {
makeVisible(binding.bookmarksTV, binding.bookmarksRV)
binding.bookmarksRV.layoutManager = LinearLayoutManager(
context,
LinearLayoutManager.HORIZONTAL,
false
)
binding.bookmarksRV.adapter = PlaylistBookmarkAdapter(
bookmarkedPlaylists,
PlaylistBookmarkAdapter.Companion.BookmarkMode.HOME
)
lifecycleScope.launch {
val bookmarkedPlaylists = withContext(Dispatchers.IO) {
DatabaseHolder.Database.playlistBookmarkDao().getAll()
}
if (bookmarkedPlaylists.isNotEmpty()) {
makeVisible(binding.bookmarksTV, binding.bookmarksRV)
binding.bookmarksRV.layoutManager = LinearLayoutManager(
context,
LinearLayoutManager.HORIZONTAL,
false
)
binding.bookmarksRV.adapter = PlaylistBookmarkAdapter(
bookmarkedPlaylists,
PlaylistBookmarkAdapter.Companion.BookmarkMode.HOME
)
}
}
}

View File

@ -7,6 +7,7 @@ import android.view.View
import android.view.ViewGroup
import android.view.ViewGroup.MarginLayoutParams
import android.widget.Toast
import androidx.core.view.isVisible
import androidx.core.view.updateLayoutParams
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
@ -19,7 +20,6 @@ import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.databinding.FragmentLibraryBinding
import com.github.libretube.db.DatabaseHolder
import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.awaitQuery
import com.github.libretube.extensions.dpToPx
import com.github.libretube.helpers.NavBarHelper
import com.github.libretube.helpers.PreferenceHelper
@ -28,6 +28,9 @@ import com.github.libretube.ui.adapters.PlaylistsAdapter
import com.github.libretube.ui.base.BaseFragment
import com.github.libretube.ui.dialogs.CreatePlaylistDialog
import com.github.libretube.ui.models.PlayerViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class LibraryFragment : BaseFragment() {
@ -91,14 +94,16 @@ class LibraryFragment : BaseFragment() {
}
private fun initBookmarks() {
val bookmarks = awaitQuery {
DatabaseHolder.Database.playlistBookmarkDao().getAll()
lifecycleScope.launch {
val bookmarks = withContext(Dispatchers.IO) {
DatabaseHolder.Database.playlistBookmarkDao().getAll()
}
binding.bookmarksCV.isVisible = bookmarks.isNotEmpty()
if (bookmarks.isNotEmpty()) {
binding.bookmarksRecView.adapter = PlaylistBookmarkAdapter(bookmarks)
}
}
binding.bookmarksCV.visibility = if (bookmarks.isEmpty()) View.GONE else View.VISIBLE
if (bookmarks.isEmpty()) return
binding.bookmarksRecView.adapter = PlaylistBookmarkAdapter(bookmarks)
}
private fun updateFABMargin(isMiniPlayerVisible: Boolean) {

View File

@ -21,9 +21,7 @@ import com.github.libretube.databinding.FragmentPlaylistBinding
import com.github.libretube.db.DatabaseHolder
import com.github.libretube.enums.PlaylistType
import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.awaitQuery
import com.github.libretube.extensions.dpToPx
import com.github.libretube.extensions.query
import com.github.libretube.extensions.serializable
import com.github.libretube.extensions.toID
import com.github.libretube.helpers.ImageHelper
@ -34,6 +32,9 @@ import com.github.libretube.ui.models.PlayerViewModel
import com.github.libretube.ui.sheets.PlaylistOptionsBottomSheet
import com.github.libretube.util.PlayingQueue
import com.github.libretube.util.TextUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
class PlaylistFragment : BaseFragment() {
private lateinit var binding: FragmentPlaylistBinding
@ -78,7 +79,7 @@ class PlaylistFragment : BaseFragment() {
binding.playlistProgress.visibility = View.VISIBLE
isBookmarked = awaitQuery {
isBookmarked = runBlocking(Dispatchers.IO) {
DatabaseHolder.Database.playlistBookmarkDao().includes(playlistId!!)
}
updateBookmarkRes()
@ -148,14 +149,13 @@ class PlaylistFragment : BaseFragment() {
binding.bookmark.setOnClickListener {
isBookmarked = !isBookmarked
updateBookmarkRes()
query {
lifecycleScope.launch(Dispatchers.IO) {
if (!isBookmarked) {
DatabaseHolder.Database.playlistBookmarkDao()
.deleteById(playlistId!!)
} else {
DatabaseHolder.Database.playlistBookmarkDao().insertAll(
response.toPlaylistBookmark(playlistId!!)
)
DatabaseHolder.Database.playlistBookmarkDao()
.insertAll(listOf(response.toPlaylistBookmark(playlistId!!)))
}
}
}
@ -254,13 +254,12 @@ class PlaylistFragment : BaseFragment() {
}
// update the playlist thumbnail if bookmarked
val playlistBookmark = awaitQuery {
DatabaseHolder.Database.playlistBookmarkDao().getAll()
}.firstOrNull { it.playlistId == playlistId }
playlistBookmark?.let {
if (it.thumbnailUrl != response.thumbnailUrl) {
it.thumbnailUrl = response.thumbnailUrl
query {
lifecycleScope.launch(Dispatchers.IO) {
val playlistBookmark = DatabaseHolder.Database.playlistBookmarkDao().getAll()
.firstOrNull { it.playlistId == playlistId }
playlistBookmark?.let {
if (it.thumbnailUrl != response.thumbnailUrl) {
it.thumbnailUrl = response.thumbnailUrl
DatabaseHolder.Database.playlistBookmarkDao().update(it)
}
}

View File

@ -7,7 +7,6 @@ import com.github.libretube.api.RetrofitInstance
import com.github.libretube.db.DatabaseHolder
import com.github.libretube.enums.PlaylistType
import com.github.libretube.enums.ShareObjectType
import com.github.libretube.extensions.awaitQuery
import com.github.libretube.extensions.toID
import com.github.libretube.extensions.toastFromMainThread
import com.github.libretube.helpers.BackgroundHelper
@ -16,6 +15,7 @@ import com.github.libretube.ui.dialogs.DeletePlaylistDialog
import com.github.libretube.ui.dialogs.RenamePlaylistDialog
import com.github.libretube.ui.dialogs.ShareDialog
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
class PlaylistOptionsBottomSheet(
@ -31,7 +31,7 @@ class PlaylistOptionsBottomSheet(
getString(R.string.playOnBackground)
)
val isBookmarked = awaitQuery {
val isBookmarked = runBlocking(Dispatchers.IO) {
DatabaseHolder.Database.playlistBookmarkDao().includes(playlistId)
}
@ -99,7 +99,8 @@ class PlaylistOptionsBottomSheet(
} catch (e: Exception) {
return@withContext
}.toPlaylistBookmark(playlistId)
DatabaseHolder.Database.playlistBookmarkDao().insertAll(bookmark)
DatabaseHolder.Database.playlistBookmarkDao()
.insertAll(listOf(bookmark))
}
}
}