├── .gitignore
├── .idea
├── dictionaries
│ └── ravi.xml
├── gradle.xml
├── misc.xml
├── modules.xml
├── runConfigurations.xml
└── vcs.xml
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── info
│ │ └── androidhive
│ │ └── rxandroidexamples
│ │ └── ExampleInstrumentedTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── ic_launcher-web.png
│ ├── java
│ │ └── info
│ │ │ └── androidhive
│ │ │ └── rxandroidexamples
│ │ │ ├── MainActivity.java
│ │ │ ├── app
│ │ │ └── Const.java
│ │ │ ├── basics
│ │ │ ├── Example1Activity.java
│ │ │ ├── Example2Activity.java
│ │ │ ├── Example3Activity.java
│ │ │ ├── Example4Activity.java
│ │ │ └── Example5Activity.java
│ │ │ ├── networking
│ │ │ └── retrofit
│ │ │ │ ├── ApiClient.java
│ │ │ │ ├── ApiService.java
│ │ │ │ ├── ContactsListActivity.java
│ │ │ │ ├── ContactsListMultiRemoteActivity.java
│ │ │ │ ├── ContactsSearchLocalActivity.java
│ │ │ │ ├── ContactsSearchRemoteActivity.java
│ │ │ │ ├── adapter
│ │ │ │ ├── ContactsAdapter.java
│ │ │ │ └── ContactsAdapterFilterable.java
│ │ │ │ └── model
│ │ │ │ └── Contact.java
│ │ │ ├── observers
│ │ │ ├── CompletableObserverActivity.java
│ │ │ ├── FlowableObserverActivity.java
│ │ │ ├── MaybeObserverActivity.java
│ │ │ ├── ObserverActivity.java
│ │ │ ├── SingleObserverActivity.java
│ │ │ └── model
│ │ │ │ └── Note.java
│ │ │ ├── operators
│ │ │ ├── AverageOperatorActivity.java
│ │ │ ├── BufferOperatorActivity.java
│ │ │ ├── ConcatMapOperatorActivity.java
│ │ │ ├── ConcatOperatorActivity.java
│ │ │ ├── CountOperatorActivity.java
│ │ │ ├── DebounceOperatorActivity.java
│ │ │ ├── DistinctOperatorActivity.java
│ │ │ ├── FilterOperatorActivity.java
│ │ │ ├── FlatMapActivity.java
│ │ │ ├── FromOperatorActivity.java
│ │ │ ├── GroupByOperatorActivity.java
│ │ │ ├── JoinOperatorActivity.java
│ │ │ ├── JustOperatorActivity.java
│ │ │ ├── MapOperatorActivity.java
│ │ │ ├── MaxOperatorActivity.java
│ │ │ ├── MergeOperatorActivity.java
│ │ │ ├── MinOperatorActivity.java
│ │ │ ├── RangeOperatorActivity.java
│ │ │ ├── ReduceOperatorActivity.java
│ │ │ ├── RepeatOperatorActivity.java
│ │ │ ├── ReplayOperatorActivity.java
│ │ │ ├── SkipOperatorActivity.java
│ │ │ ├── SumOperatorActivity.java
│ │ │ ├── SwitchMapOperatorActivity.java
│ │ │ ├── TakeOperatorActivity.java
│ │ │ ├── TakeUntilOperatorActivity.java
│ │ │ ├── ZipOperatorActivity.java
│ │ │ └── model
│ │ │ │ ├── Address.java
│ │ │ │ ├── Sport.java
│ │ │ │ └── User.java
│ │ │ ├── recyclerview
│ │ │ └── RecyclerViewPagingActivity.java
│ │ │ ├── rxbinding
│ │ │ ├── RecyclerViewRxBindingActivity.java
│ │ │ ├── SpinnerRxBindingActivity.java
│ │ │ └── ViewBindingActivity.java
│ │ │ └── subjects
│ │ │ ├── AsyncSubjectActivity.java
│ │ │ ├── BehaviorSubjectActivity.java
│ │ │ ├── PublishSubjectActivity.java
│ │ │ └── ReplaySubjectActivity.java
│ └── res
│ │ ├── drawable-v24
│ │ └── ic_launcher_foreground.xml
│ │ ├── drawable
│ │ └── ic_launcher_background.xml
│ │ ├── layout
│ │ ├── activity_async_subject.xml
│ │ ├── activity_average_operator.xml
│ │ ├── activity_behavior_subject.xml
│ │ ├── activity_buffer_operator.xml
│ │ ├── activity_completable_observer.xml
│ │ ├── activity_concat_map.xml
│ │ ├── activity_concat_operator.xml
│ │ ├── activity_contacts_list.xml
│ │ ├── activity_contacts_list_multi_remote.xml
│ │ ├── activity_contacts_search.xml
│ │ ├── activity_contacts_search_remote.xml
│ │ ├── activity_count_operator.xml
│ │ ├── activity_debounce_operator.xml
│ │ ├── activity_distinct_operator.xml
│ │ ├── activity_example1.xml
│ │ ├── activity_example2.xml
│ │ ├── activity_example3.xml
│ │ ├── activity_example4.xml
│ │ ├── activity_example5.xml
│ │ ├── activity_filter_operator.xml
│ │ ├── activity_flat_map.xml
│ │ ├── activity_flowable_observer.xml
│ │ ├── activity_from_operator.xml
│ │ ├── activity_group_by_operator.xml
│ │ ├── activity_join_operator.xml
│ │ ├── activity_json_object_request.xml
│ │ ├── activity_just_operator.xml
│ │ ├── activity_main.xml
│ │ ├── activity_map_operator.xml
│ │ ├── activity_max_operator.xml
│ │ ├── activity_maybe_observer.xml
│ │ ├── activity_merge_operator.xml
│ │ ├── activity_min_operator.xml
│ │ ├── activity_observer.xml
│ │ ├── activity_publish_subject.xml
│ │ ├── activity_range_operator.xml
│ │ ├── activity_recycler_view_paging.xml
│ │ ├── activity_recycler_view_rx_binding.xml
│ │ ├── activity_reduce_operator.xml
│ │ ├── activity_repeat_operator.xml
│ │ ├── activity_replay_operator.xml
│ │ ├── activity_replay_subject.xml
│ │ ├── activity_single_observer.xml
│ │ ├── activity_skip_operator.xml
│ │ ├── activity_spinner_rx_binding.xml
│ │ ├── activity_sum_operator.xml
│ │ ├── activity_switch_map_operator.xml
│ │ ├── activity_take_operator.xml
│ │ ├── activity_take_until_operator.xml
│ │ ├── activity_view_binding.xml
│ │ ├── activity_zip_operator.xml
│ │ └── contact_row_item.xml
│ │ ├── mipmap-anydpi-v26
│ │ ├── ic_launcher.xml
│ │ └── ic_launcher_round.xml
│ │ ├── mipmap-hdpi
│ │ ├── ic_launcher.png
│ │ ├── ic_launcher_foreground.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-mdpi
│ │ ├── ic_launcher.png
│ │ ├── ic_launcher_foreground.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xhdpi
│ │ ├── ic_launcher.png
│ │ ├── ic_launcher_foreground.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxhdpi
│ │ ├── ic_launcher.png
│ │ ├── ic_launcher_foreground.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxxhdpi
│ │ ├── ic_launcher.png
│ │ ├── ic_launcher_foreground.png
│ │ └── ic_launcher_round.png
│ │ └── values
│ │ ├── colors.xml
│ │ ├── dimens.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── test
│ └── java
│ └── info
│ └── androidhive
│ └── rxandroidexamples
│ └── ExampleUnitTest.java
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 | .externalNativeBuild
10 |
--------------------------------------------------------------------------------
/.idea/dictionaries/ravi.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | flowable
5 |
6 |
7 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
17 |
18 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion rootProject.ext.compileSdkVersion
5 | defaultConfig {
6 | applicationId "info.androidhive.rxandroidexamples"
7 | minSdkVersion rootProject.ext.minSdkVersion
8 | targetSdkVersion rootProject.ext.targetSdkVersion
9 | versionCode 1
10 | versionName "1.0"
11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | }
20 |
21 | dependencies {
22 | implementation fileTree(dir: 'libs', include: ['*.jar'])
23 | implementation "com.android.support:appcompat-v7:$rootProject.supportLibraryVersion"
24 | implementation "com.android.support.constraint:constraint-layout:$rootProject.constraintLayout"
25 | implementation "com.android.support:design:$rootProject.supportLibraryVersion"
26 |
27 | implementation "com.android.support:support-v4:$rootProject.supportLibraryVersion"
28 | implementation 'com.android.support.constraint:constraint-layout:1.0.2'
29 | testImplementation "junit:junit:$rootProject.junitVersion"
30 |
31 | androidTestImplementation "com.android.support.test:runner:$rootProject.runnerVersion"
32 | androidTestImplementation "com.android.support.test.espresso:espresso-core:$rootProject.espressoVersion"
33 |
34 | implementation "com.android.support:recyclerview-v7:$rootProject.supportLibraryVersion"
35 |
36 | implementation "io.reactivex.rxjava2:rxjava:$rootProject.rxjava"
37 | implementation "io.reactivex.rxjava2:rxandroid:$rootProject.rxandroid"
38 | implementation "io.reactivex:rxjava-math:$rootProject.rxjavaMath"
39 |
40 | // butter knife
41 | implementation "com.jakewharton:butterknife:$rootProject.butterKnfie"
42 | annotationProcessor "com.jakewharton:butterknife-compiler:$rootProject.butterKnfie"
43 |
44 | // volley
45 | implementation "com.mcxiaoke.volley:library:$rootProject.volley"
46 |
47 | implementation "com.squareup.retrofit2:retrofit:$rootProject.retrofit"
48 | implementation "com.squareup.retrofit2:converter-gson:$rootProject.retrofit"
49 | implementation "com.jakewharton.retrofit:retrofit2-rxjava2-adapter:$rootProject.retrofitRxjavaAdapter"
50 | implementation "com.squareup.okhttp3:okhttp:$rootProject.retrofitRxjavaAdapter"
51 | implementation "com.squareup.okhttp3:okhttp-urlconnection:$rootProject.okhttp"
52 | implementation "com.squareup.okhttp3:logging-interceptor:$rootProject.okhttpLogger"
53 |
54 | // glide image library
55 | implementation "com.github.bumptech.glide:glide:$rootProject.glide"
56 |
57 | implementation "com.jakewharton.rxbinding2:rxbinding:$rootProject.rxbinding"
58 |
59 | }
60 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/info/androidhive/rxandroidexamples/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package info.androidhive.rxandroidexamples;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumented test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("info.androidhive.rxandroidexamples", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
--------------------------------------------------------------------------------
/app/src/main/ic_launcher-web.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ravi8x/RxAndroidExamples/d75524adb0bdf5a900beb58633513c4ec928c0ac/app/src/main/ic_launcher-web.png
--------------------------------------------------------------------------------
/app/src/main/java/info/androidhive/rxandroidexamples/MainActivity.java:
--------------------------------------------------------------------------------
1 | package info.androidhive.rxandroidexamples;
2 |
3 | import android.support.v7.app.AppCompatActivity;
4 | import android.os.Bundle;
5 |
6 | public class MainActivity extends AppCompatActivity {
7 |
8 | @Override
9 | protected void onCreate(Bundle savedInstanceState) {
10 | super.onCreate(savedInstanceState);
11 | setContentView(R.layout.activity_main);
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/app/src/main/java/info/androidhive/rxandroidexamples/app/Const.java:
--------------------------------------------------------------------------------
1 | package info.androidhive.rxandroidexamples.app;
2 |
3 | /**
4 | * Created by ravi on 31/01/18.
5 | */
6 |
7 | public class Const {
8 | // JSON endpoint used for demos
9 | // will return list of contacts
10 | public static final String BASE_URL = "https://api.androidhive.info/json/";
11 | }
12 |
--------------------------------------------------------------------------------
/app/src/main/java/info/androidhive/rxandroidexamples/basics/Example1Activity.java:
--------------------------------------------------------------------------------
1 | package info.androidhive.rxandroidexamples.basics;
2 |
3 | import android.support.v7.app.AppCompatActivity;
4 | import android.os.Bundle;
5 | import android.util.Log;
6 |
7 |
8 | import info.androidhive.rxandroidexamples.R;
9 | import io.reactivex.Observable;
10 | import io.reactivex.Observer;
11 | import io.reactivex.android.schedulers.AndroidSchedulers;
12 | import io.reactivex.disposables.Disposable;
13 | import io.reactivex.schedulers.Schedulers;
14 |
15 | public class Example1Activity extends AppCompatActivity {
16 |
17 | /**
18 | * Basic Observable, Observer, Subscriber example
19 | * Observable emits list of animal names
20 | */
21 |
22 | private static final String TAG = Example1Activity.class.getSimpleName();
23 |
24 | @Override
25 | protected void onCreate(Bundle savedInstanceState) {
26 | super.onCreate(savedInstanceState);
27 | setContentView(R.layout.activity_example1);
28 |
29 | // observable
30 | Observable animalsObservable = getAnimalsObservable();
31 |
32 | // observer
33 | Observer animalsObserver = getAnimalsObserver();
34 |
35 | // observer subscribing to observable
36 | animalsObservable
37 | .subscribeOn(Schedulers.io())
38 | .observeOn(AndroidSchedulers.mainThread())
39 | .subscribe(animalsObserver);
40 | }
41 |
42 | private Observer getAnimalsObserver() {
43 | return new Observer() {
44 | @Override
45 | public void onSubscribe(Disposable d) {
46 | Log.d(TAG, "onSubscribe");
47 | }
48 |
49 | @Override
50 | public void onNext(String s) {
51 | Log.d(TAG, "Name: " + s);
52 | }
53 |
54 | @Override
55 | public void onError(Throwable e) {
56 | Log.e(TAG, "onError: " + e.getMessage());
57 | }
58 |
59 | @Override
60 | public void onComplete() {
61 | Log.d(TAG, "All items are emitted!");
62 | }
63 | };
64 | }
65 |
66 | private Observable getAnimalsObservable() {
67 | return Observable.just("Ant", "Bee", "Cat", "Dog", "Fox");
68 | }
69 | }
--------------------------------------------------------------------------------
/app/src/main/java/info/androidhive/rxandroidexamples/basics/Example2Activity.java:
--------------------------------------------------------------------------------
1 | package info.androidhive.rxandroidexamples.basics;
2 |
3 | import android.os.Bundle;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.util.Log;
6 |
7 | import info.androidhive.rxandroidexamples.R;
8 | import io.reactivex.Observable;
9 | import io.reactivex.Observer;
10 | import io.reactivex.android.schedulers.AndroidSchedulers;
11 | import io.reactivex.disposables.CompositeDisposable;
12 | import io.reactivex.disposables.Disposable;
13 | import io.reactivex.observers.DisposableObserver;
14 | import io.reactivex.schedulers.Schedulers;
15 |
16 | public class Example2Activity extends AppCompatActivity {
17 |
18 | /**
19 | * Basic Observable, Observer, Subscriber example
20 | * Observable emits list of animal names
21 | * You can see Disposable introduced in this example
22 | */
23 | private static final String TAG = Example2Activity.class.getSimpleName();
24 |
25 | private Disposable disposable;
26 |
27 | @Override
28 | protected void onCreate(Bundle savedInstanceState) {
29 | super.onCreate(savedInstanceState);
30 | setContentView(R.layout.activity_example2);
31 |
32 | // observable
33 | Observable animalsObservable = getAnimalsObservable();
34 |
35 | // observer
36 | Observer animalsObserver = getAnimalsObserver();
37 |
38 | // observer subscribing to observable
39 | animalsObservable
40 | .subscribeOn(Schedulers.io())
41 | .observeOn(AndroidSchedulers.mainThread())
42 | .subscribeWith(animalsObserver);
43 | }
44 |
45 | private Observer getAnimalsObserver() {
46 | return new Observer() {
47 |
48 | @Override
49 | public void onSubscribe(Disposable d) {
50 | Log.d(TAG, "onSubscribe");
51 | disposable = d;
52 | }
53 |
54 | @Override
55 | public void onNext(String s) {
56 | Log.d(TAG, "Name: " + s);
57 | }
58 |
59 | @Override
60 | public void onError(Throwable e) {
61 | Log.e(TAG, "onError: " + e.getMessage());
62 | }
63 |
64 | @Override
65 | public void onComplete() {
66 | Log.d(TAG, "All items are emitted!");
67 | }
68 | };
69 | }
70 |
71 | private Observable getAnimalsObservable() {
72 | return Observable.just("Ant", "Bee", "Cat", "Dog", "Fox");
73 | }
74 |
75 | @Override
76 | protected void onDestroy() {
77 | super.onDestroy();
78 |
79 | // don't send events once the activity is destroyed
80 | disposable.dispose();
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/app/src/main/java/info/androidhive/rxandroidexamples/basics/Example3Activity.java:
--------------------------------------------------------------------------------
1 | package info.androidhive.rxandroidexamples.basics;
2 |
3 | import android.support.v7.app.AppCompatActivity;
4 | import android.os.Bundle;
5 | import android.util.Log;
6 |
7 | import info.androidhive.rxandroidexamples.R;
8 | import io.reactivex.Observable;
9 | import io.reactivex.Observer;
10 | import io.reactivex.android.schedulers.AndroidSchedulers;
11 | import io.reactivex.disposables.Disposable;
12 | import io.reactivex.functions.Predicate;
13 | import io.reactivex.schedulers.Schedulers;
14 |
15 | public class Example3Activity extends AppCompatActivity {
16 |
17 | /**
18 | * Basic Observable, Observer, Subscriber example
19 | * Observable emits list of animal names
20 | * You can see filter() operator is used to filter out the
21 | * animal names that starts with letter `b`
22 | */
23 | private static final String TAG = Example3Activity.class.getSimpleName();
24 |
25 | private Disposable disposable;
26 |
27 | @Override
28 | protected void onCreate(Bundle savedInstanceState) {
29 | super.onCreate(savedInstanceState);
30 | setContentView(R.layout.activity_example3);
31 |
32 | // observable
33 | Observable animalsObservable = getAnimalsObservable();
34 |
35 | // observer
36 | Observer animalsObserver = getAnimalsObserver();
37 |
38 | // observer subscribing to observable
39 | animalsObservable
40 | .subscribeOn(Schedulers.io())
41 | .observeOn(AndroidSchedulers.mainThread())
42 | .filter(new Predicate() {
43 | @Override
44 | public boolean test(String s) throws Exception {
45 | return s.toLowerCase().startsWith("b");
46 | }
47 | })
48 | .subscribeWith(animalsObserver);
49 | }
50 |
51 | private Observer getAnimalsObserver() {
52 | return new Observer() {
53 |
54 | @Override
55 | public void onSubscribe(Disposable d) {
56 | Log.d(TAG, "onSubscribe");
57 | disposable = d;
58 | }
59 |
60 | @Override
61 | public void onNext(String s) {
62 | Log.d(TAG, "Name: " + s);
63 | }
64 |
65 | @Override
66 | public void onError(Throwable e) {
67 | Log.e(TAG, "onError: " + e.getMessage());
68 | }
69 |
70 | @Override
71 | public void onComplete() {
72 | Log.d(TAG, "All items are emitted!");
73 | }
74 | };
75 | }
76 |
77 | private Observable getAnimalsObservable() {
78 | return Observable.fromArray(
79 | "Ant", "Ape",
80 | "Bat", "Bee", "Bear", "Butterfly",
81 | "Cat", "Crab", "Cod",
82 | "Dog", "Dove",
83 | "Fox", "Frog");
84 | }
85 |
86 | @Override
87 | protected void onDestroy() {
88 | super.onDestroy();
89 |
90 | // don't send events once the activity is destroyed
91 | disposable.dispose();
92 | }
93 | }
--------------------------------------------------------------------------------
/app/src/main/java/info/androidhive/rxandroidexamples/basics/Example4Activity.java:
--------------------------------------------------------------------------------
1 | package info.androidhive.rxandroidexamples.basics;
2 |
3 | import android.os.Bundle;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.util.Log;
6 |
7 | import info.androidhive.rxandroidexamples.R;
8 | import io.reactivex.Observable;
9 | import io.reactivex.android.schedulers.AndroidSchedulers;
10 | import io.reactivex.disposables.CompositeDisposable;
11 | import io.reactivex.functions.Function;
12 | import io.reactivex.functions.Predicate;
13 | import io.reactivex.observers.DisposableObserver;
14 | import io.reactivex.schedulers.Schedulers;
15 |
16 | public class Example4Activity extends AppCompatActivity {
17 |
18 | /**
19 | * Basic Observable, Observer, Subscriber example
20 | * Observable emits list of animal names
21 | * You can see filter() operator is used to filter out the
22 | * animal names that starts with letter `b`
23 | */
24 | private static final String TAG = Example4Activity.class.getSimpleName();
25 |
26 | private CompositeDisposable compositeDisposable = new CompositeDisposable();
27 |
28 | @Override
29 | protected void onCreate(Bundle savedInstanceState) {
30 | super.onCreate(savedInstanceState);
31 | setContentView(R.layout.activity_example4);
32 |
33 | Observable animalsObservable = getAnimalsObservable();
34 |
35 | DisposableObserver animalsObserver = getAnimalsObserver();
36 |
37 | DisposableObserver animalsObserverAllCaps = getAnimalsAllCapsObserver();
38 |
39 | /**
40 | * filter() is used to filter out the animal names starting with `b`
41 | * */
42 | compositeDisposable.add(
43 | animalsObservable
44 | .subscribeOn(Schedulers.io())
45 | .observeOn(AndroidSchedulers.mainThread())
46 | .filter(new Predicate() {
47 | @Override
48 | public boolean test(String s) throws Exception {
49 | return s.toLowerCase().startsWith("b");
50 | }
51 | })
52 | .subscribeWith(animalsObserver));
53 |
54 | /**
55 | * filter() is used to filter out the animal names starting with 'c'
56 | * map() is used to transform all the characters to UPPER case
57 | * */
58 |
59 | compositeDisposable.add(
60 | animalsObservable
61 | .subscribeOn(Schedulers.io())
62 | .observeOn(AndroidSchedulers.mainThread())
63 | .filter(new Predicate() {
64 | @Override
65 | public boolean test(String s) throws Exception {
66 | return s.toLowerCase().startsWith("c");
67 | }
68 | })
69 | .map(new Function() {
70 | @Override
71 | public String apply(String s) throws Exception {
72 | return s.toUpperCase();
73 | }
74 | })
75 | .subscribeWith(animalsObserverAllCaps));
76 | }
77 |
78 | private DisposableObserver getAnimalsObserver() {
79 | return new DisposableObserver() {
80 |
81 | @Override
82 | public void onNext(String s) {
83 | Log.d(TAG, "Name: " + s);
84 | }
85 |
86 | @Override
87 | public void onError(Throwable e) {
88 | Log.e(TAG, "onError: " + e.getMessage());
89 | }
90 |
91 | @Override
92 | public void onComplete() {
93 | Log.d(TAG, "All items are emitted!");
94 | }
95 | };
96 | }
97 |
98 | private DisposableObserver getAnimalsAllCapsObserver() {
99 | return new DisposableObserver() {
100 |
101 |
102 | @Override
103 | public void onNext(String s) {
104 | Log.d(TAG, "Name: " + s);
105 | }
106 |
107 | @Override
108 | public void onError(Throwable e) {
109 | Log.e(TAG, "onError: " + e.getMessage());
110 | }
111 |
112 | @Override
113 | public void onComplete() {
114 | Log.d(TAG, "All items are emitted!");
115 | }
116 | };
117 | }
118 |
119 | private Observable getAnimalsObservable() {
120 | return Observable.fromArray(
121 | "Ant", "Ape",
122 | "Bat", "Bee", "Bear", "Butterfly",
123 | "Cat", "Crab", "Cod",
124 | "Dog", "Dove",
125 | "Fox", "Frog");
126 | }
127 |
128 | @Override
129 | protected void onDestroy() {
130 | super.onDestroy();
131 |
132 | // don't send events once the activity is destroyed
133 | compositeDisposable.clear();
134 | }
135 | }
136 |
--------------------------------------------------------------------------------
/app/src/main/java/info/androidhive/rxandroidexamples/basics/Example5Activity.java:
--------------------------------------------------------------------------------
1 | package info.androidhive.rxandroidexamples.basics;
2 |
3 | import android.os.Bundle;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.util.Log;
6 |
7 | import java.util.ArrayList;
8 | import java.util.List;
9 |
10 | import info.androidhive.rxandroidexamples.R;
11 | import io.reactivex.Observable;
12 | import io.reactivex.ObservableEmitter;
13 | import io.reactivex.ObservableOnSubscribe;
14 | import io.reactivex.android.schedulers.AndroidSchedulers;
15 | import io.reactivex.disposables.CompositeDisposable;
16 | import io.reactivex.functions.Function;
17 | import io.reactivex.observers.DisposableObserver;
18 | import io.reactivex.schedulers.Schedulers;
19 |
20 | public class Example5Activity extends AppCompatActivity {
21 |
22 |
23 | /**
24 | * Basic Observable, Observer, Subscriber example
25 | * Introduced CompositeDisposable and DisposableObserver
26 | * The observable emits custom data type (Note) instead of primitive data types
27 | * ----
28 | * .map() operator is used to turn the note into all uppercase letters
29 | * ----
30 | * You can also notice we got rid of the below declarations
31 | * Observable notesObservable = getNotesObservable();
32 | * DisposableObserver notesObserver = getNotesObserver();
33 | */
34 | private static final String TAG = Example5Activity.class.getSimpleName();
35 |
36 | private CompositeDisposable disposable = new CompositeDisposable();
37 |
38 | @Override
39 | protected void onCreate(Bundle savedInstanceState) {
40 | super.onCreate(savedInstanceState);
41 | setContentView(R.layout.activity_example5);
42 |
43 | // add to Composite observable
44 | // .map() operator is used to turn the note into all uppercase letters
45 | disposable.add(getNotesObservable()
46 | .subscribeOn(Schedulers.io())
47 | .observeOn(AndroidSchedulers.mainThread())
48 | .map(new Function() {
49 | @Override
50 | public Note apply(Note note) throws Exception {
51 | // Making the note to all uppercase
52 | note.setNote(note.getNote().toUpperCase());
53 | return note;
54 | }
55 | })
56 | .subscribeWith(getNotesObserver()));
57 | }
58 |
59 | private DisposableObserver getNotesObserver() {
60 | return new DisposableObserver() {
61 |
62 | @Override
63 | public void onNext(Note note) {
64 | Log.d(TAG, "Id: " + note.getId() + ", note: " + note.getNote());
65 | }
66 |
67 | @Override
68 | public void onError(Throwable e) {
69 | Log.e(TAG, "onError: " + e.getMessage());
70 | }
71 |
72 | @Override
73 | public void onComplete() {
74 | Log.d(TAG, "All notes are emitted!");
75 | }
76 | };
77 | }
78 |
79 | private Observable getNotesObservable() {
80 | final List notes = prepareNotes();
81 |
82 | return Observable.create(new ObservableOnSubscribe() {
83 | @Override
84 | public void subscribe(ObservableEmitter emitter) throws Exception {
85 | for (Note note : notes) {
86 | if (!emitter.isDisposed()) {
87 | emitter.onNext(note);
88 | }
89 | }
90 |
91 | if (!emitter.isDisposed()) {
92 | emitter.onComplete();
93 | }
94 | }
95 | });
96 | }
97 |
98 | private List prepareNotes() {
99 | List notes = new ArrayList<>();
100 | notes.add(new Note(1, "buy tooth paste!"));
101 | notes.add(new Note(2, "call brother!"));
102 | notes.add(new Note(3, "watch narcos tonight!"));
103 | notes.add(new Note(4, "pay power bill!"));
104 |
105 | return notes;
106 | }
107 |
108 | class Note {
109 | int id;
110 | String note;
111 |
112 | public Note(int id, String note) {
113 | this.id = id;
114 | this.note = note;
115 | }
116 |
117 | public int getId() {
118 | return id;
119 | }
120 |
121 | public String getNote() {
122 | return note;
123 | }
124 |
125 | public void setId(int id) {
126 | this.id = id;
127 | }
128 |
129 | public void setNote(String note) {
130 | this.note = note;
131 | }
132 | }
133 |
134 | @Override
135 | protected void onDestroy() {
136 | super.onDestroy();
137 | disposable.clear();
138 | }
139 | }
--------------------------------------------------------------------------------
/app/src/main/java/info/androidhive/rxandroidexamples/networking/retrofit/ApiClient.java:
--------------------------------------------------------------------------------
1 | package info.androidhive.rxandroidexamples.networking.retrofit;
2 |
3 | import android.util.Log;
4 |
5 | import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
6 |
7 | import java.io.IOException;
8 | import java.util.concurrent.TimeUnit;
9 |
10 | import info.androidhive.rxandroidexamples.app.Const;
11 | import okhttp3.Interceptor;
12 | import okhttp3.OkHttpClient;
13 | import okhttp3.Request;
14 | import okhttp3.Response;
15 | import okhttp3.logging.HttpLoggingInterceptor;
16 | import retrofit2.Retrofit;
17 | import retrofit2.converter.gson.GsonConverterFactory;
18 |
19 | /**
20 | * Created by ravi on 31/01/18.
21 | */
22 |
23 | public class ApiClient {
24 | private static String TAG = ApiClient.class.getSimpleName();
25 | private static Retrofit retrofit = null;
26 | private static int REQUEST_TIMEOUT = 60;
27 | private static OkHttpClient okHttpClient;
28 |
29 |
30 | public static Retrofit getClient() {
31 |
32 | if (okHttpClient == null)
33 | initOkHttp();
34 |
35 | if (retrofit == null) {
36 | retrofit = new Retrofit.Builder()
37 | .baseUrl(Const.BASE_URL)
38 | .client(okHttpClient)
39 | .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
40 | .addConverterFactory(GsonConverterFactory.create())
41 | .build();
42 | }
43 | return retrofit;
44 | }
45 |
46 | private static void initOkHttp() {
47 | OkHttpClient.Builder httpClient = new OkHttpClient().newBuilder()
48 | .connectTimeout(REQUEST_TIMEOUT, TimeUnit.SECONDS)
49 | .readTimeout(REQUEST_TIMEOUT, TimeUnit.SECONDS)
50 | .writeTimeout(REQUEST_TIMEOUT, TimeUnit.SECONDS);
51 |
52 | HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
53 | interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
54 |
55 | httpClient.addInterceptor(interceptor);
56 |
57 | httpClient.addInterceptor(new Interceptor() {
58 | @Override
59 | public Response intercept(Chain chain) throws IOException {
60 | Request original = chain.request();
61 | Request.Builder requestBuilder = original.newBuilder()
62 | .addHeader("Accept", "application/json")
63 | .addHeader("Request-Type", "Android")
64 | .addHeader("Content-Type", "application/json");
65 |
66 | Request request = requestBuilder.build();
67 | return chain.proceed(request);
68 | }
69 | });
70 |
71 | okHttpClient = httpClient.build();
72 | }
73 |
74 | public static void resetApiClient() {
75 | retrofit = null;
76 | okHttpClient = null;
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/app/src/main/java/info/androidhive/rxandroidexamples/networking/retrofit/ApiService.java:
--------------------------------------------------------------------------------
1 | package info.androidhive.rxandroidexamples.networking.retrofit;
2 |
3 | import java.util.List;
4 |
5 | import info.androidhive.rxandroidexamples.networking.retrofit.model.Contact;
6 | import io.reactivex.Single;
7 | import retrofit2.http.GET;
8 | import retrofit2.http.Query;
9 |
10 | /**
11 | * Created by ravi on 31/01/18.
12 | */
13 |
14 | public interface ApiService {
15 |
16 | @GET("contacts.php")
17 | Single> getContacts(@Query("source") String source, @Query("search") String query);
18 | }
19 |
--------------------------------------------------------------------------------
/app/src/main/java/info/androidhive/rxandroidexamples/networking/retrofit/ContactsListActivity.java:
--------------------------------------------------------------------------------
1 | package info.androidhive.rxandroidexamples.networking.retrofit;
2 |
3 | import android.support.v7.app.AppCompatActivity;
4 | import android.os.Bundle;
5 | import android.support.v7.widget.DefaultItemAnimator;
6 | import android.support.v7.widget.DividerItemDecoration;
7 | import android.support.v7.widget.LinearLayoutManager;
8 | import android.support.v7.widget.RecyclerView;
9 | import android.util.Log;
10 |
11 | import java.util.ArrayList;
12 | import java.util.List;
13 |
14 | import butterknife.BindView;
15 | import butterknife.ButterKnife;
16 | import butterknife.Unbinder;
17 | import info.androidhive.rxandroidexamples.R;
18 | import info.androidhive.rxandroidexamples.networking.retrofit.adapter.ContactsAdapter;
19 | import info.androidhive.rxandroidexamples.networking.retrofit.model.Contact;
20 | import io.reactivex.android.schedulers.AndroidSchedulers;
21 | import io.reactivex.disposables.CompositeDisposable;
22 | import io.reactivex.observers.DisposableSingleObserver;
23 | import io.reactivex.schedulers.Schedulers;
24 |
25 | public class ContactsListActivity extends AppCompatActivity implements ContactsAdapter.ContactsAdapterListener {
26 |
27 | private static final String TAG = ContactsListActivity.class.getSimpleName();
28 |
29 | private CompositeDisposable disposable = new CompositeDisposable();
30 | private ApiService apiService;
31 | private ContactsAdapter mAdapter;
32 | private List contactsList = new ArrayList<>();
33 |
34 | @BindView(R.id.recycler_view)
35 | RecyclerView recyclerView;
36 |
37 | private Unbinder unbinder;
38 |
39 | @Override
40 | protected void onCreate(Bundle savedInstanceState) {
41 | super.onCreate(savedInstanceState);
42 | setContentView(R.layout.activity_contacts_list);
43 | unbinder = ButterKnife.bind(this);
44 |
45 | mAdapter = new ContactsAdapter(this, contactsList, this);
46 |
47 | RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
48 | recyclerView.setLayoutManager(mLayoutManager);
49 | recyclerView.setItemAnimator(new DefaultItemAnimator());
50 | recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
51 | recyclerView.setAdapter(mAdapter);
52 |
53 | apiService = ApiClient.getClient().create(ApiService.class);
54 |
55 |
56 | // source: `gmail` or `linkedin`
57 | fetchContacts("gmail");
58 | }
59 |
60 | private void fetchContacts(String source) {
61 | disposable.add(apiService
62 | .getContacts(source, null)
63 | .subscribeOn(Schedulers.io())
64 | .observeOn(AndroidSchedulers.mainThread())
65 | .subscribeWith(new DisposableSingleObserver>() {
66 | @Override
67 | public void onSuccess(List contacts) {
68 | Log.d(TAG, "onSuccess: " + contacts.size());
69 | contactsList.clear();
70 | contactsList.addAll(contacts);
71 | mAdapter.notifyDataSetChanged();
72 | }
73 |
74 | @Override
75 | public void onError(Throwable e) {
76 |
77 | }
78 | }));
79 | }
80 |
81 | @Override
82 | protected void onDestroy() {
83 | disposable.clear();
84 | unbinder.unbind();
85 | super.onDestroy();
86 | }
87 |
88 | @Override
89 | public void onContactSelected(Contact contact) {
90 |
91 | }
92 | }
93 |
--------------------------------------------------------------------------------
/app/src/main/java/info/androidhive/rxandroidexamples/networking/retrofit/ContactsListMultiRemoteActivity.java:
--------------------------------------------------------------------------------
1 | package info.androidhive.rxandroidexamples.networking.retrofit;
2 |
3 | import android.support.v7.app.AppCompatActivity;
4 | import android.os.Bundle;
5 | import android.support.v7.widget.DefaultItemAnimator;
6 | import android.support.v7.widget.DividerItemDecoration;
7 | import android.support.v7.widget.LinearLayoutManager;
8 | import android.support.v7.widget.RecyclerView;
9 | import android.util.Log;
10 |
11 | import java.util.ArrayList;
12 | import java.util.List;
13 |
14 | import butterknife.BindView;
15 | import butterknife.ButterKnife;
16 | import butterknife.Unbinder;
17 | import info.androidhive.rxandroidexamples.R;
18 | import info.androidhive.rxandroidexamples.networking.retrofit.adapter.ContactsAdapter;
19 | import info.androidhive.rxandroidexamples.networking.retrofit.model.Contact;
20 | import io.reactivex.Single;
21 | import io.reactivex.android.schedulers.AndroidSchedulers;
22 | import io.reactivex.disposables.CompositeDisposable;
23 | import io.reactivex.functions.BiFunction;
24 | import io.reactivex.observers.DisposableSingleObserver;
25 | import io.reactivex.schedulers.Schedulers;
26 |
27 | public class ContactsListMultiRemoteActivity extends AppCompatActivity implements ContactsAdapter.ContactsAdapterListener {
28 |
29 | private static final String TAG = ContactsListMultiRemoteActivity.class.getSimpleName();
30 |
31 | private CompositeDisposable disposable = new CompositeDisposable();
32 | private ApiService apiService;
33 | private ContactsAdapter mAdapter;
34 | private List contactsList = new ArrayList<>();
35 |
36 | @BindView(R.id.recycler_view)
37 | RecyclerView recyclerView;
38 |
39 | private Unbinder unbinder;
40 |
41 | @Override
42 | protected void onCreate(Bundle savedInstanceState) {
43 | super.onCreate(savedInstanceState);
44 | setContentView(R.layout.activity_contacts_list_multi_remote);
45 | unbinder = ButterKnife.bind(this);
46 |
47 | mAdapter = new ContactsAdapter(this, contactsList, this);
48 |
49 | RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
50 | recyclerView.setLayoutManager(mLayoutManager);
51 | recyclerView.setItemAnimator(new DefaultItemAnimator());
52 | recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
53 | recyclerView.setAdapter(mAdapter);
54 |
55 | apiService = ApiClient.getClient().create(ApiService.class);
56 |
57 |
58 | // source: `gmail` or `linkedin`
59 | fetchContactsFromMultipleApiSources();
60 | }
61 |
62 | private void fetchContactsFromMultipleApiSources() {
63 | Single.zip(getGmailContacts(), getLinkedInContacts(),
64 | new BiFunction, List, List>() {
65 | @Override
66 | public List apply(List gmailContacts, List linkedInContacts) throws Exception {
67 | return compareAndMergeContacts(gmailContacts, linkedInContacts);
68 | }
69 | })
70 | .subscribeOn(Schedulers.io())
71 | .observeOn(AndroidSchedulers.mainThread())
72 | .subscribeWith(new DisposableSingleObserver>() {
73 | @Override
74 | public void onSuccess(List contacts) {
75 | Log.d(TAG, "onSuccess: " + contacts.size());
76 | contactsList.clear();
77 | contactsList.addAll(contacts);
78 | mAdapter.notifyDataSetChanged();
79 | }
80 |
81 | @Override
82 | public void onError(Throwable e) {
83 |
84 | }
85 | });
86 | }
87 |
88 | // merging two array lists
89 | private List compareAndMergeContacts(List gmailContacts, List linkedInContacts) {
90 | List contacts = new ArrayList<>();
91 | contacts.addAll(gmailContacts);
92 | for (Contact contact : linkedInContacts) {
93 | if (!contacts.contains(contact)) {
94 | contacts.add(contact);
95 | }
96 | }
97 | return contacts;
98 | }
99 |
100 | public Single> getGmailContacts() {
101 | return apiService.getContacts("gmail", null);
102 | }
103 |
104 | public Single> getLinkedInContacts() {
105 | return apiService.getContacts("linkedin", null);
106 | }
107 |
108 |
109 | @Override
110 | protected void onDestroy() {
111 | disposable.clear();
112 | unbinder.unbind();
113 | super.onDestroy();
114 | }
115 |
116 | @Override
117 | public void onContactSelected(Contact contact) {
118 |
119 | }
120 | }
121 |
--------------------------------------------------------------------------------
/app/src/main/java/info/androidhive/rxandroidexamples/networking/retrofit/adapter/ContactsAdapter.java:
--------------------------------------------------------------------------------
1 | package info.androidhive.rxandroidexamples.networking.retrofit.adapter;
2 |
3 | /**
4 | * Created by ravi on 31/01/18.
5 | */
6 |
7 | import android.content.Context;
8 | import android.support.v7.widget.RecyclerView;
9 | import android.view.LayoutInflater;
10 | import android.view.View;
11 | import android.view.ViewGroup;
12 | import android.widget.Filter;
13 | import android.widget.Filterable;
14 | import android.widget.ImageView;
15 | import android.widget.TextView;
16 |
17 | import com.bumptech.glide.Glide;
18 | import com.bumptech.glide.request.RequestOptions;
19 |
20 | import java.util.ArrayList;
21 | import java.util.List;
22 |
23 | import info.androidhive.rxandroidexamples.R;
24 | import info.androidhive.rxandroidexamples.networking.retrofit.model.Contact;
25 |
26 | public class ContactsAdapter extends RecyclerView.Adapter {
27 | private Context context;
28 | private List contactList;
29 | private ContactsAdapterListener listener;
30 |
31 | public class MyViewHolder extends RecyclerView.ViewHolder {
32 | public TextView name, phone;
33 | public ImageView thumbnail;
34 |
35 | public MyViewHolder(View view) {
36 | super(view);
37 | name = view.findViewById(R.id.name);
38 | phone = view.findViewById(R.id.phone);
39 | thumbnail = view.findViewById(R.id.thumbnail);
40 |
41 | view.setOnClickListener(new View.OnClickListener() {
42 | @Override
43 | public void onClick(View view) {
44 | // send selected contact in callback
45 | listener.onContactSelected(contactList.get(getAdapterPosition()));
46 | }
47 | });
48 | }
49 | }
50 |
51 |
52 | public ContactsAdapter(Context context, List contactList, ContactsAdapterListener listener) {
53 | this.context = context;
54 | this.listener = listener;
55 | this.contactList = contactList;
56 | }
57 |
58 | @Override
59 | public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
60 | View itemView = LayoutInflater.from(parent.getContext())
61 | .inflate(R.layout.contact_row_item, parent, false);
62 |
63 | return new MyViewHolder(itemView);
64 | }
65 |
66 | @Override
67 | public void onBindViewHolder(MyViewHolder holder, final int position) {
68 | final Contact contact = contactList.get(position);
69 | holder.name.setText(contact.getName());
70 | holder.phone.setText(contact.getPhone());
71 |
72 | Glide.with(context)
73 | .load(contact.getProfileImage())
74 | .apply(RequestOptions.circleCropTransform())
75 | .into(holder.thumbnail);
76 | }
77 |
78 | @Override
79 | public int getItemCount() {
80 | return contactList.size();
81 | }
82 |
83 | public interface ContactsAdapterListener {
84 | void onContactSelected(Contact contact);
85 | }
86 | }
87 |
--------------------------------------------------------------------------------
/app/src/main/java/info/androidhive/rxandroidexamples/networking/retrofit/adapter/ContactsAdapterFilterable.java:
--------------------------------------------------------------------------------
1 | package info.androidhive.rxandroidexamples.networking.retrofit.adapter;
2 |
3 | /**
4 | * Created by ravi on 31/01/18.
5 | */
6 |
7 | import android.content.Context;
8 | import android.support.v7.widget.RecyclerView;
9 | import android.view.LayoutInflater;
10 | import android.view.View;
11 | import android.view.ViewGroup;
12 | import android.widget.Filter;
13 | import android.widget.Filterable;
14 | import android.widget.ImageView;
15 | import android.widget.TextView;
16 |
17 | import com.bumptech.glide.Glide;
18 | import com.bumptech.glide.request.RequestOptions;
19 |
20 | import java.util.ArrayList;
21 | import java.util.List;
22 |
23 | import info.androidhive.rxandroidexamples.R;
24 | import info.androidhive.rxandroidexamples.networking.retrofit.model.Contact;
25 |
26 | public class ContactsAdapterFilterable extends RecyclerView.Adapter
27 | implements Filterable {
28 | private Context context;
29 | private List contactList;
30 | private List contactListFiltered;
31 | private ContactsAdapterListener listener;
32 |
33 | public class MyViewHolder extends RecyclerView.ViewHolder {
34 | public TextView name, phone;
35 | public ImageView thumbnail;
36 |
37 | public MyViewHolder(View view) {
38 | super(view);
39 | name = view.findViewById(R.id.name);
40 | phone = view.findViewById(R.id.phone);
41 | thumbnail = view.findViewById(R.id.thumbnail);
42 |
43 | view.setOnClickListener(new View.OnClickListener() {
44 | @Override
45 | public void onClick(View view) {
46 | // send selected contact in callback
47 | listener.onContactSelected(contactListFiltered.get(getAdapterPosition()));
48 | }
49 | });
50 | }
51 | }
52 |
53 |
54 | public ContactsAdapterFilterable(Context context, List contactList, ContactsAdapterListener listener) {
55 | this.context = context;
56 | this.listener = listener;
57 | this.contactList = contactList;
58 | this.contactListFiltered = contactList;
59 | }
60 |
61 | @Override
62 | public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
63 | View itemView = LayoutInflater.from(parent.getContext())
64 | .inflate(R.layout.contact_row_item, parent, false);
65 |
66 | return new MyViewHolder(itemView);
67 | }
68 |
69 | @Override
70 | public void onBindViewHolder(MyViewHolder holder, final int position) {
71 | final Contact contact = contactListFiltered.get(position);
72 | holder.name.setText(contact.getName());
73 | holder.phone.setText(contact.getPhone());
74 |
75 | Glide.with(context)
76 | .load(contact.getProfileImage())
77 | .apply(RequestOptions.circleCropTransform())
78 | .into(holder.thumbnail);
79 | }
80 |
81 | @Override
82 | public int getItemCount() {
83 | return contactListFiltered.size();
84 | }
85 |
86 | @Override
87 | public Filter getFilter() {
88 | return new Filter() {
89 | @Override
90 | protected FilterResults performFiltering(CharSequence charSequence) {
91 | String charString = charSequence.toString();
92 | if (charString.isEmpty()) {
93 | contactListFiltered = contactList;
94 | } else {
95 | List filteredList = new ArrayList<>();
96 | for (Contact row : contactList) {
97 |
98 | // name match condition. this might differ depending on your requirement
99 | // here we are looking for name or phone number match
100 | if (row.getName().toLowerCase().contains(charString.toLowerCase()) || row.getPhone().contains(charSequence)) {
101 | filteredList.add(row);
102 | }
103 | }
104 |
105 | contactListFiltered = filteredList;
106 | }
107 |
108 | FilterResults filterResults = new FilterResults();
109 | filterResults.values = contactListFiltered;
110 | return filterResults;
111 | }
112 |
113 | @Override
114 | protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
115 | contactListFiltered = (ArrayList) filterResults.values;
116 | notifyDataSetChanged();
117 | }
118 | };
119 | }
120 |
121 | public interface ContactsAdapterListener {
122 | void onContactSelected(Contact contact);
123 | }
124 | }
--------------------------------------------------------------------------------
/app/src/main/java/info/androidhive/rxandroidexamples/networking/retrofit/model/Contact.java:
--------------------------------------------------------------------------------
1 | package info.androidhive.rxandroidexamples.networking.retrofit.model;
2 |
3 | import com.google.gson.annotations.SerializedName;
4 |
5 | /**
6 | * Created by ravi on 31/01/18.
7 | */
8 |
9 | public class Contact {
10 | String name;
11 |
12 | @SerializedName("image")
13 | String profileImage;
14 |
15 | String phone;
16 | String email;
17 |
18 | public String getName() {
19 | return name;
20 | }
21 |
22 | public String getProfileImage() {
23 | return profileImage;
24 | }
25 |
26 | public String getPhone() {
27 | return phone;
28 | }
29 |
30 | public String getEmail() {
31 | return email;
32 | }
33 |
34 | @Override
35 | public boolean equals(Object obj) {
36 | if (obj != null && (obj instanceof Contact)) {
37 | return ((Contact) obj).getEmail().equalsIgnoreCase(email);
38 | }
39 | return false;
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/app/src/main/java/info/androidhive/rxandroidexamples/observers/CompletableObserverActivity.java:
--------------------------------------------------------------------------------
1 | package info.androidhive.rxandroidexamples.observers;
2 |
3 | import android.support.v7.app.AppCompatActivity;
4 | import android.os.Bundle;
5 | import android.util.Log;
6 |
7 | import info.androidhive.rxandroidexamples.R;
8 | import info.androidhive.rxandroidexamples.observers.model.Note;
9 | import io.reactivex.Completable;
10 | import io.reactivex.CompletableEmitter;
11 | import io.reactivex.CompletableObserver;
12 | import io.reactivex.CompletableOnSubscribe;
13 | import io.reactivex.android.schedulers.AndroidSchedulers;
14 | import io.reactivex.disposables.Disposable;
15 | import io.reactivex.schedulers.Schedulers;
16 |
17 | public class CompletableObserverActivity extends AppCompatActivity {
18 |
19 | private static final String TAG = CompletableObserverActivity.class.getSimpleName();
20 | private Disposable disposable;
21 |
22 | /**
23 | * Completable won't emit any item, instead it returns
24 | * Success or failure state
25 | * Consider an example of making a PUT request to server to update
26 | * something where you are not expecting any response but the
27 | * success status
28 | * -
29 | * Completable : CompletableObserver
30 | */
31 | // TODO - link to Retrofit tutorial
32 | @Override
33 | protected void onCreate(Bundle savedInstanceState) {
34 | super.onCreate(savedInstanceState);
35 | setContentView(R.layout.activity_completable_observer);
36 |
37 | Note note = new Note(1, "Home work!");
38 |
39 | Completable completableObservable = updateNote(note);
40 |
41 | CompletableObserver completableObserver = completableObserver();
42 |
43 | completableObservable
44 | .subscribeOn(Schedulers.io())
45 | .observeOn(AndroidSchedulers.mainThread())
46 | .subscribe(completableObserver);
47 | }
48 |
49 |
50 | /**
51 | * Assume this making PUT request to server to update the Note
52 | */
53 | private Completable updateNote(Note note) {
54 | return Completable.create(new CompletableOnSubscribe() {
55 | @Override
56 | public void subscribe(CompletableEmitter emitter) throws Exception {
57 | if (!emitter.isDisposed()) {
58 | Thread.sleep(1000);
59 | emitter.onComplete();
60 | }
61 | }
62 | });
63 | }
64 |
65 | private CompletableObserver completableObserver() {
66 | return new CompletableObserver() {
67 | @Override
68 | public void onSubscribe(Disposable d) {
69 | Log.d(TAG, "onSubscribe");
70 | disposable = d;
71 | }
72 |
73 | @Override
74 | public void onComplete() {
75 | Log.d(TAG, "onComplete: Note updated successfully!");
76 | }
77 |
78 | @Override
79 | public void onError(Throwable e) {
80 | Log.e(TAG, "onError: " + e.getMessage());
81 | }
82 | };
83 | }
84 |
85 | @Override
86 | protected void onDestroy() {
87 | super.onDestroy();
88 | disposable.dispose();
89 | }
90 | }
--------------------------------------------------------------------------------
/app/src/main/java/info/androidhive/rxandroidexamples/observers/FlowableObserverActivity.java:
--------------------------------------------------------------------------------
1 | package info.androidhive.rxandroidexamples.observers;
2 |
3 | import android.os.Bundle;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.util.Log;
6 |
7 | import info.androidhive.rxandroidexamples.R;
8 | import io.reactivex.Flowable;
9 | import io.reactivex.SingleObserver;
10 | import io.reactivex.android.schedulers.AndroidSchedulers;
11 | import io.reactivex.disposables.Disposable;
12 | import io.reactivex.functions.BiFunction;
13 | import io.reactivex.schedulers.Schedulers;
14 |
15 | public class FlowableObserverActivity extends AppCompatActivity {
16 |
17 | private static final String TAG = FlowableObserverActivity.class.getSimpleName();
18 | private Disposable disposable;
19 |
20 | /**
21 | * Simple example of Flowable just to show the syntax
22 | * the use of Flowable is best explained when used with BackPressure
23 | * Read the below link to know the best use cases to use Flowable operator
24 | * https://github.com/ReactiveX/RxJava/wiki/What%27s-different-in-2.0#when-to-use-flowable
25 | * -
26 | * Flowable : SingleObserver
27 | */
28 |
29 | @Override
30 | protected void onCreate(Bundle savedInstanceState) {
31 | super.onCreate(savedInstanceState);
32 | setContentView(R.layout.activity_flowable_observer);
33 |
34 | Flowable flowableObservable = getFlowableObservable();
35 |
36 | SingleObserver observer = getFlowableObserver();
37 |
38 | flowableObservable
39 | .subscribeOn(Schedulers.io())
40 | .observeOn(AndroidSchedulers.mainThread())
41 | .reduce(0, new BiFunction() {
42 | @Override
43 | public Integer apply(Integer result, Integer number) {
44 | //Log.e(TAG, "Result: " + result + ", new number: " + number);
45 | return result + number;
46 | }
47 | })
48 | .subscribe(observer);
49 | }
50 |
51 | private SingleObserver getFlowableObserver() {
52 | return new SingleObserver() {
53 | @Override
54 | public void onSubscribe(Disposable d) {
55 | Log.d(TAG, "onSubscribe");
56 | disposable = d;
57 | }
58 |
59 | @Override
60 | public void onSuccess(Integer integer) {
61 | Log.d(TAG, "onSuccess: " + integer);
62 | }
63 |
64 | @Override
65 | public void onError(Throwable e) {
66 | Log.e(TAG, "onError: " + e.getMessage());
67 | }
68 | };
69 | }
70 |
71 | private Flowable getFlowableObservable() {
72 | return Flowable.range(1, 100);
73 | }
74 |
75 | @Override
76 | protected void onDestroy() {
77 | super.onDestroy();
78 | disposable.dispose();
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/app/src/main/java/info/androidhive/rxandroidexamples/observers/MaybeObserverActivity.java:
--------------------------------------------------------------------------------
1 | package info.androidhive.rxandroidexamples.observers;
2 |
3 | import android.support.v7.app.AppCompatActivity;
4 | import android.os.Bundle;
5 | import android.util.Log;
6 |
7 | import info.androidhive.rxandroidexamples.R;
8 | import info.androidhive.rxandroidexamples.observers.model.Note;
9 | import io.reactivex.Maybe;
10 | import io.reactivex.MaybeEmitter;
11 | import io.reactivex.MaybeObserver;
12 | import io.reactivex.MaybeOnSubscribe;
13 | import io.reactivex.android.schedulers.AndroidSchedulers;
14 | import io.reactivex.disposables.Disposable;
15 | import io.reactivex.schedulers.Schedulers;
16 |
17 | public class MaybeObserverActivity extends AppCompatActivity {
18 |
19 | private static final String TAG = MaybeObserverActivity.class.getSimpleName();
20 | private Disposable disposable;
21 |
22 | /**
23 | * Consider an example getting a note from db using ID
24 | * There is possibility of not finding the note by ID in the db
25 | * In this situation, MayBe can be used
26 | * -
27 | * Maybe : MaybeObserver
28 | */
29 |
30 | @Override
31 | protected void onCreate(Bundle savedInstanceState) {
32 | super.onCreate(savedInstanceState);
33 | setContentView(R.layout.activity_maybe_observer);
34 |
35 | Maybe noteObservable = getNoteObservable();
36 |
37 | MaybeObserver noteObserver = getNoteObserver();
38 |
39 | noteObservable.subscribeOn(Schedulers.io())
40 | .observeOn(AndroidSchedulers.mainThread())
41 | .subscribe(noteObserver);
42 | }
43 |
44 | private MaybeObserver getNoteObserver() {
45 | return new MaybeObserver() {
46 | @Override
47 | public void onSubscribe(Disposable d) {
48 | disposable = d;
49 | }
50 |
51 | @Override
52 | public void onSuccess(Note note) {
53 | Log.d(TAG, "onSuccess: " + note.getNote());
54 | }
55 |
56 | @Override
57 | public void onError(Throwable e) {
58 | Log.e(TAG, "onError: " + e.getMessage());
59 | }
60 |
61 | @Override
62 | public void onComplete() {
63 | Log.e(TAG, "onComplete");
64 | }
65 | };
66 | }
67 |
68 | /**
69 | * Emits optional data (0 or 1 emission)
70 | * But for now it emits 1 Note always
71 | */
72 | private Maybe getNoteObservable() {
73 | return Maybe.create(new MaybeOnSubscribe() {
74 | @Override
75 | public void subscribe(MaybeEmitter emitter) throws Exception {
76 | Note note = new Note(1, "Call brother!");
77 | if (!emitter.isDisposed()) {
78 | emitter.onSuccess(note);
79 | }
80 | }
81 | });
82 | }
83 |
84 | @Override
85 | protected void onDestroy() {
86 | super.onDestroy();
87 | disposable.dispose();
88 | }
89 | }
--------------------------------------------------------------------------------
/app/src/main/java/info/androidhive/rxandroidexamples/observers/ObserverActivity.java:
--------------------------------------------------------------------------------
1 | package info.androidhive.rxandroidexamples.observers;
2 |
3 | import android.os.Bundle;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.util.Log;
6 |
7 | import java.util.ArrayList;
8 | import java.util.List;
9 |
10 | import info.androidhive.rxandroidexamples.R;
11 | import info.androidhive.rxandroidexamples.observers.model.Note;
12 | import io.reactivex.Observable;
13 | import io.reactivex.ObservableEmitter;
14 | import io.reactivex.ObservableOnSubscribe;
15 | import io.reactivex.Observer;
16 | import io.reactivex.android.schedulers.AndroidSchedulers;
17 | import io.reactivex.disposables.Disposable;
18 | import io.reactivex.schedulers.Schedulers;
19 |
20 | public class ObserverActivity extends AppCompatActivity {
21 |
22 | private static final String TAG = ObserverActivity.class.getSimpleName();
23 | private Disposable disposable;
24 |
25 | /**
26 | * Simple Observable emitting multiple Notes
27 | * -
28 | * Observable : Observer
29 | */
30 |
31 | @Override
32 | protected void onCreate(Bundle savedInstanceState) {
33 | super.onCreate(savedInstanceState);
34 | setContentView(R.layout.activity_observer);
35 |
36 | Observable notesObservable = getNotesObservable();
37 |
38 | Observer notesObserver = getNotesObserver();
39 |
40 | notesObservable.observeOn(Schedulers.io())
41 | .subscribeOn(AndroidSchedulers.mainThread())
42 | .subscribeWith(notesObserver);
43 | }
44 |
45 | private Observer getNotesObserver() {
46 | return new Observer() {
47 |
48 | @Override
49 | public void onSubscribe(Disposable d) {
50 | Log.d(TAG, "onSubscribe");
51 | disposable = d;
52 | }
53 |
54 | @Override
55 | public void onNext(Note note) {
56 | Log.d(TAG, "onNext: " + note.getNote());
57 | }
58 |
59 | @Override
60 | public void onError(Throwable e) {
61 | Log.e(TAG, "onError: " + e.getMessage());
62 | }
63 |
64 | @Override
65 | public void onComplete() {
66 | Log.d(TAG, "onComplete");
67 | }
68 | };
69 | }
70 |
71 | private Observable getNotesObservable() {
72 | final List notes = prepareNotes();
73 |
74 | return Observable.create(new ObservableOnSubscribe() {
75 | @Override
76 | public void subscribe(ObservableEmitter emitter) throws Exception {
77 | for (Note note : notes) {
78 | if (!emitter.isDisposed()) {
79 | emitter.onNext(note);
80 | }
81 | }
82 |
83 | // all notes are emitted
84 | if (!emitter.isDisposed()) {
85 | emitter.onComplete();
86 | }
87 | }
88 | });
89 | }
90 |
91 | private List prepareNotes() {
92 | List notes = new ArrayList<>();
93 | notes.add(new Note(1, "Buy tooth paste!"));
94 | notes.add(new Note(2, "Call brother!"));
95 | notes.add(new Note(3, "Watch Narcos tonight!"));
96 | notes.add(new Note(4, "Pay power bill!"));
97 | return notes;
98 | }
99 |
100 | @Override
101 | protected void onDestroy() {
102 | super.onDestroy();
103 | disposable.dispose();
104 | }
105 | }
106 |
--------------------------------------------------------------------------------
/app/src/main/java/info/androidhive/rxandroidexamples/observers/SingleObserverActivity.java:
--------------------------------------------------------------------------------
1 | package info.androidhive.rxandroidexamples.observers;
2 |
3 | import android.os.Bundle;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.util.Log;
6 |
7 | import info.androidhive.rxandroidexamples.R;
8 | import info.androidhive.rxandroidexamples.observers.model.Note;
9 | import io.reactivex.Single;
10 | import io.reactivex.SingleEmitter;
11 | import io.reactivex.SingleObserver;
12 | import io.reactivex.SingleOnSubscribe;
13 | import io.reactivex.android.schedulers.AndroidSchedulers;
14 | import io.reactivex.disposables.Disposable;
15 | import io.reactivex.schedulers.Schedulers;
16 |
17 | public class SingleObserverActivity extends AppCompatActivity {
18 |
19 | private static final String TAG = SingleObserverActivity.class.getSimpleName();
20 | private Disposable disposable;
21 |
22 | /**
23 | * Single Observable emitting single Note
24 | * Single Observable is more useful in making network calls
25 | * where you expect a single response object to be emitted
26 | * -
27 | * Single : SingleObserver
28 | */
29 |
30 | // TODO - link to Retrofit tutorial
31 | @Override
32 | protected void onCreate(Bundle savedInstanceState) {
33 | super.onCreate(savedInstanceState);
34 | setContentView(R.layout.activity_single_observer);
35 |
36 | Single noteObservable = getNoteObservable();
37 |
38 | SingleObserver singleObserver = getSingleObserver();
39 |
40 | noteObservable
41 | .observeOn(Schedulers.io())
42 | .subscribeOn(AndroidSchedulers.mainThread())
43 | .subscribe(singleObserver);
44 |
45 | }
46 |
47 | private SingleObserver getSingleObserver() {
48 | return new SingleObserver() {
49 | @Override
50 | public void onSubscribe(Disposable d) {
51 | Log.d(TAG, "onSubscribe");
52 | disposable = d;
53 | }
54 |
55 | @Override
56 | public void onSuccess(Note note) {
57 | Log.e(TAG, "onSuccess: " + note.getNote());
58 | }
59 |
60 | @Override
61 | public void onError(Throwable e) {
62 | Log.d(TAG, "onError: " + e.getMessage());
63 | }
64 | };
65 | }
66 |
67 | private Single getNoteObservable() {
68 | return Single.create(new SingleOnSubscribe() {
69 | @Override
70 | public void subscribe(SingleEmitter emitter) throws Exception {
71 | Note note = new Note(1, "Buy milk!");
72 | emitter.onSuccess(note);
73 | }
74 | });
75 | }
76 |
77 | @Override
78 | protected void onDestroy() {
79 | super.onDestroy();
80 | disposable.dispose();
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/app/src/main/java/info/androidhive/rxandroidexamples/observers/model/Note.java:
--------------------------------------------------------------------------------
1 | package info.androidhive.rxandroidexamples.observers.model;
2 |
3 | /**
4 | * Created by ravi on 30/01/18.
5 | */
6 |
7 | public class Note {
8 | int id;
9 | String note;
10 |
11 | public Note(int id, String note) {
12 | this.id = id;
13 | this.note = note;
14 | }
15 |
16 | public int getId() {
17 | return id;
18 | }
19 |
20 | public String getNote() {
21 | return note;
22 | }
23 |
24 | @Override
25 | public boolean equals(Object obj) {
26 | if (obj == this) {
27 | return true;
28 | }
29 |
30 | if (!(obj instanceof Note)) {
31 | return false;
32 | }
33 |
34 | return note.equalsIgnoreCase(((Note) obj).getNote());
35 | }
36 |
37 | @Override
38 | public int hashCode() {
39 | int hash = 3;
40 | hash = 53 * hash + (this.note != null ? this.note.hashCode() : 0);
41 | return hash;
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/app/src/main/java/info/androidhive/rxandroidexamples/operators/AverageOperatorActivity.java:
--------------------------------------------------------------------------------
1 | package info.androidhive.rxandroidexamples.operators;
2 |
3 | import android.support.v7.app.AppCompatActivity;
4 | import android.os.Bundle;
5 | import android.util.Log;
6 |
7 | import info.androidhive.rxandroidexamples.R;
8 | import rx.Observable;
9 | import rx.Subscriber;
10 | import rx.observables.MathObservable;
11 |
12 | public class AverageOperatorActivity extends AppCompatActivity {
13 |
14 | private static final String TAG = AverageOperatorActivity.class.getSimpleName();
15 |
16 | @Override
17 | protected void onCreate(Bundle savedInstanceState) {
18 | super.onCreate(savedInstanceState);
19 | setContentView(R.layout.activity_average_operator);
20 |
21 | Observable numbersObservable = Observable.just(1, 2, 3, 4, 5);
22 | MathObservable.averageInteger(numbersObservable)
23 | .subscribe(new rx.Observer() {
24 | @Override
25 | public void onCompleted() {
26 |
27 | }
28 |
29 | @Override
30 | public void onError(Throwable e) {
31 |
32 | }
33 |
34 | @Override
35 | public void onNext(Integer integer) {
36 | Log.e(TAG, "Average of 1,2,3,4,5 = " + integer);
37 | }
38 | });
39 |
40 | Observable floatObservable = Observable.just(10.5f, 11.5f, 14.5f);
41 | MathObservable.averageFloat(floatObservable)
42 | .subscribe(new Subscriber() {
43 | @Override
44 | public void onCompleted() {
45 |
46 | }
47 |
48 | @Override
49 | public void onError(Throwable e) {
50 |
51 | }
52 |
53 | @Override
54 | public void onNext(Float aFloat) {
55 | Log.e(TAG, "Average of 10.5f, 11.5f, 14.5f= " + aFloat);
56 | }
57 | });
58 |
59 | Observable doubleObservable = Observable.just(13.5D, 45.3D, 33.6D);
60 | MathObservable.averageDouble(doubleObservable)
61 | .subscribe(new Subscriber() {
62 | @Override
63 | public void onCompleted() {
64 |
65 | }
66 |
67 | @Override
68 | public void onError(Throwable e) {
69 |
70 | }
71 |
72 | @Override
73 | public void onNext(Double aDouble) {
74 | Log.e(TAG, "Average of 13.5D, 45.3D, 33.6D= " + aDouble);
75 | }
76 | });
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/app/src/main/java/info/androidhive/rxandroidexamples/operators/BufferOperatorActivity.java:
--------------------------------------------------------------------------------
1 | package info.androidhive.rxandroidexamples.operators;
2 |
3 | import android.os.Bundle;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.util.Log;
6 | import android.widget.Button;
7 | import android.widget.TextView;
8 |
9 | import com.jakewharton.rxbinding2.view.RxView;
10 |
11 | import java.util.List;
12 | import java.util.concurrent.TimeUnit;
13 |
14 | import butterknife.BindView;
15 | import butterknife.ButterKnife;
16 | import butterknife.Unbinder;
17 | import info.androidhive.rxandroidexamples.R;
18 | import io.reactivex.Observable;
19 | import io.reactivex.Observer;
20 | import io.reactivex.android.schedulers.AndroidSchedulers;
21 | import io.reactivex.disposables.Disposable;
22 | import io.reactivex.functions.Function;
23 | import io.reactivex.schedulers.Schedulers;
24 |
25 | public class BufferOperatorActivity extends AppCompatActivity {
26 |
27 | private static final String TAG = BufferOperatorActivity.class.getSimpleName();
28 |
29 | @BindView(R.id.tap_result)
30 | TextView txtTapResult;
31 |
32 | @BindView(R.id.tap_result_max_count)
33 | TextView txtTapResultMax;
34 |
35 | @BindView(R.id.layout_tap_area)
36 | Button btnTapArea;
37 |
38 | private Disposable disposable;
39 | private Unbinder unbinder;
40 | private int maxTaps = 0;
41 |
42 | @Override
43 | protected void onCreate(Bundle savedInstanceState) {
44 | super.onCreate(savedInstanceState);
45 | setContentView(R.layout.activity_buffer_operator);
46 | unbinder = ButterKnife.bind(this);
47 |
48 | RxView.clicks(btnTapArea)
49 | .map(new Function