From 1b493e036afbe15a6dfd3d6dd9d64fe187c24be8 Mon Sep 17 00:00:00 2001 From: 0x24d Date: Sat, 29 Jul 2023 23:51:11 +0100 Subject: [PATCH] fix: Sort local playlists based on selected preference Move sorting from LibraryFragment to PlaylistsHelper --- .../com/github/libretube/api/PlaylistsHelper.kt | 17 +++++++++++++++-- .../libretube/ui/fragments/LibraryFragment.kt | 14 +------------- 2 files changed, 16 insertions(+), 15 deletions(-) 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 adbaabfc7..afbc3f422 100644 --- a/app/src/main/java/com/github/libretube/api/PlaylistsHelper.kt +++ b/app/src/main/java/com/github/libretube/api/PlaylistsHelper.kt @@ -6,6 +6,7 @@ import com.github.libretube.api.obj.Message import com.github.libretube.api.obj.Playlist import com.github.libretube.api.obj.Playlists import com.github.libretube.api.obj.StreamItem +import com.github.libretube.constants.PreferenceKeys import com.github.libretube.constants.YOUTUBE_FRONTEND_URL import com.github.libretube.db.DatabaseHolder import com.github.libretube.db.obj.LocalPlaylist @@ -31,10 +32,10 @@ object PlaylistsHelper { private fun Message.isOk() = this.message == "ok" suspend fun getPlaylists(): List = withContext(Dispatchers.IO) { - if (loggedIn) { + val playlists = if (loggedIn) { RetrofitInstance.authApi.getUserPlaylists(token) } else { - DatabaseHolder.Database.localPlaylistsDao().getAll().reversed() + DatabaseHolder.Database.localPlaylistsDao().getAll() .map { Playlists( id = it.playlist.id.toString(), @@ -45,6 +46,18 @@ object PlaylistsHelper { ) } } + + when ( + PreferenceHelper.getString(PreferenceKeys.PLAYLISTS_ORDER, "creation_date") + ) { + "creation_date" -> playlists + "creation_date_reversed" -> playlists.reversed() + "alphabetic" -> playlists.sortedBy { it.name?.lowercase() } + "alphabetic_reversed" -> playlists.sortedBy { it.name?.lowercase() } + .reversed() + + else -> playlists + } } suspend fun getPlaylist(playlistId: String): Playlist { diff --git a/app/src/main/java/com/github/libretube/ui/fragments/LibraryFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/LibraryFragment.kt index 802f87e74..337d96f3d 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/LibraryFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/LibraryFragment.kt @@ -128,7 +128,7 @@ class LibraryFragment : Fragment() { _binding?.playlistRefresh?.isRefreshing = true lifecycleScope.launch { repeatOnLifecycle(Lifecycle.State.CREATED) { - var playlists = try { + val playlists = try { withContext(Dispatchers.IO) { PlaylistsHelper.getPlaylists() } @@ -142,18 +142,6 @@ class LibraryFragment : Fragment() { binding.playlistRefresh.isRefreshing = false if (playlists.isNotEmpty()) { - playlists = when ( - PreferenceHelper.getString(PreferenceKeys.PLAYLISTS_ORDER, "creation_date") - ) { - "creation_date" -> playlists - "creation_date_reversed" -> playlists.reversed() - "alphabetic" -> playlists.sortedBy { it.name?.lowercase() } - "alphabetic_reversed" -> playlists.sortedBy { it.name?.lowercase() } - .reversed() - - else -> playlists - } - val playlistsAdapter = PlaylistsAdapter( playlists.toMutableList(), PlaylistsHelper.getPrivatePlaylistType()