-
Notifications
You must be signed in to change notification settings - Fork 89
Home
Welcome to the dart wiki!
Dart and Henson is a an Android Library that structures the navigation layer of your apps. It helps to create intents and consume them in a structured way. We believe it's the best way to organize your navigation layer, and make it less error-prone and easier to maintain.
It is composed of 2 components: Dart and Henson. Both of them use annotated classes that describe the parameters (extras of the intent) of a target activity. DH3 is not easy to setup manually, and we strongly encourage to use the gradle henson-plugin that we provide to use it. See the samples for more details.
A navigation model class is a simple pojo with annotated non private fields. The fields describe an extra passed to the target of an intent:
@DartModel
public class MyActivityNavigationModel {
//a simple requested field, it's name is used as the extra key
public String extra1;
//a named field using an annotation
public @BindExtra(MY_CONSTANT_NAME) String extra2;
//an optional field
public @Nullable MyParcelableOrSerializable extra3;
}
Note that in DH3, navigation models:
- are mandatory, it's not possible to annotate activities directly.
- must follow a naming convention: they should have the same fully qualified name as the activity or service they describe the navigation of, plus the suffix:
NavigationModel
. (e.g.:com.foo.wooper.app.MyActivityNavigationModel
). - must be placed in the navigation source set: in
src/navigation/main/java
.
The historical first component of the library is used to map intents to Pojos (navigation models). Typically, a target activity will define a navigation model class, a pojo with annotated fields and will map the intents it receives to an instance of its model:
public void onCreate(Bundle savedInstanceState) {
Dart.bind(this, myNavigationModel)
}
An activity can map the extras of the intent it receives, or a bundle like savedInstanceState
.
The second component of the library is used to create intents. Based on the navigation model, henson will create an intent builder for the described class (remember the name of the activity / service can be dedudced from the FQN of the model). It creates also some useful wrapper around them, see below.
An intent builder is a generated class that will offer a fluent API to build intents. For the navigation model above, it would create something like:
Intent intent = MyActivityNavigationModel.getInitialState(context)
.extra1("foo")
.extra2(42)
.extra3(myObj) //optional
.build();
The Henson
class is generated to group all the intent builders exposed by a given module. The same intent as above can be built doing:
Intent intent = Henson.gotoMyActivity(context)
.extra1("foo")
.extra2(42)
.extra3(myObj) //optional
.build();
We consider the Henson
class legacy and we suggest to use the HensonNavigator
class instead, see below.
The Henson
class is generated on a producer basis: it wraps all intent builders exposed by a module. On the other hand, the HensonNavigator
is generated on a user basis: it wraps all the intent builder that a module can use.
Intent intent = HensonNavigator.gotoMyActivity(context)
.extra1("foo")
.extra2(42)
.extra3(myObj) //optional
.build();
The intent builders used by a module are detected automatically during the build, based on the dependencies a module uses, and the HensonNavigator
is generated accordingly.
Briefly:
- DH3 fully supports modularization. It was the main motivation for the version 3, and it requested quite a few changes. We don't have a migration guide yet.
- navigation models have changed and are mandatory, they need a single annotation:
@DartModel
. - navigation models must follow a naming convention
- they must be placed in a different source set in:
src/navigation/main/java
- DH3 offers a gradle plugin. DH3 uses a lot of annotation processing internally, configurations, source sets, artifacts, custom tasks. Do not set it up manually unless you know gradle well.
- annotations have changed, the new model is simpler.
- DH3 classes have been repackaged. Though, unfortunately, we couldn't rename the groupId of the library, and gradle doesn't let you use DH2 and DH3 simultaneously.