Reactive Architecture Boston Droidcon

16d15e8b2767f7da4c2147f61e7df0bd?s=47 HIFILEO
March 27, 2018

Reactive Architecture Boston Droidcon

Presentation given at Boston Droidcon about Reactive Architecture.

16d15e8b2767f7da4c2147f61e7df0bd?s=128

HIFILEO

March 27, 2018
Tweet

Transcript

  1. Reactive Architecture Daniel Leonardis @HIFILEO https://github.com/HIFILEO https://medium.com/@dan.leonardis

  2. None
  3. Kudos • Managing State with RxJava - Jack Wharton •

    The Rx Workflow Problem - Ray Ryan • Reactive Clean Architecture - Lucia Payo • Don’t Break The Chain - Dan Lew
  4. Goal • Define it • History Lesson • Rx Problems

    & Solution • Think in Images, Dream in Code
  5. Maximum Exposure

  6. Reactive Architecture is simple but not easy.

  7. Reactive Architecture • Responsive • Resilient • Elastic • Message

    Driven
  8. Reactive Architecture

  9. History Lesson

  10. History Lesson

  11. History Lesson • Death of MVP

  12. History Lesson

  13. History Lesson Clear Winner, MVVM!

  14. MVVM Rx Problems Well… not quite

  15. MVVM Rx Problem Unless you can model your entire system

    synchronously, a single asynchronous source breaks imperative programming. Jake Wharton - Managing State with Rx
  16. MVVM Rx Problem .doOnNext(loadMoreCommand -> { loadMoreEnabledBehaviorSubject.onNext(false); if (loadMoreCommand.getLoadMoreType() ==

    SCROLL) { adapterDataPublishSubject.onNext(AdapterUiCommand.createAddNull()); movieViewInfoList.add(null); } } }) .observeOn(Schedulers.computation()) .flatMap(loadMoreCommand -> serviceGateway.getNowPlaying(++pageNumber)) .delay(sleepSeconds, TimeUnit.SECONDS) .flatMap(new NowPlayingViewModel.MovieListFetcher()) .flatMap(new TranslateForUiFunction()) .doOnError(throwable -> pageNumber--;) .observeOn(AndroidSchedulers.mainThread()) .doOnNext(movieViewInfos -> { if (!firstLoad.get()) { movieViewInfoList.remove(movieViewInfoList.size() - 1); adapterDataPublishSubject.onNext(AdapterUiCommand.createRemoveNull()); } }) .onErrorResumeNext(movieViewInfos -> { showErrorPublishSubject.onNext(true); loadMorePublishSubject.onNext(new LoadMoreCommand(SCROLL)); }) .subscribe(movieViewInfos -> { movieViewInfoList.addAll(movieViewInfos); adapterDataPublishSubject.onNext( new AdapterUiCommand(AdapterUiCommand.CommandType.ADD, movieViewInfos)); //address first load //… //enable scroll }
  17. MVVM Rx Problem < >

  18. MVVM Rx Problem < >

  19. Reactive Architecture

  20. Reactive Architecture < > Observable<UiEvent>

  21. Spinner Button Click Reactive Architecture RxBinding SubmitEvent LoadMoreEvent FilterEvent Scroll

    }Observable.merge<UiEvent>
  22. Reactive Architecture < > Observable<UiEvent> Observable<Action>

  23. Reactive Architecture

  24. Reactive Architecture

  25. Reactive Architecture < > Observable<UiEvent> Observable<Action>

  26. Reactive Architecture Observable<UiEvent> TestObserver<UiEvent> Subject<?>

  27. Reactive Architecture Observable<Action> Observable.Just<Action> TestObserver<?>

  28. Reactive Architecture < > Observable<UiEvent> Observable<Action>

  29. Reactive Architecture Observable<Action> Observable<UiEvent> Transformers Dan Lew - Don't break

    the chain: use RxJava's compose() operator ?
  30. AutoCompleteEvent } Observable.merge<Action> Observable<Action> AutoCompleteAction SubmitAction Reactive Architecture Observable<UiEvent> {

    Observable.publish() SubmitEvent
  31. Reactive Architecture < > Observable<UiEvent> Observable<Action> Observable<Result>

  32. Reactive Architecture Observable<Result> Observable<Action> Transformers

  33. } Observable.merge<Result> Observable<Result> Reactive Architecture Observable<Action> { Observable.publish() SubmitAction FilterAction

    Data Set Data Set SubmitResult FilterResult
  34. Reactive Architecture < > Observable<UiEvent> Observable<Action> Observable<Result> Observable<UiModel>

  35. Reactive Architecture

  36. Reactive Architecture Observable<Result> Observable<UiModel> Observable.scan() ?

  37. Observable<UiModel> Reactive Architecture Observable<Result> Observable.scan(initialState, Function())

  38. Reactive Architecture < > Observable<UiEvent> Observable<Action> Observable<Result> Observable<UiModel> WEB =>

    REDUX
  39. Reactive Architecture ViewModel View View Controller Controller <I> Controller Impl

    Entity Entity API
  40. Reactive Architecture ViewModel View View Controller Controller <I> Controller Impl

    Entity Entity API Interactor
  41. Deep Dive

  42. Deep Dive • As a user, show a list of

    "Now Playing" movies. Poster, Title, Release Date, & Rating. • As a user, any rating at eight or above should be stared. • As a user, I only want to see ratings rounded to the nearest whole digit. • As a user, while scrolling, keep loading "Now Playing" movies until you hit the last page. • Example #1
  43. Deep Dive

  44. Deeper Dive • New requirement • As a user, restore

    the last page I was on when the "Now Playing" screen restarts. • Example #2
  45. Deeper Dive

  46. Deeper Dive

  47. Deeper Dive

  48. We Need To Go Deeper • New Requirement • As

    a user, I want to filter my results based on Rating. • Note - Filter at any moment.
  49. We Need To Go Deeper }Observable.merge<UiEvent> ScrollListener FilterSpinner FilterEvent LoadMoreEvent

  50. }Observable.merge<UiEvent> We Need To Go Deeper

  51. Observable.merge<UiEvent> We Need To Go Deeper Action

  52. We Need To Go Deeper { Action Observable.Publish

  53. We Need To Go Deeper { Action Observable.Publish FilterAction LoadMoreResult

  54. We Need To Go Deeper FilterAction LoadMoreResult }Observable.merge<Object>

  55. We Need To Go Deeper }Observable.merge<Object> { Concat + Publish

  56. { We Need To Go Deeper Concat + Publish Filter

    Result Filter On / Off }Observable.merge<Result>
  57. We Need To Go Deeper Result Observable.scan() Observable<UiModel>

  58. We Need To Go Deeper • So let’s look at

    code for the: New Requirement • Example #3
  59. Summary • Read, Watch, Repeat… • Remember (Like Redux) •

    Save State using UiModel • Don’t break the stream • Do you need a subject? • Speed Increase + Quality Increase
  60. Summary