Last active
July 13, 2018 03:07
-
-
Save marukami/927473aca90ff6b98d1a to your computer and use it in GitHub Desktop.
Android Picasso Rewrite Retry Policy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Kotlin conversion from https://gist.github.com/extralam/cda4ce0d7341b929efd4 | |
// This could be better. For now this works | |
inline fun OkHttpClient.init(f: OkHttpClient.() -> Unit): OkHttpClient { | |
this.f() | |
return this | |
} | |
class PabloPicasso { | |
val picasso: Picasso | |
constructor(ctx: Context) { | |
val client = OkHttpClient().init { | |
retryOnConnectionFailure = true | |
interceptors().add(Interceptor { chain -> | |
val req = chain.request() | |
var rep = chain.proceed(req) | |
var tryCount = 0 | |
while(rep.isSuccessful.not() && tryCount < MAX_RETRY_TIME) { | |
Timber.tag("PabloPicasso.client.chain") | |
.d("intercept : Request is not successful - ${tryCount}") | |
tryCount += 1 | |
rep = chain.proceed(req) | |
} | |
rep | |
}) | |
} | |
picasso = Picasso.Builder(ctx) | |
.executor(Executors.newFixedThreadPool(MAX_DOWNLOADING_THREAD)) | |
.downloader(OkHttpDownloader(client)) | |
.build() | |
if(DEBUG) { | |
picasso.setIndicatorsEnabled(true); | |
picasso.isLoggingEnabled = true; | |
} | |
} | |
companion object { | |
val MAX_DOWNLOADING_THREAD = 4 | |
val MAX_RETRY_TIME = 4; // Default is 3 in Picasso | |
} | |
} | |
/* | |
I'm currently using the custom application onCreate to set this | |
val pablo = PabloPicasso(this); | |
Picasso.setSingletonInstance(pablo.getPicasso()); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment