Skip to content

Instantly share code, notes, and snippets.

@blundell
Created June 20, 2015 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blundell/4c4ad0fab121ea78c8eb to your computer and use it in GitHub Desktop.
Save blundell/4c4ad0fab121ea78c8eb to your computer and use it in GitHub Desktop.
Creates a builder for the SpotifyApi allowing easier client creation & hiding retrofit implementation. Idea from: https://github.com/kaaes/spotify-web-api-android/pull/75
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import kaaes.spotify.webapi.android.SpotifyApi;
import retrofit.android.MainThreadExecutor;
final class SpotifyApiBuilder {
private Executor executeExecutor;
private Executor callbackExecutor;
public SpotifyApiBuilder() {
this.executeExecutor = Executors.newSingleThreadExecutor();
this.callbackExecutor = new MainThreadExecutor();
}
public SpotifyApiBuilder executeOnBackgroundThread() {
executeOn(Executors.newSingleThreadExecutor());
return this;
}
public SpotifyApiBuilder executeOnMainThread() {
executeOn(new MainThreadExecutor());
return this;
}
public SpotifyApiBuilder executeOn(Executor executor) {
this.executeExecutor = executor;
return this;
}
public SpotifyApiBuilder callbackOnBackgroundThread() {
callbackOn(Executors.newSingleThreadExecutor());
return this;
}
public SpotifyApiBuilder callbackOnMainThread() {
callbackOn(new MainThreadExecutor());
return this;
}
public SpotifyApiBuilder callbackOn(Executor executor) {
this.callbackExecutor = executor;
return this;
}
public SpotifyApi create() {
return new SpotifyApi(executeExecutor, callbackExecutor);
}
}
SpotifyApi spotifyApi = new SpotifyApiBuilder().executeOnBackgroundThread().callbackOnMainThread().create();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment