Merge pull request #4328 from 0x24D/local-playlist-order

fix: Sort local playlists based on selected preference
This commit is contained in:
Bnyro 2023-07-30 16:48:47 +02:00 committed by GitHub
commit c24c147ae6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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.Playlist
import com.github.libretube.api.obj.Playlists import com.github.libretube.api.obj.Playlists
import com.github.libretube.api.obj.StreamItem 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.constants.YOUTUBE_FRONTEND_URL
import com.github.libretube.db.DatabaseHolder import com.github.libretube.db.DatabaseHolder
import com.github.libretube.db.obj.LocalPlaylist import com.github.libretube.db.obj.LocalPlaylist
@ -31,10 +32,10 @@ object PlaylistsHelper {
private fun Message.isOk() = this.message == "ok" private fun Message.isOk() = this.message == "ok"
suspend fun getPlaylists(): List<Playlists> = withContext(Dispatchers.IO) { suspend fun getPlaylists(): List<Playlists> = withContext(Dispatchers.IO) {
if (loggedIn) { val playlists = if (loggedIn) {
RetrofitInstance.authApi.getUserPlaylists(token) RetrofitInstance.authApi.getUserPlaylists(token)
} else { } else {
DatabaseHolder.Database.localPlaylistsDao().getAll().reversed() DatabaseHolder.Database.localPlaylistsDao().getAll()
.map { .map {
Playlists( Playlists(
id = it.playlist.id.toString(), 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 { suspend fun getPlaylist(playlistId: String): Playlist {

View File

@ -128,7 +128,7 @@ class LibraryFragment : Fragment() {
_binding?.playlistRefresh?.isRefreshing = true _binding?.playlistRefresh?.isRefreshing = true
lifecycleScope.launch { lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.CREATED) { repeatOnLifecycle(Lifecycle.State.CREATED) {
var playlists = try { val playlists = try {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
PlaylistsHelper.getPlaylists() PlaylistsHelper.getPlaylists()
} }
@ -142,18 +142,6 @@ class LibraryFragment : Fragment() {
binding.playlistRefresh.isRefreshing = false binding.playlistRefresh.isRefreshing = false
if (playlists.isNotEmpty()) { 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( val playlistsAdapter = PlaylistsAdapter(
playlists.toMutableList(), playlists.toMutableList(),
PlaylistsHelper.getPrivatePlaylistType() PlaylistsHelper.getPrivatePlaylistType()