Remove playlists at library on deletion

This commit is contained in:
Bnyro 2022-12-22 11:55:51 +01:00
parent 73a7e62e57
commit 295caa3093
3 changed files with 24 additions and 11 deletions

View File

@ -51,11 +51,7 @@ class PlaylistsAdapter(
deletePlaylist.setOnClickListener {
DeletePlaylistDialog(playlist.id!!, playlistType) {
playlists.removeAt(position)
(root.context as BaseActivity).runOnUiThread {
notifyItemRemoved(position)
notifyItemRangeChanged(position, itemCount)
}
onDelete(position, root.context as BaseActivity)
}.show(
(root.context as BaseActivity).supportFragmentManager,
null
@ -69,7 +65,10 @@ class PlaylistsAdapter(
val playlistOptionsDialog = PlaylistOptionsBottomSheet(
playlistId = playlist.id!!,
playlistName = playlist.name!!,
playlistType = playlistType
playlistType = playlistType,
onDelete = {
onDelete(position, root.context as BaseActivity)
}
)
playlistOptionsDialog.show(
(root.context as BaseActivity).supportFragmentManager,
@ -79,4 +78,12 @@ class PlaylistsAdapter(
}
}
}
private fun onDelete(position: Int, activity: BaseActivity) {
playlists.removeAt(position)
activity.runOnUiThread {
notifyItemRemoved(position)
notifyItemRangeChanged(position, itemCount)
}
}
}

View File

@ -16,11 +16,12 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class DeletePlaylistDialog(
private val playlistId: String,
private val playlistType: PlaylistType,
private val onSuccess: () -> Unit = {}
private val onSuccess: () -> Unit
) : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return MaterialAlertDialogBuilder(requireContext())
@ -58,8 +59,10 @@ class DeletePlaylistDialog(
}
try {
if (response.message == "ok") {
withContext(Dispatchers.Main) {
onSuccess.invoke()
}
}
} catch (e: Exception) {
Log.e(TAG(), e.toString())
}

View File

@ -17,7 +17,8 @@ import kotlinx.coroutines.runBlocking
class PlaylistOptionsBottomSheet(
private val playlistId: String,
private val playlistName: String,
private val playlistType: PlaylistType
private val playlistType: PlaylistType,
private val onDelete: () -> Unit = {}
) : BaseBottomSheet() {
private val shareData = ShareData(currentPlaylist = playlistName)
override fun onCreate(savedInstanceState: Bundle?) {
@ -63,8 +64,10 @@ class PlaylistOptionsBottomSheet(
shareDialog.show(parentFragmentManager, ShareDialog::class.java.name)
}
context?.getString(R.string.deletePlaylist) -> {
DeletePlaylistDialog(playlistId, playlistType)
.show(parentFragmentManager, null)
DeletePlaylistDialog(playlistId, playlistType) {
// try to refresh the playlists in the library on deletion success
onDelete.invoke()
}.show(parentFragmentManager, null)
}
context?.getString(R.string.renamePlaylist) -> {
RenamePlaylistDialog(playlistId, playlistName)