Merge pull request #2175 from Bnyro/master

Show the current name when renaming a playlist
This commit is contained in:
Bnyro 2022-12-02 12:50:20 +01:00 committed by GitHub
commit 7b3b145e2d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 71 additions and 36 deletions

View File

@ -154,7 +154,7 @@ interface PipedApi {
suspend fun renamePlaylist(
@Header("Authorization") token: String,
@Body playlistId: PlaylistId
)
): PlaylistId
@POST("user/playlists/delete")
suspend fun deletePlaylist(

View File

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

View File

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

View File

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