Merge pull request #2920 from Isira-Seneviratne/LocalPlaylistDao_suspend

Convert LocalPlaylistsDao methods to suspend functions.
This commit is contained in:
Bnyro 2023-01-31 11:36:02 +01:00 committed by GitHub
commit 41426f4fca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 94 additions and 137 deletions

View File

@ -12,7 +12,6 @@ import com.github.libretube.db.DatabaseHolder
import com.github.libretube.db.obj.LocalPlaylist
import com.github.libretube.enums.PlaylistType
import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.awaitQuery
import com.github.libretube.extensions.toID
import com.github.libretube.extensions.toLocalPlaylistItem
import com.github.libretube.extensions.toStreamItem
@ -35,26 +34,22 @@ object PlaylistsHelper {
private val token get() = PreferenceHelper.getToken()
val loggedIn: Boolean get() = token != ""
val loggedIn: Boolean get() = token.isNotEmpty()
suspend fun getPlaylists(): List<Playlists> {
if (loggedIn) return RetrofitInstance.authApi.getUserPlaylists(token)
val localPlaylists = awaitQuery {
suspend fun getPlaylists(): List<Playlists> = withContext(Dispatchers.IO) {
if (loggedIn) {
RetrofitInstance.authApi.getUserPlaylists(token)
} else {
DatabaseHolder.Database.localPlaylistsDao().getAll()
}
val playlists = mutableListOf<Playlists>()
localPlaylists.forEach {
playlists.add(
.map {
Playlists(
id = it.playlist.id.toString(),
name = it.playlist.name,
thumbnail = ProxyHelper.rewriteUrl(it.playlist.thumbnailUrl),
videos = it.videos.size.toLong()
)
)
}
return playlists
}
}
suspend fun getPlaylist(playlistId: String): Playlist {
@ -63,9 +58,8 @@ object PlaylistsHelper {
PlaylistType.PRIVATE -> RetrofitInstance.authApi.getPlaylist(playlistId)
PlaylistType.PUBLIC -> RetrofitInstance.api.getPlaylist(playlistId)
PlaylistType.LOCAL -> {
val relation = awaitQuery {
DatabaseHolder.Database.localPlaylistsDao().getAll()
}.first { it.playlist.id.toString() == playlistId }
val relation = DatabaseHolder.Database.localPlaylistsDao().getAll()
.first { it.playlist.id.toString() == playlistId }
return Playlist(
name = relation.playlist.name,
thumbnailUrl = ProxyHelper.rewriteUrl(relation.playlist.thumbnailUrl),
@ -81,18 +75,11 @@ object PlaylistsHelper {
appContext: Context
): String? {
if (!loggedIn) {
awaitQuery {
DatabaseHolder.Database.localPlaylistsDao().createPlaylist(
LocalPlaylist(
name = playlistName,
thumbnailUrl = ""
)
)
}
return awaitQuery {
DatabaseHolder.Database.localPlaylistsDao().getAll()
}.last().playlist.id.toString()
}
val playlist = LocalPlaylist(name = playlistName, thumbnailUrl = "")
DatabaseHolder.Database.localPlaylistsDao().createPlaylist(playlist)
return DatabaseHolder.Database.localPlaylistsDao().getAll()
.last().playlist.id.toString()
} else {
val response = try {
RetrofitInstance.authApi.createPlaylist(token, Playlists(name = playlistName))
} catch (e: IOException) {
@ -105,9 +92,9 @@ object PlaylistsHelper {
}
if (response.playlistId != null) {
appContext.toastFromMainThread(R.string.playlistCreated)
}
return response.playlistId
}
return null
}
suspend fun addToPlaylist(playlistId: String, vararg videos: StreamItem): Boolean {
@ -117,7 +104,6 @@ object PlaylistsHelper {
for (video in videos) {
val localPlaylistItem = video.toLocalPlaylistItem(playlistId)
awaitQuery {
// avoid duplicated videos in a playlist
DatabaseHolder.Database.localPlaylistsDao()
.deletePlaylistItemsByVideoId(playlistId, localPlaylistItem.videoId)
@ -125,14 +111,12 @@ object PlaylistsHelper {
// add the new video to the database
DatabaseHolder.Database.localPlaylistsDao().addPlaylistVideo(localPlaylistItem)
if (localPlaylist.playlist.thumbnailUrl == "") {
val playlist = localPlaylist.playlist
if (playlist.thumbnailUrl == "") {
// set the new playlist thumbnail URL
localPlaylistItem.thumbnailUrl?.let {
localPlaylist.playlist.thumbnailUrl = it
DatabaseHolder.Database.localPlaylistsDao().updatePlaylist(
localPlaylist.playlist
)
}
playlist.thumbnailUrl = it
DatabaseHolder.Database.localPlaylistsDao().updatePlaylist(playlist)
}
}
}
@ -144,61 +128,41 @@ object PlaylistsHelper {
}
suspend fun renamePlaylist(playlistId: String, newName: String): Boolean {
if (!loggedIn) {
val playlist = awaitQuery {
DatabaseHolder.Database.localPlaylistsDao().getAll()
}.first { it.playlist.id.toString() == playlistId }.playlist
return if (!loggedIn) {
val playlist = DatabaseHolder.Database.localPlaylistsDao().getAll()
.first { it.playlist.id.toString() == playlistId }.playlist
playlist.name = newName
awaitQuery {
DatabaseHolder.Database.localPlaylistsDao().updatePlaylist(playlist)
true
} else {
val playlist = PlaylistId(playlistId, newName = newName)
RetrofitInstance.authApi.renamePlaylist(token, playlist).playlistId != null
}
return true
}
return RetrofitInstance.authApi.renamePlaylist(
token,
PlaylistId(playlistId, newName = newName)
).playlistId != null
}
suspend fun removeFromPlaylist(playlistId: String, index: Int) {
if (!loggedIn) {
val transaction = awaitQuery {
DatabaseHolder.Database.localPlaylistsDao().getAll()
}.first { it.playlist.id.toString() == playlistId }
awaitQuery {
val transaction = DatabaseHolder.Database.localPlaylistsDao().getAll()
.first { it.playlist.id.toString() == playlistId }
DatabaseHolder.Database.localPlaylistsDao().removePlaylistVideo(
transaction.videos[index]
)
}
if (transaction.videos.size > 1) {
if (index == 0) {
transaction.videos[1].thumbnailUrl?.let {
transaction.playlist.thumbnailUrl = it
}
awaitQuery {
DatabaseHolder.Database.localPlaylistsDao().updatePlaylist(
transaction.playlist
)
}
}
return
}
// remove thumbnail if playlist now empty
awaitQuery {
transaction.playlist.thumbnailUrl = ""
DatabaseHolder.Database.localPlaylistsDao().updatePlaylist(transaction.playlist)
}
return
}
RetrofitInstance.authApi.removeFromPlaylist(
PreferenceHelper.getToken(),
PlaylistId(
playlistId = playlistId,
index = index
)
)
// remove thumbnail if playlist now empty
transaction.playlist.thumbnailUrl = ""
DatabaseHolder.Database.localPlaylistsDao().updatePlaylist(transaction.playlist)
} else {
val playlist = PlaylistId(playlistId = playlistId, index = index)
RetrofitInstance.authApi.removeFromPlaylist(PreferenceHelper.getToken(), playlist)
}
}
suspend fun importPlaylists(appContext: Context, playlists: List<ImportPlaylist>) {

View File

@ -14,29 +14,26 @@ import com.github.libretube.db.obj.LocalPlaylistWithVideos
interface LocalPlaylistsDao {
@Transaction
@Query("SELECT * FROM LocalPlaylist")
fun getAll(): List<LocalPlaylistWithVideos>
suspend fun getAll(): List<LocalPlaylistWithVideos>
@Insert
fun createPlaylist(playlist: LocalPlaylist)
suspend fun createPlaylist(playlist: LocalPlaylist)
@Update
fun updatePlaylist(playlist: LocalPlaylist)
@Delete
fun deletePlaylist(playlist: LocalPlaylist)
suspend fun updatePlaylist(playlist: LocalPlaylist)
@Query("DELETE FROM localPlaylist WHERE id = :playlistId")
fun deletePlaylistById(playlistId: String)
suspend fun deletePlaylistById(playlistId: String)
@Insert
fun addPlaylistVideo(playlistVideo: LocalPlaylistItem)
suspend fun addPlaylistVideo(playlistVideo: LocalPlaylistItem)
@Delete
fun removePlaylistVideo(playlistVideo: LocalPlaylistItem)
suspend fun removePlaylistVideo(playlistVideo: LocalPlaylistItem)
@Query("DELETE FROM localPlaylistItem WHERE playlistId = :playlistId")
fun deletePlaylistItemsByPlaylistId(playlistId: String)
suspend fun deletePlaylistItemsByPlaylistId(playlistId: String)
@Query("DELETE FROM localPlaylistItem WHERE playlistId = :playlistId AND videoId = :videoId")
fun deletePlaylistItemsByVideoId(playlistId: String, videoId: String)
suspend fun deletePlaylistItemsByVideoId(playlistId: String, videoId: String)
}

View File

@ -4,16 +4,15 @@ import android.app.Dialog
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.DialogFragment
import androidx.lifecycle.lifecycleScope
import com.github.libretube.R
import com.github.libretube.api.RetrofitInstance
import com.github.libretube.api.obj.PlaylistId
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.util.PreferenceHelper
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
@ -28,26 +27,24 @@ class DeletePlaylistDialog(
.setTitle(R.string.deletePlaylist)
.setMessage(R.string.areYouSure)
.setPositiveButton(R.string.yes) { _, _ ->
PreferenceHelper.getToken()
lifecycleScope.launch(Dispatchers.IO) {
deletePlaylist()
}
}
.setNegativeButton(R.string.cancel, null)
.show()
}
private fun deletePlaylist() {
private suspend fun deletePlaylist() {
if (playlistType == PlaylistType.LOCAL) {
awaitQuery {
DatabaseHolder.Database.localPlaylistsDao().deletePlaylistById(playlistId)
DatabaseHolder.Database.localPlaylistsDao().deletePlaylistItemsByPlaylistId(
playlistId
)
DatabaseHolder.Database.localPlaylistsDao().deletePlaylistItemsByPlaylistId(playlistId)
withContext(Dispatchers.Main) {
onSuccess()
}
onSuccess.invoke()
return
}
CoroutineScope(Dispatchers.IO).launch {
val response = try {
RetrofitInstance.authApi.deletePlaylist(
PreferenceHelper.getToken(),
@ -55,17 +52,16 @@ class DeletePlaylistDialog(
)
} catch (e: Exception) {
Log.e(TAG(), e.toString())
return@launch
return
}
try {
if (response.message == "ok") {
withContext(Dispatchers.Main) {
onSuccess.invoke()
onSuccess()
}
}
} catch (e: Exception) {
Log.e(TAG(), e.toString())
}
}
}
}