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

View actions #27

Merged
merged 4 commits into from
Oct 19, 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
51 changes: 51 additions & 0 deletions src/main/java/rx/functions/ViewAction1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* 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.functions;

import android.view.View;

import rx.functions.Action1;

import java.lang.ref.WeakReference;

/**
* An {@link Action1} implementation specific for {@link View}s.
*
* @param <V> the type of {@link View} upon which to perform the action.
* @param <T> the type being observed
*/
public abstract class ViewAction1<V extends View, T> implements Action1<T> {

private final WeakReference<V> viewReference;

public ViewAction1(V view) {
viewReference = new WeakReference<V>(view);
}

@Override
public final void call(T t) {
V view = viewReference.get();
if (view != null) {
call(view, t);
}
}

/**
* Implement this instead of {@link Action1#call(Object)}.
* @param view the view given in the constructor.
* @param t the object being observed
*/
public abstract void call(V view, T t);

}
29 changes: 29 additions & 0 deletions src/main/java/rx/functions/ViewActionSetActivated.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* 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.functions;

import android.view.View;

public class ViewActionSetActivated extends ViewAction1<View, Boolean> {

public ViewActionSetActivated(View view) {
super(view);
}

@Override
public void call(View view, Boolean aBoolean) {
view.setActivated(aBoolean);
}
}

29 changes: 29 additions & 0 deletions src/main/java/rx/functions/ViewActionSetClickable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* 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.functions;

import android.view.View;

public class ViewActionSetClickable extends ViewAction1<View, Boolean> {

public ViewActionSetClickable(View view) {
super(view);
}

@Override
public void call(View view, Boolean aBoolean) {
view.setClickable(aBoolean);
}
}

29 changes: 29 additions & 0 deletions src/main/java/rx/functions/ViewActionSetEnabled.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* 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.functions;

import android.view.View;

public class ViewActionSetEnabled extends ViewAction1<View, Boolean> {

public ViewActionSetEnabled(View view) {
super(view);
}

@Override
public void call(View view, Boolean aBoolean) {
view.setEnabled(aBoolean);
}
}

29 changes: 29 additions & 0 deletions src/main/java/rx/functions/ViewActionSetFocusable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* 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.functions;

import android.view.View;

public class ViewActionSetFocusable extends ViewAction1<View, Boolean> {

public ViewActionSetFocusable(View view) {
super(view);
}

@Override
public void call(View view, Boolean aBoolean) {
view.setFocusable(aBoolean);
}
}

29 changes: 29 additions & 0 deletions src/main/java/rx/functions/ViewActionSetSelected.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* 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.functions;

import android.view.View;

public class ViewActionSetSelected extends ViewAction1<View, Boolean> {

public ViewActionSetSelected(View view) {
super(view);
}

@Override
public void call(View view, Boolean aBoolean) {
view.setSelected(aBoolean);
}
}

43 changes: 43 additions & 0 deletions src/main/java/rx/functions/ViewActionSetVisibility.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* 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.functions;

import android.view.View;

public class ViewActionSetVisibility extends ViewAction1<View, Boolean> {

private final int visibilityOnFalse;

public ViewActionSetVisibility(View view) {
this(view, View.GONE);
}

public ViewActionSetVisibility(View view, int visibilityOnFalse) {
super(view);
if (visibilityOnFalse != View.GONE &&
visibilityOnFalse != View.INVISIBLE &&
visibilityOnFalse != View.VISIBLE) {
throw new IllegalArgumentException(visibilityOnFalse +
" is not a valid visibility value. See android.view.View VISIBLE, GONE, and INVISIBLE");
}
this.visibilityOnFalse = visibilityOnFalse;
}

@Override
public void call(View view, Boolean aBoolean) {
int visibility = aBoolean ? View.VISIBLE : visibilityOnFalse;
view.setVisibility(visibility);
}
}

115 changes: 115 additions & 0 deletions src/main/java/rx/functions/ViewActions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/**
* 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.functions;

import android.view.View;

import rx.functions.Action1;

/**
* Utility class for the Action interfaces for use with Android {@link View}s.
*/
public final class ViewActions {

private ViewActions() {
throw new IllegalStateException("No instances!");
}

/**
* Set whether a view is enabled based on {@code boolean} values that are emitted by an
* Observable.
*
* @param view
* to modify
* @return an {@link Action1} that sets the enabled of a view when boolean values are emitted.
*/
public static Action1<? super Boolean> setEnabled(View view) {
return new ViewActionSetEnabled(view);
}

/**
* Set whether a view is activated based on {@code boolean} values that are emitted by an
* Observable.
*
* @param view
* to modify
* @return an {@link Action1} that sets the activated of a view when boolean values are emitted.
*/
public static Action1<? super Boolean> setActivated(View view) {
return new ViewActionSetActivated(view);
}

/**
* Set whether a view is clickable based on {@code boolean} values that are emitted by an
* Observable.
*
* @param view
* to modify
* @return an {@link Action1} that sets the clickable of a view when boolean values are emitted.
*/
public static Action1<? super Boolean> setClickable(View view) {
return new ViewActionSetClickable(view);
}

/**
* Set whether a view is focusable based on {@code boolean} values that are emitted by an
* Observable.
*
* @param view
* to modify
* @return an {@link Action1} that sets the focusable of a view when boolean values are emitted.
*/
public static Action1<? super Boolean> setFocusable(View view) {
return new ViewActionSetFocusable(view);
}

/**
* Set whether a view is selected based on {@code boolean} values that are emitted by an
* Observable.
*
* @param view
* to modify
* @return an {@link Action1} that sets the selected of a view when boolean values are emitted.
*/
public static Action1<? super Boolean> setSelected(View view) {
return new ViewActionSetSelected(view);
}

/**
* Set the visibility of a view based on {@code boolean} values that are emitted by an
* Observable. When {@code false} is emitted, visibility is set to {@link View#GONE}.
*
* @param view
* to modify
* @return an {@link Action1} that sets the visibility of a view when boolean values are emitted.
*/
public static Action1<? super Boolean> setVisibility(View view) {
return new ViewActionSetVisibility(view);
}

/**
* Set the visibility of a view based on {@code boolean} values that are emitted by an
* Observable.
*
* @param view
* to modify
* @param visibilityOnFalse
* value to use on {@link View#setVisibility(int)} when a {@code false} is emitted by
* the {@link rx.Observable}
* @return an {@link Action1} that sets the visibility of a view when boolean values are emitted.
*/
public static Action1<? super Boolean> setVisibility(View view, int visibilityOnFalse) {
return new ViewActionSetVisibility(view, visibilityOnFalse);
}
}
30 changes: 30 additions & 0 deletions src/test/java/rx/TestUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* 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;

import android.view.View;

import org.robolectric.Robolectric;

public class TestUtil {

private TestUtil() {
throw new AssertionError("Utility class");
}

public static View createView() {
return new View(Robolectric.application);
}

}
Loading