PickerViewModule.java 19.5 KB
Newer Older
xwenliang's avatar
xwenliang committed
1 2 3
package com.beefe.picker;

import android.app.Activity;
4
import android.app.Dialog;
xwenliang's avatar
xwenliang committed
5
import android.graphics.Color;
6
import android.graphics.PixelFormat;
xwenliang's avatar
xwenliang committed
7 8 9 10
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
11
import android.view.Window;
xwenliang's avatar
xwenliang committed
12 13 14
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;
Karthikeyan Ganesan's avatar
Karthikeyan Ganesan committed
15
import android.os.Build;
xwenliang's avatar
xwenliang committed
16

travelgeezer's avatar
travelgeezer committed
17
import com.beefe.picker.util.MIUIUtils;
xwenliang's avatar
xwenliang committed
18 19 20
import com.beefe.picker.view.OnSelectedListener;
import com.beefe.picker.view.PickerViewAlone;
import com.beefe.picker.view.PickerViewLinkage;
xwenliang's avatar
xwenliang committed
21
import com.beefe.picker.view.ReturnData;
xwenliang's avatar
xwenliang committed
22 23
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
xwenliang's avatar
xwenliang committed
24
import com.facebook.react.bridge.LifecycleEventListener;
xwenliang's avatar
xwenliang committed
25 26 27 28 29 30 31 32 33 34 35 36
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;

import java.util.ArrayList;

xwenliang's avatar
xwenliang committed
37 38
import static android.graphics.Color.argb;

xwenliang's avatar
xwenliang committed
39
/**
xwenliang's avatar
xwenliang committed
40
 * Author: <a href="https://github.com/shexiaoheng">heng</a>
41
 * <p>
xwenliang's avatar
xwenliang committed
42
 * Created by heng on 16/9/5.
43
 * <p>
xwenliang's avatar
xwenliang committed
44
 * Edited by heng on 16/9/22.
45 46
 * 1. PopupWindow height : full screen -> assignation
 * 2. Added pickerToolBarHeight support
47
 * <p>
48 49 50
 * Edited by heng on 2016/10/19.
 * 1. Added weights support
 * 2. Fixed return data bug
51 52 53 54 55 56 57 58 59
 * <p>
 * Edited by heng on 2016/11/16.
 * 1. Used WindowManager replace PopupWindow
 * 2. Removed method initOK() toggle() show() isPickerShow()
 * 3. Implements Application.ActivityLifecycleCallbacks
 * <p>
 * Edited by heng on 2016/11/17
 * 1. Used Dialog replace WindowManger
 * 2. Restore method show() isPickerShow()
xwenliang's avatar
xwenliang committed
60 61 62 63
 * <p>
 * Edited by heng on 2016/12/23
 * 1. Changed returnData type
 * 2. Added pickerToolBarFontSize
xwenliang's avatar
xwenliang committed
64
 * <p>
xwenliang's avatar
xwenliang committed
65 66 67 68 69 70
 * Edited by heng on 2016/12/26
 * 1. Fixed returnData bug
 * 2. Added pickerFontColor
 * 3. Added pickerFontSize
 * 4. Used LifecycleEventListener replace Application.ActivityLifecycleCallbacks
 * 5. Fixed other bug
xwenliang's avatar
xwenliang committed
71 72 73 74
 *
 * Edited by heng on 2017/01/17
 * 1. Added select(ReadableArray array, Callback callback)
 * 2. Optimization code
xwenliang's avatar
xwenliang committed
75 76
 */

xwenliang's avatar
xwenliang committed
77
public class PickerViewModule extends ReactContextBaseJavaModule implements LifecycleEventListener {
xwenliang's avatar
xwenliang committed
78 79 80 81 82

    private static final String REACT_CLASS = "BEEPickerManager";

    private static final String PICKER_DATA = "pickerData";
    private static final String SELECTED_VALUE = "selectedValue";
83

xwenliang's avatar
xwenliang committed
84
    private static final String IS_LOOP = "isLoop";
85 86 87

