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.
This commit is contained in:
OxygenCobalt 2021-09-06 19:20:57 -06:00
parent 00b7e0cac3
commit d8c0037b10
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
4 changed files with 28 additions and 27 deletions

View file

@ -47,7 +47,7 @@ class AlbumDetailAdapter(
private val doOnLongClick: (view: View, data: Song) -> Unit private val doOnLongClick: (view: View, data: Song) -> Unit
) : ListAdapter<BaseModel, RecyclerView.ViewHolder>(DiffCallback()) { ) : ListAdapter<BaseModel, RecyclerView.ViewHolder>(DiffCallback()) {
private var currentSong: Song? = null private var currentSong: Song? = null
private var lastHolder: Highlightable? = null private var currentHolder: Highlightable? = null
override fun getItemViewType(position: Int): Int { override fun getItemViewType(position: Int): Int {
return when (getItem(position)) { return when (getItem(position)) {
@ -83,8 +83,8 @@ class AlbumDetailAdapter(
if (currentSong != null && position > 0) { if (currentSong != null && position > 0) {
if (item.id == currentSong?.id) { if (item.id == currentSong?.id) {
// Reset the last ViewHolder before assigning the new, correct one to be highlighted // Reset the last ViewHolder before assigning the new, correct one to be highlighted
lastHolder?.setHighlighted(false) currentHolder?.setHighlighted(false)
lastHolder = (holder as Highlightable) currentHolder = (holder as Highlightable)
holder.setHighlighted(true) holder.setHighlighted(true)
} else { } else {
(holder as Highlightable).setHighlighted(false) (holder as Highlightable).setHighlighted(false)
@ -97,11 +97,11 @@ class AlbumDetailAdapter(
* @param recycler The recyclerview the highlighting should act on. * @param recycler The recyclerview the highlighting should act on.
*/ */
fun highlightSong(song: Song?, recycler: RecyclerView) { fun highlightSong(song: Song?, recycler: RecyclerView) {
// Clear out the last ViewHolder as a song update usually signifies that this current if (song == currentSong) return // Already highlighting this viewholder
// ViewHolder is likely invalid.
lastHolder?.setHighlighted(false)
lastHolder = null
// Clear the current viewholder since it's invalid
currentHolder?.setHighlighted(false)
currentHolder = null
currentSong = song currentSong = song
if (song != null) { if (song != null) {
@ -115,9 +115,9 @@ class AlbumDetailAdapter(
// it does become visible. // it does become visible.
recycler.layoutManager?.findViewByPosition(pos)?.let { child -> recycler.layoutManager?.findViewByPosition(pos)?.let { child ->
recycler.getChildViewHolder(child)?.let { recycler.getChildViewHolder(child)?.let {
lastHolder = it as Highlightable currentHolder = it as Highlightable
lastHolder?.setHighlighted(true) currentHolder?.setHighlighted(true)
} }
} }
} }

View file

@ -98,7 +98,8 @@ class ArtistDetailAdapter(
is Header -> (holder as ArtistSongHeaderViewHolder).bind(item) is Header -> (holder as ArtistSongHeaderViewHolder).bind(item)
is Song -> (holder as ArtistSongViewHolder).bind(item) is Song -> (holder as ArtistSongViewHolder).bind(item)
else -> {} else -> {
}
} }
if (holder is Highlightable) { if (holder is Highlightable) {
@ -122,11 +123,11 @@ class ArtistDetailAdapter(
* @param recycler The recyclerview the highlighting should act on. * @param recycler The recyclerview the highlighting should act on.
*/ */
fun highlightAlbum(album: Album?, recycler: RecyclerView) { fun highlightAlbum(album: Album?, recycler: RecyclerView) {
// Clear out the last ViewHolder as a song update usually signifies that this current if (album == currentAlbum) return // Already highlighting this viewholder
// ViewHolder is likely invalid.
// Album is no longer valid, clear out this ViewHolder.
currentAlbumHolder?.setHighlighted(false) currentAlbumHolder?.setHighlighted(false)
currentAlbumHolder = null currentAlbumHolder = null
currentAlbum = album currentAlbum = album
if (album != null) { if (album != null) {
@ -151,11 +152,11 @@ class ArtistDetailAdapter(
* @param recycler The recyclerview the highlighting should act on. * @param recycler The recyclerview the highlighting should act on.
*/ */
fun highlightSong(song: Song?, recycler: RecyclerView) { fun highlightSong(song: Song?, recycler: RecyclerView) {
// Clear out the last ViewHolder as a song update usually signifies that this current if (song == currentSong) return // Already highlighting this viewholder
// ViewHolder is likely invalid.
// Clear the current viewholder since it's invalid
currentSongHolder?.setHighlighted(false) currentSongHolder?.setHighlighted(false)
currentSongHolder = null currentSongHolder = null
currentSong = song currentSong = song
if (song != null) { if (song != null) {

View file

@ -47,7 +47,7 @@ class GenreDetailAdapter(
private val doOnLongClick: (view: View, data: Song) -> Unit private val doOnLongClick: (view: View, data: Song) -> Unit
) : ListAdapter<BaseModel, RecyclerView.ViewHolder>(DiffCallback()) { ) : ListAdapter<BaseModel, RecyclerView.ViewHolder>(DiffCallback()) {
private var currentSong: Song? = null private var currentSong: Song? = null
private var lastHolder: Highlightable? = null private var currentHolder: Highlightable? = null
override fun getItemViewType(position: Int): Int { override fun getItemViewType(position: Int): Int {
return when (getItem(position)) { return when (getItem(position)) {
@ -84,8 +84,8 @@ class GenreDetailAdapter(
if (currentSong != null && position > 0) { if (currentSong != null && position > 0) {
if (item.id == currentSong?.id) { if (item.id == currentSong?.id) {
// Reset the last ViewHolder before assigning the new, correct one to be highlighted // Reset the last ViewHolder before assigning the new, correct one to be highlighted
lastHolder?.setHighlighted(false) currentHolder?.setHighlighted(false)
lastHolder = (holder as Highlightable) currentHolder = (holder as Highlightable)
holder.setHighlighted(true) holder.setHighlighted(true)
} else { } else {
(holder as Highlightable).setHighlighted(false) (holder as Highlightable).setHighlighted(false)
@ -98,11 +98,11 @@ class GenreDetailAdapter(
* @param recycler The recyclerview the highlighting should act on. * @param recycler The recyclerview the highlighting should act on.
*/ */
fun highlightSong(song: Song?, recycler: RecyclerView) { fun highlightSong(song: Song?, recycler: RecyclerView) {
// Clear out the last ViewHolder as a song update usually signifies that this current if (song == currentSong) return // Already highlighting this viewholder
// ViewHolder is likely invalid.
lastHolder?.setHighlighted(false)
lastHolder = null
// Clear the current viewholder since it's invalid
currentHolder?.setHighlighted(false)
currentHolder = null
currentSong = song currentSong = song
if (song != null) { if (song != null) {
@ -116,9 +116,9 @@ class GenreDetailAdapter(
// it does become visible. // it does become visible.
recycler.layoutManager?.findViewByPosition(pos)?.let { child -> recycler.layoutManager?.findViewByPosition(pos)?.let { child ->
recycler.getChildViewHolder(child)?.let { recycler.getChildViewHolder(child)?.let {
lastHolder = it as Highlightable currentHolder = it as Highlightable
lastHolder?.setHighlighted(true) currentHolder?.setHighlighted(true)
} }
} }
} }

View file

@ -41,8 +41,8 @@ import org.oxycblt.auxio.R
/** /**
* Apply a [MaterialShapeDrawable] to this view, automatically initializing the elevation overlay * 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 * and setting the fill color. The [View]'s current elevation will be applied to the drawable.
* crash if not. * This functions assumes that the background is a [ColorDrawable] and will crash if not.
*/ */
fun View.applyMaterialDrawable() { fun View.applyMaterialDrawable() {
check(background is ColorDrawable) { "Background was not defined as a solid color" } check(background is ColorDrawable) { "Background was not defined as a solid color" }