architecture and rxjava

38
Architecture with RxJava Jolanda Verhoef

Upload: jolanda-verhoef

Post on 15-Apr-2017

1.183 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Architecture and RxJava

Architecture with RxJava

Jolanda Verhoef

Page 2: Architecture and RxJava

Blendle

Makes the world’s best

journalism available to

everyone, everywhere

@lojanda

Page 3: Architecture and RxJava

Blendle

Makes the world’s best

journalism available to

everyone, everywhere

@lojanda

Page 4: Architecture and RxJava

Architecture

@lojanda

Page 5: Architecture and RxJava

Mutable Data Stores

Context Stores

Mutable Data RepositoriesStatic Data

Repositories

Business Use Cases

Presenters

Custom Views Fragments Activities

@lojanda

Page 6: Architecture and RxJava

Reactive Programming

@lojanda

Page 7: Architecture and RxJava

Today’s subjects

Building standalone UI components

Building real standalone UI components

Synchronising UI components throughout the app

Updating lists after user interaction

@lojanda

Page 8: Architecture and RxJava

Today’s subjects

Building standalone UI components

Building real standalone UI components

Synchronising UI components throughout the app

Updating lists after user interaction

@lojanda

Page 9: Architecture and RxJava

Standalone UI componentsOpening the Blendle Reader

@lojanda

Page 10: Architecture and RxJava

Standalone UI componentsOpening the Blendle Reader

@lojanda

Page 11: Architecture and RxJava

Mutable Data Stores

Context Stores

Mutable Data RepositoriesStatic Data

Repositories

Business Use Cases

Presenters

Custom Views Fragments Activities

@lojanda

Page 12: Architecture and RxJava

ArticlePreviewRepositorypublic 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 ); } }

@lojandaMutable Data Stores

Context Stores

Mutable Data RepositoriesStatic Data

Repositories

Business Use Cases

Presenters

Custom Views Fragments Activities

Page 13: Architecture and RxJava

GetArticlePreviewUseCase

public Single<ArticlePreview> execute( String articleId ) { return articleRepository.articlePreview$( articleId ) .subscribeOn( Schedulers.io() ) .observeOn( AndroidSchedulers.mainThread() ); }

@lojandaMutable Data Stores

Context Stores

Mutable Data RepositoriesStatic Data

Repositories

Business Use Cases

Presenters

Custom Views Fragments Activities

Page 14: Architecture and RxJava

ReaderPresenter

public void init( String articleId ) { getArticlePreviewUseCase.execute( articleId ) .subscribe( getView()::showArticlePreview, getView()::loadArticlePreviewFailed ); }

@lojandaMutable Data Stores

Context Stores

Mutable Data RepositoriesStatic Data

Repositories

Business Use Cases

Presenters

Custom Views Fragments Activities

Page 15: Architecture and RxJava

Today’s subjects

Building standalone UI components

Building real standalone UI components

Synchronising UI components throughout the app

Updating lists after user interaction

@lojanda

Page 16: Architecture and RxJava

Standalone UI components++Inside the Blendle Reader

@lojanda

Page 17: Architecture and RxJava

@lojanda

ReaderFragment

Page 18: Architecture and RxJava

@lojanda

ReaderFragment

Toolbar

Featured Image

RecyclerView

Page 19: Architecture and RxJava

@lojanda

ReaderFragment

Toolbar

Featured Image

RecyclerView

Publisher Logo

Bookmark Icon

Share Icon

Text Row

Image Row

……. Row

Page 20: Architecture and RxJava

Mutable Data Stores

Context Stores

Mutable Data RepositoriesStatic Data

Repositories

Business Use Cases

Presenters

Custom Views Fragments Activities

@lojanda

Page 21: Architecture and RxJava

@lojanda

Page 22: Architecture and RxJava

ArticleContextStore

private BehaviorSubject<String> subject;

public void changeArticleContext( final String newArticleId ) { subject.onNext( newArticleId ); }

