⚠️ Deprecated ⚠️
Deprecated! Use Architecture Components
RainbowMVP
Lightweight MVP library(for personal using) with easy lifecycle.
For good understanding approach read this article.
- Really lighweight library
- Easy integrate to project
- Minimum actions to implement MVP
- Doesn't destroy presenter after rotation device
- You can cached data in presenter for restore data after rotate device
In presenter you have 3 methods:
- bindView(V view) - you need call this method for attach your view to presenter.
- unbindView() - you need call this method when view not available already.
- onDestoy() - call, when your presenter will destroy.
Important thing: You can bind your view to presenter ONLY after onStart() in Activity, and after onResume() in Fragment.
Dependency
Step 1. Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
Step 2. Add the dependency
dependencies {
implementation 'com.github.ne1c:rainbowmvp:1.2.4'
annotationProcessor 'com.github.ne1c.rainbowmvp:processor:1.2.4'
}
Step 1
Create you View interface:
public interface MyView {
void showResult(String text);
void showError(@StringRes int resId);
}
Step 2
Create presenter. You need inherit of BasePresenter and add tag:
public class MyPresenter extends BasePresenter<MyView> {
public static final TAG = "my_presenter";
...
public void makeParty() {
// some actions
boolean success = ...
if (success) {
getView().showResult(...);
} else {
getView().showError(R.string.error);
}
}
...
}
Step 3
- Implement PresenterStorage:
public class MyPresenterStorage implements PresenterStorage {
public MyPresenterStorage(...) {
...
}
@Override
public BasePresenter create(String tag) {
if (tag.equals(MyPresenter.TAG)) {
return new MyPresenter(...);
}
return null;
}
}
Step 4
- Init PresenterFactory, before use any presenter. You can do it in onCreate() of Applicaton or splash screen:
public class MyApplication extends android.app.Application {
@Override
public void onCreate() {
...
PresenterFactory.init(new MyPresenterStorage(...));
}
}
Step 5
Your activity or fragment need to inherit of BaseActivity/BaseFragment and override getPresenterTag():
@PresenterTag(MyPresenter.TAG)
public class MyActivity extends BaseActivity<MyPresenter> implements MyView {
...
@Ovveride
public void onStart() {
super.onStart();
getPresenter().bindView(this);
}
@Ovveride
public void onStop() {
super.onStop();
getPresenter().unbindView();
}
...
}
ViewState
You can use ViewState for saving state of view, and than restore after bindView(...). For it action you need implement ViewStateListener in your presenter or another place. Method stateChanged(...) will call after every change of ViewState or after call method bindView(...). Example how it works:
public class MyPresenter extends BasePresenter<MyView> implements ViewStateListener {
public MyPresenter(...) {
...
addViewStateListener(this);
}
...
public void makeParty() {
setViewState(ViewState.IN_PROGRESS);
getApi().loadData(response -> {
if (response.isSuccess()) {
setViewState(ViewState.SUCCESS);
if (getView() != null) {
getView().showResult(response.getData());
}
} else if (response.isError()) {
setViewState(ViewState.ERROR);
if (getView() != null) {
getView().showErrorLoadData();
}
}
});
}
@Override
public void stateChanged(ViewState state) {
if (state == ViewState.IN_PROGRESS) {
getView().showProgress();
}
if (state == ViewState.SUCCESS) {
getView().showRepos(mCachedData);
getView().hideProgress();
}
if (state == ViewState.ERROR) {
getView().showErrorLoadData()
}
setViewState(ViewState.NOTHING);
}
...
}