MVP-Bakery helps you structure your Android app to implement MVP principles and deals with retaining the presenter across configuration (e.g. rotation) changes.
It is the successor to OMMVPLib
MVP-Bakery uses ViewModel
to retain Presenter
.
Checkout the demo application!
Add the library:
implementation 'eu.darken.mvpbakery:library:<latest>'
The Presenter
and the View
that our Activity
will implement.
public class MainPresenter extends Presenter<MainPresenter.View> {
@Override
public void onBindChange(@Nullable View view) {
super.onBindChange(view);
onView(v -> doSomething());
}
public interface View extends Presenter.View {
void doSomething();
}
}
When MVP-Bakery is attach
ed it will handle all the lifecycle events for you.
public class MainActivity extends AppCompatActivity implements MainPresenter.View {
MainPresenter presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MVPBakery.<MainPresenter.View, MainPresenter>builder()
.addPresenterCallback(new PresenterRetainer.Callback<MainPresenter.View, MainPresenter>() {
@Override
public void onPresenterCreated(MainPresenter presenter) {
}
@Override
public void onPresenterDestroyed() {
}
})
.presenterFactory(new PresenterFactory<MainPresenter>() {
@Override
public MainPresenter createPresenter() {
return null;
}
})
.presenterRetainer(new ViewModelRetainer<>(this))
.attach(this);
setContentView(R.layout.activity_main);
}
}
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);
MVPBakery.<MainPresenter.View, MainPresenter>builder()
.addPresenterCallback(new PresenterInjectionCallback<>(this))
.presenterFactory(new InjectedPresenter<>(this))
.presenterRetainer(new ViewModelRetainer<>(this))
.attach(this);
setContentView(R.layout.activity_main);
}
}
The dagger component.
@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 {}
}
This library combines multiple concepts:
- ommvplib this libraries predecessor
- 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