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

View File

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

View File

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

View File

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