Patrick64319 / ommvplib

One More MVP Lib.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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.

About

One More MVP Lib.

License:MIT License


Languages

Language:Java 100.0%