mirror of
https://github.com/cmclark00/RetroMusicPlayer.git
synced 2025-05-20 00:55:20 +01:00
Fix loading placeholder image for artist
This commit is contained in:
parent
6966351113
commit
b41ef5bff5
8 changed files with 96 additions and 115 deletions
|
@ -1,99 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2019 Hemanth Savarala.
|
||||
*
|
||||
* Licensed under the GNU General Public License v3
|
||||
*
|
||||
* This is free software: you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation either version 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
* See the GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
package code.name.monkey.retromusic.rest;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import code.name.monkey.retromusic.rest.service.LastFMService;
|
||||
import okhttp3.Cache;
|
||||
import okhttp3.Call;
|
||||
import okhttp3.ConnectionPool;
|
||||
import okhttp3.Interceptor;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.logging.HttpLoggingInterceptor;
|
||||
import retrofit2.Retrofit;
|
||||
import retrofit2.converter.gson.GsonConverterFactory;
|
||||
|
||||
|
||||
public class LastFMRestClient {
|
||||
|
||||
private static final String BASE_URL = "https://ws.audioscrobbler.com/2.0/";
|
||||
|
||||
private LastFMService apiService;
|
||||
|
||||
public LastFMRestClient(@NonNull Context context) {
|
||||
this(createDefaultOkHttpClientBuilder(context).build());
|
||||
}
|
||||
|
||||
private LastFMRestClient(@NonNull Call.Factory client) {
|
||||
Retrofit restAdapter = new Retrofit.Builder()
|
||||
.baseUrl(BASE_URL)
|
||||
.callFactory(client)
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.build();
|
||||
|
||||
apiService = restAdapter.create(LastFMService.class);
|
||||
}
|
||||
|
||||
private static Interceptor createCacheControlInterceptor() {
|
||||
return chain -> {
|
||||
Request modifiedRequest = chain.request().newBuilder()
|
||||
.addHeader("Cache-Control", "max-age=31536000, max-stale=31536000")
|
||||
.build();
|
||||
return chain.proceed(modifiedRequest);
|
||||
};
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static Cache createDefaultCache(Context context) {
|
||||
File cacheDir = new File(context.getCacheDir().getAbsolutePath(), "/okhttp-lastfm/");
|
||||
if (cacheDir.mkdirs() || cacheDir.isDirectory()) {
|
||||
return new Cache(cacheDir, 1024 * 1024 * 10);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private static OkHttpClient.Builder createDefaultOkHttpClientBuilder(@NonNull Context context) {
|
||||
return new OkHttpClient.Builder()
|
||||
.connectionPool(new ConnectionPool(0, 1, TimeUnit.NANOSECONDS))
|
||||
.retryOnConnectionFailure(true)
|
||||
.connectTimeout(1, TimeUnit.MINUTES) // connect timeout
|
||||
.writeTimeout(1, TimeUnit.MINUTES) // write timeout
|
||||
.readTimeout(1, TimeUnit.MINUTES) // read timeout
|
||||
.cache(createDefaultCache(context))
|
||||
.addInterceptor(createCacheControlInterceptor())
|
||||
.addInterceptor(createLogInterceptor());
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private static Interceptor createLogInterceptor() {
|
||||
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
|
||||
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
|
||||
return interceptor;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public LastFMService getApiService() {
|
||||
return apiService;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
package code.name.monkey.retromusic.rest
|
||||
|
||||
import code.name.monkey.retromusic.App
|
||||
import code.name.monkey.retromusic.rest.service.LastFMService
|
||||
import com.google.gson.Gson
|
||||
import okhttp3.Cache
|
||||
import okhttp3.ConnectionPool
|
||||
import okhttp3.Interceptor
|
||||
import okhttp3.Interceptor.Chain
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.logging.HttpLoggingInterceptor
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.converter.gson.GsonConverterFactory
|
||||
import java.io.File
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
object LastFmClient {
|
||||
private const val baseUrl = "https://ws.audioscrobbler.com/2.0/"
|
||||
|
||||
private var lastFMService: LastFMService
|
||||
|
||||
fun getApiService(): LastFMService {
|
||||
return lastFMService
|
||||
}
|
||||
|
||||
init {
|
||||
val retrofit = Retrofit.Builder()
|
||||
.baseUrl(baseUrl)
|
||||
.callFactory(createDefaultOkHttpClientBuilder().build())
|
||||
.addConverterFactory(GsonConverterFactory.create(Gson()))
|
||||
.build()
|
||||
|
||||
lastFMService = retrofit.create(LastFMService::class.java)
|
||||
}
|
||||
|
||||
private fun createDefaultOkHttpClientBuilder(): OkHttpClient.Builder {
|
||||
return OkHttpClient.Builder()
|
||||
.connectionPool(ConnectionPool(0, 1, TimeUnit.NANOSECONDS))
|
||||
.retryOnConnectionFailure(true)
|
||||
.connectTimeout(1, TimeUnit.MINUTES) // connect timeout
|
||||
.writeTimeout(1, TimeUnit.MINUTES) // write timeout
|
||||
.readTimeout(1, TimeUnit.MINUTES) // read timeout
|
||||
.cache(createDefaultCache())
|
||||
.addInterceptor(createCacheControlInterceptor())
|
||||
.addInterceptor(createLogInterceptor())
|
||||
}
|
||||
|
||||
private fun createLogInterceptor(): Interceptor {
|
||||
val interceptor = HttpLoggingInterceptor()
|
||||
interceptor.level = HttpLoggingInterceptor.Level.BASIC
|
||||
return interceptor
|
||||
}
|
||||
|
||||
private fun createCacheControlInterceptor(): Interceptor {
|
||||
return Interceptor { chain: Chain ->
|
||||
val modifiedRequest = chain.request().newBuilder()
|
||||
.addHeader("Cache-Control", "max-age=31536000, max-stale=31536000")
|
||||
.build()
|
||||
chain.proceed(modifiedRequest)
|
||||
}
|
||||
}
|
||||
|
||||
private fun createDefaultCache(): Cache? {
|
||||
val cacheDir = File(App.getContext().cacheDir.absolutePath, "/okhttp-lastfm/")
|
||||
if (cacheDir.mkdirs() || cacheDir.isDirectory) {
|
||||
return Cache(cacheDir, 1024 * 1024 * 10)
|
||||
}
|
||||
return null
|
||||
}
|
||||
}
|
|
@ -25,9 +25,8 @@ import retrofit2.http.Query
|
|||
*/
|
||||
|
||||
interface LastFMService {
|
||||
|
||||
companion object {
|
||||
const val API_KEY = "c679c8d3efa84613dc7dcb2e8d42da4c"
|
||||
private const val API_KEY = "c679c8d3efa84613dc7dcb2e8d42da4c"
|
||||
const val BASE_QUERY_PARAMETERS = "?format=json&autocorrect=1&api_key=$API_KEY"
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue