Reload playlists after successfully cloning

This commit is contained in:
Bnyro 2023-01-31 15:55:02 +01:00
parent b1e703b8c3
commit 7b23da5eca
4 changed files with 52 additions and 43 deletions

View File

@ -20,11 +20,9 @@ import com.github.libretube.obj.ImportPlaylist
import com.github.libretube.util.PreferenceHelper
import com.github.libretube.util.ProxyHelper
import java.io.IOException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import retrofit2.HttpException
@ -205,49 +203,35 @@ object PlaylistsHelper {
}
}
fun clonePlaylist(context: Context, playlistId: String) {
suspend fun clonePlaylist(context: Context, playlistId: String): String? {
val appContext = context.applicationContext
if (!loggedIn) {
CoroutineScope(Dispatchers.IO).launch {
val playlist = try {
RetrofitInstance.api.getPlaylist(playlistId)
} catch (e: Exception) {
appContext.toastFromMainThread(R.string.server_error)
return@launch
}
val newPlaylist = createPlaylist(playlist.name ?: "Unknown name", appContext)
newPlaylist ?: return@launch
addToPlaylist(newPlaylist, *playlist.relatedStreams.toTypedArray())
var nextPage = playlist.nextpage
while (nextPage != null) {
nextPage = try {
RetrofitInstance.api.getPlaylistNextPage(playlistId, nextPage).apply {
addToPlaylist(newPlaylist, *relatedStreams.toTypedArray())
}.nextpage
} catch (e: Exception) {
return@launch
}
}
}
return
}
CoroutineScope(Dispatchers.IO).launch {
val response = try {
RetrofitInstance.authApi.clonePlaylist(
token,
PlaylistId(playlistId)
)
val playlist = try {
RetrofitInstance.api.getPlaylist(playlistId)
} catch (e: Exception) {
Log.e(TAG(), e.toString())
return@launch
appContext.toastFromMainThread(R.string.server_error)
return null
}
appContext?.toastFromMainThread(
if (response.playlistId != null) R.string.playlistCloned else R.string.server_error
)
val newPlaylist = createPlaylist(playlist.name ?: "Unknown name", appContext) ?: return null
addToPlaylist(newPlaylist, *playlist.relatedStreams.toTypedArray())
var nextPage = playlist.nextpage
while (nextPage != null) {
nextPage = try {
RetrofitInstance.api.getPlaylistNextPage(playlistId, nextPage).apply {
addToPlaylist(newPlaylist, *relatedStreams.toTypedArray())
}.nextpage
} catch (e: Exception) {
break
}
}
return playlistId
}
return runCatching {
RetrofitInstance.authApi.clonePlaylist(token, PlaylistId(playlistId))
}.getOrNull()?.playlistId
}
fun getPrivatePlaylistType(): PlaylistType {

View File

@ -7,6 +7,7 @@ import androidx.fragment.app.DialogFragment
import com.github.libretube.R
import com.github.libretube.api.PlaylistsHelper
import com.github.libretube.databinding.DialogCreatePlaylistBinding
import com.github.libretube.extensions.toastFromMainThread
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
@ -24,8 +25,19 @@ class CreatePlaylistDialog(
binding.clonePlaylist.setOnClickListener {
val playlistUrl = binding.playlistUrl.text.toString().toHttpUrlOrNull()
val appContext = context?.applicationContext
playlistUrl?.queryParameter("list")?.let {
PlaylistsHelper.clonePlaylist(requireContext(), it)
CoroutineScope(Dispatchers.IO).launch {
val playlistId = PlaylistsHelper.clonePlaylist(requireContext(), it)?.also {
withContext(Dispatchers.Main) {
onSuccess.invoke()
}
}
appContext?.toastFromMainThread(
if (playlistId != null) R.string.playlistCloned else R.string.server_error
)
}
dismiss()
} ?: run {
Toast.makeText(context, R.string.invalid_url, Toast.LENGTH_SHORT).show()

View File

@ -11,6 +11,7 @@ import com.github.libretube.extensions.awaitQuery
import com.github.libretube.extensions.query
import com.github.libretube.extensions.toID
import com.github.libretube.extensions.toPlaylistBookmark
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
@ -64,14 +65,23 @@ class PlaylistOptionsBottomSheet(
}
BackgroundHelper.playOnBackground(
context = requireContext(),
videoId = playlist.relatedStreams!![0].url!!.toID(),
videoId = playlist.relatedStreams[0].url!!.toID(),
playlistId = playlistId
)
}
}
// Clone the playlist to the users Piped account
getString(R.string.clonePlaylist) -> {
PlaylistsHelper.clonePlaylist(requireContext(), playlistId)
val appContext = context?.applicationContext
CoroutineScope(Dispatchers.IO).launch {
val playlistId = PlaylistsHelper.clonePlaylist(
requireContext().applicationContext,
playlistId
)
appContext?.toastFromMainThread(
if (playlistId != null) R.string.playlistCloned else R.string.server_error
)
}
}
// share the playlist
getString(R.string.share) -> {

View File

@ -13,6 +13,7 @@ import com.github.libretube.obj.BackupFile
import com.github.libretube.obj.PreferenceItem
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.booleanOrNull
import kotlinx.serialization.json.decodeFromStream
import kotlinx.serialization.json.encodeToStream
@ -27,6 +28,7 @@ class BackupHelper(private val context: Context) {
/**
* Write a [BackupFile] containing the database content as well as the preferences
*/
@OptIn(ExperimentalSerializationApi::class)
fun createAdvancedBackup(uri: Uri?, backupFile: BackupFile) {
uri?.let {
try {
@ -42,6 +44,7 @@ class BackupHelper(private val context: Context) {
/**
* Restore data from a [BackupFile]
*/
@OptIn(ExperimentalSerializationApi::class)
fun restoreAdvancedBackup(uri: Uri?) {
val backupFile = uri?.let {
context.contentResolver.openInputStream(it)?.use { inputStream ->