Skip to content

Commit 66ceea9

Browse files
author
Kaushik Gopal
committed
feat: wiring up example for polling
1 parent 4e6be41 commit 66ceea9

5 files changed

Lines changed: 163 additions & 1 deletion

File tree

app/src/main/java/com/morihacky/android/rxjava/MainFragment.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ public void demoDoubleBindingWithPublishSubject() {
6767
.commit();
6868
}
6969

70+
@OnClick(R.id.btn_demo_polling)
71+
public void demoPolling() {
72+
getActivity().getSupportFragmentManager()
73+
.beginTransaction()
74+
.addToBackStack(this.toString())
75+
.replace(R.id.activity_main, new PollingFragment(), this.toString())
76+
.commit();
77+
}
78+
7079
//@OnClick(R.id.btn_demo_subject_timeout)
7180
public void demoTimeout() {
7281
getActivity().getSupportFragmentManager()
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package com.morihacky.android.rxjava;
2+
3+
import android.content.Context;
4+
import android.os.Bundle;
5+
import android.os.Handler;
6+
import android.os.Looper;
7+
import android.support.annotation.Nullable;
8+
import android.support.v4.app.Fragment;
9+
import android.view.LayoutInflater;
10+
import android.view.View;
11+
import android.view.ViewGroup;
12+
import android.widget.ArrayAdapter;
13+
import android.widget.Button;
14+
import android.widget.ListView;
15+
import butterknife.ButterKnife;
16+
import butterknife.InjectView;
17+
import butterknife.OnClick;
18+
import com.morihacky.android.rxjava.app.R;
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
import rx.subscriptions.CompositeSubscription;
22+
import timber.log.Timber;
23+
24+
public class PollingFragment
25+
extends Fragment {
26+
27+
@InjectView(R.id.list_threading_log) ListView _logsList;
28+
@InjectView(R.id.btn_start_simple_polling) Button _btnSimplePolling;
29+
30+
private LogAdapter _adapter;
31+
private List<String> _logs;
32+
private CompositeSubscription _subscriptions;
33+
34+
@Override
35+
public void onDestroy() {
36+
super.onDestroy();
37+
_subscriptions.unsubscribe();
38+
}
39+
40+
@Override
41+
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
42+
super.onActivityCreated(savedInstanceState);
43+
_setupLogger();
44+
}
45+
46+
@Override
47+
public View onCreateView(LayoutInflater inflater,
48+
@Nullable ViewGroup container,
49+
@Nullable Bundle savedInstanceState) {
50+
View layout = inflater.inflate(R.layout.fragment_polling, container, false);
51+
ButterKnife.inject(this, layout);
52+
return layout;
53+
}
54+
55+
@OnClick(R.id.btn_start_simple_polling)
56+
public void onStartSimplePollingClicked() {
57+
58+
}
59+
60+
61+
// -----------------------------------------------------------------------------------
62+
// Method that help wiring up the example (irrelevant to RxJava)
63+
64+
private void _doSomeLongOperation_thatBlocksCurrentThread() {
65+
_log("performing long operation");
66+
67+
try {
68+
Thread.sleep(3000);
69+
} catch (InterruptedException e) {
70+
Timber.d("Operation was interrupted");
71+
}
72+
}
73+
74+
private void _log(String logMsg) {
75+
if (_isCurrentlyOnMainThread()) {
76+
_logs.add(0, logMsg + " (main thread) ");
77+
_adapter.clear();
78+
_adapter.addAll(_logs);
79+
} else {
80+
_logs.add(0, logMsg + " (NOT main thread) ");
81+
82+
// You can only do below stuff on main thread.
83+
new Handler(Looper.getMainLooper()).post(new Runnable() {
84+
85+
@Override
86+
public void run() {
87+
_adapter.clear();
88+
_adapter.addAll(_logs);
89+
}
90+
});
91+
}
92+
}
93+
94+
private void _setupLogger() {
95+
_logs = new ArrayList<String>();
96+
_adapter = new LogAdapter(getActivity(), new ArrayList<String>());
97+
_logsList.setAdapter(_adapter);
98+
}
99+
100+
private boolean _isCurrentlyOnMainThread() {
101+
return Looper.myLooper() == Looper.getMainLooper();
102+
}
103+
104+
private class LogAdapter
105+
extends ArrayAdapter<String> {
106+
107+
public LogAdapter(Context context, List<String> logs) {
108+
super(context, R.layout.item_log, R.id.item_log, logs);
109+
}
110+
}
111+
}

