From 295caa3093af38d867ad6434c06abe1b9ba0bfc8 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Thu, 22 Dec 2022 11:55:51 +0100 Subject: [PATCH] Remove playlists at library on deletion --- .../libretube/ui/adapters/PlaylistsAdapter.kt | 19 +++++++++++++------ .../ui/dialogs/DeletePlaylistDialog.kt | 7 +++++-- .../ui/sheets/PlaylistOptionsBottomSheet.kt | 9 ++++++--- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/com/github/libretube/ui/adapters/PlaylistsAdapter.kt b/app/src/main/java/com/github/libretube/ui/adapters/PlaylistsAdapter.kt index 63dba1787..4c4f4b7b6 100644 --- a/app/src/main/java/com/github/libretube/ui/adapters/PlaylistsAdapter.kt +++ b/app/src/main/java/com/github/libretube/ui/adapters/PlaylistsAdapter.kt @@ -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) + } + } } diff --git a/app/src/main/java/com/github/libretube/ui/dialogs/DeletePlaylistDialog.kt b/app/src/main/java/com/github/libretube/ui/dialogs/DeletePlaylistDialog.kt index f8840a8eb..091966ad8 100644 --- a/app/src/main/java/com/github/libretube/ui/dialogs/DeletePlaylistDialog.kt +++ b/app/src/main/java/com/github/libretube/ui/dialogs/DeletePlaylistDialog.kt @@ -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()) diff --git a/app/src/main/java/com/github/libretube/ui/sheets/PlaylistOptionsBottomSheet.kt b/app/src/main/java/com/github/libretube/ui/sheets/PlaylistOptionsBottomSheet.kt index f517a55b8..d9dc759a4 100644 --- a/app/src/main/java/com/github/libretube/ui/sheets/PlaylistOptionsBottomSheet.kt +++ b/app/src/main/java/com/github/libretube/ui/sheets/PlaylistOptionsBottomSheet.kt @@ -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)