Skip to content

Commit

Permalink
Merge pull request #783 from mironov-nsk/android-ui-operators
Browse files Browse the repository at this point in the history
Implement some Android UI related operators
  • Loading branch information
benjchristensen committed Feb 6, 2014
2 parents 29162d7 + 1da668a commit 20bbe7c
Show file tree
Hide file tree
Showing 8 changed files with 738 additions and 1 deletion.
2 changes: 1 addition & 1 deletion rxjava-contrib/rxjava-android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dependencies {
// testing
provided 'junit:junit-dep:4.10'
provided 'org.mockito:mockito-core:1.8.5'
provided 'org.robolectric:robolectric:2.1.1'
provided 'org.robolectric:robolectric:2.2'
}

javadoc {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Copyright 2014 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.android.observables;

import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import rx.Observable;
import rx.operators.OperatorCompoundButtonInput;
import rx.operators.OperatorEditTextInput;
import rx.operators.OperatorViewClick;

public class ViewObservable {

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

public static Observable<String> input(final EditText input, final boolean emitInitialValue) {
return Observable.create(new OperatorEditTextInput(input, emitInitialValue));
}

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

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**
* Copyright 2014 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.operators;

import android.view.View;
import android.widget.CompoundButton;
import rx.Observable;
import rx.Observer;
import rx.Subscription;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;

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

public OperatorCompoundButtonInput(final CompoundButton button, final boolean emitInitialValue) {
this.emitInitialValue = emitInitialValue;
this.button = button;
}

@Override
public void call(final Observer<? super Boolean> observer) {
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);
}
};

final Subscription subscription = new Subscription() {
@Override
public void unsubscribe() {
composite.removeOnCheckedChangeListener(listener);
}
};

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

composite.addOnCheckedChangeListener(listener);
observer.add(subscription);
}

private static class CompositeOnCheckedChangeListener implements CompoundButton.OnCheckedChangeListener {
private final List<CompoundButton.OnCheckedChangeListener> listeners = new ArrayList<CompoundButton.OnCheckedChangeListener>();

public boolean addOnCheckedChangeListener(final CompoundButton.OnCheckedChangeListener listener) {
return listeners.add(listener);
}

public boolean removeOnCheckedChangeListener(final CompoundButton.OnCheckedChangeListener listener) {
return listeners.remove(listener);
}

@Override
public void onCheckedChanged(final CompoundButton button, final boolean checked) {
for (final CompoundButton.OnCheckedChangeListener listener : listeners) {
listener.onCheckedChanged(button, checked);
}
}
}

private static class CachedListeners {
private static final Map<View, CompositeOnCheckedChangeListener> sCachedListeners = new WeakHashMap<View, CompositeOnCheckedChangeListener>();

public static CompositeOnCheckedChangeListener getFromViewOrCreate(final CompoundButton button) {
final CompositeOnCheckedChangeListener cached = sCachedListeners.get(button);

if (cached != null) {
return cached;
}

final CompositeOnCheckedChangeListener listener = new CompositeOnCheckedChangeListener();

sCachedListeners.put(button, listener);
button.setOnCheckedChangeListener(listener);

return listener;
}
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* Copyright 2014 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.operators;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import rx.Observable;
import rx.Observer;
import rx.Subscription;

public class OperatorEditTextInput implements Observable.OnSubscribe<String> {
private final EditText input;
private final boolean emitInitialValue;

public OperatorEditTextInput(final EditText input, final boolean emitInitialValue) {
this.input = input;
this.emitInitialValue = emitInitialValue;
}

@Override
public void call(final Observer<? super String> observer) {
final TextWatcher watcher = new SimpleTextWatcher() {
@Override
public void afterTextChanged(final Editable editable) {
observer.onNext(editable.toString());
}
};

final Subscription subscription = new Subscription() {
@Override
public void unsubscribe() {
input.removeTextChangedListener(watcher);
}
};

if (emitInitialValue) {
observer.onNext(input.getEditableText().toString());
}

input.addTextChangedListener(watcher);
observer.add(subscription);
}

private static class SimpleTextWatcher implements TextWatcher {
@Override
public void beforeTextChanged(final CharSequence sequence, final int start, final int count, final int after) {
// nothing to do
}

@Override
public void onTextChanged(final CharSequence sequence, final int start, final int before, final int count) {
// nothing to do
}

@Override
public void afterTextChanged(final Editable editable) {
// nothing to do
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* Copyright 2014 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.operators;

import android.view.View;
import rx.Observable;
import rx.Observer;
import rx.Subscription;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;

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

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

@Override
public void call(final Observer<? super View> observer) {
final CompositeOnClickListener composite = CachedListeners.getFromViewOrCreate(view);

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

final Subscription subscription = new Subscription() {
@Override
public void unsubscribe() {
composite.removeOnClickListener(listener);
}
};

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

composite.addOnClickListener(listener);
observer.add(subscription);
}

private static class CompositeOnClickListener implements View.OnClickListener {
private final List<View.OnClickListener> listeners = new ArrayList<View.OnClickListener>();

public boolean addOnClickListener(final View.OnClickListener listener) {
return listeners.add(listener);
}

public boolean removeOnClickListener(final View.OnClickListener listener) {
return listeners.remove(listener);
}

@Override
public void onClick(final View view) {
for (final View.OnClickListener listener : listeners) {
listener.onClick(view);
}
}
}

private static class CachedListeners {
private static final Map<View, CompositeOnClickListener> sCachedListeners = new WeakHashMap<View, CompositeOnClickListener>();

public static CompositeOnClickListener getFromViewOrCreate(final View view) {
final CompositeOnClickListener cached = sCachedListeners.get(view);

if (cached != null) {
return cached;
}

final CompositeOnClickListener listener = new CompositeOnClickListener();

sCachedListeners.put(view, listener);
view.setOnClickListener(listener);

return listener;
}
}
}
Loading

0 comments on commit 20bbe7c

Please sign in to comment.