From 1bb3429b4769ab36d856c8956d90e28eae1df054 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sat, 19 Nov 2022 16:03:03 +0100 Subject: [PATCH] add confirmation for playlist deletion --- app/release/output-metadata.json | 42 ++++++------- .../libretube/ui/adapters/PlaylistsAdapter.kt | 61 ++++--------------- .../ui/dialogs/DeletePlaylistDialog.kt | 53 ++++++++++++++++ .../ui/sheets/PlaylistOptionsBottomSheet.kt | 19 +----- 4 files changed, 88 insertions(+), 87 deletions(-) create mode 100644 app/src/main/java/com/github/libretube/ui/dialogs/DeletePlaylistDialog.kt diff --git a/app/release/output-metadata.json b/app/release/output-metadata.json index 876d1ffc4..7e8abec17 100644 --- a/app/release/output-metadata.json +++ b/app/release/output-metadata.json @@ -11,23 +11,10 @@ "type": "UNIVERSAL", "filters": [], "attributes": [], - "versionCode": 22, - "versionName": "0.7.1", + "versionCode": 23, + "versionName": "0.8.0", "outputFile": "app-universal-release.apk" }, - { - "type": "ONE_OF_MANY", - "filters": [ - { - "filterType": "ABI", - "value": "x86_64" - } - ], - "attributes": [], - "versionCode": 22, - "versionName": "0.7.1", - "outputFile": "app-x86_64-release.apk" - }, { "type": "ONE_OF_MANY", "filters": [ @@ -37,8 +24,8 @@ } ], "attributes": [], - "versionCode": 22, - "versionName": "0.7.1", + "versionCode": 23, + "versionName": "0.8.0", "outputFile": "app-x86-release.apk" }, { @@ -50,10 +37,23 @@ } ], "attributes": [], - "versionCode": 22, - "versionName": "0.7.1", + "versionCode": 23, + "versionName": "0.8.0", "outputFile": "app-arm64-v8a-release.apk" }, + { + "type": "ONE_OF_MANY", + "filters": [ + { + "filterType": "ABI", + "value": "x86_64" + } + ], + "attributes": [], + "versionCode": 23, + "versionName": "0.8.0", + "outputFile": "app-x86_64-release.apk" + }, { "type": "ONE_OF_MANY", "filters": [ @@ -63,8 +63,8 @@ } ], "attributes": [], - "versionCode": 22, - "versionName": "0.7.1", + "versionCode": 23, + "versionName": "0.8.0", "outputFile": "app-armeabi-v7a-release.apk" } ], 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 0169c0b95..254cb80b1 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 @@ -1,28 +1,17 @@ package com.github.libretube.ui.adapters -import android.app.Activity -import android.util.Log import android.view.LayoutInflater import android.view.ViewGroup import androidx.recyclerview.widget.RecyclerView import com.github.libretube.R -import com.github.libretube.api.RetrofitInstance -import com.github.libretube.api.obj.PlaylistId import com.github.libretube.api.obj.Playlists import com.github.libretube.databinding.PlaylistsRowBinding -import com.github.libretube.extensions.TAG import com.github.libretube.ui.base.BaseActivity +import com.github.libretube.ui.dialogs.DeletePlaylistDialog import com.github.libretube.ui.sheets.PlaylistOptionsBottomSheet import com.github.libretube.ui.viewholders.PlaylistsViewHolder import com.github.libretube.util.ImageHelper import com.github.libretube.util.NavigationHelper -import com.github.libretube.util.PreferenceHelper -import com.google.android.material.dialog.MaterialAlertDialogBuilder -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.launch -import retrofit2.HttpException -import java.io.IOException class PlaylistsAdapter( private val playlists: MutableList @@ -59,15 +48,16 @@ class PlaylistsAdapter( videoCount.text = playlist.videos.toString() deletePlaylist.setOnClickListener { - val builder = MaterialAlertDialogBuilder(root.context) - builder.setTitle(R.string.deletePlaylist) - builder.setMessage(R.string.areYouSure) - builder.setPositiveButton(R.string.yes) { _, _ -> - PreferenceHelper.getToken() - deletePlaylist(root.context as Activity, playlist.id!!, position) - } - builder.setNegativeButton(R.string.cancel, null) - builder.show() + DeletePlaylistDialog(playlist.id!!) { + playlists.removeAt(position) + (root.context as BaseActivity).runOnUiThread { + notifyItemRemoved(position) + notifyItemRangeChanged(position, itemCount) + } + }.show( + (root.context as BaseActivity).supportFragmentManager, + null + ) } root.setOnClickListener { NavigationHelper.navigatePlaylist(root.context, playlist.id, true) @@ -87,33 +77,4 @@ class PlaylistsAdapter( } } } - - private fun deletePlaylist(activity: Activity, id: String, position: Int) { - CoroutineScope(Dispatchers.IO).launch { - val response = try { - RetrofitInstance.authApi.deletePlaylist( - PreferenceHelper.getToken(), - PlaylistId(id) - ) - } catch (e: IOException) { - println(e) - Log.e(TAG(), "IOException, you might not have internet connection") - return@launch - } catch (e: HttpException) { - Log.e(TAG(), "HttpException, unexpected response") - return@launch - } - try { - if (response.message == "ok") { - playlists.removeAt(position) - activity.runOnUiThread { - notifyItemRemoved(position) - notifyItemRangeChanged(position, itemCount) - } - } - } catch (e: Exception) { - Log.e(TAG(), e.toString()) - } - } - } } 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 new file mode 100644 index 000000000..c943a8e17 --- /dev/null +++ b/app/src/main/java/com/github/libretube/ui/dialogs/DeletePlaylistDialog.kt @@ -0,0 +1,53 @@ +package com.github.libretube.ui.dialogs + +import android.app.Dialog +import android.os.Bundle +import android.util.Log +import androidx.fragment.app.DialogFragment +import com.github.libretube.R +import com.github.libretube.api.RetrofitInstance +import com.github.libretube.api.obj.PlaylistId +import com.github.libretube.extensions.TAG +import com.github.libretube.util.PreferenceHelper +import com.google.android.material.dialog.MaterialAlertDialogBuilder +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch + +class DeletePlaylistDialog( + private val playlistId: String, + private val onSuccess: () -> Unit = {} +) : DialogFragment() { + override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { + return MaterialAlertDialogBuilder(requireContext()) + .setTitle(R.string.deletePlaylist) + .setMessage(R.string.areYouSure) + .setPositiveButton(R.string.yes) { _, _ -> + PreferenceHelper.getToken() + deletePlaylist() + } + .setNegativeButton(R.string.cancel, null) + .show() + } + + private fun deletePlaylist() { + CoroutineScope(Dispatchers.IO).launch { + val response = try { + RetrofitInstance.authApi.deletePlaylist( + PreferenceHelper.getToken(), + PlaylistId(playlistId) + ) + } catch (e: Exception) { + Log.e(TAG(), e.toString()) + return@launch + } + try { + if (response.message == "ok") { + 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 d3a2d0ccd..9d1868149 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 @@ -11,6 +11,7 @@ import com.github.libretube.enums.ShareObjectType import com.github.libretube.extensions.toID import com.github.libretube.extensions.toastFromMainThread import com.github.libretube.obj.ShareData +import com.github.libretube.ui.dialogs.DeletePlaylistDialog import com.github.libretube.ui.dialogs.ShareDialog import com.github.libretube.util.BackgroundHelper import com.github.libretube.util.PreferenceHelper @@ -81,9 +82,8 @@ class PlaylistOptionsBottomSheet( shareDialog.show(parentFragmentManager, ShareDialog::class.java.name) } context?.getString(R.string.deletePlaylist) -> { - deletePlaylist( - playlistId - ) + DeletePlaylistDialog(playlistId) + .show(parentFragmentManager, null) } context?.getString(R.string.renamePlaylist) -> { val binding = DialogTextPreferenceBinding.inflate(layoutInflater) @@ -145,17 +145,4 @@ class PlaylistOptionsBottomSheet( } } } - - private fun deletePlaylist(id: String) { - CoroutineScope(Dispatchers.IO).launch { - try { - RetrofitInstance.authApi.deletePlaylist( - PreferenceHelper.getToken(), - PlaylistId(id) - ) - } catch (e: Exception) { - return@launch - } - } - } }