Reactive Libraries for Android

Reactive Libraries for Android

Presentation covering basics of RxJava in a nutshell and showing exemplary reactive libraries for Android. It also touches the topic of unit testing of the reactive code. The presentation was shown on 27.10.2015 in Katowice, Poland during Silesia Mobile Group meetup.

E8bb35e5d8c868e7dffcd9859248d6f1?s=128

Piotr Wittchen

October 27, 2015
Tweet

Transcript

  1. None
  2. None
  3. None
  4. None
  5. None
  6. Observable.from(names).subscribe(new Action1<String>() { @Override public void call(String s) { System.out.println("Hello

    " + s + "!"); } });
  7. Observable.from(names.subscribe(new Subscriber<String>() { @Override public void onCompleted() { // called

    after calling onNext() for the final time } @Override public void onError(Throwable e) { // called whenever error occurrs } @Override public void onNext(String s) { // called whenever Observable emits an item } });
  8. Observable.from(names) .map(...) .filter(...) .merge(...) ... .subscribe(new Action1<String>() { @Override public

    void call(String s) { System.out.println("Hello " + s + "!"); } });
  9. Observable.from(names) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Action1<String>() { @Override public void call(String

    s) { System.out.println("Hello " + s + "!"); } });
  10. None
  11. • • • •

  12. None
  13. new ReactiveNetwork().observeConnectivity(context) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Action1<Connectivity>() { @Override public void

    call(Connectivity connectivity) { // do something with connectivity } });
  14. new ReactiveSensors(context).observeSensor(Sensor.TYPE_GYROSCOPE) .filter(ReactiveSensorEvent.filterSensorChanged()) .subscribe(new Action1<ReactiveSensorEvent>() { @Override public void call(ReactiveSensorEvent

    reactiveSensorEvent) { SensorEvent event = reactiveSensorEvent.getSensorEvent(); float x = event.values[0]; float y = event.values[1]; float z = event.values[2]; } }); }
  15. None
  16. Prefser prefser = new Prefser(context); // or new Prefser(sharedPreferences); prefser.put("key1",

    true); prefser.put("key2", new MyObject()); prefser.put("key3", Arrays.asList(true, false, true)); Boolean singleBoolean = prefser.get("key1", Boolean.class, false); MyObject myObject = prefser.get("key2", MyObject.class, new MyObject()); TypeToken<List<Boolean>> typeToken = new TypeToken<List<Boolean>>() {}; List<Boolean> booleans = prefser.get("key3", typeToken, defaultBooleans);
  17. • observe(key, classOfT/TypeTokenOfT, defaultValue) ➔ • getAndObserve(key, classOfT/TypeTokenOfT, defaultValue) ➔

    • observePreferences() ➔
  18. prefser.observe(key, Boolean.class, false) .subscribe(new Action1<Boolean>() { @Override public void call(Boolean

    value) { // do something with value } });
  19. new ReactiveBeacons(context).observe() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Action1<Beacon>() { @Override public void

    call(Beacon beacon) { // do something with beacon } });
  20. None
  21. None
  22. Observable<String> getComputedResult() { … } @Test public void shouldReturnExpectedResult() {

    String expected = "result" String generated = getComputedResult().toBlocking().first(); assertThat(generated).isEqualTo(expected); }
  23. @Test public void shouldObserveBoolean() { String givenKey = "someKey"; boolean

    givenValue = true; Boolean defaultValue = false; RecordingObserver<Boolean> observer = new RecordingObserver<>(); prefser.observe(givenKey, Boolean.class, defaultValue).subscribe(observer); prefser.put(givenKey, givenValue); assertThat(observer.takeNext()).isTrue(); observer.assertNoMoreEvents(); }
  24. • Pros • Cons

  25. None