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

View file

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

View file

@ -3,9 +3,6 @@ package org.oxycblt.auxio.music.processing
import android.app.Application import android.app.Application
import android.content.ContentResolver import android.content.ContentResolver
import android.database.Cursor 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.Albums
import android.provider.MediaStore.Audio.Artists import android.provider.MediaStore.Audio.Artists
import android.provider.MediaStore.Audio.Genres 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.models.Song
import org.oxycblt.auxio.music.toAlbumArtURI import org.oxycblt.auxio.music.toAlbumArtURI
import org.oxycblt.auxio.music.toNamedGenre import org.oxycblt.auxio.music.toNamedGenre
import java.io.FileNotFoundException
enum class MusicLoaderResponse { enum class MusicLoaderResponse {
DONE, FAILURE, NO_MUSIC DONE, FAILURE, NO_MUSIC
@ -193,29 +189,12 @@ class MusicLoader(private val app: Application) {
val year = cursor.getInt(yearIndex) val year = cursor.getInt(yearIndex)
val numSongs = cursor.getInt(numIndex) val numSongs = cursor.getInt(numIndex)
// TODO: val coverUri = id.toAlbumArtURI()
// 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
}
albums.add( albums.add(
Album( Album(
id, name, artist, id, name, artist,
cover, year, numSongs coverUri, year, numSongs
) )
) )
} }

View file

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

View file

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