PickerViewModule.java 17.2 KB
Newer Older
xwenliang's avatar
xwenliang committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
package com.beefe.picker;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.beefe.picker.view.OnSelectedListener;
import com.beefe.picker.view.PickerViewAlone;
import com.beefe.picker.view.PickerViewLinkage;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
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;

32 33
import static android.R.attr.textSize;

xwenliang's avatar
xwenliang committed
34
/**
xwenliang's avatar
xwenliang committed
35
 * Author: heng <a href="https://github.com/shexiaoheng"/>
36
 * <p>
xwenliang's avatar
xwenliang committed
37
 * Created by heng on 16/9/5.
38
 * <p>
xwenliang's avatar
xwenliang committed
39
 * Edited by heng on 16/9/22.
40 41 42 43 44 45
 * 1. PopupWindow height : full screen -> assignation
 * 2. Added pickerToolBarHeight support
 *
 * Edited by heng on 2016/10/19.
 * 1. Added weights support
 * 2. Fixed return data bug
xwenliang's avatar
xwenliang committed
46 47 48 49 50 51 52 53
 */

public class PickerViewModule extends ReactContextBaseJavaModule {

    private static final String REACT_CLASS = "BEEPickerManager";

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

xwenliang's avatar
xwenliang committed
55
    private static final String IS_LOOP = "isLoop";
56 57 58

    private static final String WEIGHTS = "wheelFlex";

xwenliang's avatar
xwenliang committed
59
    private static final String PICKER_BG_COLOR = "pickerBg";
60

xwenliang's avatar
xwenliang committed
61
    private static final String TEXT_BAR_COLOR = "pickerToolBarBg";
xwenliang's avatar
xwenliang committed
62
    private static final String TEXT_BAR_HEIGHT = "pickerToolBarHeight";
63

xwenliang's avatar
xwenliang committed
64 65
    private static final String CONFIRM_TEXT = "pickerConfirmBtnText";
    private static final String CONFIRM_TEXT_COLOR = "pickerConfirmBtnColor";
66

xwenliang's avatar
xwenliang committed
67 68
    private static final String CANCEL_TEXT = "pickerCancelBtnText";
    private static final String CANCEL_TEXT_COLOR = "pickerCancelBtnColor";
69

xwenliang's avatar
xwenliang committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    private static final String TITLE_TEXT = "pickerTitleText";
    private static final String TITLE_TEXT_COLOR = "pickerTitleColor";

    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";

    private static final String ERROR_NOT_INIT = "please initialize";

    private View view;
    private PopupWindow popupWindow = null;

    private boolean isLoop = true;

    private String confirmText;
    private String cancelText;
    private String titleText;

89 90
    private double[] weights;

xwenliang's avatar
xwenliang committed
91 92 93 94 95 96
    private int[] pickerColor = new int[4];
    private int[] barBgColor = new int[4];
    private int[] confirmTextColor = new int[4];
    private int[] cancelTextColor = new int[4];
    private int[] titleTextColor = new int[4];

97
    private ArrayList<String> returnData;
xwenliang's avatar
xwenliang committed
98

99
    private int curStatus;
xwenliang's avatar
xwenliang committed
100

xwenliang's avatar
xwenliang committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114
    public PickerViewModule(ReactApplicationContext reactContext) {
        super(reactContext);
    }

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

