From 0daca207b086c76d6e649a57dfb99268ff9edf95 Mon Sep 17 00:00:00 2001 From: OxygenCobalt Date: Sun, 2 May 2021 18:07:38 -0600 Subject: [PATCH] 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. --- app/build.gradle | 3 +- .../detail/adapters/AlbumDetailAdapter.kt | 10 ++++-- .../detail/adapters/ArtistDetailAdapter.kt | 31 ++++++++----------- .../detail/adapters/GenreDetailAdapter.kt | 10 ++++-- .../org/oxycblt/auxio/ui/InterfaceUtils.kt | 15 ++++++--- 5 files changed, 39 insertions(+), 30 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index c995942a9..142447021 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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" 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 5b5e3e2aa..49172db43 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 @@ -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. 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 6fc61a5a2..8ab3d6535 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 @@ -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. 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 b097fa587..915ce8d62 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 @@ -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. diff --git a/app/src/main/java/org/oxycblt/auxio/ui/InterfaceUtils.kt b/app/src/main/java/org/oxycblt/auxio/ui/InterfaceUtils.kt index 5c1b33007..2c24a62cf 100644 --- a/app/src/main/java/org/oxycblt/auxio/ui/InterfaceUtils.kt +++ b/app/src/main/java/org/oxycblt/auxio/ui/InterfaceUtils.kt @@ -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 Context.getSystemServiceSafe(serviceClass: KClass): 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)