MVP String ResourceProvider API Generator for Android
Java
Latest commit 011830a Dec 29, 2017 @dapp dapp Merge pull request #13 from Comcast/use_getters
v0.11 - make ResourceProvider use getters to enable Mocking for unit tests

README.md

Android Resource Provider - BETA

A Java annotation processor that builds a ResourceProvider class which contains an API to get Android Resources

The ResouceProvider class currently provides APIs for R.string, R.plurals, and R.drawable elements.
Future releases will include support for other types of resources.

Resource Provider allows the presentation layer in your MVP implemenation to explicitly control presentation details without needing access or knowledge of Android's Context or R classes. Resource Provider automatically generates an API for every string resource in your application, allowing the details of fetching resources to be opaque to your presenters, maintaining strict separation of concerns.

ResourceProvider provides APIs that mirror the application's string resource names, but in lower camel case and with the standard underscore delimiter stripped. For example, for the string resource

 <string name="one_arg_formatted_string">This format strings has %1$d args</string>

The resourceprovider-compiler will generate the API:

public String getOneArgFormattedString(Object... formatArgs) { ... }

For any plural:

<plurals name="days_until_friday">
     <item quantity="one">Only 1 day until Friday!</item>
     <item quantity="other">%d days until Friday</item>
</plurals>

resourceprovider-compiler will generate the API:

public String getDaysUntilFridayQuantityString(int quantity, Object... formatArgs) { ... }

And for any drawable file

 any_drawable.png ( or any_drawable.xml)

The resourceprovider-compiler will generate the API:

public Drawable getAnyDrawable() { ... } 

Calling ResourceProvider APIS

In order to avoid conflicts with duplicate resource ids, ResourceProvider organizes its APIs into delegate providers for each resource type. To call a ResourceProvider API, clients will make a call in the format:

resourceProvider.<resource_type>.<resource_name>

For example, to get a String resource:

resourceProvider.string.getSomeString()

And for a color

resourceProvider.color.getSomeColor()

Setup

ResourceProvider only requires an application context for construction, so can easily be provided as a singleton by dependency injection, and can also be mocked for unit testing.

To use ResourceProvider, in your project build.gradle add

compile 'com.xfinity:resourceprovider-library:<version>'

and either

annotationProcessor "com.xfinity:resourceprovider-compiler:<version>"

or, if you're using kotlin,

kapt "com.xfinity:resourceprovider-compiler:<version>"

Also, when using kapt, don't forget to include

kapt {
 correctErrorTypes = true
}

For compatibility with other annotation processors, like Dagger

This library used the https://github.com/jenzz/Android-StaticLauncher project as a template.