This repository was archived by the owner on Aug 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathActionSheetModule.java
More file actions
91 lines (78 loc) · 2.81 KB
/
ActionSheetModule.java
File metadata and controls
91 lines (78 loc) · 2.81 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package com.actionsheet;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.util.Log;
import android.widget.ArrayAdapter;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
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.WritableMap;
import java.util.List;
import java.util.ArrayList;
public class ActionSheetModule extends ReactContextBaseJavaModule {
WritableMap response;
public ActionSheetModule(ReactApplicationContext reactContext) {
super(reactContext);
}
@Override
public String getName() {
return "ActionSheetAndroid";
}
@ReactMethod
public void showActionSheetWithOptions(final ReadableMap options, final Callback callback) {
Activity currentActivity = getCurrentActivity();
if (currentActivity == null) {
response = Arguments.createMap();
response.putString("error", "can't find current Activity");
callback.invoke(response);
return;
}
final List<String> titles = new ArrayList<String>();
int _cancelInde = 99;
if (options.hasKey("cancelButtonIndex")) {
_cancelInde = options.getInt("cancelButtonIndex");
}
final int cancelIndex = _cancelInde;
if (options.hasKey("options")) {
ReadableArray customButtons = options.getArray("options");
for (int i = 0; i < customButtons.size(); i++) {
int currentIndex = titles.size();
if (i != cancelIndex) {
titles.add(currentIndex, customButtons.getString(i));
}
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(currentActivity,
android.R.layout.select_dialog_item, titles);
AlertDialog.Builder builder = new AlertDialog.Builder(currentActivity);
if (options.hasKey("title") && options.getString("title") != null && !options.getString("title").isEmpty()) {
builder.setTitle(options.getString("title"));
}
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int index) {
if (index < cancelIndex) {
callback.invoke(index);
} else {
callback.invoke(index + 1);
}
}
});
final AlertDialog dialog = builder.create();
/**
* override onCancel method to callback cancel in case of a touch outside of
* the dialog or the BACK key pressed
*/
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
dialog.dismiss();
}
});
dialog.show();
}
}