From 158185a01027b150671a201a5e59f855f2994a71 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Fri, 24 Mar 2023 15:41:20 +0100 Subject: [PATCH] Update playlist name properly after renaming --- .../com/github/libretube/api/obj/Playlists.kt | 2 +- .../libretube/ui/adapters/PlaylistsAdapter.kt | 4 ++++ .../ui/dialogs/RenamePlaylistDialog.kt | 12 +++++++----- .../libretube/ui/fragments/PlaylistFragment.kt | 17 ++++++++++++++--- .../ui/sheets/PlaylistOptionsBottomSheet.kt | 3 ++- 5 files changed, 28 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/github/libretube/api/obj/Playlists.kt b/app/src/main/java/com/github/libretube/api/obj/Playlists.kt index bf5c4faa2..48d7db543 100644 --- a/app/src/main/java/com/github/libretube/api/obj/Playlists.kt +++ b/app/src/main/java/com/github/libretube/api/obj/Playlists.kt @@ -5,7 +5,7 @@ import kotlinx.serialization.Serializable @Serializable data class Playlists( val id: String? = null, - val name: String? = null, + var name: String? = null, val shortDescription: String? = null, val thumbnail: String? = null, val videos: Long = 0 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 ca7cccc9c..fb537b060 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 @@ -68,6 +68,10 @@ class PlaylistsAdapter( playlistType = playlistType, onDelete = { onDelete(position, root.context as BaseActivity) + }, + onRename = { + playlistTitle.text = it + playlist.name = it } ) playlistOptionsDialog.show( 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 index 47f0fdc66..a032537eb 100644 --- a/app/src/main/java/com/github/libretube/ui/dialogs/RenamePlaylistDialog.kt +++ b/app/src/main/java/com/github/libretube/ui/dialogs/RenamePlaylistDialog.kt @@ -20,7 +20,8 @@ import kotlinx.coroutines.withContext class RenamePlaylistDialog( private val playlistId: String, - private val currentPlaylistName: String + private val currentPlaylistName: String, + private val onSuccess: (String) -> Unit ) : DialogFragment() { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { val binding = DialogTextPreferenceBinding.inflate(layoutInflater) @@ -36,20 +37,20 @@ class RenamePlaylistDialog( .show() .apply { getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener { - val input = binding.input.text?.toString() - if (input.isNullOrEmpty()) { + val newPlaylistName = binding.input.text?.toString() + if (newPlaylistName.isNullOrEmpty()) { Toast.makeText(context, R.string.emptyPlaylistName, Toast.LENGTH_SHORT) .show() return@setOnClickListener } - if (input == currentPlaylistName) return@setOnClickListener + if (newPlaylistName == currentPlaylistName) return@setOnClickListener val appContext = requireContext().applicationContext lifecycleScope.launch { requireDialog().hide() val success = try { withContext(Dispatchers.IO) { - PlaylistsHelper.renamePlaylist(playlistId, input) + PlaylistsHelper.renamePlaylist(playlistId, newPlaylistName) } } catch (e: Exception) { Log.e(TAG(), e.toString()) @@ -58,6 +59,7 @@ class RenamePlaylistDialog( } if (success) { appContext.toastFromMainThread(R.string.success) + onSuccess.invoke(newPlaylistName) } else { appContext.toastFromMainThread(R.string.server_error) } diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlaylistFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlaylistFragment.kt index d879d872c..19c496fbd 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlaylistFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlaylistFragment.kt @@ -1,5 +1,6 @@ package com.github.libretube.ui.fragments +import android.annotation.SuppressLint import android.os.Bundle import android.util.Log import android.view.LayoutInflater @@ -129,9 +130,18 @@ class PlaylistFragment : Fragment() { // show playlist options binding.optionsMenu.setOnClickListener { - PlaylistOptionsBottomSheet(playlistId!!, playlistName.orEmpty(), playlistType) { - findNavController().popBackStack() - }.show( + PlaylistOptionsBottomSheet( + playlistId = playlistId.orEmpty(), + playlistName = playlistName.orEmpty(), + playlistType = playlistType, + onDelete = { + findNavController().popBackStack() + }, + onRename = { + binding.playlistName.text = it + playlistName = it + } + ).show( childFragmentManager, PlaylistOptionsBottomSheet::class.java.name ) @@ -258,6 +268,7 @@ class PlaylistFragment : Fragment() { } } + @SuppressLint("StringFormatInvalid", "StringFormatMatches") private fun getChannelAndVideoString(playlist: Playlist, count: Int): String { return playlist.uploader?.let { getString(R.string.uploaderAndVideoCount, it, count) 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 f8ddbe104..88c587257 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 @@ -22,6 +22,7 @@ class PlaylistOptionsBottomSheet( private val playlistId: String, private val playlistName: String, private val playlistType: PlaylistType, + private val onRename: (newName: String) -> Unit = {}, private val onDelete: () -> Unit = {} ) : BaseBottomSheet() { private val shareData = ShareData(currentPlaylist = playlistName) @@ -86,7 +87,7 @@ class PlaylistOptionsBottomSheet( }.show(parentFragmentManager, null) } getString(R.string.renamePlaylist) -> { - RenamePlaylistDialog(playlistId, playlistName) + RenamePlaylistDialog(playlistId, playlistName, onRename) .show(parentFragmentManager, null) } else -> {