A drop-in Volley module for use with Square's Dagger
- Easily Switch between multiple server types on the fly
- Add Mocks through use of a custom xml file
- Simple provides to make fixed object injection easy to replicate in production version
- Additional plugin available for Barstool if used
Create your module
public class MyApplication extends Application {
@Override public void onCreate() {
super.onCreate();
VolleyBall network = new VolleyBall(this);
network.forEnv("Production").addServer"http://www.google.com/");
network.forEnv("Staging").addServer("http://staging.google.com/");
network.addMocks(R.xml.routes);
mObjectGraph = ObjectGraph.create(new MainModule(), network);
}
}
Setup your mocks
<routes>
<!-- Parameterized Asset Files -->
<route type="get" path="feed/news?p={page}">newsfeed_{page}.xml</route>
<!-- Static HTTP codes -->
<route type="post" path="feed/news/create">201</route>
<!-- Strings -->
<route type="get" path="profile/name">R.string.test_name</route>
<!-- And Raw Files -->
<route type="put" path="profile/update">R.raw.user</route>
</routes>
Inject your RequestQueue
public class VolleyNewsFeedService implements NewsFeedService {
@Inject RequestQueue mRequestQueue;
@Inject @BaseUrl String mBaseUrl;
public void fetchPage(int aPage) {
...
}
}
Change the server and re-inject
public void switchToMocks() {
EnvSwitcher changer = getObjectGraph().get(EnvSwitcher.class);
changer.changEnv(VolleyBall.MOCK);
getObjectGraph().inject(mNewsFeedService);
}
public class MyApplication extends Application {
@Override public void onCreate() {
super.onCreate();
VolleyBall network = new VolleyBall(this);
network.forEnv("Production").addServer("main", "http://www.google.com/");
network.forEnv("Production").addServer("twitter", "http://www.twitter.com/");
network.forEnv("Staging").addServer("main", "http://staging.google.com/");
network.forEnv("Staging").addServer("twitter", "http://www.twitter.com/");
network.addMocks(R.xml.routes);
mObjectGraph = ObjectGraph.create(new MainModule(), network);
}
}
Inject your RequestQueue and Environment
public class VolleyNewsFeedService implements NewsFeedService {
@Inject RequestQueue mRequestQueue;
@Inject EnvMap mMap;
public void fetchPage(int aPage) {
...
}
}
OR
// In Module
@Provides @Named("main") String provideMain(EnvMap aMap) {
return aMap.get("main");
}
@Provides @Named("twitter") String providesTwitter(EnvMap aMap) {
return aMap.get("twitter");
}
// In Service
public class VolleyNewsFeedService implements NewsFeedService {
@Inject RequestQueue mRequestQueue;
@Inject @Named("main") String mBaseUrl;
public void fetchPage(int aPage) {
...
}
}
VolleyBall is not a standalone module, it is indended to be used with another module even if it is empty. Because of this you need mark your module as incomplete like so.
@Module(
complete=false,
injects=VolleyNewsFeedService.class
)
public class MainModule {
}