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) {
fun run() {
CoroutineScope(Dispatchers.IO).launch {
val response = try {
RetrofitInstance.authApi.deletePlaylist(
@ -110,8 +109,6 @@ class PlaylistsAdapter(
}
}
}
run()
}
}
class PlaylistsViewHolder(val binding: PlaylistsRowBinding) : RecyclerView.ViewHolder(binding.root)

View File

@ -140,6 +140,12 @@ interface PipedApi {
@GET("user/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")
suspend fun deletePlaylist(
@Header("Authorization") token: String,

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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