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

import android.app.Activity;
4 5
import android.app.Application;
import android.app.Dialog;
xwenliang's avatar
xwenliang committed
6
import android.graphics.Color;
7 8
import android.graphics.PixelFormat;
import android.os.Bundle;
xwenliang's avatar
xwenliang committed
9 10 11 12
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
13
import android.view.Window;
xwenliang's avatar
xwenliang committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
import android.view.WindowManager;
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;

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

58
public class PickerViewModule extends ReactContextBaseJavaModule implements Application.ActivityLifecycleCallbacks {
xwenliang's avatar
xwenliang committed
59 60 61 62 63

    private static final String REACT_CLASS = "BEEPickerManager";

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

xwenliang's avatar
xwenliang committed
65
    private static final String IS_LOOP = "isLoop";
66 67 68

    private static final String WEIGHTS = "wheelFlex";

xwenliang's avatar
xwenliang committed
69
    private static final String PICKER_BG_COLOR = "pickerBg";
70

xwenliang's avatar
xwenliang committed
71
    private static final String TEXT_BAR_COLOR = "pickerToolBarBg";
xwenliang's avatar
xwenliang committed
72
    private static final String TEXT_BAR_HEIGHT = "pickerToolBarHeight";
73

xwenliang's avatar
xwenliang committed
74 75
    private static final String CONFIRM_TEXT = "pickerConfirmBtnText";
    private static final String CONFIRM_TEXT_COLOR = "pickerConfirmBtnColor";
76

xwenliang's avatar
xwenliang committed
77 78
    private static final String CANCEL_TEXT = "pickerCancelBtnText";
    private static final String CANCEL_TEXT_COLOR = "pickerCancelBtnColor";
79

xwenliang's avatar
xwenliang committed
80 81 82 83 84 85 86 87 88 89
    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";

90
    private Dialog dialog = null;
xwenliang's avatar
xwenliang committed
91 92 93 94 95 96 97

    private boolean isLoop = true;

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

98 99 100
    private double[] weights;

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

102
    private int curStatus;
xwenliang's avatar
xwenliang committed
103

xwenliang's avatar
xwenliang committed
104 105 106 107 108 109 110 111 112 113 114 115 116
    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)) {
117
            View view = activity.getLayoutInflater().inflate(R.layout.picker_view, null);
118 119 120 121
            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);
122
            RelativeLayout pickerLayout = (RelativeLayout) view.findViewById(R.id.pickerLayout);
123 124 125 126
            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
127 128 129 130 131
            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
132
                }
xwenliang's avatar
xwenliang committed
133 134 135 136 137 138 139
            } 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
140 141 142

            if (options.hasKey(TEXT_BAR_COLOR)) {
                ReadableArray array = options.getArray(TEXT_BAR_COLOR);
143 144
                int[] colors = getColor(array);
                barLayout.setBackgroundColor(Color.argb(colors[3], colors[0], colors[1], colors[2]));
xwenliang's avatar
xwenliang committed
145 146 147 148 149 150 151 152 153 154
            }


            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);
155 156
                int[] colors = getColor(array);
                confirmTV.setTextColor(Color.argb(colors[3], colors[0], colors[1], colors[2]));
xwenliang's avatar
xwenliang committed
157 158 159 160
            }
            confirmTV.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
161
                    switch (curStatus) {
162 163 164 165 166 167 168
                        case 0:
                            returnData = pickerViewAlone.getSelectedData();
                            break;
                        case 1:
                            returnData = pickerViewLinkage.getSelectedData();
                            break;
                    }
xwenliang's avatar
xwenliang committed
169 170 171 172 173 174 175 176 177 178 179 180
                    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);
181 182
                int[] colors = getColor(array);
                titleTV.setTextColor(Color.argb(colors[3], colors[0], colors[1], colors[2]));
xwenliang's avatar
xwenliang committed
183 184 185 186 187 188 189 190
            }

            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);
191 192
                int[] colors = getColor(array);
                cancelTV.setTextColor(Color.argb(colors[3], colors[0], colors[1], colors[2]));
xwenliang's avatar
xwenliang committed
193 194 195 196
            }
            cancelTV.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
197
                    switch (curStatus) {
198 199 200 201 202 203 204
                        case 0:
                            returnData = pickerViewAlone.getSelectedData();
                            break;
                        case 1:
                            returnData = pickerViewLinkage.getSelectedData();
                            break;
                    }
xwenliang's avatar
xwenliang committed
205 206 207 208 209 210 211 212 213
                    commonEvent(EVENT_KEY_CANCEL);
                    hide();
                }
            });

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

214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
            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
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
            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
265 266
            ReadableArray pickerData = options.getArray(PICKER_DATA);

267
            int pickerViewHeight;
