From d5d77ae45dd51a33ed616146ea5a71fdb5a4b8eb Mon Sep 17 00:00:00 2001 From: Suhas Dissanayake Date: Sat, 28 Oct 2023 16:31:43 +0530 Subject: [PATCH] feat: add album and track numbers for artists --- .../suhasdissa/vibeyou/backend/data/Artist.kt | 4 +++- .../repository/LocalMusicRepository.kt | 24 ++++++++++++------- .../vibeyou/ui/components/ArtistCard.kt | 15 ++++++++++++ 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/app/suhasdissa/vibeyou/backend/data/Artist.kt b/app/src/main/java/app/suhasdissa/vibeyou/backend/data/Artist.kt index 500aef8e..6e4e9571 100644 --- a/app/src/main/java/app/suhasdissa/vibeyou/backend/data/Artist.kt +++ b/app/src/main/java/app/suhasdissa/vibeyou/backend/data/Artist.kt @@ -6,5 +6,7 @@ data class Artist( val id: String, val artistsText: String, val thumbnailUri: Uri? = null, - val description: String? = null + val description: String? = null, + val numberOfTracks: Int? = null, + val numberOfAlbums: Int? = null ) diff --git a/app/src/main/java/app/suhasdissa/vibeyou/backend/repository/LocalMusicRepository.kt b/app/src/main/java/app/suhasdissa/vibeyou/backend/repository/LocalMusicRepository.kt index c790e7c2..49c7c49a 100644 --- a/app/src/main/java/app/suhasdissa/vibeyou/backend/repository/LocalMusicRepository.kt +++ b/app/src/main/java/app/suhasdissa/vibeyou/backend/repository/LocalMusicRepository.kt @@ -7,7 +7,6 @@ import android.net.Uri import android.os.Build import android.provider.MediaStore import android.text.format.DateUtils -import android.util.Log import androidx.core.net.toUri import app.suhasdissa.vibeyou.backend.data.Album import app.suhasdissa.vibeyou.backend.data.Artist @@ -47,7 +46,7 @@ class LocalMusicRepository( MediaStore.Audio.Media.ALBUM_ID, MediaStore.Audio.Media.ARTIST_ID, MediaStore.Audio.Media.DATE_MODIFIED, - MediaStore.Audio.Media.DATE_ADDED, + MediaStore.Audio.Media.DATE_ADDED ) val sortOrder = "${MediaStore.Audio.Media.TITLE} ASC" @@ -69,7 +68,9 @@ class LocalMusicRepository( val artistColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST) val albumColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID) val artistIdColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST_ID) - val creationDateColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATE_MODIFIED) + val creationDateColumn = cursor.getColumnIndexOrThrow( + MediaStore.Audio.Media.DATE_MODIFIED + ) val dateAddedColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATE_ADDED) while (cursor.moveToNext()) { @@ -180,7 +181,7 @@ class LocalMusicRepository( val artists = mutableListOf() val collection = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - MediaStore.Audio.Albums.getContentUri( + MediaStore.Audio.Artists.getContentUri( MediaStore.VOLUME_EXTERNAL ) } else { @@ -190,10 +191,11 @@ class LocalMusicRepository( val projection = arrayOf( MediaStore.Audio.Artists._ID, MediaStore.Audio.Artists.ARTIST, - MediaStore.Audio.Artists.Albums.ALBUM_ID + MediaStore.Audio.Artists.NUMBER_OF_ALBUMS, + MediaStore.Audio.Artists.NUMBER_OF_TRACKS ) - val sortOrder = "${MediaStore.Audio.Albums.ALBUM} ASC" + val sortOrder = "${MediaStore.Audio.Artists.ARTIST} ASC" val query = contentResolver.query( collection, @@ -205,8 +207,10 @@ class LocalMusicRepository( query?.use { cursor -> val idColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Artists._ID) val artistColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Artists.ARTIST) - val albumIdColumn = - cursor.getColumnIndexOrThrow(MediaStore.Audio.Artists.Albums.ALBUM_ID) + val noOfAlbums = + cursor.getColumnIndexOrThrow(MediaStore.Audio.ArtistColumns.NUMBER_OF_ALBUMS) + val noOfTracks = + cursor.getColumnIndexOrThrow(MediaStore.Audio.ArtistColumns.NUMBER_OF_TRACKS) while (cursor.moveToNext()) { val id = cursor.getLong(idColumn) @@ -215,7 +219,9 @@ class LocalMusicRepository( Artist( id = id.toString(), artistsText = cursor.getString(artistColumn), - thumbnailUri = getAlbumArt(cursor.getLong(albumIdColumn)) + thumbnailUri = null, + numberOfAlbums = cursor.getInt(noOfAlbums), + numberOfTracks = cursor.getInt(noOfTracks) ) ) } diff --git a/app/src/main/java/app/suhasdissa/vibeyou/ui/components/ArtistCard.kt b/app/src/main/java/app/suhasdissa/vibeyou/ui/components/ArtistCard.kt index 31ae0ec2..517dc623 100644 --- a/app/src/main/java/app/suhasdissa/vibeyou/ui/components/ArtistCard.kt +++ b/app/src/main/java/app/suhasdissa/vibeyou/ui/components/ArtistCard.kt @@ -3,6 +3,7 @@ package app.suhasdissa.vibeyou.ui.components import android.view.SoundEffectConstants import androidx.compose.foundation.ExperimentalFoundationApi import androidx.compose.foundation.combinedClickable +import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.aspectRatio @@ -74,6 +75,20 @@ fun ArtistCard( maxLines = 1, overflow = TextOverflow.Ellipsis ) + Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) { + artist.numberOfAlbums?.let { + Text( + text = "${stringResource(id = R.string.albums)} $it", + style = MaterialTheme.typography.bodyMedium + ) + } + artist.numberOfTracks?.let { + Text( + text = "${stringResource(id = R.string.songs)} $it", + style = MaterialTheme.typography.bodyMedium + ) + } + } } } }