Skip to content

Getting started

Alexey Ershov edited this page Jan 30, 2020 · 12 revisions

Getting Moxy

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: Bintray

Base modules integration:

Java

dependencies {
  // ...
  implementation "com.github.moxy-community:moxy:$moxyVersion"
  annotationProcessor "com.github.moxy-community:moxy-compiler:$moxyVersion"
}

Kotlin

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 MvpPresenter creation and injection
  • of ViewState
  • for injection ViewState to Presenter

Default android module

For additional base view classes MvpActivity and MvpFragment add this:

implementation "com.github.moxy-community:moxy-android:$moxyVersion"

AppCompat module

If you are using AppCompat, you'll need MvpAppCompatActivity and MvpAppCompatFragment classes. Add this:

implementation "com.github.moxy-community:moxy-app-compat:$moxyVersion"

AndroidX module

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"

AndroidX (Google material) module

If you're using Google material, use MvpBottomSheetDialogFragment and add this:

implementation "com.github.moxy-community:moxy-material:$moxyVersion"

Kotlin extensions

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"

Hello world

The simplest use of Moxy is as follows:

Create View interface

public interface HelloWorldView extends MvpView {
	void showMessage(int message);
}

This allow Presenter to know, what View can do.

Create Presenter

@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.

Implementation of View

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.

Run

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 =)

Clone this wiki locally