-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from ronshapiro/view-actions
View actions
- Loading branch information
Showing
16 changed files
with
769 additions
and
0 deletions.
There are no files selected for viewing
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,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); | ||
|
||
} |
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,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); | ||
} | ||
} | ||
|
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,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); | ||
} | ||
} | ||
|
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,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); | ||
} | ||
} | ||
|
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,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); | ||
} | ||
} | ||
|
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,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); | ||
} | ||
} | ||
|
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,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); | ||
} | ||
} | ||
|
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,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); | ||
} | ||
} |
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,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); | ||
} | ||
|
||
} |
Oops, something went wrong.