Patrick64319 / mvp-bakery

Android MVP library with presenter retention via ViewModel and Dagger2 support.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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:

About

Android MVP library with presenter retention via ViewModel and Dagger2 support.

License:MIT License


Languages

Language:Kotlin 100.0%