Twelve: Use the Thumbnail class for media items

Change-Id: Iee86907e0a2ebb8d9c2f21d331ad6c4a4ba5eaaf
diff --git a/app/src/main/java/org/lineageos/twelve/datasources/LocalDataSource.kt b/app/src/main/java/org/lineageos/twelve/datasources/LocalDataSource.kt
index 95fcb6b..3bdadd8 100644
--- a/app/src/main/java/org/lineageos/twelve/datasources/LocalDataSource.kt
+++ b/app/src/main/java/org/lineageos/twelve/datasources/LocalDataSource.kt
@@ -32,6 +32,7 @@
 import org.lineageos.twelve.models.Genre
 import org.lineageos.twelve.models.Playlist
 import org.lineageos.twelve.models.RequestStatus
+import org.lineageos.twelve.models.Thumbnail
 import org.lineageos.twelve.query.Query
 import org.lineageos.twelve.query.and
 import org.lineageos.twelve.query.eq
@@ -68,7 +69,9 @@
             contentResolver.loadThumbnail(
                 uri, Size(512, 512), null
             )
-        }.getOrNull()
+        }.getOrNull()?.let {
+            Thumbnail(bitmap = it)
+        }
 
         Album(
             uri,
@@ -92,7 +95,9 @@
             contentResolver.loadThumbnail(
                 uri, Size(512, 512), null
             )
-        }.getOrNull()
+        }.getOrNull()?.let {
+            Thumbnail(bitmap = it)
+        }
 
         Artist(
             uri,
diff --git a/app/src/main/java/org/lineageos/twelve/fragments/AlbumFragment.kt b/app/src/main/java/org/lineageos/twelve/fragments/AlbumFragment.kt
index 543cee4..ffbbd9b 100644
--- a/app/src/main/java/org/lineageos/twelve/fragments/AlbumFragment.kt
+++ b/app/src/main/java/org/lineageos/twelve/fragments/AlbumFragment.kt
@@ -5,6 +5,7 @@
 
 package org.lineageos.twelve.fragments
 
+import android.graphics.ImageDecoder
 import android.net.Uri
 import android.os.Bundle
 import android.util.Log
@@ -27,7 +28,6 @@
 import androidx.recyclerview.widget.RecyclerView
 import com.google.android.material.appbar.MaterialToolbar
 import com.google.android.material.progressindicator.LinearProgressIndicator
-import kotlinx.coroutines.coroutineScope
 import kotlinx.coroutines.flow.collectLatest
 import kotlinx.coroutines.launch
 import org.lineageos.twelve.R
@@ -177,9 +177,18 @@
                     toolbar.title = album.title
                     albumTitleTextView.text = album.title
 
-                    coroutineScope {
-                        thumbnailImageView.setImageBitmap(album.thumbnail)
-                    }
+                    album.thumbnail?.uri?.also { uri ->
+                        ImageDecoder.createSource(
+                            requireContext().contentResolver,
+                            uri
+                        ).let { source ->
+                            ImageDecoder.decodeBitmap(source)
+                        }.also { bitmap ->
+                            thumbnailImageView.setImageBitmap(bitmap)
+                        }
+                    } ?: album.thumbnail?.bitmap?.also { bitmap ->
+                        thumbnailImageView.setImageBitmap(bitmap)
+                    } ?: thumbnailImageView.setImageResource(R.drawable.ic_album)
 
                     artistNameTextView.text = album.artistName
                     artistNameTextView.setOnClickListener {
diff --git a/app/src/main/java/org/lineageos/twelve/fragments/ArtistFragment.kt b/app/src/main/java/org/lineageos/twelve/fragments/ArtistFragment.kt
index 8514def..a0e1364 100644
--- a/app/src/main/java/org/lineageos/twelve/fragments/ArtistFragment.kt
+++ b/app/src/main/java/org/lineageos/twelve/fragments/ArtistFragment.kt
@@ -5,6 +5,7 @@
 
 package org.lineageos.twelve.fragments
 
+import android.graphics.ImageDecoder
 import android.net.Uri
 import android.os.Bundle
 import android.util.Log
@@ -27,7 +28,6 @@
 import androidx.recyclerview.widget.RecyclerView
 import com.google.android.material.appbar.MaterialToolbar
 import com.google.android.material.progressindicator.LinearProgressIndicator
-import kotlinx.coroutines.coroutineScope
 import kotlinx.coroutines.flow.collectLatest
 import kotlinx.coroutines.launch
 import org.lineageos.twelve.R
@@ -82,8 +82,17 @@
             }
 
             override fun ViewHolder.onBindView(item: Album) {
-                item.thumbnail?.let {
-                    view.setThumbnailImage(it)
+                item.thumbnail?.uri?.also { uri ->
+                    ImageDecoder.createSource(
+                        requireContext().contentResolver,
+                        uri
+                    ).let { source ->
+                        ImageDecoder.decodeBitmap(source)
+                    }.also { bitmap ->
+                        view.setThumbnailImage(bitmap)
+                    }
+                } ?: item.thumbnail?.bitmap?.also { bitmap ->
+                    view.setThumbnailImage(bitmap)
                 } ?: view.setThumbnailImage(R.drawable.ic_album)
 
                 view.headlineText = item.title
@@ -187,9 +196,18 @@
 
                     toolbar.title = artist.name
 
-                    coroutineScope {
-                        thumbnailImageView.setImageBitmap(artist.thumbnail)
-                    }
+                    artist.thumbnail?.uri?.also { uri ->
+                        ImageDecoder.createSource(
+                            requireContext().contentResolver,
+                            uri
+                        ).let { source ->
+                            ImageDecoder.decodeBitmap(source)
+                        }.also { bitmap ->
+                            thumbnailImageView.setImageBitmap(bitmap)
+                        }
+                    } ?: artist.thumbnail?.bitmap?.also { bitmap ->
+                        thumbnailImageView.setImageBitmap(bitmap)
+                    } ?: thumbnailImageView.setImageResource(R.drawable.ic_person)
 
                     albumsAdapter.submitList(artistWorks.albums)
                     appearsInAlbumAdapter.submitList(artistWorks.appearsInAlbum)
diff --git a/app/src/main/java/org/lineageos/twelve/models/Album.kt b/app/src/main/java/org/lineageos/twelve/models/Album.kt
index ab04cdb..ab9147e 100644
--- a/app/src/main/java/org/lineageos/twelve/models/Album.kt
+++ b/app/src/main/java/org/lineageos/twelve/models/Album.kt
@@ -5,7 +5,6 @@
 
 package org.lineageos.twelve.models
 
-import android.graphics.Bitmap
 import android.net.Uri
 import androidx.media3.common.MediaMetadata
 import org.lineageos.twelve.ext.buildMediaItem
@@ -26,7 +25,7 @@
     val artistUri: Uri,
     val artistName: String,
     val year: Int?,
-    val thumbnail: Bitmap?,
+    val thumbnail: Thumbnail?,
 ) : MediaItem<Album> {
     override fun areContentsTheSame(other: Album) = compareValuesBy(
         this, other,
@@ -34,7 +33,7 @@
         Album::artistUri,
         Album::artistName,
         Album::year,
-        { it.thumbnail?.sameAs(other.thumbnail) ?: (other.thumbnail == null) },
+        Album::thumbnail,
     ) == 0
 
     override fun toMedia3MediaItem() = buildMediaItem(
diff --git a/app/src/main/java/org/lineageos/twelve/models/Artist.kt b/app/src/main/java/org/lineageos/twelve/models/Artist.kt
index d37b743..36c8af3 100644
--- a/app/src/main/java/org/lineageos/twelve/models/Artist.kt
+++ b/app/src/main/java/org/lineageos/twelve/models/Artist.kt
@@ -5,7 +5,6 @@
 
 package org.lineageos.twelve.models
 
-import android.graphics.Bitmap
 import android.net.Uri
 import androidx.media3.common.MediaMetadata
 import org.lineageos.twelve.ext.buildMediaItem
@@ -20,12 +19,12 @@
 data class Artist(
     override val uri: Uri,
     val name: String,
-    val thumbnail: Bitmap?,
+    val thumbnail: Thumbnail?,
 ) : MediaItem<Artist> {
     override fun areContentsTheSame(other: Artist) = compareValuesBy(
         this, other,
         Artist::name,
-        { it.thumbnail?.sameAs(other.thumbnail) ?: (other.thumbnail == null) },
+        Artist::thumbnail,
     ) == 0
 
     override fun toMedia3MediaItem() = buildMediaItem(