Fix image loading
This commit is contained in:
parent
a96e682e2b
commit
5369d49776
1 changed files with 21 additions and 13 deletions
|
@ -15,13 +15,9 @@
|
||||||
package code.name.monkey.retromusic.glide.artistimage
|
package code.name.monkey.retromusic.glide.artistimage
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.text.TextUtils
|
import code.name.monkey.retromusic.deezer.Data
|
||||||
import code.name.monkey.retromusic.App
|
|
||||||
import code.name.monkey.retromusic.deezer.DeezerApiService
|
import code.name.monkey.retromusic.deezer.DeezerApiService
|
||||||
import code.name.monkey.retromusic.deezer.DeezerResponse
|
import code.name.monkey.retromusic.deezer.DeezerResponse
|
||||||
import code.name.monkey.retromusic.rest.LastFMRestClient
|
|
||||||
import code.name.monkey.retromusic.rest.model.LastFmArtist
|
|
||||||
import code.name.monkey.retromusic.util.LastFMUtil
|
|
||||||
import code.name.monkey.retromusic.util.MusicUtil
|
import code.name.monkey.retromusic.util.MusicUtil
|
||||||
import code.name.monkey.retromusic.util.RetroUtil
|
import code.name.monkey.retromusic.util.RetroUtil
|
||||||
import com.bumptech.glide.Priority
|
import com.bumptech.glide.Priority
|
||||||
|
@ -65,7 +61,8 @@ class ArtistImageFetcher(private val context: Context,
|
||||||
override fun loadData(priority: Priority, callback: DataFetcher.DataCallback<in InputStream>) {
|
override fun loadData(priority: Priority, callback: DataFetcher.DataCallback<in InputStream>) {
|
||||||
try {
|
try {
|
||||||
if (!MusicUtil.isArtistNameUnknown(model.artistName) && RetroUtil.isAllowedToDownloadMetadata(context)) {
|
if (!MusicUtil.isArtistNameUnknown(model.artistName) && RetroUtil.isAllowedToDownloadMetadata(context)) {
|
||||||
call = deezerApiService.getArtistImage(model.artistName)
|
val artists = model.artistName.split(",")
|
||||||
|
call = deezerApiService.getArtistImage(artists[0])
|
||||||
call?.enqueue(object : Callback<DeezerResponse> {
|
call?.enqueue(object : Callback<DeezerResponse> {
|
||||||
override fun onFailure(call: Call<DeezerResponse>, t: Throwable) {
|
override fun onFailure(call: Call<DeezerResponse>, t: Throwable) {
|
||||||
callback.onLoadFailed(Exception(t))
|
callback.onLoadFailed(Exception(t))
|
||||||
|
@ -78,8 +75,7 @@ class ArtistImageFetcher(private val context: Context,
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
val deezerResponse: DeezerResponse? = response.body()
|
val deezerResponse: DeezerResponse? = response.body()
|
||||||
println(deezerResponse)
|
val url = deezerResponse?.data?.get(0)?.let { getHighestQuality(it) }
|
||||||
val url = deezerResponse?.data?.get(0)?.pictureXl
|
|
||||||
streamFetcher = OkHttpStreamFetcher(okHttp, GlideUrl(url))
|
streamFetcher = OkHttpStreamFetcher(okHttp, GlideUrl(url))
|
||||||
streamFetcher?.loadData(priority, callback)
|
streamFetcher?.loadData(priority, callback)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
|
@ -95,6 +91,17 @@ class ArtistImageFetcher(private val context: Context,
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun getHighestQuality(imageUrl: Data): String {
|
||||||
|
return when {
|
||||||
|
imageUrl.pictureXl.isNotEmpty() -> imageUrl.pictureXl
|
||||||
|
imageUrl.pictureBig.isNotEmpty() -> imageUrl.pictureBig
|
||||||
|
imageUrl.pictureMedium.isNotEmpty() -> imageUrl.pictureMedium
|
||||||
|
imageUrl.pictureSmall.isNotEmpty() -> imageUrl.pictureSmall
|
||||||
|
imageUrl.picture.isNotEmpty() -> imageUrl.picture
|
||||||
|
else -> ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override fun cleanup() {
|
override fun cleanup() {
|
||||||
if (streamFetcher != null) {
|
if (streamFetcher != null) {
|
||||||
streamFetcher!!.cleanup()
|
streamFetcher!!.cleanup()
|
||||||
|
@ -127,7 +134,8 @@ class ArtistImageLoader(private val context: Context,
|
||||||
}
|
}
|
||||||
|
|
||||||
class Factory(private val context: Context) : ModelLoaderFactory<ArtistImage, InputStream> {
|
class Factory(private val context: Context) : ModelLoaderFactory<ArtistImage, InputStream> {
|
||||||
private val deezerApiService: DeezerApiService = DeezerApiService.invoke(DeezerApiService.createDefaultOkHttpClient(context)
|
private val deezerApiService: DeezerApiService =
|
||||||
|
DeezerApiService.invoke(DeezerApiService.createDefaultOkHttpClient(context)
|
||||||
.connectTimeout(TIMEOUT.toLong(), TimeUnit.MILLISECONDS)
|
.connectTimeout(TIMEOUT.toLong(), TimeUnit.MILLISECONDS)
|
||||||
.readTimeout(TIMEOUT.toLong(), TimeUnit.MILLISECONDS)
|
.readTimeout(TIMEOUT.toLong(), TimeUnit.MILLISECONDS)
|
||||||
.writeTimeout(TIMEOUT.toLong(), TimeUnit.MILLISECONDS)
|
.writeTimeout(TIMEOUT.toLong(), TimeUnit.MILLISECONDS)
|
||||||
|
|
Loading…
Reference in a new issue