Slide 1

Slide 1 text

Let it Flow! Unidirectional data flow architecture on Android

Slide 2

Slide 2 text

@Dorvaryn +BenjaminAugustin-Dorvaryn Dorvaryn Benjamin Augustin Android Software Craftsman

Slide 3

Slide 3 text

What is Unidirectional Data Flow ?

Slide 4

Slide 4 text

● Remove state in UI ● Push based ● UI reactive Unidirectional Data Flow ?

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

● Apps get more complex ● Predictability ● Forces us to do things right What’s the point ?

Slide 7

Slide 7 text

Let’s build an app

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

Let’s start with getting data

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

You can use any language

Slide 19

Slide 19 text

Kotlin Syntax fun aFunction(parameter: String): Int { return parameter.length } var thisCanBeNull: Int? = null val lambda = { x: Int, y: Int -> x + y }

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

Example of Views interface CatsView { fun attach(listener: CatsPresenter.CatClickedListener) fun display(cats: Cats) fun display(favouriteCats: FavouriteCats) }

Slide 22

Slide 22 text

Example of Views interface LoadingView { fun attach(retryListener: RetryClickedListener ) fun showLoadingIndicator() fun showLoadingScreen() fun showData() fun showEmptyScreen() fun showErrorIndicator() fun showErrorScreen() }

Slide 23

Slide 23 text

Service interface CatService { fun getCat(id: Int): Observable }

Slide 24

Slide 24 text

fun startPresenting() { catsView.attach(catClickedListener) loadingView.attach(retryListener) loadingView.showLoadingScreen() subscriptions = catsService.getCats() .subscribe(catsObserver) } What does not belong here ?

Slide 25

Slide 25 text

fun startPresenting() { catsView.attach(catClickedListener) loadingView.attach(retryListener) loadingView.showLoadingScreen() subscriptions = catsService.getCats() .subscribe(catsObserver) } Loading when we request data

Slide 26

Slide 26 text

