Slide 1

Slide 1 text

RxJava A Stream of Joy and Woe @chris_h_codes

Slide 2

Slide 2 text

Why have we chased the dragon?

Slide 3

Slide 3 text

Slide 4

Slide 4 text

Slide 5

Slide 5 text

Slide 6

Slide 6 text

Slide 7

Slide 7 text

Slide 8

Slide 8 text

webServices.getSomething() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe { something -> // ... }/

Slide 9

Slide 9 text

webServices.getSomething() .map { result -> transform(result) } .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe { something -> // ... }/

Slide 10

Slide 10 text

webServices.getSomething() .flatMap { firstResult -> webServices.getSomethingElse(firstResult) } .map { result -> transform(result) } .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe { something -> // ... }/

Slide 11

Slide 11 text

webServices.getSomething() .flatMap { firstResult -> webServices.getSomethingElse(firstResult) } .map { result -> transform(result) } .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe ( { something -> // ... }, { error -> // ... } )

Slide 12

Slide 12 text

webServices.getSomething() .flatMap { firstResult -> webServices.getSomethingElse(firstResult) } .map { result -> transform(result) } .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe ( { something -> // ... }, { error -> // ... } ) OF TERMINAL EVENTS BEWARE

Slide 13

Slide 13 text

val getSomethingFromWeb = webServices.getSomething() .flatMap { firstResult -> webServices.getSomethingElse(firstResult) } .map { result -> transform(result) } .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread())

Slide 14

Slide 14 text

systemEventEmitter() .switchMap { getSomethingFromWeb.toObservable() } .withLatestFrom(somethingElse) { // ... }

Slide 15

Slide 15 text

interface WebServices { @GET("something") fun getSomething(): Single> }/

Slide 16

Slide 16 text

interface WebServices { @GET("something") fun getSomething(): Single> }/

Slide 17

Slide 17 text

webServices.getSomething() .flatMap { firstResult -> webServices.getSomethingElse(firstResult) } .map { result -> transform(result) } .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe { something -> // ... }/

Slide 18

Slide 18 text

webServices.getSomething() .flatMap { firstResult -> webServices.getSomethingElse(firstResult) } .map { result -> transform(result) } .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe { something -> // ... }/

Slide 19

Slide 19 text

webServices.getSomething() .flatMap { firstResult -> if (!firstResult.isError()) { webServices.getSomethingElse(firstResult.response().body()) } else { Single.just(ERROR) }/ }/ .map { result -> transform(result) } .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe { something -> // ... }/

Slide 20

Slide 20 text

webServices.getSomething() .flatMap { firstResult -> if (!firstResult.isError() && firstResult.response().isSuccessful) { webServices.getSomethingElse(firstResult.response().body()) } else { Single.just(ERROR) }/ }/ .map { result -> transform(result) } .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe { something -> // ... }/

Slide 21

Slide 21 text

webServices.getSomething() .flatMap { firstResult -> if (!firstResult.isError() && firstResult.response().isSuccessful) { webServices.getSomethingElse(firstResult.response().body()) } else { Single.just(ERROR) }/ }/ .map { result -> transform(result) } .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe { something -> // ... }/

Slide 22

Slide 22 text

suspend fun doSomeWork() { try { val firstResponse = webServices.getSomething().await() if (firstResponse.isSuccessful) { val something = firstResponse.body() val secondResponse = webServices.getSomethingElse(something).await() // .. } } catch (e: IOException) { // .. } }

Slide 23

Slide 23 text

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

• Interfacing with an engine monitoring system • Over bluetooth • Which is emulating a serial connection • Enabling a (very slow) satellite internet service • Via SOAP • Sending location and engine data over that connection • Meanwhile monitoring the accelerometer to check for accidents

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

Monitoring for accidents

Slide 28

Slide 28 text

Z X

Slide 29

Slide 29 text

Y

Slide 30

Slide 30 text

android.hardware.Sensor.TYPE_GRAVITY XYZ XYZ XYZ XZ XZ XZ XYZ Check against some threshold ! Throttle detections

Slide 31

Slide 31 text

sensors.observeSensor(TYPE_GRAVITY) .distinctUntilChanged() .map { it.sensorEvent.values } .filter { it[0] > ROLLOVER_THRESHOLD || it[2] > ROLLOVER_THRESHOLD } .map { Emergency.ROLLOVER } .throttleFirst(THROTTLE_SECONDS, TimeUnit.SECONDS) .observeOn(RealRxSchedulers.mainThread()) .subscribe { }

Slide 32

Slide 32 text

• Location updates • Bluetooth payloads from engine • Managing output sockets as network conditions change • Shift status timers

Slide 33

Slide 33 text

RxPermissions .request(Manifest.permission.CAMERA) .subscribe { granted -> if (granted) { // I can control the camera now. } else { // Oh no, permission denied. } }

Slide 34

Slide 34 text

RxActivityResult.on(this) .startIntent(takePhoto) .subscribe { result, resultCode -> if (resultCode == RESULT_OK) { result.targetUi().showImage(data) } else { result.targetUi().printUserCancelled() } }

Slide 35

Slide 35 text

Slide 36

Slide 36 text

13:37 chris_h_codes

Slide 37

Slide 37 text

13:37 chris_h_codes

Slide 38

Slide 38 text

13:37 chris_h_codes

Slide 39

Slide 39 text

fun onViewAttached() { val user = userStore.getCurrentValue() avatarView.setImage(user.profilePic) usernameView.text = user.username }/

Slide 40

Slide 40 text

fun onViewAttached() { val user = userStore.getCurrentValue()() avatarView.setImage(user.profilePic) usernameView.text = user.username userStore.observe().subscribe { user -> avatarView.setImage(user.profilePic) usernameView.text = user.usernmame } }/

Slide 41

Slide 41 text

https:/ /github.com/Gridstone/RxStore

Slide 42

Slide 42 text

https:/ /github.com/Gridstone/RxStore val userStore = storeProvider.valueStore(file, converter) userStore.put(user) val observeUser: Observable = userStore.observe() val putUser: Single = userStore.observePut(user)

Slide 43

Slide 43 text

webServices.getUser() .concatMap { user -> userStore.observePut(user) } .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread())

Slide 44

Slide 44 text

Slide 45

Slide 45 text

Controller View

Slide 46

Slide 46 text

Controller View

Slide 47

Slide 47 text

State Controller View

Slide 48

Slide 48 text

Controller View State Event

Slide 49

Slide 49 text

Controller View Observable Observable

Slide 50

Slide 50 text

val events: Observable = Observable.merge( loginButton.clicks().map { Event.AttemptLogin(username, password) }, cancelButton.clicks().map { Event.Cancel } ) https:/ /github.com/JakeWharton/RxBinding

Slide 51

Slide 51 text

Observables .combineLatest( usernameView.textChanges().map { it.isNotEmpty() }, passwordView.textChanges().map { it.isNotEmpty() }) { hasUsername, hasPassword -> hasUsername && hasPassword } .subscribe(loginButton::setEnabled) Username Password Login

Slide 52

Slide 52 text

Controller View Observable Observable

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

View() onFinishInflate() onRestoreInstanceState() draw() display(state)

Slide 56

Slide 56 text

stateStream .subscribeOn(Schedulers.computation()) .observeOn(AndroidSchedulers.mainThread()) .subscribe { state -> view.display(state) }/

Slide 57

Slide 57 text

stateStream .replay(1) .autoConnect() .subscribeOn(Schedulers.computation()) .observeOn(AndroidSchedulers.mainThread()) .subscribe { state -> view.display(state) }/

Slide 58

Slide 58 text

stateStream .subscribeOn(Schedulers.computation()) .observeOn(AndroidSchedulers.mainThread()) .replay(1) .autoConnect() .subscribe { state -> view.display(state) }/

Slide 59

Slide 59 text

stateStream

Slide 60

Slide 60 text

action state reduce(action, previousState)

Slide 61

Slide 61 text

Takeaways

Slide 62

Slide 62 text

Takeaways • If you only use Rx for callbacks with nice threading, consider coroutines • Beware terminal events • Be mindful of thread boundaries and state delivery • Don’t try to Rx-AllTheThings

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

RxJava A Stream of Joy and Woe chris_h_codes chris-horner chrishorner.codes