Sort artist repository in a localized way
This commit is contained in:
parent
6b51eb84fa
commit
41039bcdd5
2 changed files with 52 additions and 35 deletions
|
@ -17,6 +17,7 @@ package code.name.monkey.retromusic.model
|
||||||
import code.name.monkey.retromusic.helper.SortOrder
|
import code.name.monkey.retromusic.helper.SortOrder
|
||||||
import code.name.monkey.retromusic.util.MusicUtil
|
import code.name.monkey.retromusic.util.MusicUtil
|
||||||
import code.name.monkey.retromusic.util.PreferenceUtil
|
import code.name.monkey.retromusic.util.PreferenceUtil
|
||||||
|
import java.text.Collator
|
||||||
|
|
||||||
data class Artist(
|
data class Artist(
|
||||||
val id: Long,
|
val id: Long,
|
||||||
|
@ -60,37 +61,34 @@ data class Artist(
|
||||||
get() = albums.flatMap { it.songs }
|
get() = albums.flatMap { it.songs }
|
||||||
|
|
||||||
val sortedSongs: List<Song>
|
val sortedSongs: List<Song>
|
||||||
get() = songs.sortedWith(
|
get() {
|
||||||
when (PreferenceUtil.artistDetailSongSortOrder) {
|
val collator = Collator.getInstance()
|
||||||
SortOrder.ArtistSongSortOrder.SONG_A_Z -> { o1, o2 ->
|
return songs.sortedWith(
|
||||||
o1.title.compareTo(
|
when (PreferenceUtil.artistDetailSongSortOrder) {
|
||||||
o2.title
|
SortOrder.ArtistSongSortOrder.SONG_A_Z -> { o1, o2 ->
|
||||||
)
|
collator.compare(o1.title, o2.title)
|
||||||
}
|
}
|
||||||
SortOrder.ArtistSongSortOrder.SONG_Z_A -> { o1, o2 ->
|
SortOrder.ArtistSongSortOrder.SONG_Z_A -> { o1, o2 ->
|
||||||
o2.title.compareTo(
|
collator.compare(o2.title, o1.title)
|
||||||
o1.title
|
}
|
||||||
)
|
SortOrder.ArtistSongSortOrder.SONG_ALBUM -> { o1, o2 ->
|
||||||
}
|
collator.compare(o1.albumName, o2.albumName)
|
||||||
SortOrder.ArtistSongSortOrder.SONG_ALBUM -> { o1, o2 ->
|
}
|
||||||
o1.albumName.compareTo(
|
SortOrder.ArtistSongSortOrder.SONG_YEAR -> { o1, o2 ->
|
||||||
o2.albumName
|
o2.year.compareTo(
|
||||||
)
|
o1.year
|
||||||
}
|
)
|
||||||
SortOrder.ArtistSongSortOrder.SONG_YEAR -> { o1, o2 ->
|
}
|
||||||
o2.year.compareTo(
|
SortOrder.ArtistSongSortOrder.SONG_DURATION -> { o1, o2 ->
|
||||||
o1.year
|
o1.duration.compareTo(
|
||||||
)
|
o2.duration
|
||||||
}
|
)
|
||||||
SortOrder.ArtistSongSortOrder.SONG_DURATION -> { o1, o2 ->
|
}
|
||||||
o1.duration.compareTo(
|
else -> {
|
||||||
o2.duration
|
throw IllegalArgumentException("invalid ${PreferenceUtil.artistDetailSongSortOrder}")
|
||||||
)
|
}
|
||||||
}
|
})
|
||||||
else -> {
|
}
|
||||||
throw IllegalArgumentException("invalid ${PreferenceUtil.artistDetailSongSortOrder}")
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
fun safeGetFirstAlbum(): Album {
|
fun safeGetFirstAlbum(): Album {
|
||||||
return albums.firstOrNull() ?: Album.empty
|
return albums.firstOrNull() ?: Album.empty
|
||||||
|
|
|
@ -20,6 +20,7 @@ import code.name.monkey.retromusic.helper.SortOrder
|
||||||
import code.name.monkey.retromusic.model.Album
|
import code.name.monkey.retromusic.model.Album
|
||||||
import code.name.monkey.retromusic.model.Artist
|
import code.name.monkey.retromusic.model.Artist
|
||||||
import code.name.monkey.retromusic.util.PreferenceUtil
|
import code.name.monkey.retromusic.util.PreferenceUtil
|
||||||
|
import java.text.Collator
|
||||||
|
|
||||||
interface ArtistRepository {
|
interface ArtistRepository {
|
||||||
fun artists(): List<Artist>
|
fun artists(): List<Artist>
|
||||||
|
@ -103,7 +104,8 @@ class RealArtistRepository(
|
||||||
getSongLoaderSortOrder()
|
getSongLoaderSortOrder()
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
return splitIntoArtists(albumRepository.splitIntoAlbums(songs))
|
val artists = splitIntoArtists(albumRepository.splitIntoAlbums(songs))
|
||||||
|
return sortArtists(artists)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun albumArtists(): List<Artist> {
|
override fun albumArtists(): List<Artist> {
|
||||||
|
@ -115,7 +117,8 @@ class RealArtistRepository(
|
||||||
if (PreferenceUtil.artistSortOrder == SortOrder.ArtistSortOrder.ARTIST_A_Z) "" else " DESC"
|
if (PreferenceUtil.artistSortOrder == SortOrder.ArtistSortOrder.ARTIST_A_Z) "" else " DESC"
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
return splitIntoAlbumArtists(albumRepository.splitIntoAlbums(songs))
|
val artists = splitIntoAlbumArtists(albumRepository.splitIntoAlbums(songs))
|
||||||
|
return sortArtists(artists)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun albumArtists(query: String): List<Artist> {
|
override fun albumArtists(query: String): List<Artist> {
|
||||||
|
@ -126,7 +129,8 @@ class RealArtistRepository(
|
||||||
getSongLoaderSortOrder()
|
getSongLoaderSortOrder()
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
return splitIntoAlbumArtists(albumRepository.splitIntoAlbums(songs))
|
val artists = splitIntoAlbumArtists(albumRepository.splitIntoAlbums(songs))
|
||||||
|
return sortArtists(artists)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun artists(query: String): List<Artist> {
|
override fun artists(query: String): List<Artist> {
|
||||||
|
@ -137,7 +141,8 @@ class RealArtistRepository(
|
||||||
getSongLoaderSortOrder()
|
getSongLoaderSortOrder()
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
return splitIntoArtists(albumRepository.splitIntoAlbums(songs))
|
val artists = splitIntoArtists(albumRepository.splitIntoAlbums(songs))
|
||||||
|
return sortArtists(artists)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -165,4 +170,18 @@ class RealArtistRepository(
|
||||||
return albums.groupBy { it.artistId }
|
return albums.groupBy { it.artistId }
|
||||||
.map { Artist(it.key, it.value) }
|
.map { Artist(it.key, it.value) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun sortArtists(artists: List<Artist>): List<Artist> {
|
||||||
|
return when (PreferenceUtil.artistSortOrder) {
|
||||||
|
SortOrder.ArtistSortOrder.ARTIST_A_Z -> {
|
||||||
|
val collator = Collator.getInstance()
|
||||||
|
artists.sortedWith{ a1, a2 -> collator.compare(a1.name, a2.name) }
|
||||||
|
}
|
||||||
|
SortOrder.ArtistSortOrder.ARTIST_Z_A -> {
|
||||||
|
val collator = Collator.getInstance()
|
||||||
|
artists.sortedWith{ a1, a2 -> collator.compare(a2.name, a1.name) }
|
||||||
|
}
|
||||||
|
else -> artists
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue