Created
June 20, 2015 15:40
-
-
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
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
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); | |
} | |
} |
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
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