Android MVP library with presenter retention via ViewModel and Dagger2 support.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.idea/codeStyles Library/Build updates. Oct 19, 2018
example Maintenance. Nov 30, 2018
gradle/wrapper Library/Build updates. Oct 19, 2018
library Typo fix Nov 30, 2018
.gitignore Initial commit Mar 3, 2018
.travis.yml Pr travisstuff (#1) Oct 19, 2018
LICENSE Initial commit Mar 3, 2018
README.md Pr travisstuff (#1) Oct 19, 2018
build.gradle Maintenance. Nov 30, 2018
gradle.properties Initial Mar 4, 2018
gradlew Travis update. Oct 19, 2018
gradlew.bat Initial Mar 4, 2018
publish-to-bintray.gradle Dependency updates. Apr 27, 2018
settings.gradle Initial Mar 4, 2018

README.md

MVP-Bakery

Download Coverage Status Build Status

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!

Quickstart

Add the library:

implementation 'eu.darken.mvpbakery:library:<latest>'

Without Dagger

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 attached 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);
    }
}

With Dagger

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 {}
}

Acknowledgements

This library combines multiple concepts: