Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Fixing MVP with RxJava

Fixing MVP with RxJava

How I cleaned up my implementation of MVP with RxJava

Patrick Doyle

August 23, 2016
Tweet

Other Decks in Programming

Transcript

  1. What’s the problem? Current implementations of MVP tend to become

    really messy Fragile & confusing testing nightmare https://github.com/googlesamples/android-architecture
  2. MVP

  3. MVP

  4. MVP Circular dependency between presenter and view View <-> presenter

    interaction becomes a spider web with no end in sight
  5. RxMVP Treat the activity/fragment as a main method, not a

    view Create a standalone separate view that the system renders Use RxJava to implement observer pattern Treat system data events the same as app data events
  6. Why integrate RxJava? Common abstraction for observing events and thread

    management Fixes circular dependency between Model, View and Presenter with the observer pattern Removes some OOP in favour of reactive functional programming Easy to modularise for reuse Makes mocking events for testing very easy
  7. RxMVP - Activity/Fragment Bridge to android system Creates and manages

    Model, View and Presenter Acts as a data source for the model Provides context for view inflation Informs presenter of lifecycle events
  8. RxMVP - View Separated into a single composite view/pojo containing

    a composite view Instantiated by us or DI so we have control over constructor arguments Activity displays view using the setContentView() method after injection/instantiation No dependency on the presenter, emit events via Rx observables
  9. RxMVP - Model Proxy between data sources and the presenter

    Manages all data events from activity such as saved states, intent data, activity for result Simple logic, the odd map or a filter. Does not have complex data processing Can be split multiple classes, Data Model, Navigation, System
  10. Restore State after config change Observable (RxState) Save State for

    config changes (RxState) Activity as dependency System call (Android) Network call (retrofit) Activity as data source
  11. RxMVP - Presenter Uses Rx to glue views and models

    together. Completely “stateless”- contained by RxJava
  12. Multi-pane Reuse views across activities Can use a container view

    composed of views Wireframe relays data from one model to the other.
  13. Testing All components except views can be unit tested using

    mocks Events easily mocked using Observable.just(), Observable.never() etc Views tested using espresso and test data
  14. Lifecycle event Verify Mock model and view Observable.never() = No

    click event Test data Observable Test Rx Schedulers
  15. Rx Libraries RxState - Maps saving state to Observable allowing

    saving at any point in an Rx chain https://github.com/TwistedEquations/android-rxstate