From d8c0037b1014dcf7b373af2411dc37985ce3014f Mon Sep 17 00:00:00 2001 From: OxygenCobalt Date: Mon, 6 Sep 2021 19:20:57 -0600 Subject: [PATCH] detail: fix highlighting issue Fix an issue where in certain cases a playing item would not be highlighted if it was being re-played. This was solved my simply adding a check for if the new item was equal and ignoring it if it is. --- .../detail/adapters/AlbumDetailAdapter.kt | 18 +++++++++--------- .../detail/adapters/ArtistDetailAdapter.kt | 15 ++++++++------- .../detail/adapters/GenreDetailAdapter.kt | 18 +++++++++--------- .../java/org/oxycblt/auxio/util/ViewUtil.kt | 4 ++-- 4 files changed, 28 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/org/oxycblt/auxio/detail/adapters/AlbumDetailAdapter.kt b/app/src/main/java/org/oxycblt/auxio/detail/adapters/AlbumDetailAdapter.kt index e9fb1c9dd..d6b57aad5 100644 --- a/app/src/main/java/org/oxycblt/auxio/detail/adapters/AlbumDetailAdapter.kt +++ b/app/src/main/java/org/oxycblt/auxio/detail/adapters/AlbumDetailAdapter.kt @@ -47,7 +47,7 @@ class AlbumDetailAdapter( private val doOnLongClick: (view: View, data: Song) -> Unit ) : ListAdapter(DiffCallback()) { private var currentSong: Song? = null - private var lastHolder: Highlightable? = null + private var currentHolder: Highlightable? = null override fun getItemViewType(position: Int): Int { return when (getItem(position)) { @@ -83,8 +83,8 @@ class AlbumDetailAdapter( if (currentSong != null && position > 0) { if (item.id == currentSong?.id) { // Reset the last ViewHolder before assigning the new, correct one to be highlighted - lastHolder?.setHighlighted(false) - lastHolder = (holder as Highlightable) + currentHolder?.setHighlighted(false) + currentHolder = (holder as Highlightable) holder.setHighlighted(true) } else { (holder as Highlightable).setHighlighted(false) @@ -97,11 +97,11 @@ class AlbumDetailAdapter( * @param recycler The recyclerview the highlighting should act on. */ fun highlightSong(song: Song?, recycler: RecyclerView) { - // Clear out the last ViewHolder as a song update usually signifies that this current - // ViewHolder is likely invalid. - lastHolder?.setHighlighted(false) - lastHolder = null + if (song == currentSong) return // Already highlighting this viewholder + // Clear the current viewholder since it's invalid + currentHolder?.setHighlighted(false) + currentHolder = null currentSong = song if (song != null) { @@ -115,9 +115,9 @@ class AlbumDetailAdapter( // it does become visible. recycler.layoutManager?.findViewByPosition(pos)?.let { child -> recycler.getChildViewHolder(child)?.let { - lastHolder = it as Highlightable + currentHolder = it as Highlightable - lastHolder?.setHighlighted(true) + currentHolder?.setHighlighted(true) } } } diff --git a/app/src/main/java/org/oxycblt/auxio/detail/adapters/ArtistDetailAdapter.kt b/app/src/main/java/org/oxycblt/auxio/detail/adapters/ArtistDetailAdapter.kt index 0906d8a5c..2d441b587 100644 --- a/app/src/main/java/org/oxycblt/auxio/detail/adapters/ArtistDetailAdapter.kt +++ b/app/src/main/java/org/oxycblt/auxio/detail/adapters/ArtistDetailAdapter.kt @@ -98,7 +98,8 @@ class ArtistDetailAdapter( is Header -> (holder as ArtistSongHeaderViewHolder).bind(item) is Song -> (holder as ArtistSongViewHolder).bind(item) - else -> {} + else -> { + } } if (holder is Highlightable) { @@ -122,11 +123,11 @@ class ArtistDetailAdapter( * @param recycler The recyclerview the highlighting should act on. */ fun highlightAlbum(album: Album?, recycler: RecyclerView) { - // Clear out the last ViewHolder as a song update usually signifies that this current - // ViewHolder is likely invalid. + if (album == currentAlbum) return // Already highlighting this viewholder + + // Album is no longer valid, clear out this ViewHolder. currentAlbumHolder?.setHighlighted(false) currentAlbumHolder = null - currentAlbum = album if (album != null) { @@ -151,11 +152,11 @@ class ArtistDetailAdapter( * @param recycler The recyclerview the highlighting should act on. */ fun highlightSong(song: Song?, recycler: RecyclerView) { - // Clear out the last ViewHolder as a song update usually signifies that this current - // ViewHolder is likely invalid. + if (song == currentSong) return // Already highlighting this viewholder + + // Clear the current viewholder since it's invalid currentSongHolder?.setHighlighted(false) currentSongHolder = null - currentSong = song if (song != null) { diff --git a/app/src/main/java/org/oxycblt/auxio/detail/adapters/GenreDetailAdapter.kt b/app/src/main/java/org/oxycblt/auxio/detail/adapters/GenreDetailAdapter.kt index 831256bd2..ebdea2cf5 100644 --- a/app/src/main/java/org/oxycblt/auxio/detail/adapters/GenreDetailAdapter.kt +++ b/app/src/main/java/org/oxycblt/auxio/detail/adapters/GenreDetailAdapter.kt @@ -47,7 +47,7 @@ class GenreDetailAdapter( private val doOnLongClick: (view: View, data: Song) -> Unit ) : ListAdapter(DiffCallback()) { private var currentSong: Song? = null - private var lastHolder: Highlightable? = null + private var currentHolder: Highlightable? = null override fun getItemViewType(position: Int): Int { return when (getItem(position)) { @@ -84,8 +84,8 @@ class GenreDetailAdapter( if (currentSong != null && position > 0) { if (item.id == currentSong?.id) { // Reset the last ViewHolder before assigning the new, correct one to be highlighted - lastHolder?.setHighlighted(false) - lastHolder = (holder as Highlightable) + currentHolder?.setHighlighted(false) + currentHolder = (holder as Highlightable) holder.setHighlighted(true) } else { (holder as Highlightable).setHighlighted(false) @@ -98,11 +98,11 @@ class GenreDetailAdapter( * @param recycler The recyclerview the highlighting should act on. */ fun highlightSong(song: Song?, recycler: RecyclerView) { - // Clear out the last ViewHolder as a song update usually signifies that this current - // ViewHolder is likely invalid. - lastHolder?.setHighlighted(false) - lastHolder = null + if (song == currentSong) return // Already highlighting this viewholder + // Clear the current viewholder since it's invalid + currentHolder?.setHighlighted(false) + currentHolder = null currentSong = song if (song != null) { @@ -116,9 +116,9 @@ class GenreDetailAdapter( // it does become visible. recycler.layoutManager?.findViewByPosition(pos)?.let { child -> recycler.getChildViewHolder(child)?.let { - lastHolder = it as Highlightable + currentHolder = it as Highlightable - lastHolder?.setHighlighted(true) + currentHolder?.setHighlighted(true) } } } diff --git a/app/src/main/java/org/oxycblt/auxio/util/ViewUtil.kt b/app/src/main/java/org/oxycblt/auxio/util/ViewUtil.kt index 6087f8f06..e25c1d4a1 100644 --- a/app/src/main/java/org/oxycblt/auxio/util/ViewUtil.kt +++ b/app/src/main/java/org/oxycblt/auxio/util/ViewUtil.kt @@ -41,8 +41,8 @@ import org.oxycblt.auxio.R /** * Apply a [MaterialShapeDrawable] to this view, automatically initializing the elevation overlay - * and setting the fill color. This assumes that the background is a [ColorDrawable] and will - * crash if not. + * and setting the fill color. The [View]'s current elevation will be applied to the drawable. + * This functions assumes that the background is a [ColorDrawable] and will crash if not. */ fun View.applyMaterialDrawable() { check(background is ColorDrawable) { "Background was not defined as a solid color" }