11package nl .nl2312 .rxcupboard .sample .ui ;
22
3+ import android .app .Activity ;
34import android .database .sqlite .SQLiteDatabase ;
45import android .os .Bundle ;
56import android .text .TextUtils ;
89import android .widget .ListView ;
910import 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+
1116import java .util .List ;
1217
1318import nl .nl2312 .rxcupboard .OnDatabaseChange ;
1722import nl .nl2312 .rxcupboard .sample .R ;
1823import nl .nl2312 .rxcupboard .sample .model .Item ;
1924import 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 ;
2826import rx .functions .Action1 ;
2927import 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