Fix bug with playing item highlighting

Fix a problem where the wrong playing item would be highlighted if the names of the items were identical.
This commit is contained in:
OxygenCobalt
2021-05-02 18:08:39 -06:00
parent 73949a29df
commit 0daca207b0
5 changed files with 39 additions and 30 deletions
@@ -57,13 +57,17 @@ class AlbumDetailAdapter(
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when (val item = getItem(position)) {
val item = getItem(position)
when (item) {
is Album -> (holder as AlbumHeaderViewHolder).bind(item)
is Song -> (holder as AlbumSongViewHolder).bind(item)
else -> {}
}
if (currentSong != null && position > 0) {
if (getItem(position).id == currentSong?.id) {
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)
@@ -89,7 +93,7 @@ class AlbumDetailAdapter(
if (song != null) {
// Use existing data instead of having to re-sort it.
val pos = currentList.indexOfFirst { item ->
item.name == song.name && item is Song
item.id == song.id && item is Song
}
// Check if the ViewHolder for this song is visible, if it is then highlight it.
@@ -88,20 +88,17 @@ class ArtistDetailAdapter(
}
if (holder is Highlightable) {
when (item.id) {
currentAlbum?.id -> {
currentAlbumHolder?.setHighlighted(false)
currentAlbumHolder = holder
holder.setHighlighted(true)
}
currentSong?.id -> {
currentSongHolder?.setHighlighted(false)
currentSongHolder = holder
holder.setHighlighted(true)
}
else -> holder.setHighlighted(false)
// If the item corresponds to a currently playing song/album then highlight it
if (item.id == currentAlbum?.id && item is Album) {
currentAlbumHolder?.setHighlighted(false)
currentAlbumHolder = holder
holder.setHighlighted(true)
} else if (item.id == currentSong?.id && item is Song) {
currentSongHolder?.setHighlighted(false)
currentSongHolder = holder
holder.setHighlighted(true)
} else {
holder.setHighlighted(false)
}
}
}
@@ -121,11 +118,9 @@ class ArtistDetailAdapter(
if (album != null) {
// Use existing data instead of having to re-sort it.
val pos = currentList.indexOfFirst { item ->
item.name == album.name && item is Album
item.id == album.id && item is Album
}
logD(pos)
// Check if the ViewHolder if this album is visible, and highlight it if so.
recycler.layoutManager?.findViewByPosition(pos)?.let { child ->
recycler.getChildViewHolder(child)?.let {
@@ -153,7 +148,7 @@ class ArtistDetailAdapter(
// Use existing data instead of having to re-sort it.
// We also have to account for the album count when searching for the viewholder
val pos = currentList.indexOfFirst { item ->
item.name == song.name && item is Song
item.id == song.id && item is Song
}
// Check if the ViewHolder for this song is visible, if it is then highlight it.
@@ -59,12 +59,16 @@ class GenreDetailAdapter(
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when (val item = getItem(position)) {
val item = getItem(position)
when (item) {
is Genre -> (holder as GenreHeaderViewHolder).bind(item)
is Song -> (holder as GenreSongViewHolder).bind(item)
else -> {}
}
if (currentSong != null && position > 0) {
if (getItem(position).id == currentSong?.id) {
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)
@@ -90,7 +94,7 @@ class GenreDetailAdapter(
if (song != null) {
// Use existing data instead of having to re-sort it.
val pos = currentList.indexOfFirst { item ->
item.name == song.name && item is Song
item.id == song.id && item is Song
}
// Check if the ViewHolder for this song is visible, if it is then highlight it.
@@ -18,8 +18,10 @@ import android.view.WindowManager
import android.widget.ImageButton
import android.widget.TextView
import android.widget.Toast
import androidx.annotation.AttrRes
import androidx.annotation.ColorInt
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.annotation.PluralsRes
import androidx.annotation.StringRes
import androidx.core.content.ContextCompat
@@ -84,7 +86,7 @@ fun <T : Any> Context.getSystemServiceSafe(serviceClass: KClass<T>): T {
* @return The resolved color, black if the resolving process failed.
*/
@ColorInt
fun Int.toColor(context: Context): Int {
fun @receiver:ColorRes Int.toColor(context: Context): Int {
return try {
ContextCompat.getColor(context, this)
} catch (e: Resources.NotFoundException) {
@@ -101,23 +103,26 @@ fun Int.toColor(context: Context): Int {
* @return The resolved color as a [ColorStateList]
* @see toColor
*/
fun Int.toStateList(context: Context) = ColorStateList.valueOf(toColor(context))
fun @receiver:ColorRes Int.toStateList(context: Context) =
ColorStateList.valueOf(toColor(context))
/**
* Resolve a drawable resource into a [Drawable]
*/
fun Int.toDrawable(context: Context) = ContextCompat.getDrawable(context, this)
fun @receiver:DrawableRes Int.toDrawable(context: Context) =
ContextCompat.getDrawable(context, this)
/**
* Resolve a drawable resource into an [AnimatedVectorDrawable]
* @see toDrawable
*/
fun Int.toAnimDrawable(context: Context) = toDrawable(context) as AnimatedVectorDrawable
fun @receiver:DrawableRes Int.toAnimDrawable(context: Context) =
toDrawable(context) as AnimatedVectorDrawable
/**
* Resolve this int into a color as if it was an attribute
*/
fun Int.resolveAttr(context: Context): Int {
fun @receiver:AttrRes Int.resolveAttr(context: Context): Int {
// Convert the attribute into its color
val resolvedAttr = TypedValue()
context.theme.resolveAttribute(this, resolvedAttr, true)