One More MVP Lib.
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
example
gradle/wrapper
library
.gitignore
.travis.yml
LICENSE
README.md
build.gradle
gradle.properties
gradlew
gradlew.bat
publish-to-bintray.gradle
settings.gradle

README.md

This library is deprecated , its successor is MVP-Bakery.

One More MVP Lib

Download Coverage Status Build Status

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.

Quickstart

Add the library:

implementation 'eu.darken.ommvplib:library:0.2.2'

Without Dagger

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

With Dagger

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

Acknowledgements

This library combines multiple concepts:

Check out @luszczuk's (author of tomorrow-mvp) comparison of Android MVP approaches.