Skip to content

Commit 01cb766

Browse files
author
Ramakrishna V Joshi
committed
Initial commit
0 parents  commit 01cb766

34 files changed

+852
-0
lines changed

.gitignore

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/caches
5+
/.idea/libraries
6+
/.idea/modules.xml
7+
/.idea/workspace.xml
8+
/.idea/navEditor.xml
9+
/.idea/assetWizardSettings.xml
10+
.DS_Store
11+
/build
12+
/captures
13+
.externalNativeBuild

app/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

app/build.gradle

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 28
5+
defaultConfig {
6+
applicationId "com.example.reactiveprogrammingusingrxjava2"
7+
minSdkVersion 23
8+
targetSdkVersion 28
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-optimize.txt'), 'proguard-rules.pro'
17+
}
18+
}
19+
compileOptions { //enables java 8 features like lambda....confirm this
20+
sourceCompatibility = '1.8'
21+
targetCompatibility = '1.8'
22+
}
23+
}
24+
25+
dependencies {
26+
implementation fileTree(include: ['*.jar'], dir: 'libs')
27+
implementation 'com.android.support:appcompat-v7:28.0.0'
28+
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
29+
30+
//RxJava2 : rx-android and rx-java
31+
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
32+
// Because RxAndroid releases are few and far between, it is recommended you also
33+
// explicitly depend on RxJava's latest version for bug fixes and new features.
34+
// (see https://github.com/ReactiveX/RxJava/releases for latest 2.x.x version)
35+
implementation 'io.reactivex.rxjava2:rxjava:2.2.7'
36+
37+
//Retrofit dependencies from /libs
38+
/*implementation files('libs/retrofit-2.5.0.jar')
39+
implementation files('libs/okhttp-3.14.0.jar')
40+
implementation files('libs/okio-2.2.2.jar')
41+
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.21'
42+
implementation files('libs/gson-2.8.5.jar')*/
43+
44+
45+
//Retrofit dependency
46+
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
47+
//from retrofit 2.0 converter is a must Ex: Gson, Moshi...In code add it in .addConverterFactory()
48+
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
49+
//To see requests and responses in LogCat
50+
implementation 'com.squareup.okhttp3:logging-interceptor:3.6.0'
51+
//An Adapter for adapting RxJava 2.x types.
52+
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.2.0'
53+
54+
55+
56+
}

app/proguard-rules.pro

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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

app/src/main/AndroidManifest.xml

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
package="com.example.reactiveprogrammingusingrxjava2">
5+
6+
<uses-permission android:name="android.permission.INTERNET"/>
7+
8+
<application
9+
android:allowBackup="true"
10+
android:icon="@mipmap/ic_launcher"
11+
android:label="@string/app_name"
12+
android:roundIcon="@mipmap/ic_launcher_round"
13+
android:supportsRtl="true"
14+
android:theme="@style/AppTheme"
15+
tools:ignore="GoogleAppIndexingWarning"
16+
android:networkSecurityConfig="@xml/network_security_config">
17+
18+
<activity android:name=".retrofit_api_calls.RetrofitAPICallActivity">
19+
<!--<intent-filter>
20+
<action android:name="android.intent.action.MAIN" />
21+
<category android:name="android.intent.category.LAUNCHER" />
22+
</intent-filter>-->
23+
</activity>
24+
25+
<activity android:name=".MainActivity">
26+
<!--<intent-filter>-->
27+
<!--<action android:name="android.intent.action.MAIN" />-->
28+
<!--<category android:name="android.intent.category.LAUNCHER" />-->
29+
<!--</intent-filter>-->
30+
</activity>
31+
<activity android:name=".rx_api_calls.RxAPICallActivity" >
32+
<intent-filter>
33+
<action android:name="android.intent.action.MAIN" />
34+
<category android:name="android.intent.category.LAUNCHER" />
35+
</intent-filter>
36+
</activity>
37+
</application>
38+
39+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.example.reactiveprogrammingusingrxjava2;
2+
3+
import io.reactivex.Observable;
4+
import io.reactivex.Observer;
5+
import io.reactivex.android.schedulers.AndroidSchedulers;
6+
import io.reactivex.disposables.Disposable;
7+
import io.reactivex.schedulers.Schedulers;
8+
9+
public class RxConcept1Just {
10+
11+
public static void main(String[] args) {
12+
13+
justOperatorWorking();
14+
15+
//rangeOperatorWorking();
16+
}
17+
18+
private static void rangeOperatorWorking() {
19+
Observable<Integer> observable = Observable.range(5,3);
20+
Observer observer = new Observer() {
21+
22+
Disposable disposable = new Disposable() {
23+
@Override
24+
public void dispose() {
25+
26+
}
27+
28+
@Override
29+
public boolean isDisposed() {
30+
return false;
31+
}
32+
};
33+
34+
@Override
35+
public void onSubscribe(Disposable d) {
36+
disposable = d;
37+
System.out.println("RxConcept1Just.onSubscribe");
38+
}
39+
40+
@Override
41+
public void onNext(Object o) {
42+
System.out.println("RxConcept1Just.onNext");
43+
System.out.println("o = [" + o + "]");
44+
System.out.println("onNext: d.isDisposed() :" + disposable.isDisposed()); //returns false
45+
}
46+
47+
@Override
48+
public void onError(Throwable e) {
49+
System.out.println("e = [" + e + "]");
50+
}
51+
52+
@Override
53+
public void onComplete() {
54+
System.out.println("RxConcept1Just.onComplete");
55+
System.out.println("d.isDisposed() :" + disposable.isDisposed()); //returns true
56+
}
57+
};
58+
59+
observable.subscribe(observer);
60+
}
61+
62+
private static void justOperatorWorking() {
63+
Observable<Integer> observable = Observable.just(1,2,3,4);
64+
65+
Observer observer = new Observer() {
66+
@Override
67+
public void onSubscribe(Disposable d) {
68+
69+
}
70+
71+
@Override
72+
public void onNext(Object o) {
73+
System.out.println("o = [" + o + "]");
74+
}
75+
76+
@Override
77+
public void onError(Throwable e) {
78+
79+
}
80+
81+
@Override
82+
public void onComplete() {
83+
84+
}
85+
};
86+
87+
observable
88+
.observeOn(Schedulers.io())
89+
.subscribeOn(AndroidSchedulers.mainThread())
90+
.subscribe(observer);
91+
}
92+
}

0 commit comments

Comments
 (0)