diff --git a/android/build.gradle b/android/build.gradle index 40f4f265c..9037e9151 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -1,12 +1,15 @@ apply plugin: 'com.android.library' +def safeExtGet(prop, fallback) { + rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback +} android { - compileSdkVersion 27 - buildToolsVersion "23.0.1" + compileSdkVersion safeExtGet('compileSdkVersion', 30) + buildToolsVersion safeExtGet('buildToolsVersion', "30.0.3") defaultConfig { - minSdkVersion 16 - targetSdkVersion 27 + minSdkVersion safeExtGet('minSdkVersion', 18) + targetSdkVersion safeExtGet('targetSdkVersion', 30) versionCode 1 versionName "1.0" @@ -22,6 +25,6 @@ android { } dependencies { - compile fileTree(include: ['*.jar'], dir: 'libs') - compile 'com.facebook.react:react-native:+' + implementation fileTree(include: ['*.jar'], dir: 'libs') + implementation 'com.facebook.react:react-native:+' } diff --git a/android/src/main/java/com/beefe/picker/PickerViewModule.java b/android/src/main/java/com/beefe/picker/PickerViewModule.java index 813a0bddf..b9082d8bc 100644 --- a/android/src/main/java/com/beefe/picker/PickerViewModule.java +++ b/android/src/main/java/com/beefe/picker/PickerViewModule.java @@ -6,8 +6,9 @@ import android.graphics.Color; import android.graphics.PixelFormat; import android.graphics.Typeface; -import android.support.annotation.Nullable; +import androidx.annotation.Nullable; import android.text.TextUtils; +import android.util.TypedValue; import android.view.Gravity; import android.view.View; import android.view.Window; @@ -22,6 +23,7 @@ import com.beefe.picker.view.PickerViewLinkage; import com.beefe.picker.view.ReturnData; import com.facebook.react.bridge.Arguments; +import com.facebook.react.bridge.Promise; import com.facebook.react.bridge.Callback; import com.facebook.react.bridge.LifecycleEventListener; import com.facebook.react.bridge.ReactApplicationContext; @@ -77,7 +79,7 @@ */ public class PickerViewModule extends ReactContextBaseJavaModule implements LifecycleEventListener { - + private static final String FONTS = "fonts/"; private static final String OTF = ".otf"; private static final String TTF = ".ttf"; @@ -136,6 +138,7 @@ public class PickerViewModule extends ReactContextBaseJavaModule implements Life private PickerViewLinkage pickerViewLinkage; private PickerViewAlone pickerViewAlone; + private Promise promise; public PickerViewModule(ReactApplicationContext reactContext) { super(reactContext); @@ -148,7 +151,8 @@ public String getName() { } @ReactMethod - public void _init(ReadableMap options) { + public void _init(ReadableMap options, Promise promise) { + this.promise = promise; Activity activity = getCurrentActivity(); if (activity != null && options.hasKey(PICKER_DATA)) { View view = activity.getLayoutInflater().inflate(R.layout.picker_view, null); @@ -170,6 +174,7 @@ public void _init(ReadableMap options) { } else { barViewHeight = (int) (activity.getResources().getDisplayMetrics().density * 40); } + RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.MATCH_PARENT, barViewHeight); @@ -183,9 +188,9 @@ public void _init(ReadableMap options) { 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); + cancelTV.setTextSize(TypedValue.COMPLEX_UNIT_DIP, toolBarTextSize); + titleTV.setTextSize(TypedValue.COMPLEX_UNIT_DIP,toolBarTextSize); + confirmTV.setTextSize(TypedValue.COMPLEX_UNIT_DIP,toolBarTextSize); } if (options.hasKey(PICKER_CONFIRM_BTN_TEXT)) { @@ -340,7 +345,6 @@ public void onSelected(ArrayList selectedList) { @Override public void onSelected(ArrayList selectedList) { returnData = selectedList; - commonEvent(EVENT_KEY_SELECTED); } }); @@ -386,30 +390,28 @@ public void onSelected(ArrayList selectedList) { pickerLayout.setBackgroundColor(argb(colors[3], colors[0], colors[1], colors[2])); } - int height = barViewHeight + pickerViewHeight; - if (dialog == null) { + 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) { - if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - window.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); - }else{ - if (MIUIUtils.isMIUI()) { - layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION; - }else { - //layoutParams.type = WindowManager.LayoutParams.TYPE_TOAST; - } + if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + window.setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY); + } else if (MIUIUtils.isMIUI()) { + layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION; } - 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); } + 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); + } else { dialog.dismiss(); dialog.setContentView(view); @@ -531,9 +533,7 @@ private void commonEvent(String eventKey) { private void sendEvent(ReactContext reactContext, String eventName, @Nullable WritableMap params) { - reactContext - .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) - .emit(eventName, params); + promise.resolve(params); } @Override diff --git a/android/src/main/res/layout/picker_view.xml b/android/src/main/res/layout/picker_view.xml index 9886bdd02..21189fdd3 100644 --- a/android/src/main/res/layout/picker_view.xml +++ b/android/src/main/res/layout/picker_view.xml @@ -4,53 +4,59 @@ android:layout_height="wrap_content" android:background="#00000000"> - + android:background="@android:color/white"> - - - + + + + + + + + + + + android:layout_alignParentBottom="true"> - + - + + - - - - - - \ No newline at end of file + diff --git a/index.js b/index.js index bb5d1360e..ee6bf5f3f 100644 --- a/index.js +++ b/index.js @@ -1,97 +1,97 @@ -import { - Platform, - NativeModules, - NativeAppEventEmitter -} from 'react-native'; +import { Platform, NativeModules, NativeAppEventEmitter } from 'react-native'; const ios = Platform.OS === 'ios'; const android = Platform.OS === 'android'; const Picker = NativeModules.BEEPickerManager; const options = { - isLoop: false, - pickerConfirmBtnText: 'confirm', - pickerCancelBtnText: 'cancel', - pickerTitleText: 'pls select', - pickerConfirmBtnColor: [1, 186, 245, 1], - pickerCancelBtnColor: [1, 186, 245, 1], - pickerTitleColor: [20, 20, 20, 1], - pickerToolBarBg: [232, 232, 232, 1], - pickerTextEllipsisLen: 6, - pickerBg: [196, 199, 206, 1], - pickerRowHeight: 24, - wheelFlex: [1, 1, 1], - pickerData: [], - selectedValue: [], - onPickerConfirm(){}, - onPickerCancel(){}, - onPickerSelect(){}, - pickerToolBarFontSize: 16, - pickerFontSize: 16, - pickerFontColor: [31, 31 ,31, 1] + isLoop: false, + pickerConfirmBtnText: 'confirm', + pickerCancelBtnText: 'cancel', + pickerTitleText: 'pls select', + pickerConfirmBtnColor: [1, 186, 245, 1], + pickerCancelBtnColor: [1, 186, 245, 1], + pickerTitleColor: [20, 20, 20, 1], + pickerToolBarBg: [232, 232, 232, 1], + pickerTextEllipsisLen: 6, + pickerBg: [196, 199, 206, 1], + pickerRowHeight: 24, + wheelFlex: [1, 1, 1], + pickerData: [], + selectedValue: [], + onPickerConfirm() {}, + onPickerCancel() {}, + onPickerSelect() {}, + pickerToolBarFontSize: 16, + pickerFontSize: 16, + pickerFontColor: [31, 31, 31, 1] }; export default { - init(params){ - const opt = { - ...options, - ...params - }; - const fnConf = { - confirm: opt.onPickerConfirm, - cancel: opt.onPickerCancel, - select: opt.onPickerSelect - }; + init(params) { + const opt = { + ...options, + ...params + }; + const fnConf = { + confirm: opt.onPickerConfirm, + cancel: opt.onPickerCancel, + select: opt.onPickerSelect + }; - Picker._init(opt); - //there are no `removeListener` for NativeAppEventEmitter & DeviceEventEmitter - this.listener && this.listener.remove(); - this.listener = NativeAppEventEmitter.addListener('pickerEvent', event => { - fnConf[event['type']](event['selectedValue'], event['selectedIndex']); - }); - }, + Picker._init(opt) + .then(event => { + fnConf[event['type']](event['selectedValue'], event['selectedIndex']); + this.listener && this.listener.remove(); + }) + .catch(error => { + console.log(error); + }); + //there are no `removeListener` for NativeAppEventEmitter & DeviceEventEmitter + // this.listener && this.listener.remove(); + // this.listener = NativeAppEventEmitter.addListener('pickerEvent', event => { + // fnConf[event['type']](event['selectedValue'], event['selectedIndex']); + // }); + }, - show(){ - Picker.show(); - }, + show() { + Picker.show(); + }, - hide(){ - Picker.hide(); - }, + hide() { + Picker.hide(); + }, - select(arr, fn) { - if(ios){ - Picker.select(arr); - } - else if(android){ - Picker.select(arr, err => { - typeof fn === 'function' && fn(err); - }); - } - }, + select(arr, fn) { + if (ios) { + Picker.select(arr); + } else if (android) { + Picker.select(arr, err => { + typeof fn === 'function' && fn(err); + }); + } + }, - toggle(){ - this.isPickerShow(show => { - if(show){ - this.hide(); - } - else{ - this.show(); - } - }); - }, + toggle() { + this.isPickerShow(show => { + if (show) { + this.hide(); + } else { + this.show(); + } + }); + }, - isPickerShow(fn){ - //android return two params: err(error massage) and status(show or not) - //ios return only one param: hide or not... - Picker.isPickerShow((err, status) => { - let returnValue = null; - if(android){ - returnValue = err ? false : status; - } - else if(ios){ - returnValue = !err; - } - fn && fn(returnValue); - }); - } + isPickerShow(fn) { + //android return two params: err(error massage) and status(show or not) + //ios return only one param: hide or not... + Picker.isPickerShow((err, status) => { + let returnValue = null; + if (android) { + returnValue = err ? false : status; + } else if (ios) { + returnValue = !err; + } + fn && fn(returnValue); + }); + } }; diff --git a/ios/RCTBEEPickerManager/BzwPicker.m b/ios/RCTBEEPickerManager/BzwPicker.m index 41aea4b99..fe1c0fda5 100755 --- a/ios/RCTBEEPickerManager/BzwPicker.m +++ b/ios/RCTBEEPickerManager/BzwPicker.m @@ -456,10 +456,6 @@ - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComp } else { [dic setValue:value forKey:@"selectedIndex"]; } - - if (self.backArry.count>0) { - self.bolock(dic); - } } - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component diff --git a/ios/RCTBEEPickerManager/RCTBEEPickerManager.m b/ios/RCTBEEPickerManager/RCTBEEPickerManager.m index 0215d49e2..01a0d581a 100644 --- a/ios/RCTBEEPickerManager/RCTBEEPickerManager.m +++ b/ios/RCTBEEPickerManager/RCTBEEPickerManager.m @@ -34,7 +34,7 @@ + (BOOL)requiresMainQueueSetup RCT_EXPORT_MODULE(); -RCT_EXPORT_METHOD(_init:(NSDictionary *)indic){ +RCT_EXPORT_METHOD(_init:(NSDictionary *)indic resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject){ dispatch_async(dispatch_get_main_queue(), ^{ [[UIApplication sharedApplication].keyWindow endEditing:YES]; @@ -85,8 +85,7 @@ + (BOOL)requiresMainQueueSetup _pick.bolock=^(NSDictionary *backinfoArry){ dispatch_async(dispatch_get_main_queue(), ^{ - - [self.bridge.eventDispatcher sendAppEventWithName:@"pickerEvent" body:backinfoArry]; + resolve(backinfoArry); }); };