    @ReactMethod
    public void _init(ReadableMap options) {
        Activity activity = getCurrentActivity();
        if (activity != null && options.hasKey(PICKER_DATA)) {
            view = activity.getLayoutInflater().inflate(R.layout.popup_picker_view, null);
115 116 117 118 119 120 121 122
            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);
            final PickerViewLinkage pickerViewLinkage = (PickerViewLinkage) view.findViewById(R.id.pickerViewLinkage);
            final PickerViewAlone pickerViewAlone = (PickerViewAlone) view.findViewById(R.id.pickerViewAlone);

            int barViewHeight;
xwenliang's avatar
xwenliang committed
123 124 125 126 127
            if (options.hasKey(TEXT_BAR_HEIGHT)) {
                try {
                    barViewHeight = options.getInt(TEXT_BAR_HEIGHT);
                } catch (Exception e) {
                    barViewHeight = (int) options.getDouble(TEXT_BAR_HEIGHT);
xwenliang's avatar
xwenliang committed
128
                }
xwenliang's avatar
xwenliang committed
129 130 131 132 133 134 135
            } 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
136 137 138 139

            if (options.hasKey(TEXT_BAR_COLOR)) {
                ReadableArray array = options.getArray(TEXT_BAR_COLOR);
                for (int i = 0; i < array.size(); i++) {
140 141 142 143 144 145 146 147 148 149 150
                    switch (i) {
                        case 0:
                        case 1:
                        case 2:
                            barBgColor[i] = array.getInt(i);
                            break;
                        case 3:
                            barBgColor[i] = (int) (array.getDouble(i) * 255);
                            break;
                        default:
                            break;
xwenliang's avatar
xwenliang committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164
                    }
                }
                barLayout.setBackgroundColor(Color.argb(barBgColor[3], barBgColor[0], barBgColor[1], barBgColor[2]));
            }


            if (options.hasKey(CONFIRM_TEXT)) {
                confirmText = options.getString(CONFIRM_TEXT);
            }
            confirmTV.setText(!TextUtils.isEmpty(confirmText) ? confirmText : "");

            if (options.hasKey(CONFIRM_TEXT_COLOR)) {
                ReadableArray array = options.getArray(CONFIRM_TEXT_COLOR);
                for (int i = 0; i < array.size(); i++) {
165 166 167 168 169 170 171 172 173 174 175
                    switch (i) {
                        case 0:
                        case 1:
                        case 2:
                            confirmTextColor[i] = array.getInt(i);
                            break;
                        case 3:
                            confirmTextColor[i] = (int) (array.getDouble(i) * 255);
                            break;
                        default:
                            break;
xwenliang's avatar
xwenliang committed
176 177 178 179 180 181 182
                    }
                }
                confirmTV.setTextColor(Color.argb(confirmTextColor[3], confirmTextColor[0], confirmTextColor[1], confirmTextColor[2]));
            }
            confirmTV.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
183 184 185 186 187 188 189 190
                    switch (curStatus){
                        case 0:
                            returnData = pickerViewAlone.getSelectedData();
                            break;
                        case 1:
                            returnData = pickerViewLinkage.getSelectedData();
                            break;
                    }
xwenliang's avatar
xwenliang committed
191 192 193 194 195 196 197 198 199 200 201 202 203
                    commonEvent(EVENT_KEY_CONFIRM);
                    hide();
                }
            });


            if (options.hasKey(TITLE_TEXT)) {
                titleText = options.getString(TITLE_TEXT);
            }
            titleTV.setText(!TextUtils.isEmpty(titleText) ? titleText : "");
            if (options.hasKey(TITLE_TEXT_COLOR)) {
                ReadableArray array = options.getArray(TITLE_TEXT_COLOR);
                for (int i = 0; i < array.size(); i++) {
204 205 206 207 208 209 210 211 212 213 214
                    switch (i) {
                        case 0:
                        case 1:
                        case 2:
                            titleTextColor[i] = array.getInt(i);
                            break;
                        case 3:
                            titleTextColor[i] = (int) (array.getDouble(i) * 255);
                            break;
                        default:
                            break;
xwenliang's avatar
xwenliang committed
215 216 217 218 219 220 221 222 223 224 225 226
                    }
                }
                titleTV.setTextColor(Color.argb(titleTextColor[3], titleTextColor[0], titleTextColor[1], titleTextColor[2]));
            }

            if (options.hasKey(CANCEL_TEXT)) {
                cancelText = options.getString(CANCEL_TEXT);
            }
            cancelTV.setText(!TextUtils.isEmpty(cancelText) ? cancelText : "");
            if (options.hasKey(CANCEL_TEXT_COLOR)) {
                ReadableArray array = options.getArray(CANCEL_TEXT_COLOR);
                for (int i = 0; i < array.size(); i++) {
227 228 229 230 231 232 233 234 235 236 237
                    switch (i) {
                        case 0:
                        case 1:
                        case 2:
                            cancelTextColor[i] = array.getInt(i);
                            break;
                        case 3:
                            cancelTextColor[i] = (int) (array.getDouble(i) * 255);
                            break;
                        default:
                            break;
xwenliang's avatar
xwenliang committed
238 239 240 241 242 243 244
                    }
                }
                cancelTV.setTextColor(Color.argb(cancelTextColor[3], cancelTextColor[0], cancelTextColor[1], cancelTextColor[2]));
            }
            cancelTV.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
245 246 247 248 249 250 251 252
                    switch (curStatus){
                        case 0:
                            returnData = pickerViewAlone.getSelectedData();
                            break;
                        case 1:
                            returnData = pickerViewLinkage.getSelectedData();
                            break;
                    }
xwenliang's avatar
xwenliang committed
253 254 255 256 257 258 259 260 261
                    commonEvent(EVENT_KEY_CANCEL);
                    hide();
                }
            });

            if (options.hasKey(IS_LOOP)) {
                isLoop = options.getBoolean(IS_LOOP);
            }

262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
            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
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
            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;
                }
            }

xwenliang's avatar
xwenliang committed
313 314 315
            if (options.hasKey(PICKER_BG_COLOR)) {
                ReadableArray array = options.getArray(PICKER_BG_COLOR);
                for (int i = 0; i < array.size(); i++) {
316 317 318 319 320 321 322 323 324 325 326
                    switch (i) {
                        case 0:
                        case 1:
                        case 2:
                            pickerColor[i] = array.getInt(i);
                            break;
                        case 3:
                            pickerColor[i] = (int) (array.getDouble(i) * 255);
                            break;
                        default:
                            break;
xwenliang's avatar
xwenliang committed
327 328 329 330 331 332
                    }
                }
            }

            ReadableArray pickerData = options.getArray(PICKER_DATA);

