Skip to content

Fix 0.25 deprecations (and 0.26 errors). Fix the iOS warning. Fix logic bugs due properties being set in nondeterminstic orders. #8

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions AndroidSegmented.js → AndroidSegmented.android.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';

var React = require('react-native');
var { requireNativeComponent, PropTypes, View } = React;
var React = require('react');
var { PropTypes } = React;
var { requireNativeComponent, View } = require('react-native');

var NativeAndroidSegmented = requireNativeComponent('AndroidSegmented', AndroidSegmented);

Expand Down Expand Up @@ -51,4 +52,4 @@ AndroidSegmented.defaultProps = {

};

module.exports = AndroidSegmented;
module.exports = AndroidSegmented;
15 changes: 15 additions & 0 deletions AndroidSegmented.ios.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
*
* @providesModule AndroidSegmented
* @flow
*/
'use strict';

const AndroidSegmented = {
render() {
console.error('Not implemented for iOS, perhaps you wanted to use SegmentedControlIOS?');
}
};

module.exports = AndroidSegmented;

2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'info.hoang8f:android-segmented:1.0.6'
compile 'com.facebook.react:react-native:0.19.+'
compile 'com.facebook.react:react-native:+'
}
//react-native run-android
//adb reverse tcp:8081 tcp:8081
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "react-native-segmented-android",
"version": "1.0.4",
"description": "a high imitation of iOS segmented Controls",
"main": "AndroidSegmented.js",
"main": "AndroidSegmented",
"scripts": {
"test": "react-native start"
},
Expand All @@ -19,6 +19,7 @@
"author": "zzyyppqq",
"license": "ISC",
"peerDependencies": {
"react": ">= 0.14.0",
"react-native": ">= 0.18.0"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public class AndroidSegmentedEvent extends Event<AndroidSegmentedEvent> {
public static final String EVENT_NAME = "topChange";
private final int selectedPosition;

public AndroidSegmentedEvent(int viewId, long timestampMs, int selectedPosition) {
super(viewId, timestampMs);
public AndroidSegmentedEvent(int viewId, int selectedPosition) {
super(viewId);
this.selectedPosition = selectedPosition;
}

Expand All @@ -42,7 +42,6 @@ private WritableMap serializeEventData() {
WritableMap eventData = Arguments.createMap();
// eventData.putInt("target", getViewTag());
eventData.putInt("selected", getPosition());
Log.e("AAA","position="+getPosition());

return eventData;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class AndroidSegmentedManager extends SimpleViewManager<AndroidSegmented>

private Context context;

private int selectedPosition = -1;

@Override
public String getName() {
return REACT_CLASS;
Expand Down Expand Up @@ -55,7 +57,6 @@ public void onCheckedChanged(RadioGroup group, int checkedId) {
.dispatchEvent(
new AndroidSegmentedEvent(
view.getId(),
SystemClock.uptimeMillis(),
i));
}
}
Expand All @@ -65,11 +66,27 @@ public void onCheckedChanged(RadioGroup group, int checkedId) {

}

protected void onAfterUpdateTransaction(AndroidSegmented view) {
// We don't want to update selectedPosition when it's set,
// as the childText may not have been set yet. Resulting in array-out-of-bounds accesses.
// So we delay setChecked()-ing until here.
// Also re-sets any checked values after a childText change, too.
int childCount = view.getChildCount();
for (int i = 0; i < childCount; ++i) {
RadioButton radioBt = (RadioButton) (view.getChildAt(i));
radioBt.setChecked(i == selectedPosition);
}
// Ensure our tintColor and orientation gets applied to the existing view children,
// even if they were setup before the view children were constructed to receive them.
view.updateBackground();

}

@ReactProp(name = "childText")
public void setChildText(AndroidSegmented view, ReadableArray data) {
int childCount = data.size();

view.removeAllViews();
for (int i = 0; i < childCount; ++i) {
RadioButton child = (RadioButton) LayoutInflater.from(context).inflate(R.layout.radio_button, null);

Expand All @@ -83,8 +100,7 @@ public void setChildText(AndroidSegmented view, ReadableArray data) {

@ReactProp(name = "selectedPosition")
public void setSelectedChild(AndroidSegmented view, int position) {
RadioButton radioBt = (RadioButton) (view.getChildAt(position));
radioBt.setChecked(true);
selectedPosition = position;
}


Expand Down