diff --git a/app/src/main/java/com/github/libretube/api/PipedApi.kt b/app/src/main/java/com/github/libretube/api/PipedApi.kt index f442d3104..53400a9f5 100644 --- a/app/src/main/java/com/github/libretube/api/PipedApi.kt +++ b/app/src/main/java/com/github/libretube/api/PipedApi.kt @@ -142,7 +142,7 @@ interface PipedApi { ): Message @POST("import/playlist") - suspend fun importPlaylist( + suspend fun clonePlaylist( @Header("Authorization") token: String, @Body playlistId: PlaylistId ): PlaylistId diff --git a/app/src/main/java/com/github/libretube/api/PlaylistsHelper.kt b/app/src/main/java/com/github/libretube/api/PlaylistsHelper.kt index 1698c9645..58d99586e 100644 --- a/app/src/main/java/com/github/libretube/api/PlaylistsHelper.kt +++ b/app/src/main/java/com/github/libretube/api/PlaylistsHelper.kt @@ -20,7 +20,10 @@ import com.github.libretube.extensions.toastFromMainThread import com.github.libretube.obj.ImportPlaylist import com.github.libretube.util.PreferenceHelper import com.github.libretube.util.ProxyHelper +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.async +import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking import retrofit2.HttpException import java.io.IOException @@ -28,12 +31,12 @@ import java.io.IOException object PlaylistsHelper { private val pipedPlaylistRegex = "[\\da-fA-F]{8}-[\\da-fA-F]{4}-[\\da-fA-F]{4}-[\\da-fA-F]{4}-[\\da-fA-F]{12}".toRegex() - val token get() = PreferenceHelper.getToken() + private val token get() = PreferenceHelper.getToken() - private fun loggedIn() = token != "" + val loggedIn: Boolean get() = token != "" suspend fun getPlaylists(): List { - if (loggedIn()) return RetrofitInstance.authApi.getUserPlaylists(token) + if (loggedIn) return RetrofitInstance.authApi.getUserPlaylists(token) val localPlaylists = awaitQuery { DatabaseHolder.Database.localPlaylistsDao().getAll() @@ -75,7 +78,7 @@ object PlaylistsHelper { playlistName: String, appContext: Context ): String? { - if (!loggedIn()) { + if (!loggedIn) { awaitQuery { DatabaseHolder.Database.localPlaylistsDao().createPlaylist( LocalPlaylist( @@ -109,7 +112,7 @@ object PlaylistsHelper { } suspend fun addToPlaylist(playlistId: String, vararg videoIds: String): Boolean { - if (!loggedIn()) { + if (!loggedIn) { val localPlaylist = DatabaseHolder.Database.localPlaylistsDao().getAll() .first { it.playlist.id.toString() == playlistId } @@ -144,7 +147,7 @@ object PlaylistsHelper { } suspend fun renamePlaylist(playlistId: String, newName: String): Boolean { - if (!loggedIn()) { + if (!loggedIn) { val playlist = awaitQuery { DatabaseHolder.Database.localPlaylistsDao().getAll() }.first { it.playlist.id.toString() == playlistId }.playlist @@ -165,7 +168,7 @@ object PlaylistsHelper { } suspend fun removeFromPlaylist(playlistId: String, index: Int) { - if (!loggedIn()) { + if (!loggedIn) { val transaction = awaitQuery { DatabaseHolder.Database.localPlaylistsDao().getAll() }.first { it.playlist.id.toString() == playlistId } @@ -228,8 +231,63 @@ object PlaylistsHelper { return importLists } + fun clonePlaylist(context: Context, playlistId: 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.orEmpty() + .map { it.url!!.toID() } + .toTypedArray() + ) + + var nextPage = playlist.nextpage + while (nextPage != null) { + nextPage = try { + RetrofitInstance.api.getPlaylistNextPage(playlistId, nextPage).apply { + addToPlaylist( + newPlaylist, + *relatedStreams.orEmpty() + .map { it.url!!.toID() } + .toTypedArray() + ) + }.nextpage + } catch (e: Exception) { + return@launch + } + } + } + return + } + + CoroutineScope(Dispatchers.IO).launch { + val response = try { + RetrofitInstance.authApi.clonePlaylist( + token, + PlaylistId(playlistId) + ) + } catch (e: Exception) { + Log.e(TAG(), e.toString()) + return@launch + } + appContext?.toastFromMainThread( + if (response.playlistId != null) R.string.playlistCloned else R.string.server_error + ) + } + } + fun getPrivatePlaylistType(): PlaylistType { - return if (loggedIn()) PlaylistType.PRIVATE else PlaylistType.LOCAL + return if (loggedIn) PlaylistType.PRIVATE else PlaylistType.LOCAL } private fun getPrivatePlaylistType(playlistId: String): PlaylistType { diff --git a/app/src/main/java/com/github/libretube/ui/sheets/PlaylistOptionsBottomSheet.kt b/app/src/main/java/com/github/libretube/ui/sheets/PlaylistOptionsBottomSheet.kt index 4aaf10b9f..f517a55b8 100644 --- a/app/src/main/java/com/github/libretube/ui/sheets/PlaylistOptionsBottomSheet.kt +++ b/app/src/main/java/com/github/libretube/ui/sheets/PlaylistOptionsBottomSheet.kt @@ -1,26 +1,18 @@ package com.github.libretube.ui.sheets import android.os.Bundle -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.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 kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking -import retrofit2.HttpException -import java.io.IOException class PlaylistOptionsBottomSheet( private val playlistId: String, @@ -62,16 +54,7 @@ class PlaylistOptionsBottomSheet( } // Clone the playlist to the users Piped account context?.getString(R.string.clonePlaylist) -> { - val token = PreferenceHelper.getToken() - if (token != "") { - importPlaylist(token, playlistId) - } else { - Toast.makeText( - context, - R.string.login_first, - Toast.LENGTH_SHORT - ).show() - } + PlaylistsHelper.clonePlaylist(requireContext(), playlistId) } // share the playlist context?.getString(R.string.share) -> { @@ -91,22 +74,4 @@ class PlaylistOptionsBottomSheet( } super.onCreate(savedInstanceState) } - - private fun importPlaylist(token: String, playlistId: String) { - val appContext = context?.applicationContext - CoroutineScope(Dispatchers.IO).launch { - val response = try { - RetrofitInstance.authApi.importPlaylist( - token, - PlaylistId(playlistId) - ) - } catch (e: IOException) { - println(e) - return@launch - } catch (e: HttpException) { - return@launch - } - appContext?.toastFromMainThread(if (response.playlistId != null) R.string.playlistCloned else R.string.server_error) - } - } }