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:07:38 -06:00
parent 73949a29df
commit 0daca207b0
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
5 changed files with 39 additions and 30 deletions

View file

@ -56,6 +56,7 @@ dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
// Kotlin
// TODO: Move to kotlin 1.5.0 when they actually release it
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
@ -98,7 +99,7 @@ dependencies {
implementation "com.google.android.exoplayer:extension-mediasession:$exoplayer_version"
// Image loading
implementation "io.coil-kt:coil:1.2.0"
implementation "io.coil-kt:coil:1.2.1"
// Material
implementation "com.google.android.material:material:1.3.0"

View file

@ -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.

View file

@ -88,20 +88,17 @@ class ArtistDetailAdapter(
}
if (holder is Highlightable) {
when (item.id) {
currentAlbum?.id -> {
// 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)
}
currentSong?.id -> {
} else if (item.id == currentSong?.id && item is Song) {
currentSongHolder?.setHighlighted(false)
currentSongHolder = holder
holder.setHighlighted(true)
}
else -> holder.setHighlighted(false)
} 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.

View file

@ -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.

View file

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