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

Add ViewActions utilities for Android #1584

Closed
wants to merge 2 commits into from
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* 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.functions;

import android.view.View;

import rx.functions.Action1;

public class ViewActionSetActivated implements Action1<Boolean> {

private final View view;

public ViewActionSetActivated(View view) {
this.view = view;
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* 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.functions;

import android.view.View;

import rx.functions.Action1;

public class ViewActionSetClickable implements Action1<Boolean> {

private final View view;

public ViewActionSetClickable(View view) {
this.view = view;
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* 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.functions;

import android.view.View;

import rx.functions.Action1;

public class ViewActionSetEnabled implements Action1<Boolean> {

private final View view;

public ViewActionSetEnabled(View view) {
this.view = view;
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* 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.functions;

import android.view.View;

import rx.functions.Action1;

public class ViewActionSetFocusable implements Action1<Boolean> {

private final View view;

public ViewActionSetFocusable(View view) {
this.view = view;
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* 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.functions;

import android.view.View;

import rx.functions.Action1;

public class ViewActionSetSelected implements Action1<Boolean> {

private final View view;

public ViewActionSetSelected(View view) {
this.view = view;
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* 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.functions;

import android.view.View;

import rx.functions.Action1;

public class ViewActionSetVisibility implements Action1<Boolean> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure I understand the purpose of this. It seems like an Action1 version of mapping a boolean to a visibility state. Is this really useful in practice? I looked at the test but it doesn't give much insight into what the practical use case would be. Could you make an example of how one would use this in practice?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I often display/hide views based on a conditional, especially for growth/onboarding. Here's one example:

View findYourFriendsButton = // ...
Observable<Integer> getFriendsCount = // ...
getFriendsCount()
        .map(integer -> integer < 10)
        .subscribe(setVisibility(findYourFriendsButton));


private final View view;
private final int visibilityOnFalse;

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

public ViewActionSetVisibility(View view, int visibilityOnFalse) {
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.view = view;
this.visibilityOnFalse = visibilityOnFalse;
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/**
* 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.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);
}
}
Loading