Skip to content

Commit ccb9f18

Browse files
committed
Updates the RxJava example
1 parent ea1750b commit ccb9f18

File tree

16 files changed

+171
-166
lines changed

16 files changed

+171
-166
lines changed

com.vogella.android.rxjava.simple/app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
android:label="@string/app_name"
99
android:supportsRtl="true"
1010
android:theme="@style/AppTheme">
11-
<activity android:name=".SwitcherActivity">
11+
<activity android:name=".MainActivity">
1212
<intent-filter>
1313
<action android:name="android.intent.action.MAIN" />
1414

@@ -17,7 +17,7 @@
1717
</activity>
1818
<activity android:name=".RxJavaSimpleActivity">
1919
</activity>
20-
<activity android:name=".MainActivity">
20+
<activity android:name=".ColorsActivity">
2121
</activity>
2222
<activity android:name=".BooksActivity">
2323
</activity>

com.vogella.android.rxjava.simple/app/src/main/java/com/vogella/android/rxjava/simple/BooksActivity.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ protected void onCreate(Bundle savedInstanceState) {
3232
}
3333

3434
private void createObservable() {
35-
Observable<List<String>> tvShowObservable =
36-
Observable.fromCallable(() -> restClient.getFavoriteTvShows());
37-
bookSubscription = tvShowObservable.
35+
Observable<List<String>> booksObservable =
36+
Observable.fromCallable(() -> restClient.getFavoriteBooks());
37+
bookSubscription = booksObservable.
3838
subscribeOn(Schedulers.io()).
3939
observeOn(AndroidSchedulers.mainThread()).
4040
subscribe(strings -> displayBooks(strings));
@@ -48,16 +48,16 @@ protected void onDestroy() {
4848
}
4949
}
5050

