Merge pull request #3655 from Bnyro/master

Fix crash when sorting playlists
This commit is contained in:
Bnyro 2023-04-28 11:07:40 +02:00 committed by GitHub
commit 96397abeae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 27 deletions

View File

@ -1,6 +1,5 @@
package com.github.libretube.api package com.github.libretube.api
import android.content.Context
import androidx.core.text.isDigitsOnly import androidx.core.text.isDigitsOnly
import com.github.libretube.api.obj.Playlist import com.github.libretube.api.obj.Playlist
import com.github.libretube.api.obj.PlaylistId import com.github.libretube.api.obj.PlaylistId
@ -179,8 +178,7 @@ object PlaylistsHelper {
} }
} }
suspend fun clonePlaylist(context: Context, playlistId: String): String? { suspend fun clonePlaylist(playlistId: String): String? {
val appContext = context.applicationContext
if (!loggedIn) { if (!loggedIn) {
val playlist = RetrofitInstance.api.getPlaylist(playlistId) val playlist = RetrofitInstance.api.getPlaylist(playlistId)
val newPlaylist = createPlaylist(playlist.name ?: "Unknown name") ?: return null val newPlaylist = createPlaylist(playlist.name ?: "Unknown name") ?: return null

View File

@ -30,7 +30,7 @@ class CreatePlaylistDialog(
requireDialog().hide() requireDialog().hide()
val playlistId = withContext(Dispatchers.IO) { val playlistId = withContext(Dispatchers.IO) {
runCatching { runCatching {
PlaylistsHelper.clonePlaylist(requireContext(), it) PlaylistsHelper.clonePlaylist(it)
}.getOrNull() }.getOrNull()
} }
if (playlistId != null) { if (playlistId != null) {

View File

@ -237,30 +237,20 @@ class PlaylistFragment : Fragment() {
} }
private fun showPlaylistVideos(playlist: Playlist) { private fun showPlaylistVideos(playlist: Playlist) {
val videos = if (playlistType == PlaylistType.PUBLIC) { val videos = when {
playlistFeed selectedSortOrder in listOf(0, 1) || playlistType == PlaylistType.PUBLIC -> {
} else {
when (selectedSortOrder) {
0, 1 -> {
if (playlistType == PlaylistType.LOCAL) {
playlistFeed.sortedBy {
it.url.orEmpty().toInt()
}
} else {
playlistFeed playlistFeed
} }
} selectedSortOrder in listOf(2, 3) -> {
2, 3 -> {
playlistFeed.sortedBy { it.duration } playlistFeed.sortedBy { it.duration }
} }
4, 5 -> { selectedSortOrder in listOf(4, 5) -> {
playlistFeed.sortedBy { it.title } playlistFeed.sortedBy { it.title }
} }
else -> throw IllegalArgumentException() else -> throw IllegalArgumentException()
}.let { }.let {
if (selectedSortOrder % 2 == 0) it else it.reversed() if (selectedSortOrder % 2 == 0) it else it.reversed()
} }
}
playlistAdapter = PlaylistAdapter(videos.toMutableList(), playlistId!!, playlistType) playlistAdapter = PlaylistAdapter(videos.toMutableList(), playlistId!!, playlistType)
binding.playlistRecView.adapter = playlistAdapter binding.playlistRecView.adapter = playlistAdapter

View File

@ -69,7 +69,7 @@ class PlaylistOptionsBottomSheet(
val context = requireContext() val context = requireContext()
val playlistId = withContext(Dispatchers.IO) { val playlistId = withContext(Dispatchers.IO) {
runCatching { runCatching {
PlaylistsHelper.clonePlaylist(context, playlistId) PlaylistsHelper.clonePlaylist(playlistId)
}.getOrNull() }.getOrNull()
} }
context.toastFromMainDispatcher( context.toastFromMainDispatcher(