Merge pull request #2477 from Bnyro/master

Remove playlists at library on deletion
This commit is contained in:
Bnyro 2022-12-22 11:56:15 +01:00 committed by GitHub
commit 080476ef0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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,7 +59,9 @@ class DeletePlaylistDialog(
}
try {
if (response.message == "ok") {
onSuccess.invoke()
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)