Move album art loading to Coil

Move the album art loading to Coil to decrease initial loading times.
This commit is contained in:
OxygenCobalt 2020-08-25 18:35:30 -06:00
parent 89398d9f4e
commit d9dda08731
5 changed files with 20 additions and 33 deletions

View file

@ -30,6 +30,10 @@ android {
buildFeatures {
dataBinding true
}
kotlinOptions {
jvmTarget = "1.8"
}
}
configurations {
@ -45,13 +49,16 @@ dependencies {
// Support
implementation 'androidx.core:core-ktx:1.3.1'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-rc1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0'
// Navigation
def navigation_version = "2.3.0"
implementation "androidx.navigation:navigation-fragment-ktx:$navigation_version"
implementation "androidx.navigation:navigation-ui-ktx:$navigation_version"
// Image loading
implementation("io.coil-kt:coil:0.12.0")
/*
// Room Database
def room_version = "2.2.5"

View file

@ -1,13 +1,13 @@
package org.oxycblt.auxio.music.models
import android.graphics.Bitmap
import android.net.Uri
// Abstraction for Song
data class Album(
val id: Long = 0L,
val title: String = "",
val artistName: String = "",
val cover: Bitmap? = null,
val coverUri: Uri = Uri.EMPTY,
val year: Int = 0,
var numSongs: Int = 0
) {

View file

@ -3,9 +3,6 @@ package org.oxycblt.auxio.music.processing
import android.app.Application
import android.content.ContentResolver
import android.database.Cursor
import android.graphics.ImageDecoder
import android.os.Build
import android.provider.MediaStore
import android.provider.MediaStore.Audio.Albums
import android.provider.MediaStore.Audio.Artists
import android.provider.MediaStore.Audio.Genres
@ -17,7 +14,6 @@ import org.oxycblt.auxio.music.models.Genre
import org.oxycblt.auxio.music.models.Song
import org.oxycblt.auxio.music.toAlbumArtURI
import org.oxycblt.auxio.music.toNamedGenre
import java.io.FileNotFoundException
enum class MusicLoaderResponse {
DONE, FAILURE, NO_MUSIC
@ -193,29 +189,12 @@ class MusicLoader(private val app: Application) {
val year = cursor.getInt(yearIndex)
val numSongs = cursor.getInt(numIndex)
// TODO:
// Album art loading during the initial load isn't really practical for a large amount of albums
// Use glide or something
val artUri = id.toAlbumArtURI()
// Get the album art through either ImageDecoder or MediaStore depending on the
// version.
val cover = try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
ImageDecoder.decodeBitmap(
ImageDecoder.createSource(resolver, artUri)
)
} else {
MediaStore.Images.Media.getBitmap(resolver, artUri)
}
} catch (noFound: FileNotFoundException) {
null
}
val coverUri = id.toAlbumArtURI()
albums.add(
Album(
id, name, artist,
cover, year, numSongs
coverUri, year, numSongs
)
)
}

View file

@ -1,6 +1,8 @@
package org.oxycblt.auxio.recycler
import android.net.Uri
import androidx.recyclerview.widget.RecyclerView
import coil.load
import org.oxycblt.auxio.databinding.AlbumItemBinding
import org.oxycblt.auxio.music.models.Album
@ -13,12 +15,11 @@ class AlbumViewHolder(
fun bind(album: Album) {
binding.album = album
if (album.cover == null) {
// If there is no cover, clear the ImageView so that the previous
// View's cover doesn't stick around.
binding.cover.setImageResource(android.R.color.transparent)
} else {
binding.cover.setImageBitmap(album.cover)
// Load the album cover
binding.cover.load(album.coverUri) {
crossfade(true)
placeholder(android.R.color.transparent)
error(android.R.color.transparent)
}
binding.executePendingBindings()

View file

@ -1,6 +1,6 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = "1.3.72"
ext.kotlin_version = "1.4.0"
repositories {
google()