Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

event listener changed with promise callback #342

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
15 changes: 9 additions & 6 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -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:+'
}
54 changes: 27 additions & 27 deletions android/src/main/java/com/beefe/picker/PickerViewModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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)) {
Expand Down Expand Up @@ -340,7 +345,6 @@ public void onSelected(ArrayList<ReturnData> selectedList) {
@Override
public void onSelected(ArrayList<ReturnData> selectedList) {
returnData = selectedList;
commonEvent(EVENT_KEY_SELECTED);
}
});

Expand Down Expand Up @@ -386,30 +390,28 @@ public void onSelected(ArrayList<ReturnData> 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);
Expand Down Expand Up @@ -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
Expand Down
84 changes: 45 additions & 39 deletions android/src/main/res/layout/picker_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,59 @@
android:layout_height="wrap_content"
android:background="#00000000">


<RelativeLayout
android:id="@+id/barLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/pickerLayout">
android:background="@android:color/white">

<TextView
android:id="@+id/cancel"
android:layout_width="wrap_content"
<RelativeLayout
android:id="@+id/barLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:padding="9dp" />

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_above="@+id/pickerLayout">

<TextView
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:padding="9dp" />

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:padding="9dp" />

<TextView
android:id="@+id/confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:padding="9dp" />

</RelativeLayout>

<RelativeLayout
android:id="@+id/pickerLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:padding="9dp" />
android:layout_alignParentBottom="true">

<TextView
android:id="@+id/confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:padding="9dp" />
<com.beefe.picker.view.PickerViewLinkage
android:id="@+id/pickerViewLinkage"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</RelativeLayout>
<com.beefe.picker.view.PickerViewAlone
android:id="@+id/pickerViewAlone"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>

<RelativeLayout
android:id="@+id/pickerLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >

<com.beefe.picker.view.PickerViewLinkage
android:id="@+id/pickerViewLinkage"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<com.beefe.picker.view.PickerViewAlone
android:id="@+id/pickerViewAlone"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>

</RelativeLayout>
</RelativeLayout>
Loading