Slide 1

Slide 1 text

RxJava Douglas Drumond [email protected]

Slide 2

Slide 2 text

whoami

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Por que RxJava? Eventos de UI Eventos do domínio Concorrência

Slide 5

Slide 5 text

RxJava Observer pattern Iterator pattern Functional programming

Slide 6

Slide 6 text

Lambda

Slide 7

Slide 7 text

Retrolambda export JAVA_HOME=/path/java8 export JAVA7_HOME=/path/java7

Slide 8

Slide 8 text

Retrolambda plugins { id "me.tatarka.retrolambda" version "3.2.2" } android { compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } }

Slide 9

Slide 9 text

Retrolambda view.setOnClickListener(new View. OnClickListener() { @Override public void onClick(final View v) { ... } });

Slide 10

Slide 10 text

Retrolambda view.setOnClickListener(v -> { });

Slide 11

Slide 11 text

Básico Observables Subscribers Operadores

Slide 12

Slide 12 text

Observables Emite itens Chama onNext, onComplete ou onError Hot or Cold

Slide 13

Slide 13 text

Subscriber Implementa onNext onComplete onError

Slide 14

Slide 14 text

Operadores flatMap filter debounce create defer etc

Slide 15

Slide 15 text

Criando Observable e Subscriber DEMO

Slide 16

Slide 16 text

Operators Subscribers reagem, não mudam Operadores transformam dados

Slide 17

Slide 17 text

Operators DEMO

Slide 18

Slide 18 text

Exemplo com UI DEMO

Slide 19

Slide 19 text

Mais operadores filter Ex: .filter(query -> !TextUtils.isEmpty(query))

Slide 20

Slide 20 text

Threads subscribeOn observeOn NewThreadScheduler HandlerThreadScheduler

Slide 21

Slide 21 text

Rede DEMO

Slide 22

Slide 22 text

Tratamento de erros Observable.create(...) .map(s -> exception(s)) .map(s -> exception2(s)) .subscribe(new Subscriber() { @Override public void onNext(String s) { Log.d(TAG, s); } @Override public void onCompleted() { Log.d(TAG, "uhu!"); } @Override public void onError(Throwable e) { Log.e(TAG, "ops!"); } });

Slide 23

Slide 23 text

Ciclo de vida da Activity .cache .unsubscribe ao rodar .subscribe ao restaurar Use RxLifecycle

Slide 24

Slide 24 text

Google Play Services https://github.com /mcharmas/Android-ReactiveLocation https://github.com /zmarkan/Reactive-PlayServices

Slide 25

Slide 25 text

Google Play Services ReactiveLocationProvider locationProvider = new ReactiveLocationProvider(context); locationProvider.getLastKnownLocation() .subscribe(new Action1() { @Override public void call(Location location) { doSthImportantWithObtainedLocation(location); } });

Slide 26

Slide 26 text

Testes @Test public void shouldLoadTwoUsers() throw Exception { TestSubscriber testSubscriber = new TestSubscriber<>(); databaseHelper.loadUser().subscribe(testSubscriber); testSubscriber.assertNoErrors(); testSubscriber.assertReceivedOnNext( Arrays.asList(user1, user2)) }

Slide 27

Slide 27 text

Testes https://github.com/ribot/assertj-rx + BlockingObservable

Slide 28

Slide 28 text

Testes (exemplo) assertThat(observable.toBlocking()). completes(); assertThat(observable.toBlocking()) .completes() .listOfValuesEmitted() .containsExactly("a", "b", "c");

Slide 29

Slide 29 text

Referências http://reactivex.io/ https://github.com/ReactiveX/RxJava/wiki /Alphabetical-List-of-Observable-Operators https://speakerdeck.com/benjchristensen https://github.com/mutexkid/rxjava-koans

Slide 30

Slide 30 text

Referências https://github.com/trello/RxLifecycle https://github.com/JakeWharton/RxBinding https://github.com/ReactiveX/RxAndroid/

Slide 31

Slide 31 text

Código https://github.com/ douglasdrumond/KnightsOfLambdaCalculus

Slide 32

Slide 32 text

Obrigado

Slide 33

Slide 33 text

Contato [email protected] @douglasdrumond +DouglasDrumond www.cafelinear.com [email protected]