From ca1a1a2b009ae2cd60bc4ae54f3df449ef135184 Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 30 Sep 2020 21:30:48 +0200 Subject: [PATCH] fetch albums after clicking an artist --- .../musicplayer/fragments/ArtistsFragment.kt | 44 ++++++++++++++++++- .../musicplayer/models/Album.kt | 3 ++ .../musicplayer/models/Artist.kt | 2 +- 3 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/musicplayer/models/Album.kt diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/fragments/ArtistsFragment.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/fragments/ArtistsFragment.kt index e7c70358..67dc0421 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/fragments/ArtistsFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/fragments/ArtistsFragment.kt @@ -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) -> Unit) { ensureBackgroundThread { val artists = ArrayList() @@ -58,4 +65,39 @@ class ArtistsFragment(context: Context, attributeSet: AttributeSet) : MyViewPage } } } + + private fun getAlbums(activity: Activity, artistId: Int, callback: (artists: ArrayList) -> Unit) { + ensureBackgroundThread { + val albums = ArrayList() + 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) + } + } + } } diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/models/Album.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/models/Album.kt new file mode 100644 index 00000000..c94efb5b --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/models/Album.kt @@ -0,0 +1,3 @@ +package com.simplemobiletools.musicplayer.models + +data class Album(val id: Int, val artist: String, val title: String) diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/models/Artist.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/models/Artist.kt index 5a4c7c46..3b715f64 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/models/Artist.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/models/Artist.kt @@ -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)