-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement some Android UI related operators
1. OperatorViewClick 2. OperatorEditTextInput 3. OperatorCompoundButtonInput
- Loading branch information
1 parent
86993d3
commit 1da668a
Showing
8 changed files
with
738 additions
and
1 deletion.
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
41 changes: 41 additions & 0 deletions
41
rxjava-contrib/rxjava-android/src/main/java/rx/android/observables/ViewObservable.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,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)); | ||
} | ||
|
||
} | ||
|
103 changes: 103 additions & 0 deletions
103
rxjava-contrib/rxjava-android/src/main/java/rx/operators/OperatorCompoundButtonInput.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,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; | ||
} | ||
} | ||
} | ||
|
||
|
75 changes: 75 additions & 0 deletions
75
rxjava-contrib/rxjava-android/src/main/java/rx/operators/OperatorEditTextInput.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,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 | ||
} | ||
} | ||
} | ||
|
99 changes: 99 additions & 0 deletions
99
rxjava-contrib/rxjava-android/src/main/java/rx/operators/OperatorViewClick.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,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; | ||
} | ||
} | ||
} |
Oops, something went wrong.