xwenliang's avatar
xwenliang committed
268 269
            String name = pickerData.getType(0).name();
            switch (name) {
xwenliang's avatar
xwenliang committed
270
                case "Map":
271
                    curStatus = 1;
xwenliang's avatar
xwenliang committed
272 273
                    pickerViewLinkage.setVisibility(View.VISIBLE);
                    pickerViewAlone.setVisibility(View.GONE);
274

275
                    pickerViewLinkage.setPickerData(pickerData, weights);
xwenliang's avatar
xwenliang committed
276
                    pickerViewLinkage.setIsLoop(isLoop);
277

xwenliang's avatar
xwenliang committed
278 279 280
                    pickerViewLinkage.setOnSelectListener(new OnSelectedListener() {
                        @Override
                        public void onSelected(ArrayList<String> selectedList) {
281
                            returnData = selectedList;
xwenliang's avatar
xwenliang committed
282 283 284
                            commonEvent(EVENT_KEY_SELECTED);
                        }
                    });
285
                    pickerViewLinkage.setSelectValue(selectValue);
xwenliang's avatar
xwenliang committed
286
                    pickerViewHeight = pickerViewLinkage.getViewHeight();
xwenliang's avatar
xwenliang committed
287
                    break;
xwenliang's avatar
xwenliang committed
288
                default:
289
                    curStatus = 0;
xwenliang's avatar
xwenliang committed
290 291
                    pickerViewAlone.setVisibility(View.VISIBLE);
                    pickerViewLinkage.setVisibility(View.GONE);
xwenliang's avatar
xwenliang committed
292

293
                    pickerViewAlone.setPickerData(pickerData, weights);
xwenliang's avatar
xwenliang committed
294 295 296 297 298
                    pickerViewAlone.setIsLoop(isLoop);

                    pickerViewAlone.setOnSelectedListener(new OnSelectedListener() {
                        @Override
                        public void onSelected(ArrayList<String> selectedList) {
299
                            returnData = selectedList;
xwenliang's avatar
xwenliang committed
300 301 302 303
                            commonEvent(EVENT_KEY_SELECTED);
                        }
                    });

304
                    pickerViewAlone.setSelectValue(selectValue);
xwenliang's avatar
xwenliang committed
305
                    pickerViewHeight = pickerViewAlone.getViewHeight();
xwenliang's avatar
xwenliang committed
306 307 308
                    break;
            }

309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
            if (options.hasKey(PICKER_BG_COLOR)) {
                ReadableArray array = options.getArray(PICKER_BG_COLOR);
                int[] colors = getColor(array);
                pickerLayout.setBackgroundColor(Color.argb(colors[3], colors[0], colors[1], colors[2]));
            }

            int height = barViewHeight + pickerViewHeight;
            if (dialog == null) {
                dialog = new Dialog(activity, R.style.Dialog_Full_Screen);
                dialog.setContentView(view);
                WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
                Window window = dialog.getWindow();
                if (window != null) {
                    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;
                    window.setAttributes(layoutParams);
                }
330
            } else {
331 332
                dialog.dismiss();
                dialog.setContentView(view);
xwenliang's avatar
xwenliang committed
333
            }
334
            dialog.show();
xwenliang's avatar
xwenliang committed
335 336 337 338
        }
    }

    @ReactMethod
339 340
    public void show() {
        if (dialog == null) {
xwenliang's avatar
xwenliang committed
341 342
            return;
        }
343 344
        if (!dialog.isShowing()) {
            dialog.show();
xwenliang's avatar
xwenliang committed
345 346 347 348 349
        }
    }

    @ReactMethod
    public void hide() {
350 351 352 353 354
        if (dialog == null) {
            return;
        }
        if (dialog.isShowing()) {
            dialog.dismiss();
xwenliang's avatar
xwenliang committed
355 356 357 358 359
        }
    }

    @ReactMethod
    public void isPickerShow(Callback callback) {
360
        if (dialog == null) {
xwenliang's avatar
xwenliang committed
361 362
            callback.invoke(ERROR_NOT_INIT);
        } else {
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
            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
382
        }
383
        return colors;
xwenliang's avatar
xwenliang committed
384 385 386 387 388
    }

    private void commonEvent(String eventKey) {
        WritableMap map = Arguments.createMap();
        WritableArray array = Arguments.createArray();
389
        for (String item : returnData) {
xwenliang's avatar
xwenliang committed
390 391 392 393 394 395 396 397 398 399 400 401 402
            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);
    }
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 436

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

    }

    @Override
    public void onActivityStarted(Activity activity) {

    }

    @Override
    public void onActivityResumed(Activity activity) {

    }

    @Override
    public void onActivityPaused(Activity activity) {

    }

    @Override
    public void onActivityStopped(Activity activity) {
        hide();
    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

    }

    @Override
    public void onActivityDestroyed(Activity activity) {
    }
xwenliang's avatar
xwenliang committed
437
}