Commit 5eec07e3 authored by xwenliang's avatar xwenliang

Add Picker.select feature

parent 9e28d766
...@@ -6,7 +6,6 @@ import android.graphics.Color; ...@@ -6,7 +6,6 @@ import android.graphics.Color;
import android.graphics.PixelFormat; import android.graphics.PixelFormat;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity; import android.view.Gravity;
import android.view.View; import android.view.View;
import android.view.Window; import android.view.Window;
...@@ -60,13 +59,17 @@ import static android.graphics.Color.argb; ...@@ -60,13 +59,17 @@ import static android.graphics.Color.argb;
* Edited by heng on 2016/12/23 * Edited by heng on 2016/12/23
* 1. Changed returnData type * 1. Changed returnData type
* 2. Added pickerToolBarFontSize * 2. Added pickerToolBarFontSize
* * <p>
* Edited by heng on 2016/12/26 * Edited by heng on 2016/12/26
* 1. Fixed returnData bug * 1. Fixed returnData bug
* 2. Added pickerFontColor * 2. Added pickerFontColor
* 3. Added pickerFontSize * 3. Added pickerFontSize
* 4. Used LifecycleEventListener replace Application.ActivityLifecycleCallbacks * 4. Used LifecycleEventListener replace Application.ActivityLifecycleCallbacks
* 5. Fixed other bug * 5. Fixed other bug
*
* Edited by heng on 2017/01/17
* 1. Added select(ReadableArray array, Callback callback)
* 2. Optimization code
*/ */
public class PickerViewModule extends ReactContextBaseJavaModule implements LifecycleEventListener { public class PickerViewModule extends ReactContextBaseJavaModule implements LifecycleEventListener {
...@@ -103,14 +106,10 @@ public class PickerViewModule extends ReactContextBaseJavaModule implements Life ...@@ -103,14 +106,10 @@ public class PickerViewModule extends ReactContextBaseJavaModule implements Life
private static final String EVENT_KEY_CANCEL = "cancel"; private static final String EVENT_KEY_CANCEL = "cancel";
private static final String EVENT_KEY_SELECTED = "select"; private static final String EVENT_KEY_SELECTED = "select";
private static final String ERROR_NOT_INIT = "please initialize"; private static final String ERROR_NOT_INIT = "please initialize the component first";
private Dialog dialog = null; private Dialog dialog = null;
private PickerViewLinkage pickerViewLinkage;
private PickerViewAlone pickerViewAlone;
private boolean isAlone = true;
private boolean isLoop = true; private boolean isLoop = true;
private String confirmText; private String confirmText;
...@@ -123,6 +122,9 @@ public class PickerViewModule extends ReactContextBaseJavaModule implements Life ...@@ -123,6 +122,9 @@ public class PickerViewModule extends ReactContextBaseJavaModule implements Life
private int curStatus; private int curStatus;
private PickerViewLinkage pickerViewLinkage;
private PickerViewAlone pickerViewAlone;
public PickerViewModule(ReactApplicationContext reactContext) { public PickerViewModule(ReactApplicationContext reactContext) {
super(reactContext); super(reactContext);
reactContext.addLifecycleEventListener(this); reactContext.addLifecycleEventListener(this);
...@@ -266,31 +268,6 @@ public class PickerViewModule extends ReactContextBaseJavaModule implements Life ...@@ -266,31 +268,6 @@ public class PickerViewModule extends ReactContextBaseJavaModule implements Life
} }
} }
String[] selectValue = {};
if (options.hasKey(SELECTED_VALUE)) {
ReadableArray array = options.getArray(SELECTED_VALUE);
selectValue = new String[array.size()];
String value = "";
for (int i = 0; i < array.size(); i++) {
switch (array.getType(i).name()) {
case "Boolean":
value = String.valueOf(array.getBoolean(i));
break;
case "Number":
try {
value = String.valueOf(array.getInt(i));
} catch (Exception e) {
value = String.valueOf(array.getDouble(i));
}
break;
case "String":
value = array.getString(i);
break;
}
selectValue[i] = value;
}
}
int pickerTextColor = 0xff000000; int pickerTextColor = 0xff000000;
if (options.hasKey(PICKER_TEXT_COLOR)) { if (options.hasKey(PICKER_TEXT_COLOR)) {
ReadableArray array = options.getArray(PICKER_TEXT_COLOR); ReadableArray array = options.getArray(PICKER_TEXT_COLOR);
...@@ -313,7 +290,6 @@ public class PickerViewModule extends ReactContextBaseJavaModule implements Life ...@@ -313,7 +290,6 @@ public class PickerViewModule extends ReactContextBaseJavaModule implements Life
String name = pickerData.getType(0).name(); String name = pickerData.getType(0).name();
switch (name) { switch (name) {
case "Map": case "Map":
isAlone = false;
curStatus = 1; curStatus = 1;
pickerViewLinkage.setVisibility(View.VISIBLE); pickerViewLinkage.setVisibility(View.VISIBLE);
pickerViewAlone.setVisibility(View.GONE); pickerViewAlone.setVisibility(View.GONE);
...@@ -330,7 +306,6 @@ public class PickerViewModule extends ReactContextBaseJavaModule implements Life ...@@ -330,7 +306,6 @@ public class PickerViewModule extends ReactContextBaseJavaModule implements Life
commonEvent(EVENT_KEY_SELECTED); commonEvent(EVENT_KEY_SELECTED);
} }
}); });
pickerViewLinkage.setSelectValue(selectValue);
pickerViewHeight = pickerViewLinkage.getViewHeight(); pickerViewHeight = pickerViewLinkage.getViewHeight();
break; break;
default: default:
...@@ -351,18 +326,22 @@ public class PickerViewModule extends ReactContextBaseJavaModule implements Life ...@@ -351,18 +326,22 @@ public class PickerViewModule extends ReactContextBaseJavaModule implements Life
} }
}); });
pickerViewAlone.setSelectValue(selectValue);
pickerViewHeight = pickerViewAlone.getViewHeight(); pickerViewHeight = pickerViewAlone.getViewHeight();
break; break;
} }
if (options.hasKey(SELECTED_VALUE)) {
ReadableArray array = options.getArray(SELECTED_VALUE);
String[] selectedValue = getSelectedValue(array);
select(selectedValue);
}
if (options.hasKey(PICKER_BG_COLOR)) { if (options.hasKey(PICKER_BG_COLOR)) {
ReadableArray array = options.getArray(PICKER_BG_COLOR); ReadableArray array = options.getArray(PICKER_BG_COLOR);
int[] colors = getColor(array); int[] colors = getColor(array);
pickerLayout.setBackgroundColor(argb(colors[3], colors[0], colors[1], colors[2])); pickerLayout.setBackgroundColor(argb(colors[3], colors[0], colors[1], colors[2]));
} }
Log.d("PickerView", "pickerViewHeight = " + pickerViewHeight);
int height = barViewHeight + pickerViewHeight; int height = barViewHeight + pickerViewHeight;
if (dialog == null) { if (dialog == null) {
dialog = new Dialog(activity, R.style.Dialog_Full_Screen); dialog = new Dialog(activity, R.style.Dialog_Full_Screen);
...@@ -385,6 +364,18 @@ public class PickerViewModule extends ReactContextBaseJavaModule implements Life ...@@ -385,6 +364,18 @@ public class PickerViewModule extends ReactContextBaseJavaModule implements Life
} }
} }
@ReactMethod
public void select(ReadableArray array, Callback callback) {
if (dialog == null) {
if (callback != null) {
callback.invoke(ERROR_NOT_INIT);
}
return;
}
String[] selectedValue = getSelectedValue(array);
select(selectedValue);
}
@ReactMethod @ReactMethod
public void show() { public void show() {
if (dialog == null) { if (dialog == null) {
...@@ -405,37 +396,6 @@ public class PickerViewModule extends ReactContextBaseJavaModule implements Life ...@@ -405,37 +396,6 @@ public class PickerViewModule extends ReactContextBaseJavaModule implements Life
} }
} }
@ReactMethod
public void select(ReadableArray array) {
if (dialog != null) {
String[] selectValue = new String[array.size()];
String value = "";
for (int i = 0; i < array.size(); i++) {
switch (array.getType(i).name()) {
case "Boolean":
value = String.valueOf(array.getBoolean(i));
break;
case "Number":
try {
value = String.valueOf(array.getInt(i));
} catch (Exception e) {
value = String.valueOf(array.getDouble(i));
}
break;
case "String":
value = array.getString(i);
break;
}
selectValue[i] = value;
}
if(isAlone){
pickerViewAlone.setSelectValue(selectValue);
}else{
pickerViewLinkage.setSelectValue(selectValue);
}
}
}
@ReactMethod @ReactMethod
public void isPickerShow(Callback callback) { public void isPickerShow(Callback callback) {
if (callback == null) if (callback == null)
...@@ -466,6 +426,41 @@ public class PickerViewModule extends ReactContextBaseJavaModule implements Life ...@@ -466,6 +426,41 @@ public class PickerViewModule extends ReactContextBaseJavaModule implements Life
return colors; return colors;
} }
private String[] getSelectedValue(ReadableArray array) {
String[] selectValue = new String[array.size()];
String value = "";
for (int i = 0; i < array.size(); i++) {
switch (array.getType(i).name()) {
case "Boolean":
value = String.valueOf(array.getBoolean(i));
break;
case "Number":
try {
value = String.valueOf(array.getInt(i));
} catch (Exception e) {
value = String.valueOf(array.getDouble(i));
}
break;
case "String":
value = array.getString(i);
break;
}
selectValue[i] = value;
}
return selectValue;
}
private void select(String[] selectedValue) {
switch (curStatus) {
case 0:
pickerViewAlone.setSelectValue(selectedValue);
break;
case 1:
pickerViewLinkage.setSelectValue(selectedValue);
break;
}
}
private void commonEvent(String eventKey) { private void commonEvent(String eventKey) {
WritableMap map = Arguments.createMap(); WritableMap map = Arguments.createMap();
map.putString("type", eventKey); map.putString("type", eventKey);
...@@ -501,7 +496,6 @@ public class PickerViewModule extends ReactContextBaseJavaModule implements Life ...@@ -501,7 +496,6 @@ public class PickerViewModule extends ReactContextBaseJavaModule implements Life
@Override @Override
public void onHostDestroy() { public void onHostDestroy() {
hide();
dialog = null;
} }
} }
\ No newline at end of file
...@@ -106,6 +106,7 @@ class PickerTest extends Component { ...@@ -106,6 +106,7 @@ class PickerTest extends Component {
console.log('area', pickedValue); console.log('area', pickedValue);
}, },
onPickerSelect: pickedValue => { onPickerSelect: pickedValue => {
//Picker.select(['山东', '青岛', '黄岛区'])
console.log('area', pickedValue); console.log('area', pickedValue);
} }
}); });
......
...@@ -55,8 +55,10 @@ export default { ...@@ -55,8 +55,10 @@ export default {
Picker.hide(); Picker.hide();
}, },
select(data) { select(arr, fn) {
Picker.select(data) Picker.select(arr, err => {
typeof fn === 'function' && fn(err);
});
}, },
toggle(){ toggle(){
......
{ {
"name": "react-native-picker", "name": "react-native-picker",
"version": "4.0.16", "version": "4.0.17",
"description": "", "description": "",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment