-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
rxjava-contrib/rxjava-android/src/main/java/rx/android/functions/ViewActionSetActivated.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,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); | ||
} | ||
} | ||
|
35 changes: 35 additions & 0 deletions
35
rxjava-contrib/rxjava-android/src/main/java/rx/android/functions/ViewActionSetClickable.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,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); | ||
} | ||
} | ||
|
35 changes: 35 additions & 0 deletions
35
rxjava-contrib/rxjava-android/src/main/java/rx/android/functions/ViewActionSetEnabled.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,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); | ||
} | ||
} | ||
|
35 changes: 35 additions & 0 deletions
35
rxjava-contrib/rxjava-android/src/main/java/rx/android/functions/ViewActionSetFocusable.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,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); | ||
} | ||
} | ||
|
35 changes: 35 additions & 0 deletions
35
rxjava-contrib/rxjava-android/src/main/java/rx/android/functions/ViewActionSetSelected.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,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); | ||
} | ||
} | ||
|
48 changes: 48 additions & 0 deletions
48
...va-contrib/rxjava-android/src/main/java/rx/android/functions/ViewActionSetVisibility.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,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> { | ||
|
||
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); | ||
} | ||
} | ||
|
117 changes: 117 additions & 0 deletions
117
rxjava-contrib/rxjava-android/src/main/java/rx/android/functions/ViewActions.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,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); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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: