Skip to content

Commit

Permalink
Updated sample code to use latest RxAndroid and RxBinding versions. T…
Browse files Browse the repository at this point in the history
…ime for 0.5 release.
  • Loading branch information
erickok committed Nov 2, 2015
1 parent 3d400c6 commit 0bbdabe
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 38 deletions.
10 changes: 5 additions & 5 deletions library/build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
compileSdkVersion 23
buildToolsVersion "23.0.1"

defaultConfig {
minSdkVersion 7
targetSdkVersion 22
versionCode 4
versionName "0.4"
targetSdkVersion 23
versionCode 5
versionName "0.5"
}
}

Expand Down
14 changes: 7 additions & 7 deletions sample/build.gradle
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
compileSdkVersion 23
buildToolsVersion "23.0.1"

defaultConfig {
applicationId "nl.nl2312.rxcupboard"
minSdkVersion 15
targetSdkVersion 22
versionCode 4
versionName "0.4"
targetSdkVersion 23
versionCode 5
versionName "0.5"
}
}

dependencies {
compile project(':library')
compile 'io.reactivex:rxandroid:0.24.0'
compile 'io.reactivex:rxandroid-framework:0.24.0'
compile 'io.reactivex:rxandroid:1.0.1'
compile 'com.jakewharton.rxbinding:rxbinding:0.3.0'
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package nl.nl2312.rxcupboard.sample.ui;

import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
Expand All @@ -8,6 +9,10 @@
import android.widget.ListView;
import android.widget.Toast;

import com.jakewharton.rxbinding.view.RxView;
import com.jakewharton.rxbinding.widget.RxAdapterView;
import com.jakewharton.rxbinding.widget.RxTextView;

import java.util.List;

import nl.nl2312.rxcupboard.OnDatabaseChange;
Expand All @@ -17,19 +22,14 @@
import nl.nl2312.rxcupboard.sample.R;
import nl.nl2312.rxcupboard.sample.model.Item;
import rx.Observable;
import rx.android.app.RxActivity;
import rx.android.lifecycle.LifecycleObservable;
import rx.android.view.OnClickEvent;
import rx.android.view.ViewActions;
import rx.android.view.ViewObservable;
import rx.android.widget.OnItemClickEvent;
import rx.android.widget.OnTextChangeEvent;
import rx.android.widget.WidgetObservable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.subscriptions.CompositeSubscription;

public class MainActivity extends RxActivity {
public class MainActivity extends Activity {

private CompositeSubscription subscriptions;
private ListView itemsList;
private EditText addEdit;
private Button addButton;
Expand All @@ -39,6 +39,7 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

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

// Add/remove items to/from the list view on any changes in the Item database table
rxBind(rxCupboard.changes(Item.class)).subscribe(new OnDatabaseChange<Item>() {
subscriptions.add(onUi(rxCupboard.changes(Item.class)).subscribe(new OnDatabaseChange<Item>() {
@Override
public void onInsert(Item entity) {
adapter.add(entity);
Expand All @@ -74,30 +75,30 @@ public void onInsert(Item entity) {
public void onDelete(Item entity) {
adapter.remove(entity);
}
}, toastErrorAction);
}, toastErrorAction));

// Remove an item from the database when it was clicked
rxBind(WidgetObservable.itemClicks(itemsList).map(new Func1<OnItemClickEvent, Object>() {
subscriptions.add(onUi(RxAdapterView.itemClicks(itemsList).map(new Func1<Integer, Object>() {
@Override
public Object call(OnItemClickEvent onItemClickEvent) {
public Object call(Integer position) {
// Return the object that was clicked
return adapter.getItem(onItemClickEvent.position());
return adapter.getItem(position);
}
})).subscribe(rxCupboard.delete());
})).subscribe(rxCupboard.delete()));

// Enable the Add button only when text was entered
rxBind(WidgetObservable.text(addEdit, true).map(new Func1<OnTextChangeEvent, Boolean>() {
subscriptions.add(onUi(RxTextView.textChanges(addEdit).map(new Func1<CharSequence, Boolean>() {
@Override
public Boolean call(OnTextChangeEvent onTextChangeEvent) {
public Boolean call(CharSequence text) {
// Emit whether there is now any text input
return !TextUtils.isEmpty(onTextChangeEvent.view().getText());
return !TextUtils.isEmpty(text);
}
}).distinctUntilChanged()).subscribe(ViewActions.setEnabled(addButton));
}).distinctUntilChanged()).subscribe(RxView.enabled(addButton)));

// Allow adding of items when pressing the Add button
rxBind(ViewObservable.clicks(addButton).map(new Func1<OnClickEvent, String>() {
subscriptions.add(onUi(RxView.clicks(addButton).map(new Func1<Void, String>() {
@Override
public String call(OnClickEvent onClickEvent) {
public String call(Void click) {
// Get the text to use for the new Item title
return addEdit.getText().toString();
}
Expand All @@ -115,7 +116,7 @@ public void call(Item item) {
// Clear input text
addEdit.setText(null);
}
}).subscribe(rxCupboard.put(), toastErrorAction);
}).subscribe(rxCupboard.put(), toastErrorAction));
}

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

private <T> Observable<T> rxBind(Observable<T> source) {
return LifecycleObservable.bindActivityLifecycle(lifecycle(), source);
private <T> Observable<T> onUi(Observable<T> source) {
return source.observeOn(AndroidSchedulers.mainThread());
}

@Override
protected void onStop() {
super.onStop();
subscriptions.clear();
}
}

0 comments on commit 0bbdabe

Please sign in to comment.