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
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

View File

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

View File

@ -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)
}

View File

@ -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)

View File

@ -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 -> {