    private static final String WEIGHTS = "wheelFlex";

xwenliang's avatar
xwenliang committed
88
    private static final String PICKER_BG_COLOR = "pickerBg";
89

xwenliang's avatar
xwenliang committed
90 91 92
    private static final String PICKER_TOOL_BAR_BG = "pickerToolBarBg";
    private static final String PICKER_TOOL_BAR_HEIGHT = "pickerToolBarHeight";
    private static final String PICKER_TOOL_BAR_TEXT_SIZE = "pickerToolBarFontSize";
93

xwenliang's avatar
xwenliang committed
94 95
    private static final String PICKER_CONFIRM_BTN_TEXT = "pickerConfirmBtnText";
    private static final String PICKER_CONFIRM_BTN_COLOR = "pickerConfirmBtnColor";
96

xwenliang's avatar
xwenliang committed
97 98
    private static final String PICKER_CANCEL_BTN_TEXT = "pickerCancelBtnText";
    private static final String PICKER_CANCEL_BTN_COLOR = "pickerCancelBtnColor";
99

xwenliang's avatar
xwenliang committed
100 101 102 103 104
    private static final String PICKER_TITLE_TEXT = "pickerTitleText";
    private static final String PICKER_TITLE_TEXT_COLOR = "pickerTitleColor";

    private static final String PICKER_TEXT_COLOR = "pickerFontColor";
    private static final String PICKER_TEXT_SIZE = "pickerFontSize";
105
    private static final String PICKER_TEXT_ELLIPSIS_LEN = "pickerTextEllipsisLen";
xwenliang's avatar
xwenliang committed
106 107 108 109 110 111

    private static final String PICKER_EVENT_NAME = "pickerEvent";
    private static final String EVENT_KEY_CONFIRM = "confirm";
    private static final String EVENT_KEY_CANCEL = "cancel";
    private static final String EVENT_KEY_SELECTED = "select";

xwenliang's avatar
xwenliang committed
112
    private static final String ERROR_NOT_INIT = "please initialize the component first";
xwenliang's avatar
xwenliang committed
113

114
    private Dialog dialog = null;
xwenliang's avatar
xwenliang committed
115 116 117 118 119 120

    private boolean isLoop = true;

    private String confirmText;
    private String cancelText;
    private String titleText;
121
    private int pickerTextEllipsisLen;
xwenliang's avatar
xwenliang committed
122

123 124
    private double[] weights;

xwenliang's avatar
xwenliang committed
125
    private ArrayList<ReturnData> returnData;
xwenliang's avatar
xwenliang committed
126

127
    private int curStatus;
xwenliang's avatar
xwenliang committed
128

xwenliang's avatar
xwenliang committed
129 130 131
    private PickerViewLinkage pickerViewLinkage;
    private PickerViewAlone pickerViewAlone;

xwenliang's avatar
xwenliang committed
132 133
    public PickerViewModule(ReactApplicationContext reactContext) {
        super(reactContext);
xwenliang's avatar
xwenliang committed
134
        reactContext.addLifecycleEventListener(this);
xwenliang's avatar
xwenliang committed
135 136 137 138 139 140 141 142 143 144 145
    }

    @Override
    public String getName() {
        return REACT_CLASS;
    }