app/src/main/res/layout/fragment_main.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,11 @@
4444
android:layout_height="wrap_content"
4545
android:layout_width="match_parent"
4646
android:text="@string/btn_demo_double_binding_textview"/>
47+
48+
<Button
49+
android:id="@+id/btn_demo_polling"
50+
android:layout_height="wrap_content"
51+
android:layout_width="match_parent"
52+
android:text="@string/btn_demo_polling"/>
4753
</LinearLayout>
4854
</ScrollView>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<LinearLayout
4+
android:orientation="vertical"
5+
android:layout_height="match_parent"
6+
android:layout_width="match_parent"
7+
xmlns:android="http://schemas.android.com/apk/res/android">
8+
9+
<TextView
10+
android:layout_height="wrap_content"
11+
android:layout_width="match_parent"
12+
android:padding="10dp"
13+
android:gravity="center"
14+
android:text="@string/msg_demo_polling"/>
15+
16+
17+
<LinearLayout
18+
android:layout_height="wrap_content"
19+
android:layout_width="match_parent">
20+
21+
<Button
22+
android:id="@+id/btn_start_simple_polling"
23+
android:layout_height="wrap_content"
24+
android:layout_width="wrap_content"
25+
android:layout_marginLeft="20dp"
26+
android:textSize="16sp"
27+
android:text="Start simple polling"/>
28+
</LinearLayout>
29+
30+
<ListView
31+
android:id="@+id/list_threading_log"
32+
android:layout_height="match_parent"
33+
android:layout_width="match_parent"/>
34+
</LinearLayout>

app/src/main/res/values/strings.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010
<string name="msg_demo_subject">As you type in the input box, it will not shoot out log messages at every single input character change, but rather only pick the lastly emitted event (i.e. input) and log that. \n\nThis is the debounce/throttleWithTimeout method in RxJava.</string>
1111
<string name="msg_demo_timeout">This is a demo of terminating events, that take too long to process. Events in this demo should timeout in 3 seconds. Button 1 is an event that takes 2s to process, and Button 2 is a n event that takes 5s to process. Hit Button 2, and see in the logs that it\'s been cancelled, while this is not the case for Button 1.</string>
1212
<string name="msg_demo_retrofit">Retrofit from Square is a super easy networking helper library. It works really well with RxJava and these are examples taken from Jake Wharton\'s talk at Netflix (see README). Really the only interesting bits here are in the code and logs.</string>
13+
<string name="msg_demo_doublebinding">Watch how the result gloriously auto-updates based on your changing inputs. Using a technique like this, you could achieve the two-way binding in Angular Js, or more efficiently use a pattern like the Presentation View Model.</string>
14+
<string name="msg_demo_polling">This is demo of polling or making a call repeatedly with RxJava. \n\nSimple polling: Notice in the logs how a network call (simulated) is repeatedly made in the background. \n\nExponential backoff polling: We start polling but in increasing time intervals depending on a provided condition.</string>
1315

1416
<string name="btn_demo_schedulers">bg work (schedulers &amp; concurrency)</string>
1517
<string name="btn_demo_buffer">accumulate calls (buffer)</string>
1618
<string name="btn_demo_subject_debounce">search text listener(subject debouncing)</string>
1719
<string name="btn_demo_timeout">delayed jobs (timeout)</string>
1820
<string name="btn_demo_retrofit">Retrofit + RxJava</string>
1921
<string name="btn_demo_double_binding_textview">Double binding (PublishSubject)</string>
20-
<string name="msg_demo_doublebinding">Watch how the result gloriously auto-updates based on your changing inputs. Using a technique like this, you could achieve the two-way binding in Angular Js, or more efficiently use a pattern like the Presentation View Model.</string>
22+
<string name="btn_demo_polling">Polling with RxJava</string>
2123
</resources>

0 commit comments

Comments
 (0)