Skip to content

Commit

Permalink
feat: add playlist sorting artists
Browse files Browse the repository at this point in the history
  • Loading branch information
Malopieds committed Jul 9, 2024
1 parent c79fadf commit 7f9211f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import java.text.Collator
import java.util.Locale
import javax.inject.Inject

@HiltViewModel
Expand Down Expand Up @@ -52,10 +54,11 @@ class AutoPlaylistViewModel
PlaylistSongSortType.CUSTOM -> songs
PlaylistSongSortType.CREATE_DATE -> songs.sortedBy { it.id }
PlaylistSongSortType.NAME -> songs.sortedBy { it.song.title }
PlaylistSongSortType.ARTIST ->
songs.sortedBy { song ->
song.artists.joinToString { it.name }
}
PlaylistSongSortType.ARTIST -> {
val collator = Collator.getInstance(Locale.getDefault())
collator.strength = Collator.PRIMARY
songs.sortedWith(compareBy(collator) { song -> song.artists.joinToString("") { it.name } })
}
PlaylistSongSortType.PLAY_TIME -> songs.sortedBy { it.song.totalPlayTime }
}.reversed(sortDescending && sortType != PlaylistSongSortType.CUSTOM)
}.stateIn(viewModelScope, SharingStarted.Lazily, emptyList())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import java.text.Collator
import java.util.Locale
import javax.inject.Inject

@HiltViewModel
Expand Down Expand Up @@ -45,10 +47,11 @@ class LocalPlaylistViewModel
PlaylistSongSortType.CUSTOM -> songs
PlaylistSongSortType.CREATE_DATE -> songs.sortedBy { it.map.id }
PlaylistSongSortType.NAME -> songs.sortedBy { it.song.song.title }
PlaylistSongSortType.ARTIST ->
songs.sortedBy { song ->
song.song.artists.joinToString { it.name }
}
PlaylistSongSortType.ARTIST -> {
val collator = Collator.getInstance(Locale.getDefault())
collator.strength = Collator.PRIMARY
songs.sortedWith(compareBy(collator) { song -> song.song.artists.joinToString("") { it.name } })
}
PlaylistSongSortType.PLAY_TIME -> songs.sortedBy { it.song.song.totalPlayTime }
}.reversed(sortDescending && sortType != PlaylistSongSortType.CUSTOM)
}.stateIn(viewModelScope, SharingStarted.Lazily, emptyList())
Expand Down

0 comments on commit 7f9211f

Please sign in to comment.