    @ReactMethod
    public void _init(ReadableMap options) {
        Activity activity = getCurrentActivity();
        if (activity != null && options.hasKey(PICKER_DATA)) {
146
            View view = activity.getLayoutInflater().inflate(R.layout.picker_view, null);
147 148 149 150
            RelativeLayout barLayout = (RelativeLayout) view.findViewById(R.id.barLayout);
            TextView cancelTV = (TextView) view.findViewById(R.id.cancel);
            TextView titleTV = (TextView) view.findViewById(R.id.title);
            TextView confirmTV = (TextView) view.findViewById(R.id.confirm);
151
            RelativeLayout pickerLayout = (RelativeLayout) view.findViewById(R.id.pickerLayout);
jinlongtao's avatar
jinlongtao committed
152 153
            pickerViewLinkage = (PickerViewLinkage) view.findViewById(R.id.pickerViewLinkage);
            pickerViewAlone = (PickerViewAlone) view.findViewById(R.id.pickerViewAlone);
154 155

            int barViewHeight;
xwenliang's avatar
xwenliang committed
156
            if (options.hasKey(PICKER_TOOL_BAR_HEIGHT)) {
xwenliang's avatar
xwenliang committed
157
                try {
xwenliang's avatar
xwenliang committed
158
                    barViewHeight = options.getInt(PICKER_TOOL_BAR_HEIGHT);
xwenliang's avatar
xwenliang committed
159
                } catch (Exception e) {
xwenliang's avatar
xwenliang committed
160
                    barViewHeight = (int) options.getDouble(PICKER_TOOL_BAR_HEIGHT);
xwenliang's avatar
xwenliang committed
161
                }
xwenliang's avatar
xwenliang committed
162 163 164 165 166 167 168
            } else {
                barViewHeight = (int) (activity.getResources().getDisplayMetrics().density * 40);
            }
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.MATCH_PARENT,
                    barViewHeight);
            barLayout.setLayoutParams(params);
xwenliang's avatar
xwenliang committed
169

xwenliang's avatar
xwenliang committed
170 171
            if (options.hasKey(PICKER_TOOL_BAR_BG)) {
                ReadableArray array = options.getArray(PICKER_TOOL_BAR_BG);
172
                int[] colors = getColor(array);
xwenliang's avatar
xwenliang committed
173
                barLayout.setBackgroundColor(argb(colors[3], colors[0], colors[1], colors[2]));
xwenliang's avatar
xwenliang committed
174 175
            }

xwenliang's avatar
xwenliang committed
176 177 178 179 180 181
            if (options.hasKey(PICKER_TOOL_BAR_TEXT_SIZE)) {
                int toolBarTextSize = options.getInt(PICKER_TOOL_BAR_TEXT_SIZE);
                cancelTV.setTextSize(toolBarTextSize);
                titleTV.setTextSize(toolBarTextSize);
                confirmTV.setTextSize(toolBarTextSize);
            }
xwenliang's avatar
xwenliang committed
182

xwenliang's avatar
xwenliang committed
183 184
            if (options.hasKey(PICKER_CONFIRM_BTN_TEXT)) {
                confirmText = options.getString(PICKER_CONFIRM_BTN_TEXT);
xwenliang's avatar
xwenliang committed
185 186 187
            }
            confirmTV.setText(!TextUtils.isEmpty(confirmText) ? confirmText : "");

xwenliang's avatar
xwenliang committed
188 189
            if (options.hasKey(PICKER_CONFIRM_BTN_COLOR)) {
                ReadableArray array = options.getArray(PICKER_CONFIRM_BTN_COLOR);
190
                int[] colors = getColor(array);
xwenliang's avatar
xwenliang committed
191
                confirmTV.setTextColor(argb(colors[3], colors[0], colors[1], colors[2]));
xwenliang's avatar
xwenliang committed
192 193 194 195
            }
            confirmTV.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
196
                    switch (curStatus) {
197 198 199 200 201 202 203
                        case 0:
                            returnData = pickerViewAlone.getSelectedData();
                            break;
                        case 1:
                            returnData = pickerViewLinkage.getSelectedData();
                            break;
                    }
xwenliang's avatar
xwenliang committed
204 205 206 207 208 209
                    commonEvent(EVENT_KEY_CONFIRM);
                    hide();
                }
            });


xwenliang's avatar
xwenliang committed
210 211
            if (options.hasKey(PICKER_TITLE_TEXT)) {
                titleText = options.getString(PICKER_TITLE_TEXT);
xwenliang's avatar
xwenliang committed
212 213
            }
            titleTV.setText(!TextUtils.isEmpty(titleText) ? titleText : "");
