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 { deletePlaylist.setOnClickListener {
DeletePlaylistDialog(playlist.id!!, playlistType) { DeletePlaylistDialog(playlist.id!!, playlistType) {
playlists.removeAt(position) onDelete(position, root.context as BaseActivity)
(root.context as BaseActivity).runOnUiThread {
notifyItemRemoved(position)
notifyItemRangeChanged(position, itemCount)
}
}.show( }.show(
(root.context as BaseActivity).supportFragmentManager, (root.context as BaseActivity).supportFragmentManager,
null null
@ -69,7 +65,10 @@ class PlaylistsAdapter(
val playlistOptionsDialog = PlaylistOptionsBottomSheet( val playlistOptionsDialog = PlaylistOptionsBottomSheet(
playlistId = playlist.id!!, playlistId = playlist.id!!,
playlistName = playlist.name!!, playlistName = playlist.name!!,
playlistType = playlistType playlistType = playlistType,
onDelete = {
onDelete(position, root.context as BaseActivity)
}
) )
playlistOptionsDialog.show( playlistOptionsDialog.show(
(root.context as BaseActivity).supportFragmentManager, (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.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class DeletePlaylistDialog( class DeletePlaylistDialog(
private val playlistId: String, private val playlistId: String,
private val playlistType: PlaylistType, private val playlistType: PlaylistType,
private val onSuccess: () -> Unit = {} private val onSuccess: () -> Unit
) : DialogFragment() { ) : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return MaterialAlertDialogBuilder(requireContext()) return MaterialAlertDialogBuilder(requireContext())
@ -58,8 +59,10 @@ class DeletePlaylistDialog(
} }
try { try {
if (response.message == "ok") { if (response.message == "ok") {
withContext(Dispatchers.Main) {
onSuccess.invoke() onSuccess.invoke()
} }
}
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG(), e.toString()) Log.e(TAG(), e.toString())
} }

View File

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