|
| 1 | +package com.higo.zhangyp.segmented; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.graphics.Color; |
| 5 | +import android.os.SystemClock; |
| 6 | +import android.view.LayoutInflater; |
| 7 | +import android.widget.RadioButton; |
| 8 | +import android.widget.RadioGroup; |
| 9 | + |
| 10 | +import com.facebook.react.bridge.JSApplicationIllegalArgumentException; |
| 11 | +import com.facebook.react.bridge.ReadableArray; |
| 12 | +import com.facebook.react.uimanager.annotations.ReactProp; |
| 13 | +import com.facebook.react.uimanager.SimpleViewManager; |
| 14 | +import com.facebook.react.uimanager.ThemedReactContext; |
| 15 | +import com.facebook.react.uimanager.UIManagerModule; |
| 16 | + |
| 17 | +/** |
| 18 | + * Created by zhangyipeng on 15/12/15. |
| 19 | + */ |
| 20 | +public class AndroidSegmentedManager extends SimpleViewManager<AndroidSegmented> { |
| 21 | + |
| 22 | + public static final String REACT_CLASS = "AndroidSegmented"; |
| 23 | + |
| 24 | + private static final String COLOR_REGEX = "^#([0-9A-Fa-f]{6}|[0-9A-Fa-f]{8})$"; |
| 25 | + |
| 26 | + private Context context; |
| 27 | + |
| 28 | + @Override |
| 29 | + public String getName() { |
| 30 | + return REACT_CLASS; |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + protected AndroidSegmented createViewInstance(ThemedReactContext reactContext) { |
| 35 | + this.context = reactContext; |
| 36 | + return new AndroidSegmented(reactContext); |
| 37 | + } |
| 38 | + |
| 39 | + |
| 40 | + @Override |
| 41 | + protected void addEventEmitters(final ThemedReactContext reactContext, final AndroidSegmented view) { |
| 42 | + |
| 43 | + view.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { |
| 44 | + @Override |
| 45 | + public void onCheckedChanged(RadioGroup group, int checkedId) { |
| 46 | + |
| 47 | + int childCount = view.getChildCount(); |
| 48 | + for (int i = 0; i < childCount; i++) { |
| 49 | + ((RadioButton)view.getChildAt(i)).setChecked(false); |
| 50 | + if (view.getChildAt(i).getId() == checkedId) { |
| 51 | + ((RadioButton)view.getChildAt(i)).setChecked(true); |
| 52 | + |
| 53 | + reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher() |
| 54 | + .dispatchEvent(new AndroidSegmentedEvent(view.getId(), i)); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + @ReactProp(name = "childText") |
| 63 | + public void setChildText(AndroidSegmented view, ReadableArray data) { |
| 64 | + int childCount = data.size(); |
| 65 | + |
| 66 | + for (int i = 0; i < childCount; ++i) { |
| 67 | + RadioButton child = (RadioButton) LayoutInflater.from(context).inflate(R.layout.radio_button, null); |
| 68 | + |
| 69 | + child.setText(data.getString(i)); |
| 70 | + view.addView(child); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + @ReactProp(name = "selectedPosition") |
| 76 | + public void setSelectedChild(AndroidSegmented view, int position) { |
| 77 | + RadioButton radioBt = (RadioButton) (view.getChildAt(position)); |
| 78 | + radioBt.setChecked(true); |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + @ReactProp(name = "orientation") |
| 83 | + public void setOrientation(AndroidSegmented view, String orientation) { |
| 84 | + view.setSegmentOrientation(orientation); |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + @ReactProp(name = "tintColor") |
| 89 | + public void setTintColor(AndroidSegmented view, ReadableArray data) { |
| 90 | + |
| 91 | + String type0 = data.getType(0).name(); |
| 92 | + String type1 = data.getType(1).name(); |
| 93 | + |
| 94 | + if ("String".equals(type0) && "String".equals(type1)) { |
| 95 | + String color0 = data.getString(0); |
| 96 | + String color1 = data.getString(1); |
| 97 | + if (color0 != null && color1 != null) { |
| 98 | + if (color0.matches(COLOR_REGEX) && color1.matches(COLOR_REGEX)) { |
| 99 | + |
| 100 | + view.setTintColor(Color.parseColor(color0), Color.parseColor(color1)); |
| 101 | + } else { |
| 102 | + throw new JSApplicationIllegalArgumentException("Invalid arrowColor property: " + color0); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments