This library is deprecated , its successor is MVP-Bakery.
This library offers a structure that helps build Android apps that follow MVP principles.
The core concept utilizes a Loader to store the Presenter during configuration (e.g. rotation) changes. This can be seamlessly combined with Dagger2's AndroidInjector.
The demo application shows how to use the library and includes examples on how to use unit and instrumentation tests with it.
Add the library:
implementation 'eu.darken.ommvplib:library:0.2.2'The Presenter and the View that our Activity will implement.
@MainComponent.Scope
public class MainPresenter extends Presenter<MainPresenter.View, MainComponent> {
@Inject
MainPresenter() {
}
@Override
public void onBindChange(@Nullable View view) {
super.onBindChange(view);
onView(v -> doSomething());
}
public interface View extends Presenter.View {
void doSomething();
}
}The process of attaching initialises the Loader, checks for any existing Presenter and creates one if necessary.
public class MainActivity extends AppCompatActivity implements MainPresenter.View {
MainPresenter presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
OMMVPLib.<MainPresenter.View, MainPresenter>builder()
.statePublisher(statePublisher)
.presenterCallback(new LoaderFactory.Callback<MainPresenter.View, MainPresenter>() {
@Override
public void onPresenterReady(MainPresenter presenter) {
MainActivity.this.presenter = presenter;
}
@Override
public void onPresenterDestroyed() {
}
})
.presenterSource(new PresenterSource<MainPresenter>() {
@Override
public MainPresenter create() {
return new MainPresenter();
}
})
.attach(this);
setContentView(R.layout.activity_main);
}
}The component for our injection.
@MainComponent.Scope
@Subcomponent(modules = {AndroidSupportInjectionModule.class})
public interface MainComponent extends ActivityComponent<MainActivity>, PresenterComponent<MainPresenter.View, MainPresenter> {
@Subcomponent.Builder
abstract class Builder extends ActivityComponent.Builder<MainActivity, MainComponent> {}
@javax.inject.Scope
@Retention(RetentionPolicy.RUNTIME)
@interface Scope {}
}The Presenter that will be injected, including the View that our Activity will implement.
@MainComponent.Scope
public class MainPresenter extends ComponentPresenter<MainPresenter.View, MainComponent> {
@Inject
MainPresenter() {
}
@Override
public void onBindChange(@Nullable View view) {
super.onBindChange(view);
onView(v -> doSomething());
}
public interface View extends Presenter.View {
void doSomething();
}
}Now we just need to attach the presenter.
public class MainActivity extends AppCompatActivity implements MainPresenter.View {
@Inject MainPresenter presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
OMMVPLib.<MainPresenter.View, MainPresenter>builder()
.statePublisher(statePublisher)
.presenterCallback(new PresenterInjectionCallback<>(this))
.presenterSource(new InjectedPresenter<>(this))
.attach(this);
setContentView(R.layout.activity_main);
}
}This library combines multiple concepts:
- tomorrow-mvp by Michał Łuszczuk
- toegether-mvp by Christian Langer
- activities-multibinding-in-dagger-2 by Miroslaw Stanek
- Dagger2-MultiBinding-Android-Example by Trevor Jones
- MCE3 Dagger 2 Talk by Gregory Kick
Check out @luszczuk's (author of tomorrow-mvp) comparison of Android MVP approaches.