Skip to content

jemshit/EliteMvp

Repository files navigation

Logo

Simple, Tiny, Extendable Android MVP library. No complex Base Activity, Base Fragment or Base View. Just base Presenters and View interface for you to extend from. For MVP Cheatsheet read this.

Installation

Step 1. Add JitPack repository to your top level build.gradle file

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

Step 2. Add library dependency to your module build.gradle file

dependencies {
    ...
    compile 'com.jemshit:EliteMvp:2.0.2'
}

Structure

There are 7 base Presenter classes to extend from and 1 base View interface.

  • EliteView: empty View interface. Your custom View interfaces must extend this.
  • EliteCorePresenter: Presenter interface with only 2 methods (attachView(V),detachView()). Your custom Presenter can implement this or you can use one of the ready Presenters listed below.
  • ElitePresenter: Simple Presenter with ready methods (getView(),isViewAttached(), onDestroy()) for holding View reference. Your custom Presenter can extend this and add additional methods.
  • EliteRx1Presenter: Simple Presenter that has 2 additional methods (addToOnAttachSubscriptions(s), addToOnCreateSubscriptions(S)) for adding RxJava 1 Subscriptions into CompositeSubscription.
  • EliteRx2Presenter: Simple Presenter that has 2 additional methods (addToOnAttachDisposables(D), addToOnCreateDisposables(D)) for adding RxJava 2 Disposables into CompositeDisposable.
  • EliteNullViewPresenter: Presenter which returns empty View after View is detached, so no need for view!=null check. Returned empty View methods does nothing when called, read more about Null Object Pattern here.
  • EliteNullViewRx1Presenter: Presenter which follows Null Object Pattern and has two CompositeSubscriptions for RxJava 1 Subscriptions.
  • EliteNullViewRx2Presenter: Presenter which follows Null Object Pattern and has two CompositeDisposables for RxJava 2 Disposables.

UML Diagram:

Uml

Usage

public interface YourActivityContract {
    interface View extends EliteView{
        void viewMethod();
    }

    abstract class Presenter extends ElitePresenter<YourActivityContract.View> {
        public abstract void presenterMethod();
    }
}
public class YourActivityPresenter extends YourActivityContract.Presenter{

    @Override public void presenterMethod() {
        if(isViewAttached())    
            getView().viewMethod();
    }
}
public class YourActivity extends AppCompatActivity implements YourActivityContract.View { {

    private YourActivityContract.Presenter presenter;

    @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_your);

        presenter = new YourActivityPresenter();
        presenter.attachView(this);
        presenter.presenterMethod();
    }

    @Override public void viewMethod(){...}

    @Override protected void onDestroy() {
        super.onDestroy();
        presenter.onDestroy();
    }
}

⭕ Check Samples for more examples.

⭕ For Orientation Change, Presenter Retaining, Caching... check applied solutions from community here