Skip to content

Commit 20bbe7c

Browse files
Merge pull request #783 from mironov-nsk/android-ui-operators
Implement some Android UI related operators
2 parents 29162d7 + 1da668a commit 20bbe7c

File tree

8 files changed

+738
-1
lines changed

8 files changed

+738
-1
lines changed

rxjava-contrib/rxjava-android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ dependencies {
88
// testing
99
provided 'junit:junit-dep:4.10'
1010
provided 'org.mockito:mockito-core:1.8.5'
11-
provided 'org.robolectric:robolectric:2.1.1'
11+
provided 'org.robolectric:robolectric:2.2'
1212
}
1313

1414
javadoc {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Copyright 2014 Netflix, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package rx.android.observables;
17+
18+
import android.view.View;
19+
import android.widget.CompoundButton;
20+
import android.widget.EditText;
21+
import rx.Observable;
22+
import rx.operators.OperatorCompoundButtonInput;
23+
import rx.operators.OperatorEditTextInput;
24+
import rx.operators.OperatorViewClick;
25+
26+
public class ViewObservable {
27+
28+
public static Observable<View> clicks(final View view, final boolean emitInitialValue) {
29+
return Observable.create(new OperatorViewClick(view, emitInitialValue));
30+
}
31+
32+
public static Observable<String> input(final EditText input, final boolean emitInitialValue) {
33+
return Observable.create(new OperatorEditTextInput(input, emitInitialValue));
34+
}
35+
36+
public static Observable<Boolean> input(final CompoundButton button, final boolean emitInitialValue) {
37+
return Observable.create(new OperatorCompoundButtonInput(button, emitInitialValue));
38+
}
39+
40+
}
41+
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* Copyright 2014 Netflix, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package rx.operators;
17+
18+
import android.view.View;
19+
import android.widget.CompoundButton;
20+
import rx.Observable;
21+
import rx.Observer;
22+
import rx.Subscription;
23+
24+
import java.util.ArrayList;
25+
import java.util.List;
26+
import java.util.Map;
27+
import java.util.WeakHashMap;
28+
29+
public class OperatorCompoundButtonInput implements Observable.OnSubscribe<Boolean> {
30+
private final boolean emitInitialValue;
31+
private final CompoundButton button;
32+
33+
public OperatorCompoundButtonInput(final CompoundButton button, final boolean emitInitialValue) {
34+
this.emitInitialValue = emitInitialValue;
35+
this.button = button;
36+
}
37+
38+
@Override
39+
public void call(final Observer<? super Boolean> observer) {
40+
final CompositeOnCheckedChangeListener composite = CachedListeners.getFromViewOrCreate(button);
41+
42+
final CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {
43+
@Override
44+
public void onCheckedChanged(final CompoundButton button, final boolean checked) {
45+
observer.onNext(checked);
46+
}
47+
};
48+
49+
final Subscription subscription = new Subscription() {
50+
@Override
51+
public void unsubscribe() {
52+
composite.removeOnCheckedChangeListener(listener);
53+
}
54+
};
55+
56+
if (emitInitialValue) {
57+
observer.onNext(button.isChecked());
58+
}
59+
60+
composite.addOnCheckedChangeListener(listener);
61+
observer.add(subscription);
62+
}
63+
64+
private static class CompositeOnCheckedChangeListener implements CompoundButton.OnCheckedChangeListener {
65+
private final List<CompoundButton.OnCheckedChangeListener> listeners = new ArrayList<CompoundButton.OnCheckedChangeListener>();
66+
67+
public boolean addOnCheckedChangeListener(final CompoundButton.OnCheckedChangeListener listener) {
68+
return listeners.add(listener);
69+
}
70+
71+
public boolean removeOnCheckedChangeListener(final CompoundButton.OnCheckedChangeListener listener) {
72+
return listeners.remove(listener);
73+
}
74+
75+
@Override
76+
public void onCheckedChanged(final CompoundButton button, final boolean checked) {
77+
for (final CompoundButton.OnCheckedChangeListener listener : listeners) {
78+
listener.onCheckedChanged(button, checked);
79+
}
80+
}
81+
}
82+
83+
private static class CachedListeners {
84+
private static final Map<View, CompositeOnCheckedChangeListener> sCachedListeners = new WeakHashMap<View, CompositeOnCheckedChangeListener>();
85+
86+
public static CompositeOnCheckedChangeListener getFromViewOrCreate(final CompoundButton button) {
87+
final CompositeOnCheckedChangeListener cached = sCachedListeners.get(button);
88+
89+
if (cached != null) {
90+
return cached;
91+
}
92+
93+
final CompositeOnCheckedChangeListener listener = new CompositeOnCheckedChangeListener();
94+
95+
sCachedListeners.put(button, listener);
96+
button.setOnCheckedChangeListener(listener);
97+
98+
return listener;
99+
}
100+
}
101+
}
102+
103+
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* Copyright 2014 Netflix, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package rx.operators;
17+
18+
import android.text.Editable;
19+
import android.text.TextWatcher;
20+
import android.widget.EditText;
21+
import rx.Observable;
22+
import rx.Observer;
23+
import rx.Subscription;
24+
25+
public class OperatorEditTextInput implements Observable.OnSubscribe<String> {
26+
private final EditText input;
27+
private final boolean emitInitialValue;
28+
29+
public OperatorEditTextInput(final EditText input, final boolean emitInitialValue) {
30+
this.input = input;
31+
this.emitInitialValue = emitInitialValue;
32+
}
33+
34+
@Override
35+
public void call(final Observer<? super String> observer) {
36+
final TextWatcher watcher = new SimpleTextWatcher() {
37+
@Override
38+
public void afterTextChanged(final Editable editable) {
39+
observer.onNext(editable.toString());
40+
}
41+
};
42+
43+
final Subscription subscription = new Subscription() {
44+
@Override
45+
public void unsubscribe() {
46+
input.removeTextChangedListener(watcher);
47+
}
48+
};
49+
50+
if (emitInitialValue) {
51+
observer.onNext(input.getEditableText().toString());
52+
}
53+
54+
input.addTextChangedListener(watcher);
55+
observer.add(subscription);
56+
}
57+
58+
private static class SimpleTextWatcher implements TextWatcher {
59+
@Override
60+
public void beforeTextChanged(final CharSequence sequence, final int start, final int count, final int after) {
61+
// nothing to do
62+
}
63+
64+
@Override
65+
public void onTextChanged(final CharSequence sequence, final int start, final int before, final int count) {
66+
// nothing to do
67+
}
68+
69+
@Override
70+
public void afterTextChanged(final Editable editable) {
71+
// nothing to do
72+
}
73+
}
74+
}
75+
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* Copyright 2014 Netflix, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package rx.operators;
17+
18+
import android.view.View;
19+
import rx.Observable;
20+
import rx.Observer;
21+
import rx.Subscription;
22+
import java.util.ArrayList;
23+
import java.util.List;
24+
import java.util.Map;
25+
import java.util.WeakHashMap;
26+
27+
public final class OperatorViewClick implements Observable.OnSubscribe<View> {
28+
private final boolean emitInitialValue;
29+
private final View view;
30+
31+
public OperatorViewClick(final View view, final boolean emitInitialValue) {
32+
this.emitInitialValue = emitInitialValue;
33+
this.view = view;
34+
}
35+
36+
@Override
37+
public void call(final Observer<? super View> observer) {
38+
final CompositeOnClickListener composite = CachedListeners.getFromViewOrCreate(view);
39+
40+
final View.OnClickListener listener = new View.OnClickListener() {
41+
@Override
42+
public void onClick(final View clicked) {
43+
observer.onNext(view);
44+
}
45+
};
46+
47+
final Subscription subscription = new Subscription() {
48+
@Override
49+
public void unsubscribe() {
50+
composite.removeOnClickListener(listener);
51+
}
52+
};
53+
54+
if (emitInitialValue) {
55+
observer.onNext(view);
56+
}
57+
58+
composite.addOnClickListener(listener);
59+
observer.add(subscription);
60+
}
61+
62+
private static class CompositeOnClickListener implements View.OnClickListener {
63+
private final List<View.OnClickListener> listeners = new ArrayList<View.OnClickListener>();
64+
65+
public boolean addOnClickListener(final View.OnClickListener listener) {
66+
return listeners.add(listener);
67+
}
68+
69+
public boolean removeOnClickListener(final View.OnClickListener listener) {
70+
return listeners.remove(listener);
71+
}
72+
73+
@Override
74+
public void onClick(final View view) {
75+
for (final View.OnClickListener listener : listeners) {
76+
listener.onClick(view);
77+
}
78+
}
79+
}
80+
81+
private static class CachedListeners {
82+
private static final Map<View, CompositeOnClickListener> sCachedListeners = new WeakHashMap<View, CompositeOnClickListener>();
83+
84+
public static CompositeOnClickListener getFromViewOrCreate(final View view) {
85+
final CompositeOnClickListener cached = sCachedListeners.get(view);
86+
87+
if (cached != null) {
88+
return cached;
89+
}
90+
91+
final CompositeOnClickListener listener = new CompositeOnClickListener();
92+
93+
sCachedListeners.put(view, listener);
94+
view.setOnClickListener(listener);
95+
96+
return listener;
97+
}
98+
}
99+
}

0 commit comments

Comments
 (0)