-
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 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 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 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 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 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> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure I understand the purpose of this. It seems like an There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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); | ||
} | ||
} |
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.
can make these final everywhere?
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.
Good catch - fixed here: https://github.com/ronshapiro/RxJava/commit/5bc9ac13affae0ea919742b41c20903866221b42