Merge pull request #3365 from Bnyro/master

Update playlist name properly after renaming
This commit is contained in:
Bnyro 2023-03-24 15:41:39 +01:00 committed by GitHub
commit a0bd081bb4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 10 deletions

View File

@ -5,7 +5,7 @@ import kotlinx.serialization.Serializable
@Serializable @Serializable
data class Playlists( data class Playlists(
val id: String? = null, val id: String? = null,
val name: String? = null, var name: String? = null,
val shortDescription: String? = null, val shortDescription: String? = null,
val thumbnail: String? = null, val thumbnail: String? = null,
val videos: Long = 0 val videos: Long = 0

View File

@ -68,6 +68,10 @@ class PlaylistsAdapter(
playlistType = playlistType, playlistType = playlistType,
onDelete = { onDelete = {
onDelete(position, root.context as BaseActivity) onDelete(position, root.context as BaseActivity)
},
onRename = {
playlistTitle.text = it
playlist.name = it
} }
) )
playlistOptionsDialog.show( playlistOptionsDialog.show(

View File

@ -20,7 +20,8 @@ import kotlinx.coroutines.withContext
class RenamePlaylistDialog( class RenamePlaylistDialog(
private val playlistId: String, private val playlistId: String,
private val currentPlaylistName: String private val currentPlaylistName: String,
private val onSuccess: (String) -> Unit
) : DialogFragment() { ) : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val binding = DialogTextPreferenceBinding.inflate(layoutInflater) val binding = DialogTextPreferenceBinding.inflate(layoutInflater)
@ -36,20 +37,20 @@ class RenamePlaylistDialog(
.show() .show()
.apply { .apply {
getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener { getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener {
val input = binding.input.text?.toString() val newPlaylistName = binding.input.text?.toString()
if (input.isNullOrEmpty()) { if (newPlaylistName.isNullOrEmpty()) {
Toast.makeText(context, R.string.emptyPlaylistName, Toast.LENGTH_SHORT) Toast.makeText(context, R.string.emptyPlaylistName, Toast.LENGTH_SHORT)
.show() .show()
return@setOnClickListener return@setOnClickListener
} }
if (input == currentPlaylistName) return@setOnClickListener if (newPlaylistName == currentPlaylistName) return@setOnClickListener
val appContext = requireContext().applicationContext val appContext = requireContext().applicationContext
lifecycleScope.launch { lifecycleScope.launch {
requireDialog().hide() requireDialog().hide()
val success = try { val success = try {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
PlaylistsHelper.renamePlaylist(playlistId, input) PlaylistsHelper.renamePlaylist(playlistId, newPlaylistName)
} }
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG(), e.toString()) Log.e(TAG(), e.toString())
@ -58,6 +59,7 @@ class RenamePlaylistDialog(
} }
if (success) { if (success) {
appContext.toastFromMainThread(R.string.success) appContext.toastFromMainThread(R.string.success)
onSuccess.invoke(newPlaylistName)
} else { } else {
appContext.toastFromMainThread(R.string.server_error) appContext.toastFromMainThread(R.string.server_error)
} }

View File

@ -1,5 +1,6 @@
package com.github.libretube.ui.fragments package com.github.libretube.ui.fragments
import android.annotation.SuppressLint
import android.os.Bundle import android.os.Bundle
import android.util.Log import android.util.Log
import android.view.LayoutInflater import android.view.LayoutInflater
@ -129,9 +130,18 @@ class PlaylistFragment : Fragment() {
// show playlist options // show playlist options
binding.optionsMenu.setOnClickListener { binding.optionsMenu.setOnClickListener {
PlaylistOptionsBottomSheet(playlistId!!, playlistName.orEmpty(), playlistType) { PlaylistOptionsBottomSheet(
playlistId = playlistId.orEmpty(),
playlistName = playlistName.orEmpty(),
playlistType = playlistType,
onDelete = {
findNavController().popBackStack() findNavController().popBackStack()
}.show( },
onRename = {
binding.playlistName.text = it
playlistName = it
}
).show(
childFragmentManager, childFragmentManager,
PlaylistOptionsBottomSheet::class.java.name PlaylistOptionsBottomSheet::class.java.name
) )
@ -258,6 +268,7 @@ class PlaylistFragment : Fragment() {
} }
} }
@SuppressLint("StringFormatInvalid", "StringFormatMatches")
private fun getChannelAndVideoString(playlist: Playlist, count: Int): String { private fun getChannelAndVideoString(playlist: Playlist, count: Int): String {
return playlist.uploader?.let { return playlist.uploader?.let {
getString(R.string.uploaderAndVideoCount, it, count) getString(R.string.uploaderAndVideoCount, it, count)

View File

@ -22,6 +22,7 @@ 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 onRename: (newName: String) -> Unit = {},
private val onDelete: () -> Unit = {} private val onDelete: () -> Unit = {}
) : BaseBottomSheet() { ) : BaseBottomSheet() {
private val shareData = ShareData(currentPlaylist = playlistName) private val shareData = ShareData(currentPlaylist = playlistName)
@ -86,7 +87,7 @@ class PlaylistOptionsBottomSheet(
}.show(parentFragmentManager, null) }.show(parentFragmentManager, null)
} }
getString(R.string.renamePlaylist) -> { getString(R.string.renamePlaylist) -> {
RenamePlaylistDialog(playlistId, playlistName) RenamePlaylistDialog(playlistId, playlistName, onRename)
.show(parentFragmentManager, null) .show(parentFragmentManager, null)
} }
else -> { else -> {