Merge pull request #3155 from Bnyro/master

Fix deletion and renaming feedback toasts for playlists
This commit is contained in:
Bnyro 2023-02-22 10:41:27 +01:00 committed by GitHub
commit f84353e7ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 38 additions and 41 deletions

View File

@ -169,7 +169,7 @@ interface PipedApi {
suspend fun renamePlaylist(
@Header("Authorization") token: String,
@Body playlistId: PlaylistId
): PlaylistId
): Message
@POST("user/playlists/delete")
suspend fun deletePlaylist(

View File

@ -128,7 +128,7 @@ object PlaylistsHelper {
true
} else {
val playlist = PlaylistId(playlistId, newName = newName)
RetrofitInstance.authApi.renamePlaylist(token, playlist).playlistId != null
RetrofitInstance.authApi.renamePlaylist(token, playlist).message == "ok"
}
}
@ -231,6 +231,25 @@ object PlaylistsHelper {
}.getOrNull()?.playlistId
}
suspend fun deletePlaylist(playlistId: String, playlistType: PlaylistType): Boolean {
if (playlistType == PlaylistType.LOCAL) {
DatabaseHolder.Database.localPlaylistsDao().deletePlaylistById(playlistId)
DatabaseHolder.Database.localPlaylistsDao().deletePlaylistItemsByPlaylistId(playlistId)
return true
}
val response = try {
RetrofitInstance.authApi.deletePlaylist(
PreferenceHelper.getToken(),
PlaylistId(playlistId)
)
} catch (e: Exception) {
Log.e(TAG(), e.toString())
return false
}
return response.message == "ok"
}
fun getPrivatePlaylistType(): PlaylistType {
return if (loggedIn) PlaylistType.PRIVATE else PlaylistType.LOCAL
}

View File

@ -2,17 +2,13 @@ package com.github.libretube.ui.dialogs
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.api.PlaylistsHelper
import com.github.libretube.enums.PlaylistType
import com.github.libretube.extensions.TAG
import com.github.libretube.helpers.PreferenceHelper
import com.github.libretube.extensions.toastFromMainDispatcher
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
@ -27,41 +23,20 @@ class DeletePlaylistDialog(
.setTitle(R.string.deletePlaylist)
.setMessage(R.string.areYouSure)
.setPositiveButton(R.string.yes) { _, _ ->
lifecycleScope.launch(Dispatchers.IO) {
deletePlaylist()
val appContext = context?.applicationContext
CoroutineScope(Dispatchers.IO).launch {
val success = PlaylistsHelper.deletePlaylist(playlistId, playlistType)
appContext?.toastFromMainDispatcher(
if (success) R.string.success else R.string.fail
)
withContext(Dispatchers.Main) {
runCatching {
onSuccess.invoke()
}
}
}
}
.setNegativeButton(R.string.cancel, null)
.show()
}
private suspend fun deletePlaylist() {
if (playlistType == PlaylistType.LOCAL) {
DatabaseHolder.Database.localPlaylistsDao().deletePlaylistById(playlistId)
DatabaseHolder.Database.localPlaylistsDao().deletePlaylistItemsByPlaylistId(playlistId)
withContext(Dispatchers.Main) {
onSuccess()
}
return
}
val response = try {
RetrofitInstance.authApi.deletePlaylist(
PreferenceHelper.getToken(),
PlaylistId(playlistId)
)
} catch (e: Exception) {
Log.e(TAG(), e.toString())
return
}
try {
if (response.message == "ok") {
withContext(Dispatchers.Main) {
onSuccess()
}
}
} catch (e: Exception) {
Log.e(TAG(), e.toString())
}
}
}

View File

@ -109,6 +109,7 @@ import java.io.IOException
import java.util.*
import java.util.concurrent.Executors
import kotlin.math.abs
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking

View File

@ -18,7 +18,9 @@ import com.github.libretube.ui.dialogs.DownloadDialog
import com.github.libretube.ui.dialogs.ShareDialog
import com.github.libretube.ui.fragments.SubscriptionsFragment
import com.github.libretube.util.PlayingQueue
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
/**