Getting Started - nhaarman/Triad GitHub Wiki
If you haven't already, add Triad to your dependencies in your build.gradle file:
repositories {
jcenter()
}
dependencies {
compile 'com.nhaarman:triad:x.x.x'
}If you're using appcompat-v7, add compile 'com.nhaarman:triad-appcompat-v7:x.x.x' as a dependency as well.
An application that uses Triad consists of a single Activity that extends TriadActivity and hosts all screens in the app. A screen then consists of a Screen, a ScreenPresenter, a ScreenContainer and a View.
In the Activity class, all you need to do is define your initial screen, and create a main component:
public class MainActivity extends TriadActivity<ApplicationComponent, ActivityComponent> {
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getTriad().startWith(new MyScreen());
}
@Override
@NonNull
protected ActivityComponent createActivityComponent() {
return new ActivityComponent();
}
}The Screen class tells Triad what layout to use, and which presenters to instantiate:
public class MyScreen extends Screen<ApplicationComponent> {
@Override
protected int getLayoutResId() {
return R.layout.view_my;
}
@NonNull
@Override
protected Presenter<?, ?> createPresenter(@NonNull final Class<? extends Presenter<?,?>> presenterClass) {
if (presenterClass.equals(MyPresenter.class)) {
return new MyPresenter();
}
throw new AssertionError("Unknown presenter class: " + presenterClass.getCanonicalName());
}
}The Presenter of a screen handles the logic and formats data for the Container. In this case, it manages the value of some counter:
class MyPresenter extends Presenter<MyContainer, ActivityComponent> {
private int mCounter;
MyPresenter() {
mCounter = 0;
}
@Override
public void onControlGained(@NonNull final MyContainer container, @NonNull final ActivityComponent activityComponent) {
container.setCounterText(String.valueOf(mCounter));
}
void onIncrementButtonClicked() {
if (!container.isPresent()) {
return;
}
mCounter++;
container().get().setCounterText(String.valueOf(mCounter));
}
}The onControlGained(...) method is called when the View is inflated and attached to the window. Similarly, onControlLost() is called when the View has gone away.
The Container class is an interface with which the Presenter can communicate. It contains all methods the Presenter needs to pass data to the View:
interface MyContainer extends Container {
void setCounterText(String counterText);
}The View class is shown to the user and displays the data it receives from the Presenter through the Container interface. It also passes any user interaction to the Presenter:
public class MyView extends RelativeLayoutContainer<MyPresenter, ActivityComponent> implements MyContainer {
@Bind(R.id.view_my_countertv)
protected TextView mCounterTV;
public MyView(Context context, AttributeSet attrs) {
super(context, attrs, MyPresenter.class);
}
public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr, MyPresenter.class);
}
@Override
public void setCounterText(String counterText) {
mCounterTV.setText(counterText);
}
@OnClick(R.id.view_my_incrementbutton)
public void onIncrementButtonClicked() {
getPresenter().onIncrementButtonClicked();
}
}These View classes get inflated by the Android system, after which Butter Knife injects the View components into it (hence the @InjectView and @OnClick annotations). This is done for you. The only thing you need to do, is let the root of your layout file be defined by this View:
<?xml version="1.0" encoding="utf-8"?>
<com.example.MyView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/view_my_countertv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<Button
android:id="@+id/view_my_incrementbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/view_my_countertv"
android:layout_centerHorizontal="true" />
</com.example.MyView>That's it! This is all you need to create a simple application using Triad.