xwenliang's avatar
xwenliang committed
214 215
            if (options.hasKey(PICKER_TITLE_TEXT_COLOR)) {
                ReadableArray array = options.getArray(PICKER_TITLE_TEXT_COLOR);
216
                int[] colors = getColor(array);
xwenliang's avatar
xwenliang committed
217
                titleTV.setTextColor(argb(colors[3], colors[0], colors[1], colors[2]));
xwenliang's avatar
xwenliang committed
218 219
            }

xwenliang's avatar
xwenliang committed
220 221
            if (options.hasKey(PICKER_CANCEL_BTN_TEXT)) {
                cancelText = options.getString(PICKER_CANCEL_BTN_TEXT);
xwenliang's avatar
xwenliang committed
222 223
            }
            cancelTV.setText(!TextUtils.isEmpty(cancelText) ? cancelText : "");
xwenliang's avatar
xwenliang committed
224 225
            if (options.hasKey(PICKER_CANCEL_BTN_COLOR)) {
                ReadableArray array = options.getArray(PICKER_CANCEL_BTN_COLOR);
226
                int[] colors = getColor(array);
xwenliang's avatar
xwenliang committed
227
                cancelTV.setTextColor(argb(colors[3], colors[0], colors[1], colors[2]));
xwenliang's avatar
xwenliang committed
228 229 230 231
            }
            cancelTV.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
232
                    switch (curStatus) {
233 234 235 236 237 238 239
                        case 0:
                            returnData = pickerViewAlone.getSelectedData();
                            break;
                        case 1:
                            returnData = pickerViewLinkage.getSelectedData();
                            break;
                    }
xwenliang's avatar
xwenliang committed
240 241 242 243 244
                    commonEvent(EVENT_KEY_CANCEL);
                    hide();
                }
            });

245 246 247 248
            if(options.hasKey(PICKER_TEXT_ELLIPSIS_LEN)){
                pickerTextEllipsisLen = options.getInt(PICKER_TEXT_ELLIPSIS_LEN);
            }

xwenliang's avatar
xwenliang committed
249 250 251 252
            if (options.hasKey(IS_LOOP)) {
                isLoop = options.getBoolean(IS_LOOP);
            }

253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
            if (options.hasKey(WEIGHTS)) {
                ReadableArray array = options.getArray(WEIGHTS);
                weights = new double[array.size()];
                for (int i = 0; i < array.size(); i++) {
                    switch (array.getType(i).name()) {
                        case "Number":
                            try {
                                weights[i] = array.getInt(i);
                            } catch (Exception e) {
                                weights[i] = array.getDouble(i);
                            }
                            break;
                        case "String":
                            try {
                                weights[i] = Double.parseDouble(array.getString(i));
                            } catch (Exception e) {
                                weights[i] = 1.0;
                            }
                            break;
                        default:
                            weights[i] = 1.0;
                            break;
                    }
                }
            }

xwenliang's avatar
xwenliang committed
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
            int pickerTextColor = 0xff000000;
            if (options.hasKey(PICKER_TEXT_COLOR)) {
                ReadableArray array = options.getArray(PICKER_TEXT_COLOR);
                int[] colors = getColor(array);
                pickerTextColor = Color.argb(colors[3], colors[0], colors[1], colors[2]);
            }

            int pickerTextSize = 16;
            if (options.hasKey(PICKER_TEXT_SIZE)) {
                try {
                    pickerTextSize = options.getInt(PICKER_TEXT_SIZE);
                } catch (Exception e) {
                    pickerTextSize = (int) options.getDouble(PICKER_TEXT_SIZE);
                }
            }

xwenliang's avatar
xwenliang committed
295 296
            ReadableArray pickerData = options.getArray(PICKER_DATA);

297
            int pickerViewHeight;
