commons-app-documentation

Commons Android App Style Guide

General coding style

Our coding style follows the Google Java Style Guide.

Our code style is part of the project and should be automatically used.
The code style is located at .idea/codeStyles/Project.xml. It is important that you do not modify this file unless you are working on a ticket to update our code style.

It may also be beneficial to create a macro to help you keep files formatted, indented and saved.
These actions can be found in the Code and File menus respectively.
This macro can be bound to ctrl+s for convenience

Javadocs

You must include Javadocs for all new methods and classes. This is aimed at making it easier for new contributors to dive into our codebase, especially those who are new to Android development. A few things to note:

New libraries

For reasons mentioned in #1489, new libraries can only be added if:

In short, please discuss things with us before you submit a PR with a new library, as otherwise your entire PR might be declined.

Architecture

Please follow the MVP architecture if you are submitting a large enhancement (definitions of “large” vary according to context - if you are not sure whether this is needed for your code, ask beforehand)

Unit tests

Please write unit tests to cover your own code if you are submitting an enhancement (i.e. the issue that you are working on has an “enhancement” label)

Logging

Please use Timber for logging, for instance Timber.d("Current image quality is %d", currentImageQuality);, you may need to import: import timber.log.Timber;

Please do not write e.printStackTrace(), instead output the exception to Timber.

Licensing

We can only embed libraries that allow being embedded with Apache 2-licensed software.

In particular, we can NOT embed:

We CAN embed:

Calling HTTP services powered by non open source software (example: Google Search, Crashlytics, HockeyApp) is not illegal per se, but:

Strings

Strings added must not have unescaped HTML tags.

String literals should be avoided.

View Bindings

Please use Google’s view binding.

The project has legacy Butterknife and Kotlin Android Extensions ViewBindings, please do not hesitate to replace them.

Shared Preferences

We have 2 wrapper classes to provide instances of shared prefs.

These classes handle all the logic for talking with SharedPreferences. Anyone trying to use shared prefs can do it using simple interfaces:

BasicKvStore

BasicKvStore store = new BasicKvStore(context, "storeName");

// Put string value
store.putString("test", "Hello world");

// Get String value
String value = store.getString("test");

JsonKvStore

Similiarly JsonKvStore can be used for complex objects:

JsonKvStore store = new JsonKvStore(context, "storeName");

// Put Json value
Place place = new Place(); //assume you have an instance of place object
store.putJson("test", place)

// Get Json value
Place place = store.getJson("test", Place.java)

If you need to save a List/Map/Set of values then you can simply save and retrieve it in JSON format.

The eg. below shows how to save and retrieve a Set<String>

Type setType = new TypeToken<Set<String>>() {}.getType();
directKvStore.putJson("keyName", new HashSet<String>());
directKvStore.getJson("keyName", setType);

Test-driven development