Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ViewObservables consistent with each other #22

Merged
merged 3 commits into from
Oct 15, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/main/java/rx/android/events/OnCheckedChangeEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package rx.android.events;

import android.widget.CompoundButton;

public class OnCheckedChangeEvent {
public final CompoundButton view;
public final boolean value;

public OnCheckedChangeEvent(final CompoundButton view) {
this(view, view.isChecked());
}

public OnCheckedChangeEvent(final CompoundButton view, final boolean value) {
this.view = view;
this.value = value;
}
}
11 changes: 11 additions & 0 deletions src/main/java/rx/android/events/OnClickEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package rx.android.events;

import android.view.View;

public class OnClickEvent {
public final View view;

public OnClickEvent(final View view) {
this.view = view;
}
}
18 changes: 18 additions & 0 deletions src/main/java/rx/android/events/OnTextChangeEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package rx.android.events;

import android.text.SpannableString;
import android.widget.TextView;

public class OnTextChangeEvent {
public final TextView view;
public final CharSequence text;

public OnTextChangeEvent(final TextView view) {
this(view, new SpannableString(view.getText()));
}

public OnTextChangeEvent(final TextView view, final CharSequence text) {
this.view = view;
this.text = text;
}
}
6 changes: 3 additions & 3 deletions src/main/java/rx/android/observables/AndroidObservable.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@

import rx.Observable;
import rx.functions.Func1;
import rx.operators.OperatorBroadcastRegister;
import rx.operators.OperatorConditionalBinding;
import rx.operators.OperatorLocalBroadcastRegister;
import rx.android.operators.OperatorBroadcastRegister;
import rx.android.operators.OperatorConditionalBinding;
import rx.android.operators.OperatorLocalBroadcastRegister;

import android.app.Activity;
import android.app.Fragment;
Expand Down
30 changes: 20 additions & 10 deletions src/main/java/rx/android/observables/ViewObservable.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,41 @@
package rx.android.observables;

import rx.Observable;
import rx.operators.OperatorCompoundButtonInput;
import rx.operators.OperatorTextViewInput;
import rx.operators.OperatorViewClick;
import rx.android.events.OnCheckedChangeEvent;
import rx.android.events.OnClickEvent;
import rx.android.events.OnTextChangeEvent;
import rx.android.operators.OperatorCompoundButtonInput;
import rx.android.operators.OperatorTextViewInput;
import rx.android.operators.OperatorViewClick;

import android.view.View;
import android.widget.CompoundButton;
import android.widget.TextView;

