From 258bccab045a84b3197503c6bab2b2967e44705b Mon Sep 17 00:00:00 2001 From: Bnyro Date: Fri, 2 Dec 2022 12:49:51 +0100 Subject: [PATCH] Show the current name when renaming a playlist --- .../java/com/github/libretube/api/PipedApi.kt | 2 +- .../github/libretube/api/PlaylistsHelper.kt | 8 +-- .../ui/dialogs/RenamePlaylistDialog.kt | 62 +++++++++++++++++++ .../ui/sheets/PlaylistOptionsBottomSheet.kt | 35 ++--------- 4 files changed, 71 insertions(+), 36 deletions(-) create mode 100644 app/src/main/java/com/github/libretube/ui/dialogs/RenamePlaylistDialog.kt diff --git a/app/src/main/java/com/github/libretube/api/PipedApi.kt b/app/src/main/java/com/github/libretube/api/PipedApi.kt index cfdf4457b..f442d3104 100644 --- a/app/src/main/java/com/github/libretube/api/PipedApi.kt +++ b/app/src/main/java/com/github/libretube/api/PipedApi.kt @@ -154,7 +154,7 @@ interface PipedApi { suspend fun renamePlaylist( @Header("Authorization") token: String, @Body playlistId: PlaylistId - ) + ): PlaylistId @POST("user/playlists/delete") suspend fun deletePlaylist( diff --git a/app/src/main/java/com/github/libretube/api/PlaylistsHelper.kt b/app/src/main/java/com/github/libretube/api/PlaylistsHelper.kt index 1231a0271..4036d97df 100644 --- a/app/src/main/java/com/github/libretube/api/PlaylistsHelper.kt +++ b/app/src/main/java/com/github/libretube/api/PlaylistsHelper.kt @@ -142,7 +142,7 @@ object PlaylistsHelper { ).message == "ok" } - suspend fun renamePlaylist(playlistId: String, newName: String) { + suspend fun renamePlaylist(playlistId: String, newName: String): Boolean { if (!loggedIn()) { val playlist = awaitQuery { DatabaseHolder.Database.localPlaylistsDao().getAll() @@ -151,16 +151,16 @@ object PlaylistsHelper { awaitQuery { DatabaseHolder.Database.localPlaylistsDao().updatePlaylist(playlist) } - return + return true } - RetrofitInstance.authApi.renamePlaylist( + return RetrofitInstance.authApi.renamePlaylist( token, PlaylistId( playlistId = playlistId, newName = newName ) - ) + ).playlistId != null } suspend fun removeFromPlaylist(playlistId: String, index: Int) { diff --git a/app/src/main/java/com/github/libretube/ui/dialogs/RenamePlaylistDialog.kt b/app/src/main/java/com/github/libretube/ui/dialogs/RenamePlaylistDialog.kt new file mode 100644 index 000000000..15ba841d5 --- /dev/null +++ b/app/src/main/java/com/github/libretube/ui/dialogs/RenamePlaylistDialog.kt @@ -0,0 +1,62 @@ +package com.github.libretube.ui.dialogs + +import android.app.Dialog +import android.os.Bundle +import android.text.InputType +import android.util.Log +import android.widget.Toast +import androidx.fragment.app.DialogFragment +import com.github.libretube.R +import com.github.libretube.api.PlaylistsHelper +import com.github.libretube.databinding.DialogTextPreferenceBinding +import com.github.libretube.extensions.TAG +import com.github.libretube.extensions.toastFromMainThread +import com.google.android.material.dialog.MaterialAlertDialogBuilder +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch + +class RenamePlaylistDialog( + private val playlistId: String, + private val currentPlaylistName: String +) : DialogFragment() { + override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { + val binding = DialogTextPreferenceBinding.inflate(layoutInflater) + binding.input.inputType = InputType.TYPE_CLASS_TEXT + binding.input.hint = getString(R.string.playlistName) + binding.input.setText(currentPlaylistName) + + return MaterialAlertDialogBuilder(requireContext()) + .setTitle(R.string.renamePlaylist) + .setView(binding.root) + .setPositiveButton(R.string.okay) { _, _ -> + val input = binding.input.text.toString() + if (input == "") { + Toast.makeText( + context, + R.string.emptyPlaylistName, + Toast.LENGTH_SHORT + ).show() + return@setPositiveButton + } + if (input == currentPlaylistName) return@setPositiveButton + val appContext = requireContext().applicationContext + CoroutineScope(Dispatchers.IO).launch { + val success = try { + PlaylistsHelper.renamePlaylist(playlistId, binding.input.text.toString()) + } catch (e: Exception) { + Log.e(TAG(), e.toString()) + e.localizedMessage?.let { appContext.toastFromMainThread(it) } + return@launch + } + if (success) { + appContext.toastFromMainThread(R.string.success) + } else { + appContext.toastFromMainThread(R.string.server_error) + } + } + } + .setNegativeButton(R.string.cancel, null) + .show() + } +} 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 3568eafbc..4aaf10b9f 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 @@ -1,23 +1,20 @@ package com.github.libretube.ui.sheets import android.os.Bundle -import android.text.InputType import android.widget.Toast import com.github.libretube.R -import com.github.libretube.api.PlaylistsHelper import com.github.libretube.api.RetrofitInstance import com.github.libretube.api.obj.PlaylistId -import com.github.libretube.databinding.DialogTextPreferenceBinding import com.github.libretube.enums.PlaylistType 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.RenamePlaylistDialog import com.github.libretube.ui.dialogs.ShareDialog import com.github.libretube.util.BackgroundHelper 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 @@ -27,7 +24,7 @@ import java.io.IOException class PlaylistOptionsBottomSheet( private val playlistId: String, - playlistName: String, + private val playlistName: String, private val playlistType: PlaylistType ) : BaseBottomSheet() { private val shareData = ShareData(currentPlaylist = playlistName) @@ -87,32 +84,8 @@ class PlaylistOptionsBottomSheet( .show(parentFragmentManager, null) } context?.getString(R.string.renamePlaylist) -> { - val binding = DialogTextPreferenceBinding.inflate(layoutInflater) - binding.input.hint = context?.getString(R.string.playlistName) - binding.input.inputType = InputType.TYPE_CLASS_TEXT - - MaterialAlertDialogBuilder(requireContext()) - .setTitle(R.string.renamePlaylist) - .setView(binding.root) - .setPositiveButton(R.string.okay) { _, _ -> - if (binding.input.text.toString() == "") { - Toast.makeText( - context, - R.string.emptyPlaylistName, - Toast.LENGTH_SHORT - ).show() - return@setPositiveButton - } - CoroutineScope(Dispatchers.IO).launch { - try { - PlaylistsHelper.renamePlaylist(playlistId, binding.input.text.toString()) - } catch (e: Exception) { - return@launch - } - } - } - .setNegativeButton(R.string.cancel, null) - .show() + RenamePlaylistDialog(playlistId, playlistName) + .show(parentFragmentManager, null) } } }