Skip to content

Commit 0bbdabe

Browse files
committed
Updated sample code to use latest RxAndroid and RxBinding versions. Time for 0.5 release.
1 parent 3d400c6 commit 0bbdabe

File tree

3 files changed

+44
-38
lines changed

3 files changed

+44
-38
lines changed

library/build.gradle

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
apply plugin: 'com.android.library'
22

33
android {
4-
compileSdkVersion 22
5-
buildToolsVersion "22.0.1"
4+
compileSdkVersion 23
5+
buildToolsVersion "23.0.1"
66

77
defaultConfig {
88
minSdkVersion 7
9-
targetSdkVersion 22
10-
versionCode 4
11-
versionName "0.4"
9+
targetSdkVersion 23
10+
versionCode 5
11+
versionName "0.5"
1212
}
1313
}
1414

sample/build.gradle

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
apply plugin: 'com.android.application'
22

33
android {
4-
compileSdkVersion 22
5-
buildToolsVersion "22.0.1"
4+
compileSdkVersion 23
5+
buildToolsVersion "23.0.1"
66

77
defaultConfig {
88
applicationId "nl.nl2312.rxcupboard"
99
minSdkVersion 15
10-
targetSdkVersion 22
11-
versionCode 4
12-
versionName "0.4"
10+
targetSdkVersion 23
11+
versionCode 5
12+
versionName "0.5"
1313
}
1414
}
1515

1616
dependencies {
1717
compile project(':library')
18-
compile 'io.reactivex:rxandroid:0.24.0'
19-
compile 'io.reactivex:rxandroid-framework:0.24.0'
18+
compile 'io.reactivex:rxandroid:1.0.1'
19+
compile 'com.jakewharton.rxbinding:rxbinding:0.3.0'
2020
}

sample/src/main/java/nl/nl2312/rxcupboard/sample/ui/MainActivity.java

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package nl.nl2312.rxcupboard.sample.ui;
22

3+
import android.app.Activity;
34
import android.database.sqlite.SQLiteDatabase;
45
import android.os.Bundle;
56
import android.text.TextUtils;
@@ -8,6 +9,10 @@
89
import android.widget.ListView;
910
import android.widget.Toast;
1011

12+
import com.jakewharton.rxbinding.view.RxView;
13+
import com.jakewharton.rxbinding.widget.RxAdapterView;
14+
import com.jakewharton.rxbinding.widget.RxTextView;
15+
1116
import java.util.List;
1217

1318
import nl.nl2312.rxcupboard.OnDatabaseChange;
@@ -17,19 +22,14 @@
1722
import nl.nl2312.rxcupboard.sample.R;
1823
import nl.nl2312.rxcupboard.sample.model.Item;
1924
import rx.Observable;
20-
import rx.android.app.RxActivity;
21-
import rx.android.lifecycle.LifecycleObservable;
22-
import rx.android.view.OnClickEvent;
23-
import rx.android.view.ViewActions;
24-
import rx.android.view.ViewObservable;
25-
import rx.android.widget.OnItemClickEvent;
26-
import rx.android.widget.OnTextChangeEvent;
27-
import rx.android.widget.WidgetObservable;
25+
import rx.android.schedulers.AndroidSchedulers;
2826
import rx.functions.Action1;
2927
import rx.functions.Func1;
28+
import rx.subscriptions.CompositeSubscription;
3029

