PlayerAndroid/app/src/main/java/code/name/monkey/retromusic/loaders/ArtistLoader.kt

89 lines
3.0 KiB
Kotlin
Raw Normal View History

2019-03-03 09:20:15 +00:00
/*
* 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.
*/
2018-11-30 01:06:16 +00:00
package code.name.monkey.retromusic.loaders
import android.content.Context
import android.provider.MediaStore.Audio.AudioColumns
import code.name.monkey.retromusic.model.Album
import code.name.monkey.retromusic.model.Artist
import code.name.monkey.retromusic.util.PreferenceUtil
2018-11-30 01:06:16 +00:00
object ArtistLoader {
private fun getSongLoaderSortOrder(context: Context): String {
return PreferenceUtil.getInstance(context).artistSortOrder + ", " + PreferenceUtil.getInstance(
context
).artistAlbumSortOrder + ", " + PreferenceUtil.getInstance(
2020-02-19 07:08:12 +00:00
context
).albumSongSortOrder
2018-11-30 01:06:16 +00:00
}
fun getAllArtists(context: Context): ArrayList<Artist> {
2020-02-19 07:08:12 +00:00
val songs = SongLoader.getSongs(
SongLoader.makeSongCursor(
context,
null, null,
2020-02-19 07:08:12 +00:00
getSongLoaderSortOrder(context)
)
)
return splitIntoArtists(AlbumLoader.splitIntoAlbums(songs))
2018-11-30 01:06:16 +00:00
}
fun getArtists(context: Context, query: String): ArrayList<Artist> {
2020-02-19 07:08:12 +00:00
val songs = SongLoader.getSongs(
SongLoader.makeSongCursor(
context,
AudioColumns.ARTIST + " LIKE ?",
arrayOf("%$query%"),
2020-02-19 07:08:12 +00:00
getSongLoaderSortOrder(context)
)
)
return splitIntoArtists(AlbumLoader.splitIntoAlbums(songs))
}
2018-11-30 01:06:16 +00:00
fun splitIntoArtists(albums: ArrayList<Album>?): ArrayList<Artist> {
val artists = ArrayList<Artist>()
if (albums != null) {
for (album in albums) {
getOrCreateArtist(artists, album.artistId).albums!!.add(album)
}
}
return artists
}
private fun getOrCreateArtist(artists: ArrayList<Artist>, artistId: Int): Artist {
for (artist in artists) {
if (artist.albums!!.isNotEmpty() && artist.albums[0].songs!!.isNotEmpty() && artist.albums[0].songs!![0].artistId == artistId) {
2018-11-30 01:06:16 +00:00
return artist
}
}
val album = Artist()
artists.add(album)
return album
}
2020-02-19 07:08:12 +00:00
@JvmStatic
fun getArtist(context: Context, artistId: Int): Artist {
2020-02-19 07:08:12 +00:00
val songs = SongLoader.getSongs(
SongLoader.makeSongCursor(
context,
AudioColumns.ARTIST_ID + "=?",
arrayOf(artistId.toString()),
2020-02-19 07:08:12 +00:00
getSongLoaderSortOrder(context)
)
)
return Artist(AlbumLoader.splitIntoAlbums(songs))
}
2018-11-30 01:06:16 +00:00
}