Twelve: Fix album artwork flashes for now playing bar as well

Change-Id: I23363786050fa9ecc9d54ae6b78e5174d3fd74e7
diff --git a/app/src/main/java/org/lineageos/twelve/fragments/MainFragment.kt b/app/src/main/java/org/lineageos/twelve/fragments/MainFragment.kt
index b9d0016..66a35f6 100644
--- a/app/src/main/java/org/lineageos/twelve/fragments/MainFragment.kt
+++ b/app/src/main/java/org/lineageos/twelve/fragments/MainFragment.kt
@@ -22,6 +22,7 @@
 import kotlinx.coroutines.launch
 import org.lineageos.twelve.R
 import org.lineageos.twelve.ext.getViewProperty
+import org.lineageos.twelve.models.RequestStatus
 import org.lineageos.twelve.ui.views.NowPlayingBar
 import org.lineageos.twelve.viewmodels.NowPlayingViewModel
 
@@ -116,6 +117,24 @@
                         nowPlayingBar.updateMediaMetadata(it)
                     }
                 }
+
+                launch {
+                    viewModel.mediaArtwork.collectLatest {
+                        when (it) {
+                            is RequestStatus.Loading -> {
+                                // Do nothing
+                            }
+
+                            is RequestStatus.Success -> {
+                                nowPlayingBar.updateMediaArtwork(it.data)
+                            }
+
+                            is RequestStatus.Error -> throw Exception(
+                                "Error while getting media artwork"
+                            )
+                        }
+                    }
+                }
             }
         }
     }
diff --git a/app/src/main/java/org/lineageos/twelve/ui/views/NowPlayingBar.kt b/app/src/main/java/org/lineageos/twelve/ui/views/NowPlayingBar.kt
index d680f6c..12df33f 100644
--- a/app/src/main/java/org/lineageos/twelve/ui/views/NowPlayingBar.kt
+++ b/app/src/main/java/org/lineageos/twelve/ui/views/NowPlayingBar.kt
@@ -6,7 +6,6 @@
 package org.lineageos.twelve.ui.views
 
 import android.content.Context
-import android.graphics.BitmapFactory
 import android.graphics.ImageDecoder
 import android.util.AttributeSet
 import android.widget.FrameLayout
@@ -21,6 +20,7 @@
 import org.lineageos.twelve.R
 import org.lineageos.twelve.ext.slideDown
 import org.lineageos.twelve.ext.slideUp
+import org.lineageos.twelve.models.Thumbnail
 
 class NowPlayingBar @JvmOverloads constructor(
     context: Context, attrs: AttributeSet? = null
@@ -64,23 +64,6 @@
     }
 
     fun updateMediaMetadata(mediaMetadata: MediaMetadata) {
-        mediaMetadata.artworkData?.also { artworkData ->
-            BitmapFactory.decodeByteArray(
-                artworkData, 0, artworkData.size
-            )?.let { bitmap ->
-                thumbnailImageView.setImageBitmap(bitmap)
-            }
-        } ?: mediaMetadata.artworkUri?.also { artworkUri ->
-            ImageDecoder.createSource(
-                context.contentResolver,
-                artworkUri
-            ).let { source ->
-                ImageDecoder.decodeBitmap(source)
-            }.also { bitmap ->
-                thumbnailImageView.setImageBitmap(bitmap)
-            }
-        } ?: thumbnailImageView.setImageResource(R.drawable.ic_music_note)
-
         mediaMetadata.title?.also {
             titleTextView.text = it
             titleTextView.isVisible = true
@@ -103,6 +86,21 @@
         }
     }
 
+    fun updateMediaArtwork(artwork: Thumbnail?) {
+        artwork?.bitmap?.also { bitmap ->
+            thumbnailImageView.setImageBitmap(bitmap)
+        } ?: artwork?.uri?.also { artworkUri ->
+            ImageDecoder.createSource(
+                context.contentResolver,
+                artworkUri
+            ).let { source ->
+                ImageDecoder.decodeBitmap(source)
+            }.also { bitmap ->
+                thumbnailImageView.setImageBitmap(bitmap)
+            }
+        } ?: thumbnailImageView.setImageResource(R.drawable.ic_music_note)
+    }
+
     fun updateDurationCurrentPositionMs(durationMs: Long?, currentPositionMs: Long?) {
         val currentPositionSecs = currentPositionMs?.let { it / 1000 }?.toInt() ?: 0
         val durationSecs = durationMs?.let { it / 1000 }?.toInt()?.takeIf { it != 0 } ?: 1