Added Deezer for loading Artist images

This commit is contained in:
h4h13 2019-06-06 21:57:42 +05:30
parent e082da1dcc
commit b43f71cc32
6 changed files with 227 additions and 55 deletions

View file

@ -0,0 +1,63 @@
package code.name.monkey.retromusic.deezer
import android.content.Context
import okhttp3.Cache
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.create
import retrofit2.http.GET
import retrofit2.http.Query
import java.io.File
import java.util.*
private const val BASE_QUERY_ARTIST = "search/artist"
private const val BASE_URL = "https://api.deezer.com/"
interface DeezerApiService {
@GET("$BASE_QUERY_ARTIST&limit=1")
fun getArtistImage(
@Query("q") artistName: String
): Call<DeezerResponse>
companion object {
operator fun invoke(client: okhttp3.Call.Factory): DeezerApiService {
return Retrofit.Builder()
.baseUrl(BASE_URL)
.callFactory(client)
.addConverterFactory(GsonConverterFactory.create())
.build()
.create()
}
fun createDefaultOkHttpClient(context: Context): OkHttpClient.Builder =
OkHttpClient.Builder()
.cache(createDefaultCache(context))
.addInterceptor(createCacheControlInterceptor())
private fun createDefaultCache(context: Context): Cache? {
val cacheDir = File(context.applicationContext.cacheDir.absolutePath, "/okhttp-deezer/")
if (cacheDir.mkdir() or cacheDir.isDirectory) {
return Cache(cacheDir, 1024 * 1024 * 10)
}
return null
}
private fun createCacheControlInterceptor(): Interceptor {
return Interceptor { chain ->
val modifiedRequest = chain.request().newBuilder()
.addHeader("Cache-Control",
String.format(
Locale.getDefault(),
"max-age=%d, max-stale=%d",
31536000, 31536000
)
).build()
chain.proceed(modifiedRequest)
}
}
}
}

View file

@ -0,0 +1,31 @@
package code.name.monkey.retromusic.deezer
import com.google.gson.annotations.SerializedName
data class Data(
val id: String,
val link: String,
val name: String,
@SerializedName("nb_album")
val nbAlbum: Int,
@SerializedName("nb_fan")
val nbFan: Int,
val picture: String,
@SerializedName("picture_big")
val pictureBig: String,
@SerializedName("picture_medium")
val pictureMedium: String,
@SerializedName("picture_small")
val pictureSmall: String,
@SerializedName("picture_xl")
val pictureXl: String,
val radio: Boolean,
val tracklist: String,
val type: String
)
data class DeezerResponse(
val data: List<Data>,
val next: String,
val total: Int
)