Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

RxJava and Android

40 views

Published on

Introduction to RxJava (1.x) concepts and some uses in Android development.

Published in: Software
  • Be the first to comment

  • Be the first to like this

RxJava and Android

  1. 1. RxJava and Android
  2. 2. RxJava
  3. 3. What is RxJava?
  4. 4. Observable • Emits items • Terminates * * * (maybe)
  5. 5. Observer • Consumes items - onNext() • Handles end of stream - onCompleted() • Handles errors - onError()
  6. 6. Hello, World Observable.just("Hello, World!") .subscribe(new Observer<String>() { @Override public void onNext(String s) { System.out.println(s); } @Override public void onCompleted() { } @Override public void onError(Throwable e) { } });
  7. 7. Hello, World Observable.just("Hello, World!") .subscribe(new Action1<String>() { @Override public void call(String s) { System.out.println(s); } });
  8. 8. Hello, World Observable.just("Hello, World!") .subscribe(s -> System.out.println(s));
  9. 9. Changing the stream
  10. 10. Map
  11. 11. Map Observable.just("Hello, World!") .map(s -> s.length()) .map(i -> Integer.toString(i)) .subscribe(s -> System.out.println(s)); Output: 13
  12. 12. Filter
  13. 13. Filter Observable.just("Hello", "Jello", "Yellow") .filter(s -> s.endsWith("ello")) .subscribe(s -> System.out.println(s)); Output: Hello Jello
  14. 14. Take
  15. 15. Take Observable.just("Hello", "Jello", "Yellow") .take(2) .subscribe(s -> System.out.println(s)); Output: Hello Jello
  16. 16. FlatMap
  17. 17. FlatMap Observable<List<String>> getNames(); getNames() .flatMap(names -> Observable.from(names)) .map(name -> "Hi " + name + "!") .subscribe(s -> System.out.println(s)); Output: Hi James! Hi Cristen! Hi Lauren! Input: [ “James”, “Cristen”, “Lauren” ]
  18. 18. Using RxJava in Android
  19. 19. Retrofit • REST API access • Supports RxJava • square.github.io/retrofit
  20. 20. Retrofit @GET("/v0/beststories.json")
 List<Integer> getBestStories();
 
 @GET("/v0/item/{id}.json")
 HNItem getItem(@Path("id") int id);
  21. 21. Retrofit @GET("/v0/beststories.json")
 Observable<List<Integer>> getBestStories();
 
 @GET("/v0/item/{id}.json")
 Observable<HNItem> getItem(@Path("id") int id);
  22. 22. Retrofit @GET("/v0/beststories.json")
 Observable<List<Integer>> getBestStories();
 
 @GET("/v0/item/{id}.json")
 Observable<HNItem> getItem(@Path("id") int id); hnService.getBestStories()
 .flatMap(ids -> Observable.from(ids))
 .flatMap(id -> hnService.getItem(id))
 .subscribeOn(Schedulers.io())
 .observeOn(AndroidSchedulers.mainThread())
 .subscribe(item -> addItemToList(item));
  23. 23. MVVM • Decouple business logic from UI • Much easier to test
  24. 24. MVVM - Example
  25. 25. Subject • Observer + Observable
  26. 26. MVVM - ViewModel public class MainViewModel {
 
 // input
 BehaviorSubject<Boolean> militaryTimeEnabledSubject = BehaviorSubject.create();
 
 // output
 private BehaviorSubject<String> subtitleTextSubject = BehaviorSubject.create();
 
 public MainViewModel() {
 militaryTimeEnabledSubject
 .map(enabled -> enabled ? "13:00" : "1:00 PM")
 .subscribe(subtitleTextSubject);
 }
 public Observable<String> subtitleTextObservable() {
 return subtitleTextSubject.asObservable();
 }
 }
  27. 27. MVVM - Activity final MainViewModel vm = new MainViewModel();
 
 vm.subtitleTextObservable().subscribe(s -> subtitle.setText(s));
 
 toggle.setOnCheckedChangeListener((button, checked) -> { vm.militaryTimeEnabledSubject.onNext(checked);
 });
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 final TextView subtitle = (TextView) findViewById(R.id.subtitle);
 SwitchCompat toggle = (SwitchCompat) findViewById(R.id.toggle);
 }
  28. 28. MVVM - Test! public class MainViewModelTest {
 
 @Test
 public void verifyCorrectSubtitle() {
 MainViewModel vm = new MainViewModel();
 
 TestSubscriber<String> testSubscriber = TestSubscriber.create();
 vm.subtitleTextObservable().subscribe(testSubscriber);
 
 vm.militaryTimeEnabledSubject.onNext(true);
 vm.militaryTimeEnabledSubject.onNext(false);
 
 testSubscriber.assertValueCount(2);
 testSubscriber.assertValues("13:00", "1:00 PM");
 }
 }
  29. 29. Resources • https://github.com/ReactiveX/RxJava/wiki • rxmarbles.com • http://blog.danlew.net/2014/09/15/grokking-rxjava-part-1/
  30. 30. Questions? Scott Weber @ScottDWeber scott.d.weber@gmail.com

×