Skip to content

Commit bb46e03

Browse files
author
Zachary Siegel
committed
Updating the new sample to demonstrate UI binding
This commit binds the observable to a method where a user can update their UI accordingly.
1 parent d6afe1a commit bb46e03

File tree

3 files changed

+39
-31
lines changed

3 files changed

+39
-31
lines changed

rxjava-contrib/rxjava-android-samples/samples/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
android:theme="@style/AppTheme"
1010
android:label="@string/app_name">
1111
<activity
12-
android:name=".RetainedFragmentActivityV2">
12+
android:name=".UIBindingActivity">
1313

1414
<intent-filter>
1515
<category android:name="android.intent.category.LAUNCHER"/>
Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,32 @@
1919
import rx.subscriptions.Subscriptions;
2020

2121
/**
22-
* A retained fragment whose goals are below
23-
*
24-
* 1) gracefully handle rotation - not losing any data
25-
* 2) gracefully handle the user moving in and out of the app
26-
* 3) use a button or trigger of some sort to start the observable, something more in line with typical use
27-
* 4) ensure that the callbacks are not called if the user moves away from the fragment
22+
* Problem:
23+
* You have a data source (where that data is potentially expensive to obtain), and you want to
24+
* emit this data into a fragment. However, you want to gracefully deal with rotation changes and
25+
* not lose any data already emitted.
26+
* <p/>
27+
* You also want your UI to update accordingly to the data being emitted.
2828
*
2929
* @author zsiegel ([email protected])
3030
*/
31-
public class RetainedFragmentActivityV2 extends Activity {
31+
public class UIBindingActivity extends Activity {
3232

3333
@Override
3434
protected void onCreate(Bundle savedInstanceState) {
3535
super.onCreate(savedInstanceState);
3636
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
37-
setTitle("Fake API call V2");
38-
setContentView(R.layout.retained_fragment_activity_v2);
37+
setTitle("UIBinding");
38+
setContentView(R.layout.ui_binding_activity);
3939
}
4040

4141
@SuppressWarnings("ConstantConditions")
42-
public static class RetainedFragmentV2 extends Fragment {
42+
public static class RetainedBindingFragment extends Fragment {
4343

44-
private Observable<String> observable;
45-
private Subscription subscription = Subscriptions.empty();
4644
private Button startButton;
47-
private boolean progressVisiblity;
45+
46+
private Observable<String> request;
47+
private Subscription requestSubscription = Subscriptions.empty();
4848

4949
// in a production app, you don't want to have JSON parser code in your fragment,
5050
// but we'll simplify a little here
@@ -60,7 +60,7 @@ public String call(String json) {
6060
}
6161
};
6262

63-
public RetainedFragmentV2() {
63+
public RetainedBindingFragment() {
6464
setRetainInstance(true);
6565
}
6666

@@ -69,7 +69,7 @@ public RetainedFragmentV2() {
6969
*/
7070
@Override
7171
public void onPause() {
72-
subscription.unsubscribe();
72+
requestSubscription.unsubscribe();
7373
super.onPause();
7474
}
7575

@@ -91,24 +91,21 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
9191
public void onViewCreated(final View view, Bundle savedInstanceState) {
9292
super.onViewCreated(view, savedInstanceState);
9393

94-
final TextView textView = (TextView)getView().findViewById(android.R.id.text1);
94+
final TextView textView = (TextView) getView().findViewById(android.R.id.text1);
9595

9696
startButton = (Button) view.findViewById(R.id.button);
9797
startButton.setOnClickListener(new View.OnClickListener() {
9898
@Override
9999
public void onClick(View v) {
100100
textView.setText("");
101101
start();
102-
startButton.setEnabled(false);
103102
}
104103
});
105104
}
106105

107106
private void start() {
108107

109-
progressVisiblity = true;
110-
111-
observable = SampleObservables
108+
request = SampleObservables
112109
.fakeApiCall(5000)
113110
.map(PARSE_JSON)
114111
.observeOn(AndroidSchedulers.mainThread())
@@ -121,22 +118,33 @@ private void start() {
121118
* We subscribe/re-subscribe here
122119
*/
123120
private void subscribe() {
124-
if (observable != null) {
121+
if (request != null) {
122+
123+
final TextView textView = (TextView) getView().findViewById(android.R.id.text1);
125124

126-
getActivity().setProgressBarIndeterminateVisibility(progressVisiblity);
125+
requestSubscription = request.map(new Func1<String, Boolean>() {
127126

128-
final TextView textView = (TextView)getView().findViewById(android.R.id.text1);
127+
//Consume the data that comes back and then signal that we are done
128+
@Override
129+
public Boolean call(String s) {
130+
textView.setText(s);
131+
return true;
132+
}
133+
}).startWith(false) //Before we receive data our request is not complete
134+
.subscribe(new Action1<Boolean>() {
129135

130-
subscription = observable.subscribe(new Action1<String>() {
136+
//We update the UI based on the state of the request
131137
@Override
132-
public void call(String result) {
133-
textView.setText(result);
134-
progressVisiblity = false;
135-
getActivity().setProgressBarIndeterminateVisibility(progressVisiblity);
136-
startButton.setEnabled(true);
138+
public void call(Boolean completed) {
139+
setRequestInProgress(completed);
137140
}
138141
});
139142
}
140143
}
144+
145+
private void setRequestInProgress(boolean completed) {
146+
getActivity().setProgressBarIndeterminateVisibility(!completed);
147+
startButton.setEnabled(completed);
148+
}
141149
}
142150
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
android:tag="retained_fragment_v2"
99
android:layout_width="match_parent"
1010
android:layout_height="match_parent"
11-
android:name="com.netflix.rxjava.android.samples.RetainedFragmentActivityV2$RetainedFragmentV2"
11+
android:name="com.netflix.rxjava.android.samples.UIBindingActivity$RetainedBindingFragment"
1212
tools:layout="@layout/retained_fragment" />
1313

1414
</RelativeLayout>

0 commit comments

Comments
 (0)