Git Product home page Git Product logo

mvp-bakery's Introduction

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:0.4.0'

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:

mvp-bakery's People

Contributors

d4rken avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.