forked from ReactiveX/RxJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OperatorWeakBinding to not use WeakReferences anymore
related issues: ReactiveX#1008 ReactiveX#1006 ReactiveX#979
- Loading branch information
Showing
8 changed files
with
222 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
...samples/samples/src/main/java/com/netflix/rxjava/android/samples/ListenInOutActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package com.netflix.rxjava.android.samples; | ||
|
||
import android.app.Activity; | ||
import android.os.Bundle; | ||
import android.view.View; | ||
import android.widget.TextView; | ||
import android.widget.Toast; | ||
import android.widget.ToggleButton; | ||
|
||
import rx.Observable; | ||
import rx.Observer; | ||
import rx.Subscription; | ||
import rx.observables.ConnectableObservable; | ||
|
||
import static rx.android.observables.AndroidObservable.bindActivity; | ||
|
||
/** | ||
* Activity that binds to a counting sequence and is able to listen in and out to that | ||
* sequence by pressing a toggle button. The button disables itself once the sequence | ||
* finishes. | ||
*/ | ||
public class ListenInOutActivity extends Activity implements Observer<String> { | ||
|
||
private Observable<String> source; | ||
private Subscription subscription; | ||
private TextView textView; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
|
||
setContentView(R.layout.listen_in_out_activity); | ||
|
||
textView = (TextView) findViewById(android.R.id.text1); | ||
|
||
// in a production app, you would use dependency injection, fragments, or other | ||
// means to preserve the observable, but this will suffice here | ||
source = (Observable<String>) getLastNonConfigurationInstance(); | ||
if (source == null) { | ||
source = SampleObservables.numberStrings(1, 100, 200).publish(); | ||
((ConnectableObservable) source).connect(); | ||
} | ||
|
||
subscribeToSequence(); | ||
} | ||
|
||
private void subscribeToSequence() { | ||
subscription = bindActivity(this, source).subscribe(this); | ||
} | ||
|
||
@Override | ||
public Object onRetainNonConfigurationInstance() { | ||
return source; | ||
} | ||
|
||
@Override | ||
protected void onDestroy() { | ||
subscription.unsubscribe(); | ||
super.onDestroy(); | ||
} | ||
|
||
@Override | ||
public void onCompleted() { | ||
TextView button = (TextView) findViewById(R.id.toggle_button); | ||
button.setText("Completed"); | ||
button.setEnabled(false); | ||
} | ||
|
||
@Override | ||
public void onError(Throwable e) { | ||
e.printStackTrace(); | ||
Toast.makeText(this, "Error: " + e, Toast.LENGTH_SHORT).show(); | ||
} | ||
|
||
@Override | ||
public void onNext(String s) { | ||
textView.setText(s); | ||
} | ||
|
||
public void onSequenceToggleClicked(View view) { | ||
if (((ToggleButton) view).isChecked()) { | ||
subscription.unsubscribe(); | ||
} else { | ||
subscribeToSequence(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
rxjava-contrib/rxjava-android-samples/samples/src/main/res/layout/listen_in_out_activity.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:paddingLeft="@dimen/activity_horizontal_margin" | ||
android:paddingRight="@dimen/activity_horizontal_margin" | ||
android:paddingTop="@dimen/activity_vertical_margin" | ||
android:paddingBottom="@dimen/activity_vertical_margin" | ||
tools:context="com.netflix.rxjava.android.samples.ListenInOutActivity"> | ||
|
||
<TextView | ||
android:id="@android:id/text1" | ||
android:layout_centerInParent="true" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" /> | ||
|
||
<ToggleButton | ||
android:id="@+id/toggle_button" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_below="@android:id/text1" | ||
android:layout_centerHorizontal="true" | ||
android:textOff="Pause" | ||
android:textOn="Resume" | ||
android:onClick="onSequenceToggleClicked" /> | ||
|
||
|
||
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.