xwenliang's avatar
xwenliang committed
298 299
            String name = pickerData.getType(0).name();
            switch (name) {
xwenliang's avatar
xwenliang committed
300
                case "Map":
301
                    curStatus = 1;
xwenliang's avatar
xwenliang committed
302 303
                    pickerViewLinkage.setVisibility(View.VISIBLE);
                    pickerViewAlone.setVisibility(View.GONE);
304

305
                    pickerViewLinkage.setPickerData(pickerData, weights);
xwenliang's avatar
xwenliang committed
306 307
                    pickerViewLinkage.setTextColor(pickerTextColor);
                    pickerViewLinkage.setTextSize(pickerTextSize);
308
                    pickerViewLinkage.setTextEllipsisLen(pickerTextEllipsisLen);
xwenliang's avatar
xwenliang committed
309
                    pickerViewLinkage.setIsLoop(isLoop);
310

xwenliang's avatar
xwenliang committed
311 312
                    pickerViewLinkage.setOnSelectListener(new OnSelectedListener() {
                        @Override
xwenliang's avatar
xwenliang committed
313
                        public void onSelected(ArrayList<ReturnData> selectedList) {
314
                            returnData = selectedList;
xwenliang's avatar
xwenliang committed
315 316 317
                            commonEvent(EVENT_KEY_SELECTED);
                        }
                    });
xwenliang's avatar
xwenliang committed
318
                    pickerViewHeight = pickerViewLinkage.getViewHeight();
xwenliang's avatar
xwenliang committed
319
                    break;
xwenliang's avatar
xwenliang committed
320
                default:
321
                    curStatus = 0;
xwenliang's avatar
xwenliang committed
322 323
                    pickerViewAlone.setVisibility(View.VISIBLE);
                    pickerViewLinkage.setVisibility(View.GONE);
xwenliang's avatar
xwenliang committed
324

325
                    pickerViewAlone.setPickerData(pickerData, weights);
xwenliang's avatar
xwenliang committed
326 327
                    pickerViewAlone.setTextColor(pickerTextColor);
                    pickerViewAlone.setTextSize(pickerTextSize);
328
                    pickerViewAlone.setTextEllipsisLen(pickerTextEllipsisLen);
xwenliang's avatar
xwenliang committed
329 330 331 332
                    pickerViewAlone.setIsLoop(isLoop);

                    pickerViewAlone.setOnSelectedListener(new OnSelectedListener() {
                        @Override
xwenliang's avatar
xwenliang committed
333
                        public void onSelected(ArrayList<ReturnData> selectedList) {
334
                            returnData = selectedList;
xwenliang's avatar
xwenliang committed
335 336 337 338
                            commonEvent(EVENT_KEY_SELECTED);
                        }
                    });

xwenliang's avatar
xwenliang committed
339
                    pickerViewHeight = pickerViewAlone.getViewHeight();
xwenliang's avatar
xwenliang committed
340 341 342
                    break;
            }

xwenliang's avatar
xwenliang committed
343 344 345 346 347 348
            if (options.hasKey(SELECTED_VALUE)) {
                ReadableArray array = options.getArray(SELECTED_VALUE);
                String[] selectedValue = getSelectedValue(array);
                select(selectedValue);
            }

349 350 351
            if (options.hasKey(PICKER_BG_COLOR)) {
                ReadableArray array = options.getArray(PICKER_BG_COLOR);
                int[] colors = getColor(array);
xwenliang's avatar
xwenliang committed
352
                pickerLayout.setBackgroundColor(argb(colors[3], colors[0], colors[1], colors[2]));
353 354 355 356 357
            }

            int height = barViewHeight + pickerViewHeight;
            if (dialog == null) {
                dialog = new Dialog(activity, R.style.Dialog_Full_Screen);
358 359 360 361
                dialog.setContentView(view);
                WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
                Window window = dialog.getWindow();
                if (window != null) {
Karthikeyan Ganesan's avatar
Karthikeyan Ganesan committed
362 363 364 365 366 367 368 369 370
                    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                        layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
                        window.setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
                    }else{
                        if (MIUIUtils.isMIUI()) {
                            layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION;
                        }else {
                            //layoutParams.type = WindowManager.LayoutParams.TYPE_TOAST;
                        }
371 372 373 374 375 376 377
                    }
                    layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
                    layoutParams.format = PixelFormat.TRANSPARENT;
                    layoutParams.windowAnimations = R.style.PickerAnim;
                    layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
                    layoutParams.height = height;
                    layoutParams.gravity = Gravity.BOTTOM;
Karthikeyan Ganesan's avatar
Karthikeyan Ganesan committed
378
                    window.setAttributes(layoutParams);   
379
                }
