detail: clean up adapters

Clean up the adapter implementations by removing redundant code.
This commit is contained in:
OxygenCobalt 2022-06-02 19:59:51 -06:00
parent 490c291a14
commit a396101bd0
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
4 changed files with 11 additions and 40 deletions

View file

@ -81,9 +81,7 @@ class AlbumDetailAdapter(listener: Listener) :
}
}
override fun onHighlightViewHolder(viewHolder: Highlightable, item: Item) {
viewHolder.setHighlighted(item.id == currentSong?.id)
}
override fun shouldHighlightViewHolder(item: Item) = item.id == currentSong?.id
/** Update the [song] that this adapter should highlight */
fun highlightSong(song: Song?) {
@ -189,7 +187,7 @@ class DiscHeaderViewHolder(private val binding: ItemDiscHeaderBinding) :
}
private class AlbumSongViewHolder private constructor(private val binding: ItemAlbumSongBinding) :
BindingViewHolder<Song, MenuItemListener>(binding.root), Highlightable {
BindingViewHolder<Song, MenuItemListener>(binding.root) {
override fun bind(item: Song, listener: MenuItemListener) {
// Hide the track number view if the song does not have a track.
if (item.track != null) {
@ -227,10 +225,6 @@ private class AlbumSongViewHolder private constructor(private val binding: ItemA
}
}
override fun setHighlighted(isHighlighted: Boolean) {
binding.root.isActivated = isHighlighted
}
companion object {
val CREATOR =
object : Creator<AlbumSongViewHolder> {

View file

@ -85,11 +85,9 @@ class ArtistDetailAdapter(listener: Listener) :
}
}
override fun onHighlightViewHolder(viewHolder: Highlightable, item: Item) {
viewHolder.setHighlighted(
(item is Album && item.id == currentAlbum?.id) ||
(item is Song && item.id == currentSong?.id))
}
override fun shouldHighlightViewHolder(item: Item) =
(item is Album && item.id == currentAlbum?.id) ||
(item is Song && item.id == currentSong?.id)
/** Update the current [album] that this adapter should highlight */
fun highlightAlbum(album: Album?) {
@ -169,7 +167,7 @@ private class ArtistDetailViewHolder private constructor(private val binding: It
private class ArtistAlbumViewHolder
private constructor(
private val binding: ItemParentBinding,
) : BindingViewHolder<Album, MenuItemListener>(binding.root), Highlightable {
) : BindingViewHolder<Album, MenuItemListener>(binding.root) {
override fun bind(item: Album, listener: MenuItemListener) {
binding.parentImage.bind(item)
binding.parentName.textSafe = item.resolveName(binding.context)
@ -189,10 +187,6 @@ private constructor(
}
}
override fun setHighlighted(isHighlighted: Boolean) {
binding.parentName.isActivated = isHighlighted
}
companion object {
val CREATOR =
object : Creator<ArtistAlbumViewHolder> {
@ -214,7 +208,7 @@ private constructor(
private class ArtistSongViewHolder
private constructor(
private val binding: ItemSongBinding,
) : BindingViewHolder<Song, MenuItemListener>(binding.root), Highlightable {
) : BindingViewHolder<Song, MenuItemListener>(binding.root) {
override fun bind(item: Song, listener: MenuItemListener) {
binding.songAlbumCover.bind(item)
binding.songName.textSafe = item.resolveName(binding.context)
@ -229,10 +223,6 @@ private constructor(
}
}
override fun setHighlighted(isHighlighted: Boolean) {
binding.root.isActivated = isHighlighted
}
companion object {
val CREATOR =
object : Creator<ArtistSongViewHolder> {

View file

@ -41,7 +41,7 @@ abstract class DetailAdapter<L : DetailAdapter.Listener>(
listener: L,
diffCallback: DiffUtil.ItemCallback<Item>
) : MultiAdapter<L>(listener) {
abstract fun onHighlightViewHolder(viewHolder: Highlightable, item: Item)
abstract fun shouldHighlightViewHolder(item: Item): Boolean
protected inline fun <reified T : Item> highlightItem(newItem: T?): Int? {
if (newItem == null) {
@ -84,9 +84,7 @@ abstract class DetailAdapter<L : DetailAdapter.Listener>(
}
}
if (viewHolder is Highlightable) {
onHighlightViewHolder(viewHolder, item)
}
viewHolder.itemView.isActivated = shouldHighlightViewHolder(item)
}
companion object {
@ -147,8 +145,3 @@ class SortHeaderViewHolder(private val binding: ItemSortHeaderBinding) :
}
}
}
/** Interface that allows the highlighting of certain ViewHolders */
interface Highlightable {
fun setHighlighted(isHighlighted: Boolean)
}

View file

@ -77,9 +77,7 @@ class GenreDetailAdapter(listener: Listener) :
}
}
override fun onHighlightViewHolder(viewHolder: Highlightable, item: Item) {
viewHolder.setHighlighted(item.id == currentSong?.id)
}
override fun shouldHighlightViewHolder(item: Item) = item.id == currentSong?.id
/** Update the [song] that this adapter should highlight */
fun highlightSong(song: Song?) {
@ -138,7 +136,7 @@ private class GenreDetailViewHolder private constructor(private val binding: Ite
}
class GenreSongViewHolder private constructor(private val binding: ItemSongBinding) :
BindingViewHolder<Song, MenuItemListener>(binding.root), Highlightable {
BindingViewHolder<Song, MenuItemListener>(binding.root) {
override fun bind(item: Song, listener: MenuItemListener) {
binding.songAlbumCover.bind(item)
binding.songName.textSafe = item.resolveName(binding.context)
@ -152,10 +150,6 @@ class GenreSongViewHolder private constructor(private val binding: ItemSongBindi
}
}
override fun setHighlighted(isHighlighted: Boolean) {
binding.songName.isActivated = isHighlighted
}
companion object {
val CREATOR =
object : Creator<GenreSongViewHolder> {