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
) : ListAdapter<BaseModel, RecyclerView.ViewHolder>(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)
}
}
}

View file

@ -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) {

View file

@ -47,7 +47,7 @@ class GenreDetailAdapter(
private val doOnLongClick: (view: View, data: Song) -> Unit
) : ListAdapter<BaseModel, RecyclerView.ViewHolder>(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)
}
}
}

View file

@ -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" }