Add Artist item
Add the Artist item and switch LibraryFragment over to it.
This commit is contained in:
@@ -10,7 +10,7 @@ import androidx.fragment.app.Fragment
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import org.oxycblt.auxio.R
|
||||
import org.oxycblt.auxio.databinding.FragmentLibraryBinding
|
||||
import org.oxycblt.auxio.recycler.adapters.AlbumAdapter
|
||||
import org.oxycblt.auxio.recycler.adapters.ArtistAdapter
|
||||
import org.oxycblt.auxio.recycler.applyDivider
|
||||
import org.oxycblt.auxio.recycler.viewholders.ClickListener
|
||||
|
||||
@@ -29,10 +29,10 @@ class LibraryFragment : Fragment() {
|
||||
inflater, R.layout.fragment_library, container, false
|
||||
)
|
||||
|
||||
binding.libraryRecycler.adapter = AlbumAdapter(
|
||||
libraryModel.albums.value!!,
|
||||
ClickListener { album ->
|
||||
Log.d(this::class.simpleName, album.title)
|
||||
binding.libraryRecycler.adapter = ArtistAdapter(
|
||||
libraryModel.artists.value!!,
|
||||
ClickListener { artist ->
|
||||
Log.d(this::class.simpleName, artist.name)
|
||||
}
|
||||
)
|
||||
binding.libraryRecycler.applyDivider()
|
||||
|
||||
@@ -21,6 +21,8 @@ import org.oxycblt.auxio.music.processing.MusicLoaderResponse
|
||||
|
||||
class LoadingFragment : Fragment(R.layout.fragment_loading) {
|
||||
|
||||
// TODO: Phase out LoadingFragment
|
||||
|
||||
private val loadingModel: LoadingViewModel by lazy {
|
||||
ViewModelProvider(
|
||||
this,
|
||||
@@ -108,6 +110,7 @@ class LoadingFragment : Fragment(R.layout.fragment_loading) {
|
||||
// Don't run this if the value is null, Which is what the value changes to after
|
||||
// this is run.
|
||||
repoResponse?.let { response ->
|
||||
binding.loadingBar.visibility = View.GONE
|
||||
|
||||
if (response == MusicLoaderResponse.DONE) {
|
||||
val inflater = TransitionInflater.from(requireContext())
|
||||
@@ -119,7 +122,6 @@ class LoadingFragment : Fragment(R.layout.fragment_loading) {
|
||||
} else {
|
||||
// If the response wasn't a success, then show the specific error message
|
||||
// depending on which error response was given, along with a retry button
|
||||
binding.loadingBar.visibility = View.GONE
|
||||
binding.errorText.visibility = View.VISIBLE
|
||||
binding.statusIcon.visibility = View.VISIBLE
|
||||
binding.retryButton.visibility = View.VISIBLE
|
||||
|
||||
@@ -9,6 +9,7 @@ import androidx.databinding.BindingAdapter
|
||||
import coil.load
|
||||
import org.oxycblt.auxio.R
|
||||
import org.oxycblt.auxio.music.models.Album
|
||||
import org.oxycblt.auxio.music.models.Artist
|
||||
import org.oxycblt.auxio.music.models.Song
|
||||
|
||||
private val ID3_GENRES = arrayOf(
|
||||
@@ -83,12 +84,32 @@ fun ImageView.getCoverArt(any: Any) {
|
||||
}
|
||||
}
|
||||
|
||||
fun Int.toSongCount(): Int {
|
||||
return if (this < 2) {
|
||||
R.string.label_single_song
|
||||
} else {
|
||||
R.string.format_multi_song_count
|
||||
}
|
||||
}
|
||||
|
||||
fun Int.toAlbumCount(): Int {
|
||||
return if (this < 2) {
|
||||
R.string.label_single_album
|
||||
} else {
|
||||
R.string.format_album_count
|
||||
}
|
||||
}
|
||||
|
||||
// Format the amount of songs in an album
|
||||
@BindingAdapter("songCount")
|
||||
fun TextView.getAlbumSongs(album: Album) {
|
||||
text = if (album.numSongs < 2) {
|
||||
context.getString(R.string.label_single_song)
|
||||
} else {
|
||||
context.getString(R.string.format_multi_song_count, album.numSongs.toString())
|
||||
}
|
||||
text = context.getString(album.numSongs.toSongCount(), album.numSongs)
|
||||
}
|
||||
|
||||
@BindingAdapter("albumSongCount")
|
||||
fun TextView.getSongAlbumCount(artist: Artist) {
|
||||
val albums = context.getString(artist.numAlbums.toAlbumCount(), artist.numAlbums)
|
||||
val songs = context.getString(artist.numSongs.toSongCount(), artist.numSongs)
|
||||
|
||||
text = context.getString(R.string.format_combined_song_album, albums, songs)
|
||||
}
|
||||
|
||||
@@ -8,4 +8,5 @@ data class Artist(
|
||||
) {
|
||||
val albums = mutableListOf<Album>()
|
||||
var numAlbums = 0
|
||||
var numSongs = 0
|
||||
}
|
||||
|
||||
@@ -80,6 +80,9 @@ class MusicSorter(
|
||||
}
|
||||
|
||||
artist.numAlbums = artist.albums.size
|
||||
artist.albums.forEach { album ->
|
||||
artist.numSongs += album.numSongs
|
||||
}
|
||||
|
||||
unknownAlbums.removeAll(artistAlbums)
|
||||
}
|
||||
@@ -95,7 +98,10 @@ class MusicSorter(
|
||||
unknownArtist.albums.add(album)
|
||||
}
|
||||
|
||||
unknownArtist.numAlbums = albums.size
|
||||
unknownArtist.numAlbums = unknownArtist.albums.size
|
||||
unknownArtist.albums.forEach { album ->
|
||||
unknownArtist.numSongs += album.numSongs
|
||||
}
|
||||
|
||||
artists.add(unknownArtist)
|
||||
|
||||
|
||||
@@ -29,10 +29,6 @@ class AlbumAdapter(
|
||||
override fun onBindViewHolder(holder: AlbumViewHolder, position: Int) {
|
||||
val album = data[position]
|
||||
|
||||
holder.itemView.setOnClickListener {
|
||||
listener.onClick(album)
|
||||
}
|
||||
|
||||
holder.bind(album)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
package org.oxycblt.auxio.recycler.adapters
|
||||
|
||||
import android.view.LayoutInflater
|
||||
import android.view.ViewGroup
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import org.oxycblt.auxio.databinding.ArtistItemBinding
|
||||
import org.oxycblt.auxio.music.models.Artist
|
||||
import org.oxycblt.auxio.recycler.viewholders.ArtistViewHolder
|
||||
import org.oxycblt.auxio.recycler.viewholders.ClickListener
|
||||
|
||||
class ArtistAdapter(
|
||||
private val data: List<Artist>,
|
||||
private val listener: ClickListener<Artist>
|
||||
) : RecyclerView.Adapter<ArtistViewHolder>() {
|
||||
|
||||
override fun getItemCount(): Int = data.size
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ArtistViewHolder {
|
||||
val binding = ArtistItemBinding.inflate(LayoutInflater.from(parent.context))
|
||||
|
||||
// Force the item to *actually* be the screen width so ellipsizing can work.
|
||||
binding.root.layoutParams = RecyclerView.LayoutParams(
|
||||
RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT
|
||||
)
|
||||
|
||||
return ArtistViewHolder(binding)
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: ArtistViewHolder, position: Int) {
|
||||
val album = data[position]
|
||||
|
||||
holder.bind(album)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package org.oxycblt.auxio.recycler.viewholders
|
||||
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import org.oxycblt.auxio.databinding.ArtistItemBinding
|
||||
import org.oxycblt.auxio.music.models.Artist
|
||||
|
||||
// Generic ViewHolder for an album
|
||||
class ArtistViewHolder(
|
||||
private val binding: ArtistItemBinding
|
||||
) : RecyclerView.ViewHolder(binding.root) {
|
||||
|
||||
// Bind the view w/new data
|
||||
fun bind(artist: Artist) {
|
||||
binding.artist = artist
|
||||
|
||||
binding.artistName.requestLayout()
|
||||
|
||||
binding.executePendingBindings()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user