|
| 1 | +package com.example.reactiveprogrammingusingrxjava2; |
| 2 | + |
| 3 | +import android.support.v7.app.AppCompatActivity; |
| 4 | +import android.os.Bundle; |
| 5 | +import android.widget.Toast; |
| 6 | + |
| 7 | +import java.util.concurrent.TimeUnit; |
| 8 | + |
| 9 | +import io.reactivex.Observable; |
| 10 | +import io.reactivex.ObservableSource; |
| 11 | +import io.reactivex.Observer; |
| 12 | +import io.reactivex.Scheduler; |
| 13 | +import io.reactivex.android.schedulers.AndroidSchedulers; |
| 14 | +import io.reactivex.disposables.Disposable; |
| 15 | +import io.reactivex.functions.Function; |
| 16 | +import io.reactivex.functions.Predicate; |
| 17 | +import io.reactivex.schedulers.Schedulers; |
| 18 | + |
| 19 | +public class MainActivity extends AppCompatActivity { |
| 20 | + |
| 21 | + @Override |
| 22 | + protected void onCreate(Bundle savedInstanceState) { |
| 23 | + super.onCreate(savedInstanceState); |
| 24 | + setContentView(R.layout.activity_main); |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + protected void onResume() { |
| 29 | + super.onResume(); |
| 30 | + System.out.println("MainActivity.onResume threadId : " + Thread.currentThread().getId()); |
| 31 | + justOperatorWorking(); |
| 32 | + } |
| 33 | + |
| 34 | + private void justOperatorWorking() { |
| 35 | + Observable<Long> observable = Observable.interval(2, 3, TimeUnit.SECONDS); |
| 36 | + |
| 37 | + Observer observer = new Observer() { |
| 38 | + @Override |
| 39 | + public void onSubscribe(Disposable d) { |
| 40 | + System.out.println("MainActivity.onSubscribe"); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void onNext(Object o) { |
| 45 | + System.out.println("onNext threadId : " + Thread.currentThread().getId()); |
| 46 | + System.out.println("o = [" + o.toString() + "]"); |
| 47 | + Toast.makeText(MainActivity.this, " onNext" + o.toString() , Toast.LENGTH_SHORT).show(); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void onError(Throwable e) { |
| 52 | + System.out.println("e = [" + e + "]"); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void onComplete() { |
| 57 | + System.out.println("MainActivity.onComplete"); |
| 58 | + } |
| 59 | + }; |
| 60 | + |
| 61 | + observable |
| 62 | + .subscribeOn(Schedulers.io())//whatever is executed downstream would be run on thread where observable is run (non-ui thread) |
| 63 | + //.map(x -> x+1) //using lambda |
| 64 | + .map(new Function<Long, Object>() { |
| 65 | + @Override |
| 66 | + public Object apply(Long aLong) { |
| 67 | + System.out.println("map operator .apply ThreadId : " + Thread.currentThread().getId()); |
| 68 | + return aLong + 1; |
| 69 | + } |
| 70 | + }) |
| 71 | + // .map(new TimeConverter<Long, Object>()) |
| 72 | + .filter(new Predicate<Object>() { |
| 73 | + @Override |
| 74 | + public boolean test(Object o) throws Exception { |
| 75 | + if ((Long)o % 2 == 0) |
| 76 | + return true; |
| 77 | + else |
| 78 | + return false; |
| 79 | + |
| 80 | + } |
| 81 | + }) |
| 82 | + .distinctUntilChanged() |
| 83 | + .observeOn(AndroidSchedulers.mainThread()) |
| 84 | + .map(new Function<Object, Object>() { |
| 85 | + @Override |
| 86 | + public Object apply(Object o) { |
| 87 | + System.out.println("map operator2 .apply ThreadId : " + Thread.currentThread().getId()); |
| 88 | + return o; |
| 89 | + } |
| 90 | + }) |
| 91 | + .subscribe(observer); |
| 92 | + } |
| 93 | + |
| 94 | + class TimeConverter<Long, Object> implements Function<Long, Object> { |
| 95 | + |
| 96 | + @Override |
| 97 | + public Object apply(Long aLong) { |
| 98 | + System.out.println("map operator .apply ThreadId : " + Thread.currentThread().getId()); |
| 99 | + return (Object) aLong; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | +} |
0 commit comments