Simplify the local playlist handling

This commit is contained in:
Bnyro 2023-01-31 16:12:19 +01:00
parent 7b23da5eca
commit 9607edef22

View File

@ -19,12 +19,12 @@ import com.github.libretube.extensions.toastFromMainThread
import com.github.libretube.obj.ImportPlaylist import com.github.libretube.obj.ImportPlaylist
import com.github.libretube.util.PreferenceHelper import com.github.libretube.util.PreferenceHelper
import com.github.libretube.util.ProxyHelper import com.github.libretube.util.ProxyHelper
import java.io.IOException
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import retrofit2.HttpException import retrofit2.HttpException
import java.io.IOException
object PlaylistsHelper { object PlaylistsHelper {
private val pipedPlaylistRegex = private val pipedPlaylistRegex =
@ -138,28 +138,24 @@ object PlaylistsHelper {
} }
} }
suspend fun removeFromPlaylist(playlistId: String, index: Int) { suspend fun removeFromPlaylist(playlistId: String, index: Int): Boolean {
if (!loggedIn) { return if (!loggedIn) {
val transaction = DatabaseHolder.Database.localPlaylistsDao().getAll() val transaction = DatabaseHolder.Database.localPlaylistsDao().getAll()
.first { it.playlist.id.toString() == playlistId } .first { it.playlist.id.toString() == playlistId }
DatabaseHolder.Database.localPlaylistsDao().removePlaylistVideo( DatabaseHolder.Database.localPlaylistsDao().removePlaylistVideo(
transaction.videos[index] transaction.videos[index]
) )
if (transaction.videos.size > 1) { // set a new playlist thumbnail if the first video got removed
if (index == 0) { if (index == 0) {
transaction.videos[1].thumbnailUrl?.let { transaction.playlist.thumbnailUrl = transaction.videos.getOrNull(1)?.thumbnailUrl ?: ""
transaction.playlist.thumbnailUrl = it
}
DatabaseHolder.Database.localPlaylistsDao().updatePlaylist(transaction.playlist)
}
return
} }
// remove thumbnail if playlist now empty
transaction.playlist.thumbnailUrl = ""
DatabaseHolder.Database.localPlaylistsDao().updatePlaylist(transaction.playlist) DatabaseHolder.Database.localPlaylistsDao().updatePlaylist(transaction.playlist)
true
} else { } else {
val playlist = PlaylistId(playlistId = playlistId, index = index) RetrofitInstance.authApi.removeFromPlaylist(
RetrofitInstance.authApi.removeFromPlaylist(PreferenceHelper.getToken(), playlist) PreferenceHelper.getToken(),
PlaylistId(playlistId = playlistId, index = index)
).message == "ok"
} }
} }
@ -167,13 +163,8 @@ object PlaylistsHelper {
for (playlist in playlists) { for (playlist in playlists) {
val playlistId = createPlaylist(playlist.name!!, appContext) ?: continue val playlistId = createPlaylist(playlist.name!!, appContext) ?: continue
// if logged in, add the playlists by their ID via an api call // if logged in, add the playlists by their ID via an api call
val success: Boolean = if (loggedIn) { val success = if (loggedIn) {
addToPlaylist( addToPlaylist(playlistId, *playlist.videos.map { StreamItem(url = it) }.toTypedArray())
playlistId,
*playlist.videos.map {
StreamItem(url = it)
}.toTypedArray()
)
} else { } else {
// if not logged in, all video information needs to become fetched manually // if not logged in, all video information needs to become fetched manually
try { try {