Update album items/details

Update the ArtistDetailFragment album item to show the year instead of song count. Update AlbumDetailFragment to show year, song count, and total duration.
This commit is contained in:
OxygenCobalt
2020-09-26 13:57:15 -06:00
parent f754a82ba7
commit 5191220bc0
13 changed files with 123 additions and 191 deletions
@@ -4,7 +4,7 @@ import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import org.oxycblt.auxio.databinding.ItemAlbumBinding
import org.oxycblt.auxio.databinding.ItemArtistAlbumBinding
import org.oxycblt.auxio.music.models.Album
import org.oxycblt.auxio.recycler.AlbumDiffCallback
import org.oxycblt.auxio.recycler.ClickListener
@@ -15,7 +15,7 @@ class DetailAlbumAdapter(
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(
ItemAlbumBinding.inflate(LayoutInflater.from(parent.context))
ItemArtistAlbumBinding.inflate(LayoutInflater.from(parent.context))
)
}
@@ -25,7 +25,7 @@ class DetailAlbumAdapter(
// Generic ViewHolder for an album
inner class ViewHolder(
private val binding: ItemAlbumBinding
private val binding: ItemArtistAlbumBinding
) : RecyclerView.ViewHolder(binding.root) {
init {
@@ -1,8 +1,10 @@
package org.oxycblt.auxio.music
import android.content.ContentUris
import android.content.Context
import android.net.Uri
import android.provider.MediaStore
import android.text.format.DateUtils
import android.widget.TextView
import androidx.databinding.BindingAdapter
import org.oxycblt.auxio.R
@@ -68,15 +70,17 @@ fun Long.toAlbumArtURI(): Uri {
)
}
// Cut off excess zeros from a duration string
fun String.removeDurationZeroes(): String {
val split = this.chunked(1).toMutableList()
// Convert a string into its duration
fun Long.toDuration(): String {
val durationString = DateUtils.formatElapsedTime(this)
val durationSplit = durationString.chunked(1).toMutableList()
// Iterate through the string and remove the first zero found
// If anything else is found, exit the loop.
for (i in 0 until length) {
if (this[i] == '0') {
split.removeAt(i)
for (i in 0 until durationSplit.size) {
if (durationSplit[i] == "0") {
durationSplit.removeAt(i)
break
} else {
@@ -84,19 +88,23 @@ fun String.removeDurationZeroes(): String {
}
}
return split.joinToString("")
return durationSplit.joinToString("")
}
// --- BINDING ADAPTERS ---
// Format the amount of songs in an album
@BindingAdapter("songCount")
fun TextView.getAlbumSongs(album: Album) {
text = context.resources.getQuantityString(
fun getAlbumSongCount(album: Album, context: Context): String {
return context.resources.getQuantityString(
R.plurals.format_song_count, album.numSongs, album.numSongs
)
}
// Format the amount of songs in an album
@BindingAdapter("songCount")
fun TextView.bindAlbumSongs(album: Album) {
text = getAlbumSongCount(album, context)
}
@BindingAdapter("artistCounts")
fun TextView.bindArtistCounts(artist: Artist) {
val albums = context.resources.getQuantityString(
@@ -112,7 +120,7 @@ fun TextView.bindArtistCounts(artist: Artist) {
// Get the artist genre.
// TODO: Add option to list all genres
@BindingAdapter("artistGenre")
fun TextView.getArtistGenre(artist: Artist) {
fun TextView.bindArtistGenre(artist: Artist) {
// If there are multiple genres, then pick the most "Prominent" one,
// Otherwise just pick the first one
if (artist.genres.keys.size > 1) {
@@ -123,3 +131,14 @@ fun TextView.getArtistGenre(artist: Artist) {
text = artist.genres.keys.first()
}
}
// Get a bunch of miscellaneous album information [Year, Songs, Duration] and combine them
@BindingAdapter("albumDetails")
fun TextView.bindAlbumDetails(album: Album) {
text = context.getString(
R.string.format_double_info,
album.year.toString(),
getAlbumSongCount(album, context),
album.totalDuration
)
}
@@ -1,6 +1,7 @@
package org.oxycblt.auxio.music.models
import android.net.Uri
import org.oxycblt.auxio.music.toDuration
// Abstraction for Song
data class Album(
@@ -14,4 +15,11 @@ data class Album(
val songs = mutableListOf<Song>()
val numSongs: Int get() = songs.size
val totalDuration: String get() {
var seconds: Long = 0
songs.forEach {
seconds += it.seconds
}
return seconds.toDuration()
}
}
@@ -1,7 +1,6 @@
package org.oxycblt.auxio.music.models
import android.text.format.DateUtils
import org.oxycblt.auxio.music.removeDurationZeroes
import org.oxycblt.auxio.music.toDuration
// Class containing all relevant values for a song.
data class Song(
@@ -14,5 +13,5 @@ data class Song(
lateinit var album: Album
val seconds = duration / 1000
val formattedDuration: String = DateUtils.formatElapsedTime(seconds).removeDurationZeroes()
val formattedDuration: String = seconds.toDuration()
}