1010import com .threshold .rxbus2 .RxBus ;
1111import com .threshold .rxbus2 .annotation .RxSubscribe ;
1212import com .threshold .rxbus2 .util .EventThread ;
13+ import com .threshold .rxbus2demo .bean .DemoBean1 ;
14+ import com .threshold .rxbus2demo .bean .DemoBean2 ;
15+ import com .threshold .rxbus2demo .bean .event .DemoEvent1 ;
16+ import com .threshold .rxbus2demo .bean .event .DemoEvent2 ;
17+ import com .threshold .rxbus2demo .bean .event .RxEvent ;
1318import com .threshold .rxbus2demo .util .RandomUtil ;
1419
1520import java .util .List ;
2025import io .reactivex .schedulers .Schedulers ;
2126
2227/**
28+ * Demo for showing {@link RxBus} usage.
2329 * Created by threshold on 2017/1/18.
2430 */
2531
@@ -50,20 +56,22 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
5056// }
5157// });
5258// mCompositeDisposable.add(subscribe);
53- Logger .d ("Is register Success:%s" , RxBus .getDefault ().hasObservers ());
5459 }
5560
56- @ RxSubscribe (observeOnThread = EventThread .MAIN ) @ SuppressWarnings ("unused" )
57- public void autoListenRxEvent (int code ) {
58- String text = String .format ("{ Receive event: %s\n Current thread: %s }" , code , Thread .currentThread ());
61+ @ RxSubscribe (observeOnThread = EventThread .MAIN )
62+ @ SuppressWarnings ("unused" )
63+ public void autoListenRxEvent (DemoEvent1 demoEvent1 ) {
64+ String text = String .format ("{autoListenRxEvent Receive DemoEvent1: %s\n ThreadId: %s }\n " , demoEvent1 .getDemoBean1 ().getData (), Thread .currentThread ().getId ());
5965 Logger .d (text );
6066 textView .append (text );
6167 textView .append ("\n " );
6268 }
6369
64- @ RxSubscribe (observeOnThread = EventThread .IO ,isSticky = true ) @ SuppressWarnings ("unused" )
65- public void autoListenRxEvent2 (String event ) {
66- final String text = String .format ("{ Receive event: %s\n Current thread: %s }" , event , Thread .currentThread ());
70+ //now we support private method.
71+ @ RxSubscribe (observeOnThread = EventThread .IO , isSticky = true )
72+ @ SuppressWarnings ("unused" )
73+ private void autoListenRxEvent2 (DemoEvent2 event ) {
74+ final String text = String .format ("{autoListenRxEvent2 Receive sticky DemoEvent2: %s\n ThreadId: %s }\n " , event .getDemoBean2 ().getData (), Thread .currentThread ().getId ());
6775 Logger .d (text );
6876 runOnUiThread (new Runnable () {
6977 @ Override
@@ -74,8 +82,45 @@ public void run() {
7482 });
7583 }
7684
77- public void manualListenRxEvent (String id , String event ) {
78- final String text = String .format ("{[%s Receive event]: %s}" , id , event );
85+ // Will crash on register. Because no param in method.
86+ // @RxSubscribe(observeOnThread = EventThread.IO, isSticky = true)
87+ // @SuppressWarnings("unused")
88+ // private void autoListenRxEvent3() {
89+ //
90+ // }
91+
92+ // Will crash on register. Because two param in method. We expect ONLY ONE param.
93+ // @RxSubscribe(observeOnThread = EventThread.IO, isSticky = true)
94+ // @SuppressWarnings("unused")
95+ // private void autoListenRxEvent3(DemoEvent1 event1,DemoEvent2 event2) {
96+ //
97+ // }
98+
99+ // Will crash on receive event. Because you shouldn't update view state on BackgroundThread.
100+ //You should update UI(View) on MAIN THREAD(UI THREAD).
101+ // @RxSubscribe(observeOnThread = EventThread.IO, isSticky = true) @SuppressWarnings("unused")
102+ // private void autoListenRxEvent3(DemoEvent2 event) {
103+ // final String text = String.format("{autoListenRxEvent2 Receive sticky event: %s\nThreadId: %s }\n", event.getDemoBean2().getData(), Thread.currentThread().getId());
104+ // Logger.d(text);
105+ // textView.append(text);//will crash on here.
106+ // textView.append("\n");
107+ // }
108+
109+ @ RxSubscribe (observeOnThread = EventThread .IO ) @ SuppressWarnings ("unused" )
110+ private void autoListenRxEvent3 (RxEvent event ) { //This method will listen DemoEvent1 and DemoEvent2 Both.
111+ final String text ;
112+ if (event instanceof DemoEvent1 ) {
113+ text = String .format ("{autoListenRxEvent3 Receive RxEvent: %s\n ThreadId: %s }\n " , ((DemoEvent1 ) event ).getDemoBean1 ().getData (), Thread .currentThread ().getId ());
114+ } else if (event instanceof DemoEvent2 ) {
115+ text = String .format ("{autoListenRxEvent3 Receive RxEvent: %s\n ThreadId: %s }\n " , ((DemoEvent2 ) event ).getDemoBean2 ().getData (), Thread .currentThread ().getId ());
116+ } else {
117+ text = String .format ("{autoListenRxEvent3 Receive RxEvent: %s\n ThreadId: %s }\n " , event , Thread .currentThread ().getId ());
118+ }
119+ Logger .d (text );
120+ }
121+
122+ private void manualListenRxEvent (DemoEvent1 event ) {
123+ final String text = String .format ("{manualListenRxEvent [Receive DemoEvent1]: %s}\n " , event .getDemoBean1 ().getData ());
79124 Logger .d (text );
80125 runOnUiThread (new Runnable () {
81126 @ Override
@@ -90,31 +135,35 @@ public void run() {
90135 public void onClick (View view ) {
91136 switch (view .getId ()) {
92137 case R .id .btnFireEvent :
93- RxBus . getDefault (). post (RandomUtil .random (10 ));
94- RxBus .getDefault ().post ("Hi, event " + RandomUtil . random ( 10 ) );
138+ DemoEvent1 demoEvent1 = new DemoEvent1 ( RxBusActivity . class , new DemoBean1 ( String . valueOf (RandomUtil .random (10 )) ));
139+ RxBus .getDefault ().post (demoEvent1 );
95140 break ;
96141 case R .id .btnFireStickyEvent :
97- RxBus .getDefault ().postSticky ("Hello, sticky event " +RandomUtil .random (100 ));
142+ DemoEvent2 demoEvent2 = new DemoEvent2 (RxBusActivity .class , new DemoBean2 (RandomUtil .random (10 )));
143+ RxBus .getDefault ().postSticky (demoEvent2 );
98144 break ;
99145 case R .id .btnAddNewSubscriber :
100146 Disposable subscribe = RxBus .getDefault ()
101- .ofStickyType (String .class )
147+ .ofStickyType (DemoEvent1 .class )
102148 .subscribeOn (Schedulers .io ())
103149 .observeOn (Schedulers .io ())
104- .subscribe (new Consumer <String >() {
150+ .subscribe (new Consumer <DemoEvent1 >() {
105151 @ Override
106- public void accept (String s ) throws Exception {
107- manualListenRxEvent ("Second Subscriber" , s );
152+ public void accept (DemoEvent1 event1 ) throws Exception {
153+ manualListenRxEvent (event1 );
108154 }
109155 });
110156 mCompositeDisposable .add (subscribe );
111157 view .setEnabled (false );
112158 break ;
113159 case R .id .btnRemoveStickyEvent :
114- List <String > sticky = RxBus .getDefault ()
115- .getSticky (String .class );
116- if (sticky != null && sticky .size () > 0 ) {
117- RxBus .getDefault ().removeSticky (sticky .get (0 ));
160+ List <DemoEvent2 > stickies = RxBus .getDefault ()
161+ .getSticky (DemoEvent2 .class );
162+ if (stickies != null && stickies .size () > 0 ) {
163+ RxBus .getDefault ().removeStickyEventAt (DemoEvent2 .class ,stickies .size ()-1 );//remove the last sticky event
164+ textView .append ("Already removed last sticky event, you can press back key and reenter this activity and see difference.\n " );
165+ } else {
166+ textView .append ("No sticky event found, please fire some sticky event first\n " );
118167 }
119168 break ;
120169 }
0 commit comments