Skip to content

Commit

Permalink
Merge pull request #54 from JakeWharton/jw/initial-values
Browse files Browse the repository at this point in the history
Emit an initial value on property-based observables.
  • Loading branch information
JakeWharton committed Aug 2, 2015
2 parents f4693ab + 43ea60e commit ec5cd3c
Show file tree
Hide file tree
Showing 32 changed files with 123 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import rx.functions.Action1

/**
* Create an observable of the open state of the drawer of {@code view}.
*
*
* *Warning:* The created observable keeps a strong reference to {@code view}. Unsubscribe
* to free this reference.
*/
public inline fun DrawerLayout.drawerOpen(): Observable<Boolean> = RxDrawerLayout.drawerOpen(this)
public inline fun DrawerLayout.drawerOpen(gravity: Int): Observable<Boolean> = RxDrawerLayout.drawerOpen(this, gravity)

/**
* An action which sets whether the drawer with {@code gravity} of {@code view} is open.
*
*
* *Warning:* The created observable keeps a strong reference to {@code view}. Unsubscribe
* to free this reference.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ public final class RxDrawerLayoutTest {

@Test public void drawerOpen() {
RecordingObserver<Boolean> o = new RecordingObserver<>();
Subscription subscription = RxDrawerLayout.drawerOpen(view) //
Subscription subscription = RxDrawerLayout.drawerOpen(view, RIGHT) //
.subscribeOn(AndroidSchedulers.mainThread()) //
.subscribe(o);
assertThat(o.takeNext()).isFalse();

instrumentation.runOnMainSync(new Runnable() {
@Override public void run() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.jakewharton.rxbinding.support.v4.widget;

import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.DrawerLayout.LayoutParams;
import android.view.View;
import com.jakewharton.rxbinding.internal.MainThreadSubscription;
import rx.Observable;
Expand All @@ -9,10 +10,12 @@
import static com.jakewharton.rxbinding.internal.Preconditions.checkUiThread;

final class DrawerLayoutDrawerOpenedOnSubscribe implements Observable.OnSubscribe<Boolean> {
private DrawerLayout view;
private final DrawerLayout view;
private final int gravity;

DrawerLayoutDrawerOpenedOnSubscribe(DrawerLayout view) {
DrawerLayoutDrawerOpenedOnSubscribe(DrawerLayout view, int gravity) {
this.view = view;
this.gravity = gravity;
}

@Override public void call(final Subscriber<? super Boolean> subscriber) {
Expand All @@ -21,7 +24,10 @@ final class DrawerLayoutDrawerOpenedOnSubscribe implements Observable.OnSubscrib
DrawerLayout.DrawerListener listener = new DrawerLayout.SimpleDrawerListener() {
@Override public void onDrawerOpened(View drawerView) {
if (!subscriber.isUnsubscribed()) {
subscriber.onNext(true);
int drawerGravity = ((LayoutParams) drawerView.getLayoutParams()).gravity;
if (drawerGravity == gravity) {
subscriber.onNext(true);
}
}
}

Expand All @@ -33,7 +39,10 @@ final class DrawerLayoutDrawerOpenedOnSubscribe implements Observable.OnSubscrib

@Override public void onDrawerClosed(View drawerView) {
if (!subscriber.isUnsubscribed()) {
subscriber.onNext(false);
int drawerGravity = ((LayoutParams) drawerView.getLayoutParams()).gravity;
if (drawerGravity == gravity) {
subscriber.onNext(false);
}
}
}
};
Expand All @@ -45,5 +54,8 @@ final class DrawerLayoutDrawerOpenedOnSubscribe implements Observable.OnSubscrib
});

view.setDrawerListener(listener);

// Emit initial value.
subscriber.onNext(view.isDrawerOpen(gravity));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ public final class RxDrawerLayout {
* <p>
* <em>Warning:</em> The created observable keeps a strong reference to {@code view}. Unsubscribe
* to free this reference.
* <p>
* <em>Note:</em> A value will be emitted immediately on subscribe.
*/
public static Observable<Boolean> drawerOpen(DrawerLayout view) {
return Observable.create(new DrawerLayoutDrawerOpenedOnSubscribe(view));
public static Observable<Boolean> drawerOpen(DrawerLayout view, int gravity) {
return Observable.create(new DrawerLayoutDrawerOpenedOnSubscribe(view, gravity));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public final class RxAdapterTest {
@Test @UiThreadTest public void dataChanges() {
RecordingObserver<Object> o = new RecordingObserver<>();
Subscription subscription = RxAdapter.dataChanges(adapter).subscribe(o);
o.assertNoMoreEvents(); // No initial value.
assertThat(o.takeNext()).isSameAs(adapter);

adapter.notifyDataSetChanged();
assertThat(o.takeNext()).isSameAs(adapter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public final class RxAdapterViewTest {
Subscription subscription = RxAdapterView.itemSelections(spinner)
.subscribeOn(AndroidSchedulers.mainThread())
.subscribe(o);
o.assertNoMoreEvents();
assertThat(o.takeNext()).isEqualTo(0);

instrumentation.runOnMainSync(new Runnable() {
@Override public void run() {
Expand Down Expand Up @@ -70,18 +70,22 @@ public final class RxAdapterViewTest {
Subscription subscription = RxAdapterView.selectionEvents(spinner)
.subscribeOn(AndroidSchedulers.mainThread())
.subscribe(o);
o.assertNoMoreEvents();
AdapterViewItemSelectionEvent event1 = (AdapterViewItemSelectionEvent) o.takeNext();
assertThat(event1.view()).isSameAs(spinner);
assertThat(event1.selectedView()).isNotNull();
assertThat(event1.position()).isEqualTo(0);
assertThat(event1.id()).isEqualTo(0);

instrumentation.runOnMainSync(new Runnable() {
@Override public void run() {
spinner.setSelection(2);
}
});
AdapterViewItemSelectionEvent event = (AdapterViewItemSelectionEvent) o.takeNext();
assertThat(event.view()).isSameAs(spinner);
assertThat(event.selectedView()).isNotNull();
assertThat(event.position()).isEqualTo(2);
assertThat(event.id()).isEqualTo(2);
AdapterViewItemSelectionEvent event2 = (AdapterViewItemSelectionEvent) o.takeNext();
assertThat(event2.view()).isSameAs(spinner);
assertThat(event2.selectedView()).isNotNull();
assertThat(event2.position()).isEqualTo(2);
assertThat(event2.id()).isEqualTo(2);

instrumentation.runOnMainSync(new Runnable() {
@Override public void run() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public final class RxRadioGroupTest {
@Test @UiThreadTest public void checkedChanges() {
RecordingObserver<Integer> o = new RecordingObserver<>();
Subscription subscription = RxRadioGroup.checkedChanges(view).subscribe(o);
o.assertNoMoreEvents();
assertThat(o.takeNext()).isEqualTo(-1);

view.check(1);
assertThat(o.takeNext()).isEqualTo(1);
Expand All @@ -57,7 +57,7 @@ public final class RxRadioGroupTest {
@Test @UiThreadTest public void checkedChangeEvents() {
RecordingObserver<RadioGroupCheckedChangeEvent> o = new RecordingObserver<>();
Subscription subscription = RxRadioGroup.checkedChangeEvents(view).subscribe(o);
o.assertNoMoreEvents();
assertThat(o.takeNext()).isEqualTo(RadioGroupCheckedChangeEvent.create(view, -1));

view.check(1);
assertThat(o.takeNext()).isEqualTo(RadioGroupCheckedChangeEvent.create(view, 1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
Subscription subscription = RxRatingBar.ratingChanges(view) //
.subscribeOn(AndroidSchedulers.mainThread()) //
.subscribe(o);
o.assertNoMoreEvents();
assertThat(o.takeNext()).isEqualTo(0f);

instrumentation.runOnMainSync(new Runnable() {
@Override public void run() {
Expand Down Expand Up @@ -68,7 +68,7 @@
Subscription subscription = RxRatingBar.ratingChangeEvents(view) //
.subscribeOn(AndroidSchedulers.mainThread()) //
.subscribe(o);
o.assertNoMoreEvents();
assertThat(o.takeNext()).isEqualTo(RatingBarChangeEvent.create(view, 0f, false));

instrumentation.runOnMainSync(new Runnable() {
@Override public void run() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public final class RxSeekBarTest {
Subscription subscription = RxSeekBar.changes(seekBar) //
.subscribeOn(AndroidSchedulers.mainThread()) //
.subscribe(o);
o.assertNoMoreEvents();
assertThat(o.takeNext()).isEqualTo(0);

instrumentation.sendPointerSync(motionEventAtPosition(seekBar, ACTION_DOWN, 0));
instrumentation.waitForIdleSync();
Expand All @@ -63,7 +63,7 @@ public final class RxSeekBarTest {
Subscription subscription = RxSeekBar.changeEvents(seekBar) //
.subscribeOn(AndroidSchedulers.mainThread()) //
.subscribe(o);
o.assertNoMoreEvents();
assertThat(o.takeNext()).isEqualTo(SeekBarProgressChangeEvent.create(seekBar, 0, false));

instrumentation.sendPointerSync(motionEventAtPosition(seekBar, ACTION_DOWN, 0));
instrumentation.waitForIdleSync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ public static Observable<ViewDragEvent> dragEvents(View view,
* <p>
* <em>Warning:</em> The created observable uses {@link View#setOnFocusChangeListener} to observe
* focus change. Only one observable can be used for a view at a time.
* <p>
* <em>Note:</em> A value will be emitted immediately on subscribe.
*/
public static Observable<Boolean> focusChanges(View view) {
checkNotNull(view, "view == null");
Expand All @@ -133,6 +135,8 @@ public static Observable<Boolean> focusChanges(View view) {
* <p>
* <em>Warning:</em> The created observable uses {@link View#setOnFocusChangeListener} to observe
* focus change. Only one observable can be used for a view at a time.
* <p>
* <em>Note:</em> A value will be emitted immediately on subscribe.
*/
public static Observable<ViewFocusChangeEvent> focusChangeEvents(View view) {
checkNotNull(view, "view == null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public ViewFocusChangeEventOnSubscribe(View view) {

view.setOnFocusChangeListener(listener);

// Send out the initial value.
// Emit initial value.
subscriber.onNext(ViewFocusChangeEvent.create(view, view.hasFocus()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public ViewFocusChangeOnSubscribe(View view) {

view.setOnFocusChangeListener(listener);

// Send out the initial value.
// Emit initial value.
subscriber.onNext(view.hasFocus());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,8 @@ public AdapterDataChangeOnSubscribe(T adapter) {
});

adapter.registerDataSetObserver(observer);

// Emit initial value.
subscriber.onNext(adapter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import rx.Observable;
import rx.Subscriber;

import static android.widget.AdapterView.INVALID_POSITION;
import static com.jakewharton.rxbinding.internal.Preconditions.checkUiThread;

final class AdapterViewItemSelectionOnSubscribe implements Observable.OnSubscribe<Integer> {
Expand All @@ -27,6 +28,9 @@ public void onItemSelected(AdapterView<?> parent, View view, int position, long
}

@Override public void onNothingSelected(AdapterView<?> parent) {
if (!subscriber.isUnsubscribed()) {
subscriber.onNext(INVALID_POSITION);
}
}
};

Expand All @@ -37,5 +41,8 @@ public void onItemSelected(AdapterView<?> parent, View view, int position, long
});

view.setOnItemSelectedListener(listener);

// Emit initial value.
subscriber.onNext(view.getSelectedItemPosition());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import rx.Observable;
import rx.Subscriber;

import static android.widget.AdapterView.INVALID_POSITION;
import static com.jakewharton.rxbinding.internal.Preconditions.checkUiThread;

final class AdapterViewSelectionOnSubscribe
Expand Down Expand Up @@ -41,5 +42,16 @@ public void onItemSelected(AdapterView<?> parent, View view, int position, long
});

view.setOnItemSelectedListener(listener);

// Emit initial value.
int selectedPosition = view.getSelectedItemPosition();
if (selectedPosition == INVALID_POSITION) {
subscriber.onNext(AdapterViewNothingSelectionEvent.create(view));
} else {
View selectedView = view.getSelectedView();
long selectedId = view.getSelectedItemId();
subscriber.onNext(
AdapterViewItemSelectionEvent.create(view, selectedView, selectedPosition, selectedId));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void call(final Subscriber<? super CompoundButtonCheckedChangeEvent> subs

view.setOnCheckedChangeListener(listener);

// Send out the initial value.
// Emit initial value.
subscriber.onNext(CompoundButtonCheckedChangeEvent.create(view, view.isChecked()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public CompoundButtonCheckedChangeOnSubscribe(CompoundButton view) {

view.setOnCheckedChangeListener(listener);

// Send out the initial value.
// Emit initial value.
subscriber.onNext(view.isChecked());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,7 @@ public RadioGroupCheckedChangeEventOnSubscribe(RadioGroup view) {

view.setOnCheckedChangeListener(listener);

// Emit initial value.
subscriber.onNext(RadioGroupCheckedChangeEvent.create(view, view.getCheckedRadioButtonId()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,8 @@ public RadioGroupCheckedChangeOnSubscribe(RadioGroup view) {
});

view.setOnCheckedChangeListener(listener);

// Emit initial value.
subscriber.onNext(view.getCheckedRadioButtonId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,8 @@ public RatingBarRatingChangeEventOnSubscribe(RatingBar view) {
});

view.setOnRatingBarChangeListener(listener);

// Emit initial value.
subscriber.onNext(RatingBarChangeEvent.create(view, view.getRating(), false));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,8 @@ public RatingBarRatingChangeOnSubscribe(RatingBar view) {
});

view.setOnRatingBarChangeListener(listener);

// Emit initial value.
subscriber.onNext(view.getRating());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
* Static factory methods for creating {@linkplain Observable observables} for {@link Adapter}.
*/
public final class RxAdapter {
/** Create an observable of data change events for {@code adapter}. */
/**
* Create an observable of data change events for {@code adapter}.
* <p>
* <em>Note:</em> A value will be emitted immediately on subscribe.
*/
public static <T extends Adapter> Observable<T> dataChanges(T adapter) {
return Observable.create(new AdapterDataChangeOnSubscribe<>(adapter));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
*/
public final class RxAdapterView {
/**
* Create an observable of the selected position of {@code view}.
* Create an observable of the selected position of {@code view}. If nothing is selected,
* {@link AdapterView#INVALID_POSITION} will be emitted.
* <p>
* <em>Warning:</em> The created observable keeps a strong reference to {@code view}. Unsubscribe
* to free this reference.
* <p>
* <em>Note:</em> A value will be emitted immediately on subscribe.
*/
public static <T extends Adapter> Observable<Integer> itemSelections(AdapterView<T> view) {
return Observable.create(new AdapterViewItemSelectionOnSubscribe(view));
Expand All @@ -30,6 +33,8 @@ public static <T extends Adapter> Observable<Integer> itemSelections(AdapterView
* <p>
* <em>Warning:</em> The created observable keeps a strong reference to {@code view}. Unsubscribe
* to free this reference.
* <p>
* <em>Note:</em> A value will be emitted immediately on subscribe.
*/
public static <T extends Adapter> Observable<AdapterViewSelectionEvent> selectionEvents(AdapterView<T> view) {
return Observable.create(new AdapterViewSelectionOnSubscribe(view));
Expand Down

0 comments on commit ec5cd3c

Please sign in to comment.