Skip to content

Commit 97f4832

Browse files
committed
Adds simple RxJava example to repo
1 parent 716ea03 commit 97f4832

File tree

32 files changed

+1169
-0
lines changed

32 files changed

+1169
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/workspace.xml
5+
/.idea/libraries
6+
.DS_Store
7+
/build
8+
/captures
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 23
5+
buildToolsVersion "23.0.2"
6+
7+
defaultConfig {
8+
applicationId "com.vogella.android.rxjava.simple"
9+
minSdkVersion 23
10+
targetSdkVersion 23
11+
versionCode 1
12+
versionName "1.0"
13+
}
14+
buildTypes {
15+
release {
16+
minifyEnabled false
17+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18+
}
19+
}
20+
}
21+
22+
dependencies {
23+
compile fileTree(dir: 'libs', include: ['*.jar'])
24+
testCompile 'junit:junit:4.12'
25+
compile 'com.android.support:appcompat-v7:23.1.1'
26+
compile 'com.android.support:recyclerview-v7:23.1.1'
27+
compile 'io.reactivex:rxandroid:1.1.0'
28+
compile 'io.reactivex:rxjava:1.1.0'
29+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in /home/vogella/Android/Sdk/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.vogella.android.rxjava.simple;
2+
3+
import android.app.Application;
4+
import android.test.ApplicationTestCase;
5+
6+
/**
7+
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
8+
*/
9+
public class ApplicationTest extends ApplicationTestCase<Application> {
10+
public ApplicationTest() {
11+
super(Application.class);
12+
}
13+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.vogella.android.rxjava.simple">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:icon="@mipmap/ic_launcher"
8+
android:label="@string/app_name"
9+
android:supportsRtl="true"
10+
android:theme="@style/AppTheme">
11+
<activity android:name=".Example2Activity">
12+
<intent-filter>
13+
<action android:name="android.intent.action.MAIN" />
14+
15+
<category android:name="android.intent.category.LAUNCHER" />
16+
</intent-filter>
17+
</activity>
18+
</application>
19+
20+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.vogella.android.rxjava.simple;
2+
3+
import android.os.Bundle;
4+
import android.support.v7.app.AppCompatActivity;
5+
import android.support.v7.widget.LinearLayoutManager;
6+
import android.support.v7.widget.RecyclerView;
7+
import android.view.View;
8+
import android.widget.ProgressBar;
9+
10+
import java.util.List;
11+
import java.util.concurrent.Callable;
12+
13+
import rx.Observable;
14+
import rx.Observer;
15+
import rx.Subscription;
16+
import rx.android.schedulers.AndroidSchedulers;
17+
import rx.schedulers.Schedulers;
18+
19+
public class Example2Activity extends AppCompatActivity {
20+
21+
private Subscription mTvShowSubscription;
22+
private RecyclerView mTvShowListView;
23+
private ProgressBar mProgressBar;
24+
private SimpleStringAdapter mSimpleStringAdapter;
25+
private RestClient mRestClient;
26+
27+
@Override
28+
protected void onCreate(Bundle savedInstanceState) {
29+
super.onCreate(savedInstanceState);
30+
mRestClient = new RestClient(this);
31+
configureLayout();
32+
createObservable();
33+
}
34+
35+
private void createObservable() {
36+
Observable<List<String>> tvShowObservable = Observable.fromCallable(new Callable<List<String>>() {
37+
@Override
38+
public List<String> call() {
39+
return mRestClient.getFavoriteTvShows();
40+
}
41+
});
42+
43+
mTvShowSubscription = tvShowObservable
44+
.subscribeOn(Schedulers.io())
45+
.observeOn(AndroidSchedulers.mainThread())
46+
.subscribe(
47+
new Observer<List<String>>() {
48+
@Override
49+
public void onCompleted() {
50+
51+
}
52+
53+
@Override
54+
public void onError(Throwable e) {
55+
56+
}
57+
58+
@Override
59+
public void onNext(List<String> tvShows) {
60+
displayTvShows(tvShows);
61+
}
62+
});
63+
}
64+
65+
@Override
66+
protected void onDestroy() {
67+
super.onDestroy();
68+
69+
if (mTvShowSubscription != null && !mTvShowSubscription.isUnsubscribed()) {
70+
mTvShowSubscription.unsubscribe();
71+
}
72+
}
73+
74+
private void displayTvShows(List<String> tvShows) {
75+
mSimpleStringAdapter.setStrings(tvShows);
76+
mProgressBar.setVisibility(View.GONE);
77+
mTvShowListView.setVisibility(View.VISIBLE);
78+
}
79+
80+
private void configureLayout() {
81+
setContentView(R.layout.activity_example2);
82+
mProgressBar = (ProgressBar) findViewById(R.id.loader);
83+
mTvShowListView = (RecyclerView) findViewById(R.id.tv_show_list);
84+
mTvShowListView.setLayoutManager(new LinearLayoutManager(this));
85+
mSimpleStringAdapter = new SimpleStringAdapter(this);
86+
mTvShowListView.setAdapter(mSimpleStringAdapter);
87+
}
88+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.vogella.android.rxjava.simple;
2+
3+
import android.os.Bundle;
4+
import android.support.v7.app.AppCompatActivity;
5+
import android.support.v7.widget.LinearLayoutManager;
6+
import android.support.v7.widget.RecyclerView;
7+
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
import rx.Observable;
12+
import rx.Observer;
13+
14+
public class MainActivity extends AppCompatActivity {
15+
16+
RecyclerView mColorListView;
17+
SimpleStringAdapter mSimpleStringAdapter;
18+
19+
@Override
20+
protected void onCreate(Bundle savedInstanceState) {
21+
super.onCreate(savedInstanceState);
22+
configureLayout();
23+
createObservable();
24+
}
25+
26+
private void createObservable() {
27+
Observable<List<String>> listObservable = Observable.just(getColorList());
28+
29+
listObservable.subscribe(new Observer<List<String>>() {
30+
31+
@Override
32+
public void onCompleted() {
33+
34+
}
35+
36+
@Override
37+
public void onError(Throwable e) {
38+
39+
}
40+
41+
@Override
42+
public void onNext(List<String> colors) {
43+
mSimpleStringAdapter.setStrings(colors);
44+
}
45+
});
46+
47+
}
48+
49+
private void configureLayout() {
50+
setContentView(R.layout.activity_main);
51+
mColorListView = (RecyclerView) findViewById(R.id.color_list);
52+
mColorListView.setLayoutManager(new LinearLayoutManager(this));
53+
mSimpleStringAdapter = new SimpleStringAdapter(this);
54+
mColorListView.setAdapter(mSimpleStringAdapter);
55+
}
56+
57+
private static List<String> getColorList() {
58+
ArrayList<String> colors = new ArrayList<>();
59+
colors.add("blue");
60+
colors.add("green");
61+
colors.add("red");
62+
colors.add("chartreuse");
63+
colors.add("Van Dyke Brown");
64+
return colors;
65+
}
66+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.vogella.android.rxjava.simple;
2+
3+
import android.content.Context;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
/**
9+
* This is a mock REST Client. It simulates making blocking calls to an REST endpoint.
10+
*/
11+
public class RestClient {
12+
private Context mContext;
13+
14+
public RestClient(Context context) {
15+
mContext = context;
16+
}
17+
18+
public List<String> getFavoriteTvShows() {
19+
try {
20+
// "Simulate" the delay of network.
21+
Thread.sleep(5000);
22+
} catch (InterruptedException e) {
23+
e.printStackTrace();
24+
}
25+
return createTvShowList();
26+
}
27+
28+
public List<String> getFavoriteTvShowsWithException() {
29+
try {
30+
// "Simulate" the delay of network.
31+
Thread.sleep(5000);
32+
} catch (InterruptedException e) {
33+
e.printStackTrace();
34+
}
35+
throw new RuntimeException("Failed to load");
36+
}
37+
38+
private List<String> createTvShowList() {
39+
List<String> tvShows = new ArrayList<>();
40+
tvShows.add("The Joy of Painting");
41+
tvShows.add("The Simpsons");
42+
tvShows.add("Futurama");
43+
tvShows.add("Rick & Morty");
44+
tvShows.add("The X-Files");
45+
tvShows.add("Star Trek: The Next Generation");
46+
tvShows.add("Archer");
47+
tvShows.add("30 Rock");
48+
tvShows.add("Bob's Burgers");
49+
tvShows.add("Breaking Bad");
50+
tvShows.add("Parks and Recreation");
51+
tvShows.add("House of Cards");
52+
tvShows.add("Game of Thrones");
53+
tvShows.add("Law And Order");
54+
return tvShows;
55+
}
56+
57+
public List<String> searchForCity(String searchString) {
58+
try {
59+
// "Simulate" the delay of network.
60+
Thread.sleep(500);
61+
} catch (InterruptedException e) {
62+
e.printStackTrace();
63+
}
64+
return getMatchingCities(searchString);
65+
}
66+
67+
private List<String> getMatchingCities(String searchString) {
68+
if (searchString.isEmpty()) {
69+
return new ArrayList<>();
70+
}
71+
72+
String[] cities = mContext.getResources().getStringArray(R.array.city_list);
73+
List<String> toReturn = new ArrayList<>();
74+
for (String city : cities) {
75+
if (city.toLowerCase().startsWith(searchString.toLowerCase())) {
76+
toReturn.add(city);
77+
}
78+
}
79+
return toReturn;
80+
}
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.vogella.android.rxjava.simple;
2+
3+
import android.content.Context;
4+
import android.support.v7.widget.RecyclerView;
5+
import android.view.LayoutInflater;
6+
import android.view.View;
7+
import android.view.ViewGroup;
8+
import android.widget.TextView;
9+
import android.widget.Toast;
10+
11+
import java.util.ArrayList;
12+
import java.util.List;
13+
14+
/**
15+
* Adapter used to map a String to a text view.
16+
*/
17+
public class SimpleStringAdapter extends RecyclerView.Adapter<SimpleStringAdapter.ViewHolder> {
18+
19+
private final Context mContext;
20+
private final List<String> mStrings = new ArrayList<>();
21+
22+
public SimpleStringAdapter(Context context) {
23+
mContext = context;
24+
}
25+
26+
public void setStrings(List<String> newStrings) {
27+
mStrings.clear();
28+
mStrings.addAll(newStrings);
29+
notifyDataSetChanged();
30+
}
31+
32+
@Override
33+
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
34+
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.string_list_item, parent, false);
35+
return new ViewHolder(view);
36+
}
37+
38+
@Override
39+
public void onBindViewHolder(ViewHolder holder, final int position) {
40+
holder.mColorDisplay.setText(mStrings.get(position));
41+
holder.itemView.setOnClickListener(new View.OnClickListener() {
42+
@Override
43+
public void onClick(View v) {
44+
Toast.makeText(mContext, mStrings.get(position), Toast.LENGTH_SHORT).show();
45+
}
46+
});
47+
}
48+
49+
@Override
50+
public int getItemCount() {
51+
return mStrings.size();
52+
}
53+
54+
public static class ViewHolder extends RecyclerView.ViewHolder {
55+
56+
public final TextView mColorDisplay;
57+
58+
public ViewHolder(View view) {
59+
super(view);
60+
mColorDisplay = (TextView) view.findViewById(R.id.color_display);
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)