145 lines
5.1 KiB
Kotlin
145 lines
5.1 KiB
Kotlin
/*
|
|
* Copyright (c) 2019 Hemanth Savarala.
|
|
*
|
|
* Licensed under the GNU General Public License v3
|
|
*
|
|
* This is free software: you can redistribute it and/or modify it under
|
|
* the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation either version 3 of the License, or (at your option) any later version.
|
|
*
|
|
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
* See the GNU General Public License for more details.
|
|
*/
|
|
|
|
package code.name.monkey.retromusic.repository
|
|
|
|
import code.name.monkey.retromusic.model.Album
|
|
import code.name.monkey.retromusic.model.Artist
|
|
import code.name.monkey.retromusic.util.PreferenceUtil
|
|
import software.lavender.music.query.LongFilter
|
|
import software.lavender.music.query.StringFilter
|
|
|
|
interface ArtistRepository {
|
|
fun artists(): List<Artist>
|
|
|
|
fun albumArtists(): List<Artist>
|
|
|
|
fun albumArtists(query: String): List<Artist>
|
|
|
|
fun artists(query: String): List<Artist>
|
|
|
|
fun artist(artistId: Long): Artist
|
|
|
|
fun albumArtist(artistName: String): Artist
|
|
}
|
|
|
|
class RealArtistRepository(
|
|
private val songRepository: RealSongRepository,
|
|
private val albumRepository: RealAlbumRepository
|
|
) : ArtistRepository {
|
|
|
|
private fun getSongLoaderSortOrder(): String {
|
|
return PreferenceUtil.artistSortOrder + ", " +
|
|
PreferenceUtil.artistAlbumSortOrder + ", " +
|
|
PreferenceUtil.artistSongSortOrder
|
|
}
|
|
|
|
override fun artist(artistId: Long): Artist {
|
|
if (artistId == Artist.VARIOUS_ARTISTS_ID) {
|
|
// Get Various Artists
|
|
// TODO: song loader sort order
|
|
// TODO: why is the filtering done AFTER the query here?
|
|
val songs = songRepository.songs(
|
|
SongQuery()
|
|
)
|
|
val albums = albumRepository.splitIntoAlbums(songs)
|
|
.filter { it.albumArtist == Artist.VARIOUS_ARTISTS_DISPLAY_NAME }
|
|
return Artist(Artist.VARIOUS_ARTISTS_ID, albums)
|
|
}
|
|
|
|
// TODO: song loader sort order
|
|
val songs = songRepository.songs(
|
|
SongQuery(artistId = LongFilter(eq = artistId))
|
|
)
|
|
return Artist(artistId, albumRepository.splitIntoAlbums(songs))
|
|
}
|
|
|
|
override fun albumArtist(artistName: String): Artist {
|
|
if (artistName == Artist.VARIOUS_ARTISTS_DISPLAY_NAME) {
|
|
// Get Various Artists
|
|
// TODO: song loader sort order
|
|
// TODO: why is the filtering done AFTER the query here?
|
|
val songs = songRepository.songs(
|
|
SongQuery()
|
|
)
|
|
val albums = albumRepository.splitIntoAlbums(songs)
|
|
.filter { it.albumArtist == Artist.VARIOUS_ARTISTS_DISPLAY_NAME }
|
|
return Artist(Artist.VARIOUS_ARTISTS_ID, albums, true)
|
|
}
|
|
|
|
// TODO: song loader sort order
|
|
val songs = songRepository.songs(
|
|
SongQuery(albumArtist = StringFilter(eq = artistName))
|
|
)
|
|
return Artist(artistName, albumRepository.splitIntoAlbums(songs), true)
|
|
}
|
|
|
|
override fun artists(): List<Artist> {
|
|
// TODO: song loader sort order
|
|
val songs = songRepository.songs(
|
|
SongQuery()
|
|
)
|
|
return splitIntoArtists(albumRepository.splitIntoAlbums(songs))
|
|
}
|
|
|
|
override fun albumArtists(): List<Artist> {
|
|
// TODO: sort "lower($ALBUM_ARTIST)" + if (PreferenceUtil.artistSortOrder == SortOrder.ArtistSortOrder.ARTIST_A_Z) "" else " DESC"
|
|
val songs = songRepository.songs(
|
|
SongQuery()
|
|
)
|
|
return splitIntoAlbumArtists(albumRepository.splitIntoAlbums(songs))
|
|
}
|
|
|
|
override fun albumArtists(query: String): List<Artist> {
|
|
// TODO: song loader sort order
|
|
val songs = songRepository.songs(
|
|
SongQuery(albumArtist = StringFilter(like = "%$query%"))
|
|
)
|
|
return splitIntoAlbumArtists(albumRepository.splitIntoAlbums(songs))
|
|
}
|
|
|
|
override fun artists(query: String): List<Artist> {
|
|
// TODO: song loader sort oder
|
|
val songs = songRepository.songs(
|
|
SongQuery(artistName = StringFilter(like = "%$query%"))
|
|
)
|
|
return splitIntoArtists(albumRepository.splitIntoAlbums(songs))
|
|
}
|
|
|
|
|
|
private fun splitIntoAlbumArtists(albums: List<Album>): List<Artist> {
|
|
return albums.groupBy { it.albumArtist }
|
|
.filter {
|
|
!it.key.isNullOrEmpty()
|
|
}
|
|
.map {
|
|
val currentAlbums = it.value
|
|
if (currentAlbums.isNotEmpty()) {
|
|
if (currentAlbums[0].albumArtist == Artist.VARIOUS_ARTISTS_DISPLAY_NAME) {
|
|
Artist(Artist.VARIOUS_ARTISTS_ID, currentAlbums, true)
|
|
} else {
|
|
Artist(currentAlbums[0].artistId, currentAlbums, true)
|
|
}
|
|
} else {
|
|
Artist.empty
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
fun splitIntoArtists(albums: List<Album>): List<Artist> {
|
|
return albums.groupBy { it.artistId }
|
|
.map { Artist(it.key, it.value) }
|
|
}
|
|
} |