add playlist deletion

This commit is contained in:
Bnyro 2022-11-20 16:24:17 +01:00
parent 3a63e3c546
commit 9c207d2e80
2 changed files with 29 additions and 12 deletions

View File

@ -146,6 +146,32 @@ object PlaylistsHelper {
)
}
suspend fun removeFromPlaylist(playlistId: String, index: Int) {
if (!loggedIn()) {
val transaction = awaitQuery {
DatabaseHolder.Database.localPlaylistsDao().getAll()
}.first { it.playlist.id.toString() == playlistId }
awaitQuery {
DatabaseHolder.Database.localPlaylistsDao().removePlaylistVideo(transaction.videos[index])
}
if (transaction.videos.size > 1) 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
)
)
}
fun getType(): PlaylistType {
return if (PreferenceHelper.getToken() != "") {
PlaylistType.PUBLIC

View File

@ -7,6 +7,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.api.PlaylistsHelper
import com.github.libretube.api.RetrofitInstance
import com.github.libretube.api.obj.PlaylistId
import com.github.libretube.api.obj.StreamItem
@ -89,19 +90,9 @@ class PlaylistAdapter(
}
CoroutineScope(Dispatchers.IO).launch {
try {
RetrofitInstance.authApi.removeFromPlaylist(
PreferenceHelper.getToken(),
PlaylistId(
playlistId = playlistId,
index = position
)
)
PlaylistsHelper.removeFromPlaylist(playlistId, position)
} catch (e: IOException) {
println(e)
Log.e(TAG(), "IOException, you might not have internet connection")
return@launch
} catch (e: HttpException) {
Log.e(TAG(), "HttpException, unexpected response")
Log.e(TAG(), e.toString())
return@launch
}
}