private val catsObserver = object : Observer { var displayedCats: Cats? = null override fun onNext(cats: Cats) { catsView.display(cats); displayedCats = cats loadingView.showLoadingIndicator () } override fun onError(e: Throwable?) { if (displayedCats == null) { loadingView.showErrorScreen () } else { loadingView.showErrorIndicator () } } override fun onCompleted() { if (displayedCats == null) { loadingView.showEmptyScreen () } else { loadingView.showData() } } What is the problem ?

Slide 27

Slide 27 text

private val catsObserver = object : Observer { var displayedCats: Cats? = null override fun onNext(cats: Cats) { catsView.display(cats); displayedCats = cats loadingView.showLoadingIndicator () } override fun onError(e: Throwable?) { if (displayedCats == null) { loadingView.showErrorScreen () } else { loadingView.showErrorIndicator () } } override fun onCompleted() { if (displayedCats == null) { loadingView.showEmptyScreen () } else { loadingView.showData() } } Presenter now has state

Slide 28

Slide 28 text

val retryListener = object : RetryClickedListener { override fun onRetry() { stopPresenting() startPresenting() } } What is wrong here ?

Slide 29

Slide 29 text

val retryListener = object : RetryClickedListener { override fun onRetry() { stopPresenting() startPresenting() } } Retry restarting state

Slide 30

Slide 30 text

class PersistedCatsService(...) : CatsService { override fun getCats() = repository.readCats() .flatMap { updateFromRemoteIfOutdated(it) } .switchIfEmpty(fetchRemoteCats()) } Service is request response based

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

data class Event ( val status: Status, val data: T?, val error: Throwable? ) enum class Status { LOADING, IDLE, ERROR } Modeling context

Slide 33

Slide 33 text

abstract class EventObserver: DataObserver> { override fun onNext(p0: Event) { when (p0.status) { Status.LOADING -> onLoading(p0) Status.IDLE -> onIdle(p0) Status.ERROR -> onError(p0) } } abstract fun onLoading(event: Event); abstract fun onIdle(event: Event); abstract fun onError(event: Event); Bit of tooling

Slide 34

Slide 34 text

fun startPresenting() { catsView.attach(catClickedListener) loadingView.attach(retryListener) subscriptions = catsService.getCats() .subscribe(catsObserver) } No more state based actions in Presenter

Slide 35

Slide 35 text

private val catsObserver = object : DataObserver { override fun onNext(p0: Cats) { catsView.display(p0); } } Displaying data is isolated

Slide 36

Slide 36 text

private val catsEventsObserver = object : EventObserver () { override fun onLoading(event: Event) { if (event.data != null) { loadingView.showLoadingIndicator () } else { loadingView.showLoadingScreen () } } override fun onIdle(event: Event) { if (event.data != null) { loadingView.showData() } else { loadingView.showEmptyScreen () } } override fun onError(event: Event) { if (event.data != null) { loadingView.showErrorIndicator () } else { loadingView.showErrorScreen () } } Displaying data is isolated

Slide 37

Slide 37 text

private val catsEventsObserver = object : EventObserver () { override fun onLoading(event: Event) { if (event.data != null) { loadingView.showLoadingIndicator () } else { loadingView.showLoadingScreen () } } override fun onIdle(event: Event) { if (event.data != null) { loadingView.showData() } else { loadingView.showEmptyScreen () } } override fun onError(event: Event) { if (event.data != null) { loadingView.showErrorIndicator () } else { loadingView.showErrorScreen () } } No state in Presenter

Slide 38

Slide 38 text

val retryListener = object : RetryClickedListener { override fun onRetry() { catsService.refreshCats() } } Retry is now an Action

Slide 39

Slide 39 text

class PersistedCatsService(...) : CatsService { val catsSubject = BehaviorSubject.create( Event(Status.IDLE, null, null) ) override fun getCatsEvents(): Observable> { return catsSubject.asObservable() .startWith(initialiseSubject()) .distinctUntilChanged() } } Service with events

Slide 40

Slide 40 text

class PersistedCatsService(...) : CatsService { private fun initialiseSubject(): Observable> { if (isInitialised(catsSubject)) { return Observable.empty() } return repository.readCats() .flatMap { updateFromRemoteIfOutdated (it) } .switchIfEmpty(fetchRemoteCats()) .compose(asEvent()) .doOnNext { catsSubject.onNext(it) } } } Service with events

Slide 41

Slide 41 text

class PersistedCatsService(...) : CatsService { private fun getCats(): Observable> { return getCatsEvents().compose(asData()) } } Data flow comes from events

Slide 42

Slide 42 text

class PersistedCatsService(...) : CatsService { override fun refreshCats() { fetchRemoteCats() .compose(asEvent()) .subscribe { catsSubject.onNext(it) } } } Refresh is now an Action

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

● Stateless UI ● Domain is empowered ● Background push compatible What did we gain ?

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

data class FavouriteCats( val favourites: Map ) { … } Modeling context, again

Slide 52

Slide 52 text

enum class FavouriteState { FAVOURITE, PENDING_FAVOURITE, UN_FAVOURITE, PENDING_UN_FAVOURITE } Modeling context, again

Slide 53

Slide 53 text

fun startPresenting() { catsView.attach(catClickedListener) loadingView.attach(retryListener) subscriptions.add( catsService.getCatsEvents() .subscribe(catsEventsObserver) ) subscriptions.add( catsService.getCats() .subscribe(catsObserver) ) subscriptions.add( favouriteCatsService.getFavouriteCats () .subscribe(favouriteCatsObserver) ) } UI just registers itself

Slide 54

Slide 54 text

fun startPresenting() { catsView.attach(catClickedListener) loadingView.attach(retryListener) subscriptions.add( catsService.getCatsEvents() .subscribe(catsEventsObserver) ) subscriptions.add( catsService.getCats() .subscribe(catsObserver) ) subscriptions.add( favouriteCatsService.getFavouriteCats() .subscribe( favouriteCatsObserver) ) } UI just registers itself

Slide 55

Slide 55 text

private val favCatsObserver = object : DataObserver { override fun onNext(favouriteCats: FavouriteCats) { catsView.display(favouriteCats) } } Displaying data is isolated

Slide 56

Slide 56 text

fun display(cat: Cat, favouriteState : FavouriteState , ...) { ... favouriteIndicator.setImageDrawable (favouriteDrawable (favouriteState )) favouriteIndicator.isEnabled = favouriteState == FavouriteState .FAVOURITE || favouriteState == FavouriteState .UN_FAVOURITE ... } private fun favouriteDrawable(favouriteState : FavouriteState ) = when (favouriteState ) { FavouriteState .FAVOURITE -> android.R.drawable.star_big_on FavouriteState .PENDING_FAVOURITE -> android.R.drawable.star_big_on FavouriteState .PENDING_UN_FAVOURITE -> android.R.drawable.star_big_off FavouriteState .UN_FAVOURITE -> android.R.drawable.star_big_off } View Reacts

Slide 57

Slide 57 text

override fun onFavouriteClicked(cat: Cat, currentState: FavouriteState) { if (currentState == FavouriteState.FAVOURITE) { favouriteCatsService.removeFromFavourite(cat) } else if (currentState == FavouriteState.UN_FAVOURITE) { favouriteCatsService.addToFavourite(cat) } } Modification are actions

Slide 58

Slide 58 text

class PersistedFavouriteCatsService (...): FavouriteCatsService { val favouriteCatsSubject: = BehaviorSubject .create( Event(Status.IDLE, null, null) ) override fun getFavouriteCatsEvents(): Observable> { return favouriteCatsSubject.asObservable() .startWith(initialiseSubject ()) .distinctUntilChanged () } } Similar structure in the Service

Slide 59

Slide 59 text

override fun addToFavourite(cat: Cat) { api.addToFavourite (cat) .map { Pair(cat, FavouriteState .FAVOURITE) } .onErrorReturn { Pair(cat, FavouriteState .UN_FAVOURITE) } .startWith(Pair(cat, FavouriteState .PENDING_FAVOURITE)) .doOnNext { repository.saveCatFavoriteStatus (it) } .subscribe { val value = favouriteCatsSubject.value val favouriteCats = value.data ?: FavouriteCats(mapOf()) favouriteCatsSubject.onNext(Event.of(favouriteCats.put(it))) } } Modification are actions

Slide 60

Slide 60 text

override fun addToFavourite(cat: Cat) { api.addToFavourite (cat) .map { Pair(cat, FavouriteState. FAVOURITE) } .onErrorReturn { Pair(cat, FavouriteState .UN_FAVOURITE) } .startWith(Pair(cat, FavouriteState .PENDING_FAVOURITE)) .doOnNext { repository.saveCatFavoriteStatus (it) } .subscribe { val value = favouriteCatsSubject.value val favouriteCats = value.data ?: FavouriteCats(mapOf()) favouriteCatsSubject.onNext(Event.of(favouriteCats.put(it))) } } Modification are actions

Slide 61

Slide 61 text

override fun addToFavourite(cat: Cat) { api.addToFavourite (cat) .map { Pair(cat, FavouriteState .FAVOURITE) } .onErrorReturn { Pair(cat, FavouriteState. UN_FAVOURITE) } .startWith(Pair(cat, FavouriteState .PENDING_FAVOURITE)) .doOnNext { repository.saveCatFavoriteStatus (it) } .subscribe { val value = favouriteCatsSubject.value val favouriteCats = value.data ?: FavouriteCats(mapOf()) favouriteCatsSubject.onNext(Event.of(favouriteCats.put(it))) } } Modification are actions

Slide 62

Slide 62 text

override fun addToFavourite(cat: Cat) { api.addToFavourite (cat) .map { Pair(cat, FavouriteState .FAVOURITE) } .onErrorReturn { Pair(cat, FavouriteState .UN_FAVOURITE) } .startWith(Pair(cat, FavouriteState. PENDING_FAVOURITE)) .doOnNext { repository.saveCatFavoriteStatus (it) } .subscribe { val value = favouriteCatsSubject.value val favouriteCats = value.data ?: FavouriteCats(mapOf()) favouriteCatsSubject.onNext(Event.of(favouriteCats.put(it))) } } Modification are actions

Slide 63

Slide 63 text

override fun addToFavourite(cat: Cat) { api.addToFavourite (cat) .map { Pair(cat, FavouriteState .FAVOURITE) } .onErrorReturn { Pair(cat, FavouriteState .UN_FAVOURITE) } .startWith(Pair(cat, FavouriteState .PENDING_FAVOURITE)) .doOnNext { repository.saveCatFavoriteStatus( it) } .subscribe { val value = favouriteCatsSubject.value val favouriteCats = value.data ?: FavouriteCats(mapOf()) favouriteCatsSubject.onNext(Event.of(favouriteCats.put(it))) } } Modification are actions

Slide 64

Slide 64 text

override fun addToFavourite(cat: Cat) { api.addToFavourite (cat) .map { Pair(cat, FavouriteState .FAVOURITE) } .onErrorReturn { Pair(cat, FavouriteState .UN_FAVOURITE) } .startWith(Pair(cat, FavouriteState .PENDING_FAVOURITE)) .doOnNext { repository.saveCatFavoriteStatus (it) } .subscribe { val value = favouriteCatsSubject.value val favouriteCats = value.data ?: FavouriteCats(mapOf()) favouriteCatsSubject.onNext(Event.of(favouriteCats.put(it))) } } Modification are actions

Slide 65

Slide 65 text

● Stateless UI ● Domain is empowered ● Eventual consistency is now easy What did we gain ?

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

Demo Time !

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

● Aim for stateless UI ● Model your context ● Make your domain responsible ● UI displays data ● UI sends actions In short

Slide 70

Slide 70 text

No content

Slide 71

Slide 71 text

@Dorvaryn +BenjaminAugustin-Dorvaryn Dorvaryn https://github.com/Dorvaryn/unidirectionalDataFlow/