fetch albums after clicking an artist
This commit is contained in:
parent
75df0fc4b9
commit
ca1a1a2b00
3 changed files with 47 additions and 2 deletions
|
@ -10,6 +10,7 @@ import com.simplemobiletools.commons.extensions.showErrorToast
|
|||
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
|
||||
import com.simplemobiletools.musicplayer.activities.SimpleActivity
|
||||
import com.simplemobiletools.musicplayer.adapters.ArtistsAdapter
|
||||
import com.simplemobiletools.musicplayer.models.Album
|
||||
import com.simplemobiletools.musicplayer.models.Artist
|
||||
import kotlinx.android.synthetic.main.fragment_artists.view.*
|
||||
|
||||
|
@ -17,13 +18,19 @@ class ArtistsFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
|
|||
override fun setupFragment(activity: SimpleActivity) {
|
||||
getArtists(activity) { artists ->
|
||||
ArtistsAdapter(activity, artists, artists_list) {
|
||||
|
||||
openArtist(activity, it as Artist)
|
||||
}.apply {
|
||||
artists_list.adapter = this
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun openArtist(activity: Activity, artist: Artist) {
|
||||
getAlbums(activity, artist.id) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private fun getArtists(activity: Activity, callback: (artists: ArrayList<Artist>) -> Unit) {
|
||||
ensureBackgroundThread {
|
||||
val artists = ArrayList<Artist>()
|
||||
|
@ -58,4 +65,39 @@ class ArtistsFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun getAlbums(activity: Activity, artistId: Int, callback: (artists: ArrayList<Album>) -> Unit) {
|
||||
ensureBackgroundThread {
|
||||
val albums = ArrayList<Album>()
|
||||
val uri = Audio.Albums.EXTERNAL_CONTENT_URI
|
||||
val projection = arrayOf(
|
||||
Audio.Albums.ALBUM_ID,
|
||||
Audio.Albums.ARTIST,
|
||||
Audio.Albums.ALBUM)
|
||||
|
||||
val selection = "${Audio.Albums.ARTIST_ID} = ?"
|
||||
val selectionArgs = arrayOf(artistId.toString())
|
||||
|
||||
try {
|
||||
val cursor = activity.contentResolver.query(uri, projection, selection, selectionArgs, null)
|
||||
cursor?.use {
|
||||
if (cursor.moveToFirst()) {
|
||||
do {
|
||||
val id = cursor.getIntValue(Audio.Albums.ALBUM_ID)
|
||||
val artist = cursor.getStringValue(Audio.Albums.ARTIST)
|
||||
val title = cursor.getStringValue(Audio.Albums.ALBUM)
|
||||
val album = Album(id, artist, title)
|
||||
albums.add(album)
|
||||
} while (cursor.moveToNext())
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
activity.showErrorToast(e)
|
||||
}
|
||||
|
||||
activity.runOnUiThread {
|
||||
callback(albums)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
package com.simplemobiletools.musicplayer.models
|
||||
|
||||
data class Album(val id: Int, val artist: String, val title: String)
|
|
@ -1,3 +1,3 @@
|
|||
package com.simplemobiletools.musicplayer.models
|
||||
|
||||
data class Artist(var id: Int, val title: String, val albumCnt: Int, val trackCnt: Int)
|
||||
data class Artist(val id: Int, val title: String, val albumCnt: Int, val trackCnt: Int)
|
||||
|
|
Loading…
Reference in a new issue