Skip to content

Commit

Permalink
Merge branch '0.x' to 'naghtarr/itemclicks'
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirill Boyarshinov committed Oct 15, 2014
2 parents 6b7cfe2 + 2bb5640 commit e800920
Show file tree
Hide file tree
Showing 19 changed files with 394 additions and 206 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,10 @@ bin/
# Scala build
*.cache
/.nb-gradle/private/

# Android
local.properties

# jEnv
.java-version

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;
}
}
17 changes: 14 additions & 3 deletions src/main/java/rx/android/observables/AndroidObservable.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@

import static rx.android.schedulers.AndroidSchedulers.mainThread;

import android.content.SharedPreferences;
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 All @@ -30,6 +31,7 @@
import android.content.IntentFilter;
import android.os.Build;
import android.os.Handler;
import rx.operators.OperatorSharedPreferenceChange;


public final class AndroidObservable {
Expand Down Expand Up @@ -150,4 +152,13 @@ public static Observable<Intent> fromBroadcast(Context context, IntentFilter fil
public static Observable<Intent> fromLocalBroadcast(Context context, IntentFilter filter){
return Observable.create(new OperatorLocalBroadcastRegister(context, filter));
}

/**
* Create Observable that emits String keys whenever it changes in provided SharedPreferences
*
* Items will be observed on the main Android UI thread
*/
public static Observable<String> fromSharedPreferencesChanges(SharedPreferences sharedPreferences){
return Observable.create(new OperatorSharedPreferenceChange(sharedPreferences));
}
}
31 changes: 20 additions & 11 deletions src/main/java/rx/android/observables/ViewObservable.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,43 @@
package rx.android.observables;

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

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

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));
}

Expand All @@ -51,4 +61,3 @@ public static Observable<OnItemClickEvent> itemClicks(final AdapterView<?> adapt
}

}

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
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ private AndroidSubscriptions() {
}

/**
* Create an Subscription that always runs <code>unsubscribe</code> in the UI thread.
* Create a {@link Subscription} that always runs <code>unsubscribe</code> in the UI thread.
*
* @param unsubscribe
* @return an Subscription that always runs <code>unsubscribe</code> in the UI thread.
* @return a {@link Subscription} that always runs <code>unsubscribe</code> in the UI thread.
*/
public static Subscription unsubscribeInUiThread(final Action0 unsubscribe) {
return Subscriptions.create(new Action0() {
Expand Down
Loading

0 comments on commit e800920

Please sign in to comment.