@lojandaMutable Data Stores

Context Stores

Mutable Data RepositoriesStatic Data

Repositories

Business Use Cases

Presenters

Custom Views Fragments Activities

Page 23: Architecture and RxJava

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 ); }

VIE

WP

RE

SEN

TE

R

Mutable Data Stores

Context Stores

Mutable Data RepositoriesStatic Data

Repositories

Business Use Cases

Presenters

Custom Views Fragments Activities

Page 24: Architecture and RxJava

Today’s subjects

Building standalone UI components

Building real standalone UI components

Synchronising UI components throughout the app

Updating lists after user interaction

@lojanda

Page 25: Architecture and RxJava

Synchronising UI componentsBookmarking an article

@lojanda

Page 26: Architecture and RxJava

Mutable Data Stores

Context Stores

Mutable Data RepositoriesStatic Data

Repositories

Business Use Cases

Presenters

Custom Views Fragments Activities

@lojanda

Page 27: Architecture and RxJava

Mutable Data Stores

Context Stores

Mutable Data RepositoriesStatic Data

Repositories

Business Use Cases

Presenters

Custom Views Fragments Activities

BookmarkStoreprivate 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

Page 28: Architecture and RxJava

BookmarkRepositorypublic Observable<Boolean> bookmarked$( String articleId ) { BehaviorSubject<Boolean> subject = bookmarkStore .getBookmarked( articleId );

if( ! subject.hasValue() ) { this.bookmarkedCall( articleId ) .subscribe( bookmarkStore::setBookmarked ); }

return subject; }

@lojandaMutable Data Stores

Context Stores

Mutable Data RepositoriesStatic Data

Repositories

Business Use Cases

Presenters

Custom Views Fragments Activities

Page 29: Architecture and RxJava

GetBookmarkUseCase

public Observable<Boolean> execute( String articleId ) { return bookmarkRepository.bookmarked$( articleId ) .subscribeOn( Schedulers.io() ) .observeOn( AndroidSchedulers.mainThread() ); }

@lojandaMutable Data Stores

Context Stores

Mutable Data RepositoriesStatic Data

Repositories

Business Use Cases

Presenters

Custom Views Fragments Activities

Page 30: Architecture and RxJava

BookmarkPresenter

public void init( String articleId ) { getBookmarkUseCase.execute( articleId ) .subscribe( getView()::showBookmarkValue ); }

@lojandaMutable Data Stores

Context Stores

Mutable Data RepositoriesStatic Data

Repositories

Business Use Cases

Presenters

Custom Views Fragments Activities

Page 31: Architecture and RxJava

Today’s subjects

Building standalone UI components

Building real standalone UI components

Synchronising UI components throughout the app

Updating lists after user interaction

@lojanda

Page 32: Architecture and RxJava

Updating listsArticle reading list

@lojanda

Page 33: Architecture and RxJava

Mutable Data Stores

Context Stores

Mutable Data RepositoriesStatic Data

Repositories

Business Use Cases

Presenters

Custom Views Fragments Activities

@lojanda

Page 34: Architecture and RxJava

@lojanda

Page 35: Architecture and RxJava

Mutable Data Stores

Context Stores

Mutable Data RepositoriesStatic Data

Repositories

Business Use Cases

Presenters

Custom Views Fragments Activities

BookmarkStoreprivate 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

Page 36: Architecture and RxJava

ReadingListPresenterpublic 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 ); } }

@lojandaMutable Data Stores

Context Stores

Mutable Data RepositoriesStatic Data

Repositories

Business Use Cases

Presenters

Custom Views Fragments Activities

Page 37: Architecture and RxJava

Today’s subjects

Building standalone UI components

Building real standalone UI components

Synchronising UI components throughout the app

Updating lists after user interaction

@lojanda

Page 38: Architecture and RxJava

Questions?Architecture https://8thlight.com/blog/uncle-bob/2012/08/13/the-clean-architecture.html

Static datahttp://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