Merge pull request #1254 from Bnyro/master

Option to rename playlists
This commit is contained in:
Bnyro 2022-09-10 12:43:56 +02:00 committed by GitHub
commit 7bfc1d18eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 451 additions and 442 deletions

View File

@ -85,7 +85,6 @@ class PlaylistsAdapter(
} }
private fun deletePlaylist(id: String, position: Int) { private fun deletePlaylist(id: String, position: Int) {
fun run() {
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
val response = try { val response = try {
RetrofitInstance.authApi.deletePlaylist( RetrofitInstance.authApi.deletePlaylist(
@ -110,8 +109,6 @@ class PlaylistsAdapter(
} }
} }
} }
run()
}
} }
class PlaylistsViewHolder(val binding: PlaylistsRowBinding) : RecyclerView.ViewHolder(binding.root) class PlaylistsViewHolder(val binding: PlaylistsRowBinding) : RecyclerView.ViewHolder(binding.root)

View File

@ -140,6 +140,12 @@ interface PipedApi {
@GET("user/playlists") @GET("user/playlists")
suspend fun playlists(@Header("Authorization") token: String): List<Playlists> suspend fun playlists(@Header("Authorization") token: String): List<Playlists>
@POST("user/playlists/rename")
suspend fun renamePlaylist(
@Header("Authorization") token: String,
@Body playlistId: PlaylistId
)
@POST("user/playlists/delete") @POST("user/playlists/delete")
suspend fun deletePlaylist( suspend fun deletePlaylist(
@Header("Authorization") token: String, @Header("Authorization") token: String,

View File

@ -44,7 +44,6 @@ class AddToPlaylistDialog : DialogFragment() {
} }
private fun fetchPlaylists() { private fun fetchPlaylists() {
fun run() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val response = try { val response = try {
RetrofitInstance.authApi.playlists(token) RetrofitInstance.authApi.playlists(token)
@ -88,8 +87,6 @@ class AddToPlaylistDialog : DialogFragment() {
} }
} }
} }
run()
}
private fun addToPlaylist(playlistId: String) { private fun addToPlaylist(playlistId: String) {
fun run() { fun run() {

View File

@ -50,7 +50,6 @@ class CreatePlaylistDialog : DialogFragment() {
} }
private fun createPlaylist(name: String) { private fun createPlaylist(name: String) {
fun run() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val response = try { val response = try {
RetrofitInstance.authApi.createPlaylist(token, Playlists(name = name)) RetrofitInstance.authApi.createPlaylist(token, Playlists(name = name))
@ -80,6 +79,4 @@ class CreatePlaylistDialog : DialogFragment() {
dismiss() dismiss()
} }
} }
run()
}
} }

View File

@ -41,7 +41,6 @@ class DeleteAccountDialog : DialogFragment() {
} }
private fun deleteAccount(password: String) { private fun deleteAccount(password: String) {
fun run() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val token = PreferenceHelper.getToken() val token = PreferenceHelper.getToken()
@ -58,8 +57,6 @@ class DeleteAccountDialog : DialogFragment() {
restartDialog.show(childFragmentManager, RequireRestartDialog::class.java.name) restartDialog.show(childFragmentManager, RequireRestartDialog::class.java.name)
} }
} }
run()
}
private fun logout() { private fun logout() {
PreferenceHelper.setToken("") PreferenceHelper.setToken("")

View File

@ -55,7 +55,6 @@ class LoginDialog : DialogFragment() {
} }
private fun login(login: Login) { private fun login(login: Login) {
fun run() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val response = try { val response = try {
RetrofitInstance.authApi.login(login) RetrofitInstance.authApi.login(login)
@ -83,8 +82,6 @@ class LoginDialog : DialogFragment() {
} }
} }
} }
run()
}
private fun register(login: Login) { private fun register(login: Login) {
fun run() { fun run() {

View File

@ -2,12 +2,14 @@ package com.github.libretube.dialogs
import android.app.Dialog import android.app.Dialog
import android.os.Bundle import android.os.Bundle
import android.text.InputType
import android.util.Log import android.util.Log
import android.widget.ArrayAdapter import android.widget.ArrayAdapter
import android.widget.Toast import android.widget.Toast
import androidx.fragment.app.DialogFragment import androidx.fragment.app.DialogFragment
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.api.RetrofitInstance import com.github.libretube.api.RetrofitInstance
import com.github.libretube.databinding.DialogTextPreferenceBinding
import com.github.libretube.extensions.TAG import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.toID import com.github.libretube.extensions.toID
import com.github.libretube.obj.PlaylistId import com.github.libretube.obj.PlaylistId
@ -36,6 +38,7 @@ class PlaylistOptionsDialog(
if (isOwner) { if (isOwner) {
optionsList = optionsList + optionsList = optionsList +
context?.getString(R.string.renamePlaylist)!! +
context?.getString(R.string.deletePlaylist)!! - context?.getString(R.string.deletePlaylist)!! -
context?.getString(R.string.clonePlaylist)!! context?.getString(R.string.clonePlaylist)!!
} }
@ -88,8 +91,31 @@ class PlaylistOptionsDialog(
shareDialog.show(parentFragmentManager, ShareDialog::class.java.name) shareDialog.show(parentFragmentManager, ShareDialog::class.java.name)
} }
context?.getString(R.string.deletePlaylist) -> { context?.getString(R.string.deletePlaylist) -> {
val token = PreferenceHelper.getToken() deletePlaylist(
deletePlaylist(playlistId, token) playlistId
)
}
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
}
renamePlaylist(playlistId, binding.input.text.toString())
}
.setNegativeButton(R.string.cancel, null)
.show()
} }
} }
} }
@ -97,7 +123,6 @@ class PlaylistOptionsDialog(
} }
private fun importPlaylist(token: String, playlistId: String) { private fun importPlaylist(token: String, playlistId: String) {
fun run() {
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
val response = try { val response = try {
RetrofitInstance.authApi.importPlaylist(token, PlaylistId(playlistId)) RetrofitInstance.authApi.importPlaylist(token, PlaylistId(playlistId))
@ -110,24 +135,33 @@ class PlaylistOptionsDialog(
Log.e(TAG(), response.toString()) Log.e(TAG(), response.toString())
} }
} }
run()
}
private fun deletePlaylist(id: String, token: String) { private fun renamePlaylist(id: String, newName: String) {
fun run() {
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
try { try {
RetrofitInstance.authApi.deletePlaylist(token, PlaylistId(id)) RetrofitInstance.authApi.renamePlaylist(
} catch (e: IOException) { PreferenceHelper.getToken(),
println(e) PlaylistId(
Log.e(TAG(), "IOException, you might not have internet connection") playlistId = id,
return@launch newName = newName
} catch (e: HttpException) { )
Log.e(TAG(), "HttpException, unexpected response") )
} catch (e: Exception) {
return@launch return@launch
} }
} }
} }
run()
private fun deletePlaylist(id: String) {
CoroutineScope(Dispatchers.IO).launch {
try {
RetrofitInstance.authApi.deletePlaylist(
PreferenceHelper.getToken(),
PlaylistId(id)
)
} catch (e: Exception) {
return@launch
}
}
} }
} }