51-
private void displayBooks(List<String> tvShows) {
52-
stringAdapter.setStrings(tvShows);
51+
private void displayBooks(List<String> books) {
52+
stringAdapter.setStrings(books);
5353
progressBar.setVisibility(View.GONE);
5454
booksRecyclerView.setVisibility(View.VISIBLE);
5555
}
5656

5757
private void configureLayout() {
58-
setContentView(R.layout.activity_example2);
58+
setContentView(R.layout.activity_books);
5959
progressBar = (ProgressBar) findViewById(R.id.loader);
60-
booksRecyclerView = (RecyclerView) findViewById(R.id.tv_show_list);
60+
booksRecyclerView = (RecyclerView) findViewById(R.id.books_list);
6161
booksRecyclerView.setLayoutManager(new LinearLayoutManager(this));
6262
stringAdapter = new SimpleStringAdapter(this);
6363
booksRecyclerView.setAdapter(stringAdapter);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 io.reactivex.Observable;
12+
13+
14+
public class ColorsActivity extends AppCompatActivity {
15+
16+
RecyclerView colorListView;
17+
SimpleStringAdapter simpleStringAdapter;
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+
listObservable.subscribe(colors -> simpleStringAdapter.setStrings(colors));
29+
30+
}
31+
32+
private void configureLayout() {
33+
setContentView(R.layout.activity_colors);
34+
colorListView = (RecyclerView) findViewById(R.id.color_list);
35+
colorListView.setLayoutManager(new LinearLayoutManager(this));
36+
simpleStringAdapter = new SimpleStringAdapter(this);
37+
colorListView.setAdapter(simpleStringAdapter);
38+
}
39+
40+
private static List<String> getColorList() {
41+
ArrayList<String> colors = new ArrayList<>();
42+
colors.add("red");
43+
colors.add("green");
44+
colors.add("blue");
45+
colors.add("pink");
46+
colors.add("brown");
47+
return colors;
48+
}
49+
}
Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,32 @@
11
package com.vogella.android.rxjava.simple;
22

3+
import android.app.Activity;
4+
import android.content.Intent;
35
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;
6+
import android.support.annotation.Nullable;
7+
import android.view.View;
78

8-
import java.util.ArrayList;
9-
import java.util.List;
10-
11-
import io.reactivex.Observable;
12-
13-
14-
public class MainActivity extends AppCompatActivity {
15-
16-
RecyclerView colorListView;
17-
SimpleStringAdapter simpleStringAdapter;
189

10+
public class MainActivity extends Activity {
1911
@Override
20-
protected void onCreate(Bundle savedInstanceState) {
12+
protected void onCreate(@Nullable Bundle savedInstanceState) {
2113
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(colors -> simpleStringAdapter.setStrings(colors));
30-
31-
}
32-
33-
private void configureLayout() {
3414
setContentView(R.layout.activity_main);
35-
colorListView = (RecyclerView) findViewById(R.id.color_list);
36-
colorListView.setLayoutManager(new LinearLayoutManager(this));
37-
simpleStringAdapter = new SimpleStringAdapter(this);
38-
colorListView.setAdapter(simpleStringAdapter);
3915
}
4016

41-
private static List<String> getColorList() {
42-
ArrayList<String> colors = new ArrayList<>();
43-
colors.add("blue");
44-
colors.add("green");
45-
colors.add("red");
46-
colors.add("chartreuse");
47-
colors.add("Van Dyke Brown");
48-
return colors;
17+
public void onClick(View view) {
18+
Intent i = null;
19+
switch (view.getId()) {
20+
case R.id.first:
21+
i = new Intent(this, RxJavaSimpleActivity.class);
22+
break;
23+
case R.id.second:
24+
i = new Intent(this, ColorsActivity.class);
25+
break;
26+
case R.id.third:
27+
i = new Intent(this, BooksActivity.class);
28+
break;
29+
}
30+
startActivity(i);
4931
}
5032
}

com.vogella.android.rxjava.simple/app/src/main/java/com/vogella/android/rxjava/simple/RestClient.java

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
import java.util.List;
88

99
/**
10-
* This is a mock REST Client. It simulates making blocking calls to an REST endpoint.
10+
* This is a fake REST client.
11+
*
12+
* It simulates making blocking calls to an REST endpoint.
1113
*/
1214
public class RestClient {
1315
private Context mContext;
@@ -16,13 +18,13 @@ public RestClient(Context context) {
1618
mContext = context;
1719
}
1820

19-
public List<String> getFavoriteTvShows() {
20-
SystemClock.sleep(5000);// "Simulate" the delay of network.
21+
public List<String> getFavoriteBooks() {
22+
SystemClock.sleep(8000);// "Simulate" the delay of network.
2123
return createBooks();
2224
}
2325

24-
public List<String> getFavoriteTvShowsWithException() {
25-
SystemClock.sleep(5000);// "Simulate" the delay of network.
26+
public List<String> getFavoriteBooksWithException() {
27+
SystemClock.sleep(8000);// "Simulate" the delay of network.
2628
throw new RuntimeException("Failed to load");
2729
}
2830

@@ -31,7 +33,7 @@ private List<String> createBooks() {
3133
books.add("Lord of the Rings");
3234
books.add("The dark elf");
3335
books.add("Eclipse Introduction");
34-
books.add("Histor book");
36+
books.add("History book");
3537
books.add("Der kleine Prinz");
3638
books.add("7 habits of highly effective people");
3739
books.add("Other book 1");
@@ -42,29 +44,4 @@ private List<String> createBooks() {
4244
books.add("Other book 6");
4345
return books;
4446
}
45-
46-
public List<String> searchForCity(String searchString) {
47-
try {
48-
// "Simulate" the delay of network.
49-
Thread.sleep(500);
50-
} catch (InterruptedException e) {
51-
e.printStackTrace();
52-
}
53-
return getMatchingCities(searchString);
54-
}
55-
56-
private List<String> getMatchingCities(String searchString) {
57-
if (searchString.isEmpty()) {
58-
return new ArrayList<>();
59-
}
60-
61-
String[] cities = mContext.getResources().getStringArray(R.array.city_list);
62-
List<String> toReturn = new ArrayList<>();
63-
for (String city : cities) {
64-
if (city.toLowerCase().startsWith(searchString.toLowerCase())) {
65-
toReturn.add(city);
66-
}
67-
}
68-
return toReturn;
69-
}
7047
}

com.vogella.android.rxjava.simple/app/src/main/java/com/vogella/android/rxjava/simple/RxJavaSimpleActivity.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,43 @@
66
import android.support.v7.widget.RecyclerView;
77
import android.view.View;
88
import android.widget.TextView;
9+
import android.widget.Toast;
910

1011
import io.reactivex.Observable;
1112
import io.reactivex.android.schedulers.AndroidSchedulers;
13+
import io.reactivex.disposables.CompositeDisposable;
14+
import io.reactivex.disposables.Disposable;
1215
import io.reactivex.schedulers.Schedulers;
1316

1417

1518
public class RxJavaSimpleActivity extends AppCompatActivity {
1619

1720
RecyclerView colorListView;
1821
SimpleStringAdapter simpleStringAdapter;
22+
CompositeDisposable disposable;
23+
public int value =0;
1924

2025
final Observable<Integer> serverDownloadObservable = Observable.create(emitter -> {
21-
SystemClock.sleep(1000); // simulate delay
26+
SystemClock.sleep(10000); // simulate delay
2227
emitter.onNext(5);
2328
emitter.onComplete();
2429
});
2530

2631
@Override
2732
protected void onCreate(Bundle savedInstanceState) {
2833
super.onCreate(savedInstanceState);
29-
setContentView(R.layout.activity_mainrxjavasimple);
34+
setContentView(R.layout.activity_rxjavasimple);
3035
View view = findViewById(R.id.button);
3136
view.setOnClickListener(view1 -> {
3237
view1.setEnabled(false); // disables the button until execution has finished
33-
serverDownloadObservable.
38+
Disposable subscribe = serverDownloadObservable.
3439
observeOn(AndroidSchedulers.mainThread()).
3540
subscribeOn(Schedulers.io()).
3641
subscribe(integer -> {
3742
updateTheUserInterface(integer); // this methods updates the ui
3843
view1.setEnabled(true); // enables it again
3944
});
45+
disposable.add(subscribe);
4046
});
4147
}
4248

@@ -45,5 +51,15 @@ private void updateTheUserInterface(int integer) {
4551
view.setText(String.valueOf(integer));
4652
}
4753

54+
@Override
55+
protected void onStop() {
56+
super.onStop();
57+
if (disposable!=null && !disposable.isDisposed()) {
58+
disposable.dispose();
59+
}
60+
}
4861

62+
public void onClick(View view) {
63+
Toast.makeText(this, "Still active " + value++, Toast.LENGTH_SHORT).show();
64+
}
4965
}

com.vogella.android.rxjava.simple/app/src/main/java/com/vogella/android/rxjava/simple/SwitcherActivity.java

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package com.vogella.android.rxjava.simple;
22

33
/**
4-
* Created by vogella on 21.04.17.
4+
* Created by vogella on 25.04.17.
55
*/
66

7-
class Todo {
8-
}
7+

com.vogella.android.rxjava.simple/app/src/main/res/layout/activity_example2.xml renamed to com.vogella.android.rxjava.simple/app/src/main/res/layout/activity_books.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
/>
1414

1515
<android.support.v7.widget.RecyclerView
16-
android:id="@+id/tv_show_list"
16+
android:id="@+id/books_list"
1717
android:layout_width="match_parent"
1818
android:layout_height="match_parent"
1919
android:visibility="gone"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<FrameLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent"
6+
>
7+
<android.support.v7.widget.RecyclerView
8+
android:id="@+id/color_list"
9+
android:layout_width="match_parent"
10+
android:layout_height="match_parent"
11+
/>
12+
</FrameLayout>

0 commit comments

Comments
 (0)