Skip to content

Add annotations for using model view presenter architecture pattern.

License

Notifications You must be signed in to change notification settings

nenick/androidannotations-mvp

Repository files navigation

AndroidAnnotations MVP Plugin

CircleCI branch Codacy grade Codacy branch coverage

Plugin provides annotations for Model-View-Presenter (MVP) pattern. This plugin follows the approach that activities and fragments are presenters.

Installation

Add AndroidAnnotations and MVP plugin to your android module.

repositories {
    maven {
        url "http://dl.bintray.com/nenick/maven"
    }
}

def AndroidAnnotations = 4.3.0
def AndroidAnnotationsMvp = 1.0.0 

deoendencies {
    apt "org.androidannotations:androidannotations:$AndroidAnnotations"
    compile "org.androidannotations:androidannotations:$AndroidAnnotations"
    
    apt "de.nenick:androidannotations-mvp:$AndroidAnnotationsMvp"
    compile "de.nenick:androidannotations-mvp-api:$AndroidAnnotationsMvp"
}

Usage

Enhance components

Base annotations to enable MVP plugin features.

Annotation Short Description
@EMvpPresenter Enhance activity or fragment class as presenter.
@EMvpView Enhance bean class as view.

Inject components

Annotation Short Description
@MvpActivity Inject new non static MVP presenter activity intent builder.
@MvpFragment Inject new MVP presenter fragment instance.
@MvpView Inject new MVP view instance.
@MvpCallback View Inject existing MVP presenter instance for MVP view.
@MvpCallback Presenter Inject existing parent MVP presenter instance for MVP presenter fragment.

Cookbook

ShowCase

Example how typically activity code may be separated into presenter and view ...

Typical activity class without MVP separation Presenter with MVP separation View with MVP separation
@EActivity(R.layout.activity-my.xml)
class MyActivity extends Activity {

    @ViewById(R.id.myEditText)
    EditText editText;

    @ViewById(R.id.myTextView)
    TextView textView;

    @ViewById(R.id.fragmentContainer)
    Layout container;

    @Click(R.id.myButton)
    onTextInput() {
        String text = editText.getText();
        // .. do some computing with text
        textView.setText(text)
    }

    @Click(R.id.myButtonNav)
    onNavigation() {
        MySecondActivity_.intent(this).start()
    }

    @AfterViews
    showFragment() {
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();   

        Fragment myFragment = new MyFragment();
        ft.add(container.getId(), myFragment , "my-fragment");
        ft.commit();
    }
}

@EMvpPresenter
@EActivity(R.layout.activity-my.xml)
class MyActivity extends Activity implements MyView.Callback {

    @MvpView
    MyView = view;

    @MvpActivity
    ActivityLauncher<MySecondActivity_.IntentBuilder_> mySecondActivity;

    @MvpFragment
    MyFragment fragment;

    @AfterViews
    void showFragment() {
        view.show(fragment);
    }

    @Override
    void onTextInput(String text) {
        // .. do some computing with text
        view.showComputingResult(text)
    }

    @Override
    void onNavigation() {
        mySecondActivity.intent(this).start();
    }
}

@EMvpView
@EBean
class MyView {

    interface Callback {
        void onTextInput(String text);
        void onNavigation();
        FragmentManager getFragmentManager();
    }

    @ViewById(R.id.myEditText)
    EditText editText;

    @ViewById(R.id.myTextView)
    TextView textView;

    @ViewById(R.id.fragmentContainer)
    Layout container;

    @MvpCallback
    Callback callback;

    @Click(R.id.myButton)
    void onMyButton() {
        callback.onTextInput(editText.getText());
    }

    void showComputingResult(String text) {
        textView.setText(text)
    }

    void show(Fragment fragment) {
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();   
        ft.add(container.getId(), fragment, "my-fragment");
        ft.commit();
    }
}

Support

For any issue or question please open an issue for support.

Contributing

Please contribute using Github Flow. Create a branch, add commits, and open a pull request.

Build Instructions

Build and run MVP plugin tests

  • from project root execute ./gradlew check
  • from project root execute ./gradlew connectedCheck (needs android device or emulator)

Release MVP plugin

  • update RELEASE_PLAN.md with new release information
  • from project root execute ./gradlew release -Prelease.version=X.X.X
  • after release is done, update README.md with latest project version number