Skip to content

Commit a66bd57

Browse files
committed
Make it runnable on 0.34+
1 parent e633ee6 commit a66bd57

8 files changed

+265
-24
lines changed

AndroidSegmented.js

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
'use strict';
22

3-
var React = require('react-native');
4-
var { requireNativeComponent, PropTypes, View } = React;
3+
var React = require('react');
4+
var ReactNative = require('react-native');
5+
6+
var PropTypes = React.PropTypes;
7+
8+
var {
9+
requireNativeComponent,
10+
View,
11+
} = ReactNative;
512

6-
var NativeAndroidSegmented = requireNativeComponent('AndroidSegmented', AndroidSegmented);
713

814
class AndroidSegmented extends React.Component {
915
constructor() {
@@ -13,14 +19,14 @@ class AndroidSegmented extends React.Component {
1319

1420
_onChange(event) {
1521
if (this.props.onChange) {
16-
this.props.onChange(event.nativeEvent);
22+
this.props.onChange(event.nativeEvent.selectedPosition);
1723
}
1824
}
1925

2026
render() {
2127
return (
2228
<NativeAndroidSegmented
23-
{...this.props}
29+
{...this.props}
2430
onChange={this._onChange}/>
2531
);
2632
}
@@ -47,8 +53,6 @@ AndroidSegmented.propTypes = {
4753
onChange: PropTypes.func,
4854
}
4955

50-
AndroidSegmented.defaultProps = {
51-
52-
};
56+
var NativeAndroidSegmented = requireNativeComponent('AndroidSegmented', AndroidSegmented, { nativeOnly: { onChange: true } });
5357

54-
module.exports = AndroidSegmented;
58+
module.exports = AndroidSegmented;

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ dependencies {
2222
compile fileTree(dir: 'libs', include: ['*.jar'])
2323
compile 'com.android.support:appcompat-v7:23.1.1'
2424
compile 'info.hoang8f:android-segmented:1.0.6'
25-
compile 'com.facebook.react:react-native:0.19.+'
25+
compile 'com.facebook.react:react-native:0.34.+'
2626
}
2727
//react-native run-android
2828
//adb reverse tcp:8081 tcp:8081
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.higo.zhangyp.segmented;
2+
3+
import android.view.Gravity;
4+
import android.view.ViewGroup;
5+
import android.widget.RadioGroup;
6+
7+
import com.facebook.react.uimanager.ThemedReactContext;
8+
9+
import info.hoang8f.android.segmented.SegmentedGroup;
10+
11+
/**
12+
* Created by zhangyipeng on 15/12/15.
13+
*/
14+
public class AndroidSegmented extends SegmentedGroup{
15+
16+
17+
public void setSegmentOrientation(String str){
18+
if(str.equals("horizontal")){
19+
setOrientation(RadioGroup.HORIZONTAL);
20+
}else if(str.equals("vertical")){
21+
setOrientation(RadioGroup.VERTICAL);
22+
}
23+
}
24+
25+
26+
public AndroidSegmented(ThemedReactContext context) {
27+
super(context);
28+
setGravity(Gravity.CENTER);
29+
30+
setLayoutParams(new ViewGroup.LayoutParams(
31+
ViewGroup.LayoutParams.WRAP_CONTENT,
32+
ViewGroup.LayoutParams.WRAP_CONTENT
33+
));
34+
}
35+
36+
private final Runnable mLayoutRunnable = new Runnable() {
37+
@Override
38+
public void run() {
39+
measure(MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.EXACTLY),
40+
MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.EXACTLY));
41+
layout(getLeft(), getTop(), getRight(), getBottom());
42+
}
43+
};
44+
45+
@Override
46+
public void requestLayout() {
47+
super.requestLayout();
48+
post(mLayoutRunnable);
49+
}
50+
51+
@Override
52+
protected void onLayout(boolean changed, int l, int t, int r, int b) {
53+
super.onLayout(changed, l, t, r, b);
54+
}
55+
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.higo.zhangyp.segmented;
2+
3+
import android.util.Log;
4+
5+
import com.facebook.react.bridge.Arguments;
6+
import com.facebook.react.bridge.WritableMap;
7+
import com.facebook.react.uimanager.events.Event;
8+
import com.facebook.react.uimanager.events.RCTEventEmitter;
9+
10+
/**
11+
* Created by zhangyipeng on 15/12/15.
12+
*/
13+
public class AndroidSegmentedEvent extends Event<AndroidSegmentedEvent> {
14+
15+
public static final String EVENT_NAME = "topChange";
16+
private final int selectedPosition;
17+
18+
public AndroidSegmentedEvent(int viewId, int selectedPosition) {
19+
super(viewId);
20+
this.selectedPosition = selectedPosition;
21+
}
22+
23+
24+
@Override
25+
public String getEventName() {
26+
return EVENT_NAME;
27+
}
28+
29+
@Override
30+
public void dispatch(RCTEventEmitter rctEventEmitter) {
31+
rctEventEmitter.receiveEvent(getViewTag(), getEventName(), serializeEventData());
32+
33+
}
34+
35+
36+
@Override
37+
public short getCoalescingKey() {
38+
return 0;
39+
}
40+
41+
private WritableMap serializeEventData() {
42+
WritableMap eventData = Arguments.createMap();
43+
eventData.putInt("selectedPosition", getPosition());
44+
45+
return eventData;
46+
}
47+
48+
private int getPosition() {
49+
return selectedPosition;
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.higo.zhangyp.segmented;
2+
3+
4+
import com.facebook.react.ReactPackage;
5+
import com.facebook.react.bridge.JavaScriptModule;
6+
import com.facebook.react.bridge.NativeModule;
7+
import com.facebook.react.bridge.ReactApplicationContext;
8+
import com.facebook.react.uimanager.ViewManager;
9+
10+
import java.util.Arrays;
11+
import java.util.Collections;
12+
import java.util.List;
13+
14+
15+
public class AndroidSegmentedPackage implements ReactPackage {
16+
@Override
17+
public List<NativeModule> createNativeModules(ReactApplicationContext reactApplicationContext) {
18+
return Collections.emptyList();
19+
}
20+
21+
@Override
22+
public List<ViewManager> createViewManagers(ReactApplicationContext reactApplicationContext) {
23+
return Arrays.<ViewManager>asList(
24+
new AndroidSegmentedManager());
25+
}
26+
27+
@Override
28+
public List<Class<? extends JavaScriptModule>> createJSModules() {
29+
return Collections.emptyList();
30+
}
31+
}
32+

src/main/java/com/higo/zhangyp/segmented/AndroidSegmentedEvent.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ public class AndroidSegmentedEvent extends Event<AndroidSegmentedEvent> {
1515
public static final String EVENT_NAME = "topChange";
1616
private final int selectedPosition;
1717

18-
public AndroidSegmentedEvent(int viewId, long timestampMs, int selectedPosition) {
19-
super(viewId, timestampMs);
18+
public AndroidSegmentedEvent(int viewId, int selectedPosition) {
19+
super(viewId);
2020
this.selectedPosition = selectedPosition;
2121
}
2222

src/main/java/com/higo/zhangyp/segmented/AndroidSegmentedManager.java

+3-12
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,14 @@ public void onCheckedChanged(RadioGroup group, int checkedId) {
4848
for (int i = 0; i < childCount; i++) {
4949
((RadioButton)view.getChildAt(i)).setChecked(false);
5050
if (view.getChildAt(i).getId() == checkedId) {
51-
((RadioButton)view.getChildAt(i)).setChecked(true);
51+
((RadioButton)view.getChildAt(i)).setChecked(true);
5252

53-
54-
reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher()
55-
.dispatchEvent(
56-
new AndroidSegmentedEvent(
57-
view.getId(),
58-
SystemClock.uptimeMillis(),
59-
i));
53+
reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher()
54+
.dispatchEvent(new AndroidSegmentedEvent(view.getId(), i));
6055
}
6156
}
6257
}
6358
});
64-
65-
6659
}
6760

6861

@@ -75,8 +68,6 @@ public void setChildText(AndroidSegmented view, ReadableArray data) {
7568

7669
child.setText(data.getString(i));
7770
view.addView(child);
78-
79-
8071
}
8172
}
8273

0 commit comments

Comments
 (0)