public class ViewObservable {

public static <T extends View> Observable<T> clicks(final T view, final boolean emitInitialValue) {
return Observable.create(new OperatorViewClick<T>(view, emitInitialValue));
public static Observable<OnClickEvent> clicks(final View view) {
return clicks(view, false);
}

public static <T extends TextView> Observable<T> text(final T input) {
public static Observable<OnClickEvent> clicks(final View view, final boolean emitInitialValue) {
return Observable.create(new OperatorViewClick(view, emitInitialValue));
}

public static Observable<OnTextChangeEvent> text(final TextView input) {
return text(input, false);
}

public static <T extends TextView> Observable<T> text(final T input, final boolean emitInitialValue) {
return Observable.create(new OperatorTextViewInput<T>(input, emitInitialValue));
public static Observable<OnTextChangeEvent> text(final TextView input, final boolean emitInitialValue) {
return Observable.create(new OperatorTextViewInput(input, emitInitialValue));
}

public static Observable<Boolean> input(final CompoundButton button, final boolean emitInitialValue) {
public static Observable<OnCheckedChangeEvent> input(final CompoundButton button) {
return input(button, false);
}

public static Observable<OnCheckedChangeEvent> input(final CompoundButton button, final boolean emitInitialValue) {
return Observable.create(new OperatorCompoundButtonInput(button, emitInitialValue));
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.operators;
package rx.android.operators;

import android.content.BroadcastReceiver;
import android.content.Context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,24 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.operators;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
package rx.android.operators;

import android.view.View;
import android.widget.CompoundButton;
import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.android.events.OnCheckedChangeEvent;
import rx.android.observables.Assertions;
import rx.android.subscriptions.AndroidSubscriptions;
import rx.functions.Action0;
import android.view.View;
import android.widget.CompoundButton;

public class OperatorCompoundButtonInput implements Observable.OnSubscribe<Boolean> {
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;

public class OperatorCompoundButtonInput implements Observable.OnSubscribe<OnCheckedChangeEvent> {
private final boolean emitInitialValue;
private final CompoundButton button;

Expand All @@ -39,14 +40,14 @@ public OperatorCompoundButtonInput(final CompoundButton button, final boolean em
}

@Override
public void call(final Subscriber<? super Boolean> observer) {
public void call(final Subscriber<? super OnCheckedChangeEvent> observer) {
Assertions.assertUiThread();
final CompositeOnCheckedChangeListener composite = CachedListeners.getFromViewOrCreate(button);

final CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(final CompoundButton button, final boolean checked) {
observer.onNext(checked);
public void onCheckedChanged(final CompoundButton view, final boolean checked) {
observer.onNext(new OnCheckedChangeEvent(button, checked));
}
};

Expand All @@ -58,7 +59,7 @@ public void call() {
});

if (emitInitialValue) {
observer.onNext(button.isChecked());
observer.onNext(new OnCheckedChangeEvent(button));
}

composite.addOnCheckedChangeListener(listener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.operators;
package rx.android.operators;

import rx.Observable;
import rx.Subscriber;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.operators;
package rx.android.operators;

import android.content.BroadcastReceiver;
import android.content.Context;
Expand All @@ -24,7 +24,6 @@
import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.android.subscriptions.AndroidSubscriptions;
import rx.functions.Action0;
import rx.subscriptions.Subscriptions;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,35 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.operators;
package rx.android.operators;

import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.android.events.OnTextChangeEvent;
import rx.android.observables.Assertions;
import rx.android.subscriptions.AndroidSubscriptions;
import rx.functions.Action0;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.TextView;

public class OperatorTextViewInput<T extends TextView> implements Observable.OnSubscribe<T> {
private final T input;
public class OperatorTextViewInput implements Observable.OnSubscribe<OnTextChangeEvent> {
private final boolean emitInitialValue;
private final TextView input;

public OperatorTextViewInput(final T input, final boolean emitInitialValue) {
public OperatorTextViewInput(final TextView input, final boolean emitInitialValue) {
this.input = input;
this.emitInitialValue = emitInitialValue;
}

@Override
public void call(final Subscriber<? super T> observer) {
public void call(final Subscriber<? super OnTextChangeEvent> observer) {
Assertions.assertUiThread();
final TextWatcher watcher = new SimpleTextWatcher() {
@Override
public void afterTextChanged(final Editable editable) {
observer.onNext(input);
observer.onNext(new OnTextChangeEvent(input));
}
};

Expand All @@ -52,7 +53,7 @@ public void call() {
});

if (emitInitialValue) {
observer.onNext(input);
observer.onNext(new OnTextChangeEvent(input));
}

input.addTextChangedListener(watcher);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,40 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.operators;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
package rx.android.operators;

import android.view.View;
import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.android.events.OnClickEvent;
import rx.android.observables.Assertions;
import rx.android.subscriptions.AndroidSubscriptions;
import rx.functions.Action0;
import android.view.View;

public final class OperatorViewClick<T extends View> implements Observable.OnSubscribe<T> {
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;

public final class OperatorViewClick implements Observable.OnSubscribe<OnClickEvent> {
private final boolean emitInitialValue;
private final T view;
private final View view;

public OperatorViewClick(final T view, final boolean emitInitialValue) {
public OperatorViewClick(final View view, final boolean emitInitialValue) {
this.emitInitialValue = emitInitialValue;
this.view = view;
}

@Override
public void call(final Subscriber<? super T> observer) {
public void call(final Subscriber<? super OnClickEvent> observer) {
Assertions.assertUiThread();
final CompositeOnClickListener composite = CachedListeners.getFromViewOrCreate(view);

final View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(final View clicked) {
observer.onNext(view);
observer.onNext(new OnClickEvent(view));
}
};

Expand All @@ -57,7 +58,7 @@ public void call() {
});

if (emitInitialValue) {
observer.onNext(view);
observer.onNext(new OnClickEvent(view));
}

composite.addOnClickListener(listener);
Expand Down
Loading