31-
public class MainActivity extends RxActivity {
30+
public class MainActivity extends Activity {
3231

32+
private CompositeSubscription subscriptions;
3333
private ListView itemsList;
3434
private EditText addEdit;
3535
private Button addButton;
@@ -39,6 +39,7 @@ protected void onCreate(Bundle savedInstanceState) {
3939
super.onCreate(savedInstanceState);
4040
setContentView(R.layout.activity_main);
4141

42+
subscriptions = new CompositeSubscription();
4243
itemsList = (ListView) findViewById(R.id.itemsList);
4344
addEdit = (EditText) findViewById(R.id.addEdit);
4445
addButton = (Button) findViewById(R.id.addButton);
@@ -55,16 +56,16 @@ protected void onStart() {
5556
// Load all existing items form the database into the list view
5657
final ItemsAdapter adapter = new ItemsAdapter(this);
5758
// Note that the underlying Cursor is created on calling query(), but individual Item objects are created when iterating (reactive pull)
58-
rxBind(rxCupboard.query(Item.class).toList()).subscribe(new Action1<List<Item>>() {
59+
subscriptions.add(onUi(rxCupboard.query(Item.class).toList()).subscribe(new Action1<List<Item>>() {
5960
@Override
6061
public void call(List<Item> items) {
6162
adapter.add(items);
6263
itemsList.setAdapter(adapter);
6364
}
64-
}, toastErrorAction);
65+
}, toastErrorAction));
6566

6667
// Add/remove items to/from the list view on any changes in the Item database table
67-
rxBind(rxCupboard.changes(Item.class)).subscribe(new OnDatabaseChange<Item>() {
68+
subscriptions.add(onUi(rxCupboard.changes(Item.class)).subscribe(new OnDatabaseChange<Item>() {
6869
@Override
6970
public void onInsert(Item entity) {
7071
adapter.add(entity);
@@ -74,30 +75,30 @@ public void onInsert(Item entity) {
7475
public void onDelete(Item entity) {
7576
adapter.remove(entity);
7677
}
77-
}, toastErrorAction);
78+
}, toastErrorAction));
7879

7980
// Remove an item from the database when it was clicked
80-
rxBind(WidgetObservable.itemClicks(itemsList).map(new Func1<OnItemClickEvent, Object>() {
81+
subscriptions.add(onUi(RxAdapterView.itemClicks(itemsList).map(new Func1<Integer, Object>() {
8182
@Override
82-
public Object call(OnItemClickEvent onItemClickEvent) {
83+
public Object call(Integer position) {
8384
// Return the object that was clicked
84-
return adapter.getItem(onItemClickEvent.position());
85+
return adapter.getItem(position);
8586
}
86-
})).subscribe(rxCupboard.delete());
87+
})).subscribe(rxCupboard.delete()));
8788

8889
// Enable the Add button only when text was entered
89-
rxBind(WidgetObservable.text(addEdit, true).map(new Func1<OnTextChangeEvent, Boolean>() {
90+
subscriptions.add(onUi(RxTextView.textChanges(addEdit).map(new Func1<CharSequence, Boolean>() {
9091
@Override
91-
public Boolean call(OnTextChangeEvent onTextChangeEvent) {
92+
public Boolean call(CharSequence text) {
9293
// Emit whether there is now any text input
93-
return !TextUtils.isEmpty(onTextChangeEvent.view().getText());
94+
return !TextUtils.isEmpty(text);
9495
}
95-
}).distinctUntilChanged()).subscribe(ViewActions.setEnabled(addButton));
96+
}).distinctUntilChanged()).subscribe(RxView.enabled(addButton)));
9697

9798
// Allow adding of items when pressing the Add button
98-
rxBind(ViewObservable.clicks(addButton).map(new Func1<OnClickEvent, String>() {
99+
subscriptions.add(onUi(RxView.clicks(addButton).map(new Func1<Void, String>() {
99100
@Override
100-
public String call(OnClickEvent onClickEvent) {
101+
public String call(Void click) {
101102
// Get the text to use for the new Item title
102103
return addEdit.getText().toString();
103104
}
@@ -115,7 +116,7 @@ public void call(Item item) {
115116
// Clear input text
116117
addEdit.setText(null);
117118
}
118-
}).subscribe(rxCupboard.put(), toastErrorAction);
119+
}).subscribe(rxCupboard.put(), toastErrorAction));
119120
}
120121

121122
private Action1<Throwable> toastErrorAction = new Action1<Throwable>() {
@@ -125,8 +126,13 @@ public void call(Throwable throwable) {
125126
}
126127
};
127128

128-
private <T> Observable<T> rxBind(Observable<T> source) {
129-
return LifecycleObservable.bindActivityLifecycle(lifecycle(), source);
129+
private <T> Observable<T> onUi(Observable<T> source) {
130+
return source.observeOn(AndroidSchedulers.mainThread());
130131
}
131132

133+
@Override
134+
protected void onStop() {
135+
super.onStop();
136+
subscriptions.clear();
137+
}
132138
}

0 commit comments

Comments
 (0)