333
            int pickerViewHeight;
xwenliang's avatar
xwenliang committed
334 335
            String name = pickerData.getType(0).name();
            switch (name) {
xwenliang's avatar
xwenliang committed
336
                case "Map":
337
                    curStatus = 1;
xwenliang's avatar
xwenliang committed
338 339
                    pickerViewLinkage.setVisibility(View.VISIBLE);
                    pickerViewAlone.setVisibility(View.GONE);
340
                    pickerViewLinkage.setPickerData(pickerData, weights);
xwenliang's avatar
xwenliang committed
341
                    pickerViewLinkage.setIsLoop(isLoop);
xwenliang's avatar
xwenliang committed
342 343 344
                    if (options.hasKey(PICKER_BG_COLOR)) {
                        pickerViewLinkage.setBackgroundColor(Color.argb(pickerColor[3], pickerColor[0], pickerColor[1], pickerColor[2]));
                    }
xwenliang's avatar
xwenliang committed
345 346 347
                    pickerViewLinkage.setOnSelectListener(new OnSelectedListener() {
                        @Override
                        public void onSelected(ArrayList<String> selectedList) {
348
                            returnData = selectedList;
xwenliang's avatar
xwenliang committed
349 350 351
                            commonEvent(EVENT_KEY_SELECTED);
                        }
                    });
352
                    pickerViewLinkage.setSelectValue(selectValue);
xwenliang's avatar
xwenliang committed
353
                    pickerViewHeight = pickerViewLinkage.getViewHeight();
xwenliang's avatar
xwenliang committed
354
                    break;
xwenliang's avatar
xwenliang committed
355
                default:
356
                    curStatus = 0;
xwenliang's avatar
xwenliang committed
357 358
                    pickerViewAlone.setVisibility(View.VISIBLE);
                    pickerViewLinkage.setVisibility(View.GONE);
xwenliang's avatar
xwenliang committed
359

360
                    pickerViewAlone.setPickerData(pickerData, weights);
xwenliang's avatar
xwenliang committed
361
                    pickerViewAlone.setIsLoop(isLoop);
xwenliang's avatar
xwenliang committed
362 363 364
                    if (options.hasKey(PICKER_BG_COLOR)) {
                        pickerViewAlone.setBackgroundColor(Color.argb(pickerColor[3], pickerColor[0], pickerColor[1], pickerColor[2]));
                    }
xwenliang's avatar
xwenliang committed
365 366 367 368

                    pickerViewAlone.setOnSelectedListener(new OnSelectedListener() {
                        @Override
                        public void onSelected(ArrayList<String> selectedList) {
369
                            returnData = selectedList;
xwenliang's avatar
xwenliang committed
370 371 372 373
                            commonEvent(EVENT_KEY_SELECTED);
                        }
                    });

374
                    pickerViewAlone.setSelectValue(selectValue);
xwenliang's avatar
xwenliang committed
375
                    pickerViewHeight = pickerViewAlone.getViewHeight();
xwenliang's avatar
xwenliang committed
376 377 378 379
                    break;
            }

            if (popupWindow == null) {
xwenliang's avatar
xwenliang committed
380 381
                int height = barViewHeight + pickerViewHeight;
                popupWindow = new PopupWindow(WindowManager.LayoutParams.MATCH_PARENT, height);
xwenliang's avatar
xwenliang committed
382 383
                popupWindow.setBackgroundDrawable(new ColorDrawable());
                popupWindow.setAnimationStyle(R.style.PopAnim);
384 385 386 387 388 389
                popupWindow.setContentView(view);
                popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
            } else {
                popupWindow.dismiss();
                popupWindow.setContentView(view);
                popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
xwenliang's avatar
xwenliang committed
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
            }
        }
    }

    @ReactMethod
    public void initOK(Callback callback) {
        callback.invoke(popupWindow != null);
    }

    @ReactMethod
    public void toggle() {
        if (popupWindow == null)
            return;
        if (popupWindow.isShowing()) {
            hide();
        } else {
            show();
        }
    }

    @ReactMethod
    public void show() {
        if (popupWindow != null) {
            popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
        }
    }

    @ReactMethod
    public void hide() {
        if (popupWindow != null) {
            popupWindow.dismiss();
        }
    }

    @ReactMethod
    public void isPickerShow(Callback callback) {
        if (popupWindow == null) {
            callback.invoke(ERROR_NOT_INIT);
        } else {
            callback.invoke(null, popupWindow.isShowing());
        }
    }

    private void commonEvent(String eventKey) {
        WritableMap map = Arguments.createMap();
        WritableArray array = Arguments.createArray();
436
        for (String item : returnData) {
xwenliang's avatar
xwenliang committed
437 438 439 440 441 442 443 444 445 446 447 448 449 450
            array.pushString(item);
        }
        map.putArray(eventKey, array);
        sendEvent(getReactApplicationContext(), PICKER_EVENT_NAME, map);
    }

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