Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Architecture and RxJava

1,288 views

Published on

Slides of the talk Jolanda Verhoef gave at the BABBQ Europe 2016.

Published in: Technology

Architecture and RxJava

  1. 1. Architecture with RxJava Jolanda Verhoef
  2. 2. Blendle Makes the world’s best journalism available to everyone, everywhere @lojanda
  3. 3. Blendle Makes the world’s best journalism available to everyone, everywhere @lojanda
  4. 4. Architecture @lojanda
  5. 5. Mutable Data Stores Context Stores Mutable Data Repositories Static Data Repositories Business Use Cases Presenters Custom Views Fragments Activities @lojanda
  6. 6. Reactive Programming @lojanda
  7. 7. Today’s subjects Building standalone UI components Building real standalone UI components Synchronising UI components throughout the app Updating lists after user interaction @lojanda
  8. 8. Today’s subjects Building standalone UI components Building real standalone UI components Synchronising UI components throughout the app Updating lists after user interaction @lojanda
  9. 9. Standalone UI components Opening the Blendle Reader @lojanda
  10. 10. Standalone UI components Opening the Blendle Reader @lojanda
  11. 11. Mutable Data Stores Context Stores Mutable Data Repositories Static Data Repositories Business Use Cases Presenters Custom Views Fragments Activities @lojanda
  12. 12. ArticlePreviewRepository public Single<ArticlePreview> articlePreview$( String articleId ) { if ( cache.contains( articleId ) { return Single.just( cache.retrieve( articleId )); } else { return urlResolver.urlFor( articleId ) .flatMap( blendleApi::articlePreview$ ) .doOnSuccess( cache::store ); } } @lojanda Mutable Data Stores Context Stores Mutable Data Repositories Static Data Repositories Business Use Cases Presenters Custom Views Fragments Activities
  13. 13. GetArticlePreviewUseCase public Single<ArticlePreview> execute( String articleId ) { return articleRepository.articlePreview$( articleId ) .subscribeOn( Schedulers.io() ) .observeOn( AndroidSchedulers.mainThread() ); } @lojanda Mutable Data Stores Context Stores Mutable Data Repositories Static Data Repositories Business Use Cases Presenters Custom Views Fragments Activities
  14. 14. ReaderPresenter public void init( String articleId ) { getArticlePreviewUseCase.execute( articleId ) .subscribe( getView()::showArticlePreview, getView()::loadArticlePreviewFailed ); } @lojanda Mutable Data Stores Context Stores Mutable Data Repositories Static Data Repositories Business Use Cases Presenters Custom Views Fragments Activities
  15. 15. Today’s subjects Building standalone UI components Building real standalone UI components Synchronising UI components throughout the app Updating lists after user interaction @lojanda
  16. 16. Standalone UI components++ Inside the Blendle Reader @lojanda
  17. 17. @lojanda ReaderFragment
  18. 18. @lojanda ReaderFragment Toolbar Featured Image RecyclerView
  19. 19. @lojanda ReaderFragment Toolbar Featured Image RecyclerView Publisher Logo Bookmark Icon Share Icon Text Row Image Row ……. Row
  20. 20. Mutable Data Stores Context Stores Mutable Data Repositories Static Data Repositories Business Use Cases Presenters Custom Views Fragments Activities @lojanda
  21. 21. @lojanda
  22. 22. ArticleContextStore private BehaviorSubject<String> subject; public void changeArticleContext( final String newArticleId ) { subject.onNext( newArticleId ); } @lojanda Mutable Data Stores Context Stores Mutable Data Repositories Static Data Repositories Business Use Cases Presenters Custom Views Fragments Activities
  23. 23. Reader @Override public View onCreateView( … ) { presenter.init( getArguments() != null ? getArguments().getInt( EXTRA_ARTICLE_ID ) : null ); } @lojanda public void init( @Nullable String articleId ) { if( articleId == null) articleId = articleContextStore.getArticleContext(); loadData( articleId ); } VIEWPRESENTER Mutable Data Stores Context Stores Mutable Data Repositories Static Data Repositories Business Use Cases Presenters Custom Views Fragments Activities
  24. 24. Today’s subjects Building standalone UI components Building real standalone UI components Synchronising UI components throughout the app Updating lists after user interaction @lojanda
  25. 25. Synchronising UI components Bookmarking an article @lojanda
  26. 26. Mutable Data Stores Context Stores Mutable Data Repositories Static Data Repositories Business Use Cases Presenters Custom Views Fragments Activities @lojanda
  27. 27. Mutable Data Stores Context Stores Mutable Data Repositories Static Data Repositories Business Use Cases Presenters Custom Views Fragments Activities BookmarkStore private Map<String, BehaviorSubject<Boolean>> subjectMap; public BehaviorSubject<Boolean> getBookmarked( String articleId ) { if( subjectMap.contains( articleId ) return subjectMap.get( articleId ); BehaviorSubject<Boolean> subject = BehaviorSubject.create(); subjectMap.put( articleId, subject ); return subject; } public void setBookmarked( String articleId, Boolean bookmarked ) { getBookmarked().onNext( bookmarked ); } @lojanda
  28. 28. BookmarkRepository public Observable<Boolean> bookmarked$( String articleId ) { BehaviorSubject<Boolean> subject = bookmarkStore .getBookmarked( articleId ); if( ! subject.hasValue() ) { this.bookmarkedCall( articleId ) .subscribe( bookmarkStore::setBookmarked ); } return subject; } @lojanda Mutable Data Stores Context Stores Mutable Data Repositories Static Data Repositories Business Use Cases Presenters Custom Views Fragments Activities
  29. 29. GetBookmarkUseCase public Observable<Boolean> execute( String articleId ) { return bookmarkRepository.bookmarked$( articleId ) .subscribeOn( Schedulers.io() ) .observeOn( AndroidSchedulers.mainThread() ); } @lojanda Mutable Data Stores Context Stores Mutable Data Repositories Static Data Repositories Business Use Cases Presenters Custom Views Fragments Activities
  30. 30. BookmarkPresenter public void init( String articleId ) { getBookmarkUseCase.execute( articleId ) .subscribe( getView()::showBookmarkValue ); } @lojanda Mutable Data Stores Context Stores Mutable Data Repositories Static Data Repositories Business Use Cases Presenters Custom Views Fragments Activities
  31. 31. Today’s subjects Building standalone UI components Building real standalone UI components Synchronising UI components throughout the app Updating lists after user interaction @lojanda
  32. 32. Updating lists Article reading list @lojanda
  33. 33. Mutable Data Stores Context Stores Mutable Data Repositories Static Data Repositories Business Use Cases Presenters Custom Views Fragments Activities @lojanda
  34. 34. @lojanda
  35. 35. Mutable Data Stores Context Stores Mutable Data Repositories Static Data Repositories Business Use Cases Presenters Custom Views Fragments Activities BookmarkStore private PublishSubject<Pair<String, Boolean>> subject; public Observable<Boolean> getBookmarkListener() { return subject; } public void setBookmarked( String articleId, Boolean bookmarked ) { getBookmarked( articleId ).onNext( bookmarked ); subject.onNext( new Pair<>( articleId, bookmarked ); } @lojanda
  36. 36. ReadingListPresenter public void init() { bookmarkStore.getBookmarkListener() .subscribe( this::bookmarkChanged ); } public void bookmarkChanged( Pair<String, Boolean> pair ) { String articleId = pair.first; Boolean newBookmarkValue = pair.second; if( newBookmarkValue ) { getView().addBookmark( articleId ); } else { getView().removeBookmark( articleId ); } } @lojanda Mutable Data Stores Context Stores Mutable Data Repositories Static Data Repositories Business Use Cases Presenters Custom Views Fragments Activities
  37. 37. Today’s subjects Building standalone UI components Building real standalone UI components Synchronising UI components throughout the app Updating lists after user interaction @lojanda
  38. 38. Questions? Architecture https://8thlight.com/blog/uncle-bob/2012/08/13/the-clean-architecture.html Static data
 http://blog.danlew.net/2015/06/22/loading-data-from-multiple-sources-with-rxjava/ Mutable data http://reactivex.io/RxJava/javadoc/rx/subjects/BehaviorSubject.html http://reactivex.io/RxJava/javadoc/rx/subjects/PublishSubject.html https://lorentzos.com/rxjava-as-event-bus-the-right-way-10a36bdd49ba @lojanda

×