-
Notifications
You must be signed in to change notification settings - Fork 34
Getting started
First, add the base Moxy module and the compiler module to your app/build.gradle dependencies block.
Please replace $moxyVersion with the latest version number:
dependencies {
// ...
implementation "com.github.moxy-community:moxy:$moxyVersion"
annotationProcessor "com.github.moxy-community:moxy-compiler:$moxyVersion"
}apply plugin: 'kotlin-kapt'
// ...
dependencies {
// ...
implementation "com.github.moxy-community:moxy:$moxyVersion"
kapt "com.github.moxy-community:moxy-compiler:$moxyVersion"
}Base Moxy module provides all basic components, such as MvpPresenter, MvpView, MvpDelegate and so on.
Compiler will be generate code:
- for
MvpPresentercreation and injection - of
ViewState - for injection
ViewStatetoPresenter
For additional base view classes MvpActivity and MvpFragment add this:
implementation "com.github.moxy-community:moxy-android:$moxyVersion"If you are using AppCompat, you'll need MvpAppCompatActivity and MvpAppCompatFragment classes. Add this:
implementation "com.github.moxy-community:moxy-app-compat:$moxyVersion"If you're using AndroidX, you'll need a different implementation for MvpAppCompatActivity and MvpAppCompatFragment classes. Use this one:
implementation "com.github.moxy-community:moxy-androidx:$moxyVersion"If you're using Google material, use MvpBottomSheetDialogFragment and add this:
implementation "com.github.moxy-community:moxy-material:$moxyVersion"Declare presenters in your views using property delegate:
class MyFragment: MvpFragment() {
...
private val presenter by moxyPresenter { presenterProvider.get() }
...
}Launch coroutines in presenter scope:
class MyPresenter : MvpPresenter<MvpView>() {
override fun onFirstViewAttach() {
presenterScope.launch {
// Coroutine that will be canceled when presenter is destroyed
}
}
}To use MvpDelegateHolder.moxyPresenter and MvpPresenter.presenterScope, add this:
implementation "com.github.moxy-community:moxy-ktx:$moxyVersion"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 =)