$30 off During Our Annual Pro Sale. View Details »

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.

Piotr Wittchen

October 27, 2015
Tweet

More Decks by Piotr Wittchen

Other Decks in Programming

Transcript

  1. 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 } });
  2. 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]; } }); }
  3. 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);
  4. Observable<String> getComputedResult() { … } @Test public void shouldReturnExpectedResult() {

    String expected = "result" String generated = getComputedResult().toBlocking().first(); assertThat(generated).isEqualTo(expected); }
  5. @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(); }