Skip to content

Commit

Permalink
Simplify OSS enums
Browse files Browse the repository at this point in the history
Summary: Simplifying our OSS enums (remove private variables and methods) so that redex can more easily optimize them for us.

Reviewed By: achen1, Feng23

Differential Revision: D9812796

fbshipit-source-id: 11a8272db41ff04399d1cdf366e28ddf1b07b7be
  • Loading branch information
Emily Janzer authored and grabbou committed Oct 11, 2018
1 parent e7ea1f8 commit 0df8e7d
Show file tree
Hide file tree
Showing 14 changed files with 148 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,48 @@ public class AccessibilityDelegateUtil {
*/

public enum AccessibilityRole {
NONE(null),
BUTTON("android.widget.Button"),
LINK("android.widget.ViewGroup"),
SEARCH("android.widget.EditText"),
IMAGE("android.widget.ImageView"),
IMAGEBUTTON("android.widget.ImageView"),
KEYBOARDKEY("android.inputmethodservice.Keyboard$Key"),
TEXT("android.widget.ViewGroup"),
ADJUSTABLE("android.widget.SeekBar"),
SUMMARY("android.widget.ViewGroup"),
HEADER("android.widget.ViewGroup");

@Nullable private final String mValue;

AccessibilityRole(String type) {
mValue = type;
}

@Nullable
public String getValue() {
return mValue;
NONE,
BUTTON,
LINK,
SEARCH,
IMAGE,
IMAGEBUTTON,
KEYBOARDKEY,
TEXT,
ADJUSTABLE,
SUMMARY,
HEADER;

public static String getValue(AccessibilityRole role) {
switch (role) {
case NONE:
return null;
case BUTTON:
return "android.widget.Button";
case LINK:
return "android.widget.ViewGroup";
case SEARCH:
return "android.widget.EditText";
case IMAGE:
return "android.widget.ImageView";
case IMAGEBUTTON:
return "android.widget.ImageView";
case KEYBOARDKEY:
return "android.inputmethodservice.Keyboard$Key";
case TEXT:
return "android.widget.ViewGroup";
case ADJUSTABLE:
return "android.widget.SeekBar";
case SUMMARY:
return "android.widget.ViewGroup";
case HEADER:
return "android.widget.ViewGroup";
default:
throw new IllegalArgumentException("Invalid accessibility role value: " + role);
}
}

public static AccessibilityRole fromValue(String value) {
public static AccessibilityRole fromValue(@Nullable String value) {
for (AccessibilityRole role : AccessibilityRole.values()) {
if (role.getValue() != null && role.getValue().equals(value)) {
return role;
Expand Down Expand Up @@ -109,7 +127,7 @@ public static void setRole(AccessibilityNodeInfoCompat nodeInfo, AccessibilityRo
if (role == null) {
role = AccessibilityRole.NONE;
}
nodeInfo.setClassName(role.getValue());
nodeInfo.setClassName(AccessibilityRole.getValue(role));
if (Locale.getDefault().getLanguage().equals(new Locale("en").getLanguage())) {
if (role.equals(AccessibilityRole.LINK)) {
nodeInfo.setRoleDescription(context.getString(R.string.link_description));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"phasedRegistrationNames",
MapBuilder.of("bubbled", "onSelect", "captured", "onSelectCapture")))
.put(
TouchEventType.START.getJSEventName(),
TouchEventType.getJSEventName(TouchEventType.START),
MapBuilder.of(
"phasedRegistrationNames",
MapBuilder.of(
Expand All @@ -44,7 +44,7 @@
"captured",
"onTouchStartCapture")))
.put(
TouchEventType.MOVE.getJSEventName(),
TouchEventType.getJSEventName(TouchEventType.MOVE),
MapBuilder.of(
"phasedRegistrationNames",
MapBuilder.of(
Expand All @@ -53,7 +53,7 @@
"captured",
"onTouchMoveCapture")))
.put(
TouchEventType.END.getJSEventName(),
TouchEventType.getJSEventName(TouchEventType.END),
MapBuilder.of(
"phasedRegistrationNames",
MapBuilder.of(
Expand All @@ -62,7 +62,7 @@
"captured",
"onTouchEndCapture")))
.put(
TouchEventType.CANCEL.getJSEventName(),
TouchEventType.getJSEventName(TouchEventType.CANCEL),
MapBuilder.of(
"phasedRegistrationNames",
MapBuilder.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public void onDispose() {

@Override
public String getEventName() {
return Assertions.assertNotNull(mTouchEventType).getJSEventName();
return TouchEventType.getJSEventName(Assertions.assertNotNull(mTouchEventType));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,23 @@
* Touch event types that JS module RCTEventEmitter can understand
*/
public enum TouchEventType {
START("topTouchStart"),
END("topTouchEnd"),
MOVE("topTouchMove"),
CANCEL("topTouchCancel");
START,
END,
MOVE,
CANCEL;

private final String mJSEventName;

TouchEventType(String jsEventName) {
mJSEventName = jsEventName;
}

public String getJSEventName() {
return mJSEventName;
public static String getJSEventName(TouchEventType type) {
switch (type) {
case START:
return "topTouchStart";
case END:
return "topTouchEnd";
case MOVE:
return "topTouchMove";
case CANCEL:
return "topTouchCancel";
default:
throw new IllegalArgumentException("Unexpected type " + type);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public static void sendTouchEvent(
}

rctEventEmitter.receiveTouches(
type.getJSEventName(),
TouchEventType.getJSEventName(type),
pointers,
changedIndices);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,23 @@
* view creation.
*/
/* package */ enum AnimatedPropertyType {
OPACITY("opacity"),
SCALE_X("scaleX"),
SCALE_Y("scaleY"),
SCALE_XY("scaleXY");

private final String mName;

private AnimatedPropertyType(String name) {
mName = name;
}
OPACITY,
SCALE_X,
SCALE_Y,
SCALE_XY;

public static AnimatedPropertyType fromString(String name) {
for (AnimatedPropertyType property : AnimatedPropertyType.values()) {
if (property.toString().equalsIgnoreCase(name)) {
return property;
}
switch (name) {
case "opacity":
return OPACITY;
case "scaleX":
return SCALE_X;
case "scaleY":
return SCALE_Y;
case "scaleXY":
return SCALE_XY;
default:
throw new IllegalArgumentException("Unsupported animated property: " + name);
}
throw new IllegalArgumentException("Unsupported animated property : " + name);
}

@Override
public String toString() {
return mName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,26 @@
* Enum representing the different interpolators that can be used in layout animation configuration.
*/
/* package */ enum InterpolatorType {
LINEAR("linear"),
EASE_IN("easeIn"),
EASE_OUT("easeOut"),
EASE_IN_EASE_OUT("easeInEaseOut"),
SPRING("spring");

private final String mName;

private InterpolatorType(String name) {
mName = name;
}
LINEAR,
EASE_IN,
EASE_OUT,
EASE_IN_EASE_OUT,
SPRING;

public static InterpolatorType fromString(String name) {
for (InterpolatorType type : InterpolatorType.values()) {
if (type.toString().equalsIgnoreCase(name)) {
return type;
}
switch (name.toLowerCase()) {
case "linear":
return LINEAR;
case "easein":
return EASE_IN;
case "easeout":
return EASE_OUT;
case "easeineaseout":
return EASE_IN_EASE_OUT;
case "spring":
return SPRING;
default:
throw new IllegalArgumentException("Unsupported interpolation type : " + name);
}
throw new IllegalArgumentException("Unsupported interpolation type : " + name);
}

@Override
public String toString() {
return mName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,19 @@ public void initializeFromConfig(final @Nullable ReadableMap config) {

mShouldAnimateLayout = false;
int globalDuration = config.hasKey("duration") ? config.getInt("duration") : 0;
if (config.hasKey(LayoutAnimationType.CREATE.toString())) {
if (config.hasKey(LayoutAnimationType.toString(LayoutAnimationType.CREATE))) {
mLayoutCreateAnimation.initializeFromConfig(
config.getMap(LayoutAnimationType.CREATE.toString()), globalDuration);
config.getMap(LayoutAnimationType.toString(LayoutAnimationType.CREATE)), globalDuration);
mShouldAnimateLayout = true;
}
if (config.hasKey(LayoutAnimationType.UPDATE.toString())) {
if (config.hasKey(LayoutAnimationType.toString(LayoutAnimationType.UPDATE))) {
mLayoutUpdateAnimation.initializeFromConfig(
config.getMap(LayoutAnimationType.UPDATE.toString()), globalDuration);
config.getMap(LayoutAnimationType.toString(LayoutAnimationType.UPDATE)), globalDuration);
mShouldAnimateLayout = true;
}
if (config.hasKey(LayoutAnimationType.DELETE.toString())) {
if (config.hasKey(LayoutAnimationType.toString(LayoutAnimationType.DELETE))) {
mLayoutDeleteAnimation.initializeFromConfig(
config.getMap(LayoutAnimationType.DELETE.toString()), globalDuration);
config.getMap(LayoutAnimationType.toString(LayoutAnimationType.DELETE)), globalDuration);
mShouldAnimateLayout = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@
* Enum representing the different animation type that can be specified in layout animation config.
*/
/* package */ enum LayoutAnimationType {
CREATE("create"),
UPDATE("update"),
DELETE("delete");
CREATE,
UPDATE,
DELETE;

private final String mName;

private LayoutAnimationType(String name) {
mName = name;
}

@Override
public String toString() {
return mName;
public static String toString(LayoutAnimationType type) {
switch (type) {
case CREATE:
return "create";
case UPDATE:
return "update";
case DELETE:
return "delete";
default:
throw new IllegalArgumentException("Unsupported LayoutAnimationType: " + type);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,11 @@ public void scrollToEnd(

public static Map<String, Object> createExportedCustomDirectEventTypeConstants() {
return MapBuilder.<String, Object>builder()
.put(ScrollEventType.SCROLL.getJSEventName(), MapBuilder.of("registrationName", "onScroll"))
.put(ScrollEventType.BEGIN_DRAG.getJSEventName(), MapBuilder.of("registrationName", "onScrollBeginDrag"))
.put(ScrollEventType.END_DRAG.getJSEventName(), MapBuilder.of("registrationName", "onScrollEndDrag"))
.put(ScrollEventType.MOMENTUM_BEGIN.getJSEventName(), MapBuilder.of("registrationName", "onMomentumScrollBegin"))
.put(ScrollEventType.MOMENTUM_END.getJSEventName(), MapBuilder.of("registrationName", "onMomentumScrollEnd"))
.put(ScrollEventType.getJSEventName(ScrollEventType.SCROLL), MapBuilder.of("registrationName", "onScroll"))
.put(ScrollEventType.getJSEventName(ScrollEventType.BEGIN_DRAG), MapBuilder.of("registrationName", "onScrollBeginDrag"))
.put(ScrollEventType.getJSEventName(ScrollEventType.END_DRAG), MapBuilder.of("registrationName", "onScrollEndDrag"))
.put(ScrollEventType.getJSEventName(ScrollEventType.MOMENTUM_BEGIN), MapBuilder.of("registrationName", "onMomentumScrollBegin"))
.put(ScrollEventType.getJSEventName(ScrollEventType.MOMENTUM_END), MapBuilder.of("registrationName", "onMomentumScrollEnd"))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private void init(

@Override
public String getEventName() {
return Assertions.assertNotNull(mScrollEventType).getJSEventName();
return ScrollEventType.getJSEventName(Assertions.assertNotNull(mScrollEventType));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,26 @@
* Scroll event types that JS module RCTEventEmitter can understand
*/
public enum ScrollEventType {
BEGIN_DRAG("topScrollBeginDrag"),
END_DRAG("topScrollEndDrag"),
SCROLL("topScroll"),
MOMENTUM_BEGIN("topMomentumScrollBegin"),
MOMENTUM_END("topMomentumScrollEnd");
BEGIN_DRAG,
END_DRAG,
SCROLL,
MOMENTUM_BEGIN,
MOMENTUM_END;

private final String mJSEventName;

ScrollEventType(String jsEventName) {
mJSEventName = jsEventName;
}

public String getJSEventName() {
return mJSEventName;
public static String getJSEventName(ScrollEventType type) {
switch (type) {
case BEGIN_DRAG:
return "topScrollBeginDrag";
case END_DRAG:
return "topScrollEndDrag";
case SCROLL:
return "topScroll";
case MOMENTUM_BEGIN:
return "topMomentumScrollBegin";
case MOMENTUM_END:
return "topMomentumScrollEnd";
default:
throw new IllegalArgumentException("Unsupported ScrollEventType: " + type);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public Map<String, Object> getExportedCustomBubblingEventTypeConstants() {
@Override
public Map<String, Object> getExportedCustomDirectEventTypeConstants() {
return MapBuilder.<String, Object>builder()
.put(ScrollEventType.SCROLL.getJSEventName(), MapBuilder.of("registrationName", "onScroll"))
.put(ScrollEventType.getJSEventName(ScrollEventType.SCROLL), MapBuilder.of("registrationName", "onScroll"))
.build();
}

Expand Down
Loading

0 comments on commit 0df8e7d

Please sign in to comment.