View File

@ -83,7 +83,6 @@ class ChannelFragment : BaseFragment() {
} }
private fun fetchChannel() { private fun fetchChannel() {
fun run() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val response = try { val response = try {
if (channelId != null) { if (channelId != null) {
@ -163,8 +162,6 @@ class ChannelFragment : BaseFragment() {
} }
} }
} }
run()
}
private fun fetchChannelNextPage() { private fun fetchChannelNextPage() {
fun run() { fun run() {

View File

@ -62,7 +62,6 @@ class HomeFragment : BaseFragment() {
} }
private fun fetchTrending() { private fun fetchTrending() {
fun run() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val response = try { val response = try {
RetrofitInstance.api.getTrending(region) RetrofitInstance.api.getTrending(region)
@ -106,6 +105,4 @@ class HomeFragment : BaseFragment() {
} }
} }
} }
run()
}
} }

View File

@ -95,7 +95,6 @@ class LibraryFragment : BaseFragment() {
} }
fun fetchPlaylists() { fun fetchPlaylists() {
fun run() {
binding.playlistRefresh.isRefreshing = true binding.playlistRefresh.isRefreshing = true
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val response = try { val response = try {
@ -140,6 +139,4 @@ class LibraryFragment : BaseFragment() {
} }
} }
} }
run()
}
} }

View File

@ -1332,7 +1332,6 @@ class PlayerFragment : BaseFragment() {
} }
private fun isSubscribed() { private fun isSubscribed() {
fun run() {
val channelId = streams.uploaderUrl!!.toID() val channelId = streams.uploaderUrl!!.toID()
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
isSubscribed = SubscriptionHelper.isSubscribed(channelId) isSubscribed = SubscriptionHelper.isSubscribed(channelId)
@ -1357,8 +1356,6 @@ class PlayerFragment : BaseFragment() {
} }
} }
} }
run()
}
private fun fetchComments() { private fun fetchComments() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {

View File

@ -58,7 +58,6 @@ class PlaylistFragment : BaseFragment() {
} }
private fun fetchPlaylist() { private fun fetchPlaylist() {
fun run() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val response = try { val response = try {
// load locally stored playlists with the auth api // load locally stored playlists with the auth api
@ -159,8 +158,6 @@ class PlaylistFragment : BaseFragment() {
} }
} }
} }
run()
}
private fun fetchNextPage() { private fun fetchNextPage() {
fun run() { fun run() {

View File

@ -69,7 +69,6 @@ class SearchFragment : BaseFragment() {
} }
private fun fetchSuggestions(query: String) { private fun fetchSuggestions(query: String) {
fun run() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val response = try { val response = try {
RetrofitInstance.api.getSuggestions(query) RetrofitInstance.api.getSuggestions(query)
@ -94,8 +93,6 @@ class SearchFragment : BaseFragment() {
} }
} }
} }
run()
}
private fun showHistory() { private fun showHistory() {
var historyList = listOf<String>() var historyList = listOf<String>()

View File

@ -6,5 +6,6 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties
data class PlaylistId( data class PlaylistId(
var playlistId: String? = null, var playlistId: String? = null,
var videoId: String? = null, var videoId: String? = null,
var newName: String? = null,
var index: Int = -1 var index: Int = -1
) )

View File

@ -316,4 +316,5 @@
<string name="audio_video_summary">Quality and format</string> <string name="audio_video_summary">Quality and format</string>
<string name="delete">Delete</string> <string name="delete">Delete</string>
<string name="trending_layout">Alternative trending layout</string> <string name="trending_layout">Alternative trending layout</string>
<string name="renamePlaylist">Rename playlist</string>
</resources> </resources>