380
            } else {
381
                dialog.dismiss();
382
                dialog.setContentView(view);
xwenliang's avatar
xwenliang committed
383 384 385 386
            }
        }
    }

xwenliang's avatar
xwenliang committed
387 388 389 390 391 392 393 394 395 396 397 398
    @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);
    }

xwenliang's avatar
xwenliang committed
399
    @ReactMethod
400 401
    public void show() {
        if (dialog == null) {
xwenliang's avatar
xwenliang committed
402 403
            return;
        }
404 405
        if (!dialog.isShowing()) {
            dialog.show();
xwenliang's avatar
xwenliang committed
406 407 408 409 410
        }
    }

    @ReactMethod
    public void hide() {
411 412 413 414 415
        if (dialog == null) {
            return;
        }
        if (dialog.isShowing()) {
            dialog.dismiss();
xwenliang's avatar
xwenliang committed
416 417 418 419 420
        }
    }

    @ReactMethod
    public void isPickerShow(Callback callback) {
xwenliang's avatar
xwenliang committed
421 422
        if (callback == null)
            return;
423
        if (dialog == null) {
xwenliang's avatar
xwenliang committed
424 425
            callback.invoke(ERROR_NOT_INIT);
        } else {
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
            callback.invoke(null, dialog.isShowing());
        }
    }

    private int[] getColor(ReadableArray array) {
        int[] colors = new int[4];
        for (int i = 0; i < array.size(); i++) {
            switch (i) {
                case 0:
                case 1:
                case 2:
                    colors[i] = array.getInt(i);
                    break;
                case 3:
                    colors[i] = (int) (array.getDouble(i) * 255);
                    break;
                default:
                    break;
            }
xwenliang's avatar
xwenliang committed
445
        }
446
        return colors;
xwenliang's avatar
xwenliang committed
447 448
    }

xwenliang's avatar
xwenliang committed
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
    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;
        }
    }

xwenliang's avatar
xwenliang committed
484 485
    private void commonEvent(String eventKey) {
        WritableMap map = Arguments.createMap();
xwenliang's avatar
xwenliang committed
486 487 488 489 490 491
        map.putString("type", eventKey);
        WritableArray indexes = Arguments.createArray();
        WritableArray values = Arguments.createArray();
        for (ReturnData data : returnData) {
            indexes.pushInt(data.getIndex());
            values.pushString(data.getItem());
xwenliang's avatar
xwenliang committed
492
        }
xwenliang's avatar
xwenliang committed
493 494
        map.putArray("selectedValue", values);
        map.putArray("selectedIndex", indexes);
xwenliang's avatar
xwenliang committed
495 496 497 498 499 500 501 502 503 504
        sendEvent(getReactApplicationContext(), PICKER_EVENT_NAME, map);
    }

    private void sendEvent(ReactContext reactContext,
                           String eventName,
                           @Nullable WritableMap params) {
        reactContext
                .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                .emit(eventName, params);
    }
505 506

    @Override
xwenliang's avatar
xwenliang committed
507
    public void onHostResume() {
508 509 510 511

    }

    @Override
xwenliang's avatar
xwenliang committed
512
    public void onHostPause() {
513
        hide();
xwenliang's avatar
xwenliang committed
514
        dialog = null;
515 516 517
    }

    @Override
xwenliang's avatar
xwenliang committed
518
    public void onHostDestroy() {
519 520
        hide();
        dialog = null;
521
    }
xwenliang's avatar
xwenliang committed
522
}