Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
A Dagger Injectable Mockable Volley Module
Java Groovy
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
example
gradle/wrapper
library
volleyball-debug
.gitignore
LICENSE
README.md
build.gradle
gradlew
settings.gradle

README.md

Volley Ball

A drop-in Volley module for use with Square's Dagger

Features

  • 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

Usage

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);
}

Multiple Servers per Environment

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) {
        ...
    }
}

Notes and Gotchas

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 {

}
Something went wrong with that request. Please try again.