fix: Sort local playlists based on selected preference

Move sorting from LibraryFragment to PlaylistsHelper
This commit is contained in:
0x24d 2023-07-29 23:51:11 +01:00
parent 8672d7126a
commit 1b493e036a
2 changed files with 16 additions and 15 deletions

View File

@ -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<Playlists> = 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 {

View File

@ -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()