-
Notifications
You must be signed in to change notification settings - Fork 34
Getting started
Firstly, add base Moxy module and compiler module in dependencies:
dependencies {
...
compile 'com.arello-mobile:moxy:1.1.2'
provided 'com.arello-mobile:moxy-compiler:1.1.2'
}Base Moxy module will be provide all the usual components, such as MvpPresenter, MvpView, MvpDelegate and so on.
Compiler will be generate code:
- for
MvpPresentercreation and injection - of
ViewState - for injection
ViewStatetoPresenter
These modules enough to remove boilerplate code.
Secondly, for additional base view classes MvpActivity and MvpFragment add this:
dependencies {
...
compile 'com.arello-mobile:moxy-android:1.1.2'
}If you planing to use AppCompat, then you can use MvpAppCompatActivity and MvpAppCompatFragment. For this add moxy-app-compat module instead of moxy-android:
dependencies {
...
compile 'com.arello-mobile:moxy-app-compat:1.1.2'
}The simplest use of Moxy is as follows:
public interface HelloWorldView extends MvpView {
void showMessage(int message);
}This allow Presenter to know, what View can do.
@InjectViewState
public class HelloWorldPresenter extends MvpPresenter<HelloWorldView> {
public HelloWorldPresenter() {
getViewState().showMessage(R.string.hello_world);
}
}Here we determine that Presenter will be work withHelloWorldView. And that Presenter sends Command showMessage ASAP. Also, @InjectViewState will generate ViewState-class for HelloWorldView, will create new instance of this ViewState and will inject this instance to Presenter. And then you can don't worry about nullability of getViewState() – it's always not-null.
public class HelloWorldActivity extends MvpAppCompatActivity implements HelloWorldView {
@InjectPresenter
HelloWorldPresenter mHelloWorldPresenter;
private TextView mHelloWorldTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello_world);
mHelloWorldTextView = ((TextView) findViewById(R.id.activity_hello_world_text_view_message));
}
@Override
public void showMessage(int message) {
mHelloWorldTextView.setText(message);
}
}This HelloWorldActivity extends MvpAppCompatActivity. This allow to much simply inject Presenter to this Activity. Also, this removes from us liable for processing lifecycle callbacks. Next we inject HelloWorldPresenter to this Activity. We should not worry about instance of this Presenter. Regardless of Activity will be restarted, here will be injected same instance of HelloWorldPresenter.
Thats all! Next you should build and run project. You shouldn't worry about communications between MVP-components. Moxy do it. Moxy generate boilerplate code. Moxy will take care of lifecycle Android components. You should write only logic and UI. No more =)