2015-11-01 22:58:44 +05:30
|
|
|
package com.aakashns.reactnativedialogs.modules;
|
|
|
|
|
|
2016-01-24 12:57:05 -05:00
|
|
|
import android.app.Activity;
|
2015-11-01 22:58:44 +05:30
|
|
|
import android.content.DialogInterface;
|
|
|
|
|
import android.view.View;
|
|
|
|
|
|
|
|
|
|
import com.afollestad.materialdialogs.DialogAction;
|
|
|
|
|
import com.afollestad.materialdialogs.MaterialDialog;
|
|
|
|
|
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;
|
2015-11-20 18:49:43 -08:00
|
|
|
import com.facebook.react.bridge.ReadableMapKeySetIterator;
|
2015-11-01 22:58:44 +05:30
|
|
|
|
|
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
|
|
|
|
|
|
public class DialogAndroid extends ReactContextBaseJavaModule {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String getName() {
|
|
|
|
|
return "DialogAndroid";
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-24 12:57:05 -05:00
|
|
|
Activity mActivity;
|
2015-11-01 22:58:44 +05:30
|
|
|
|
|
|
|
|
public DialogAndroid(
|
|
|
|
|
ReactApplicationContext reactContext,
|
2016-01-24 12:57:05 -05:00
|
|
|
Activity activity) {
|
2015-11-01 22:58:44 +05:30
|
|
|
super(reactContext);
|
2016-01-24 12:57:05 -05:00
|
|
|
mActivity = activity;
|
2015-11-01 22:58:44 +05:30
|
|
|
}
|
|
|
|
|
|
2015-11-01 23:58:58 +05:30
|
|
|
/* Apply the options to the provided builder */
|
2015-11-01 22:58:44 +05:30
|
|
|
private MaterialDialog.Builder applyOptions(
|
|
|
|
|
MaterialDialog.Builder builder,
|
|
|
|
|
ReadableMap options
|
|
|
|
|
) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
|
2015-11-20 18:49:43 -08:00
|
|
|
ReadableMapKeySetIterator iterator = options.keySetIterator();
|
2015-11-01 22:58:44 +05:30
|
|
|
while(iterator.hasNextKey()) {
|
|
|
|
|
String key = iterator.nextKey();
|
|
|
|
|
|
|
|
|
|
switch (key) {
|
|
|
|
|
case "title":
|
|
|
|
|
builder.title(options.getString("title"));
|
|
|
|
|
break;
|
|
|
|
|
case "content":
|
|
|
|
|
builder.content(options.getString("content"));
|
|
|
|
|
break;
|
|
|
|
|
case "positiveText":
|
|
|
|
|
builder.positiveText(options.getString("positiveText"));
|
|
|
|
|
break;
|
|
|
|
|
case "negativeText":
|
|
|
|
|
builder.negativeText(options.getString("negativeText"));
|
|
|
|
|
break;
|
|
|
|
|
case "neutralText":
|
|
|
|
|
builder.neutralText(options.getString("neutralText"));
|
|
|
|
|
break;
|
|
|
|
|
case "items":
|
|
|
|
|
ReadableArray arr = options.getArray("items");
|
|
|
|
|
String[] items = new String[arr.size()];
|
|
|
|
|
for (int i = 0; i < arr.size(); i++) {
|
|
|
|
|
items[i] = arr.getString(i);
|
|
|
|
|
}
|
|
|
|
|
builder.items(items);
|
|
|
|
|
break;
|
|
|
|
|
case "autoDismiss":
|
|
|
|
|
builder.autoDismiss(options.getBoolean("autoDismiss"));
|
|
|
|
|
break;
|
|
|
|
|
case "forceStacking":
|
|
|
|
|
builder.forceStacking(options.getBoolean("forceStacking"));
|
|
|
|
|
break;
|
|
|
|
|
case "alwaysCallSingleChoiceCallback":
|
|
|
|
|
if (options.getBoolean("alwaysCallSingleChoiceCallback")) {
|
|
|
|
|
builder.alwaysCallSingleChoiceCallback();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case "alwaysCallMultiChoiceCallback":
|
|
|
|
|
if (options.getBoolean("alwaysCallMultiChoiceCallback")) {
|
|
|
|
|
builder.alwaysCallMultiChoiceCallback();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case "alwaysCallInputCallback":
|
|
|
|
|
if (options.getBoolean("alwaysCallInputCallback")) {
|
|
|
|
|
builder.alwaysCallInputCallback();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case "cancelable":
|
|
|
|
|
builder.cancelable(options.getBoolean("cancelable"));
|
|
|
|
|
break;
|
|
|
|
|
case "progressIndeterminateStyle": // true for horizontal, DO NOT USE
|
|
|
|
|
builder.progressIndeterminateStyle(
|
|
|
|
|
options.getBoolean("progressIndeterminateStyle"));
|
|
|
|
|
break;
|
|
|
|
|
case "progress":
|
|
|
|
|
ReadableMap progress = options.getMap("progress");
|
|
|
|
|
boolean indeterminate = progress.hasKey("indeterminate") &&
|
|
|
|
|
progress.getBoolean("indeterminate");
|
|
|
|
|
|
|
|
|
|
if (indeterminate) {
|
|
|
|
|
builder.progress(true, 0);
|
|
|
|
|
boolean horizontal = progress.hasKey("style") &&
|
|
|
|
|
progress.getString("style").equals("horizontal");
|
|
|
|
|
if (horizontal) builder.progressIndeterminateStyle(horizontal);
|
|
|
|
|
} else {
|
|
|
|
|
// Determinate progress bar not supported currently
|
2015-11-01 23:58:58 +05:30
|
|
|
// TODO : Implement determinate progress bar
|
2015-11-01 22:58:44 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return builder;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-05 19:56:22 -08:00
|
|
|
MaterialDialog.Builder mBuilder;
|
|
|
|
|
MaterialDialog mDialog;
|
|
|
|
|
|
2015-11-01 22:58:44 +05:30
|
|
|
@ReactMethod
|
|
|
|
|
public void show(ReadableMap options, final Callback callback) {
|
2016-01-05 19:56:22 -08:00
|
|
|
mBuilder = new MaterialDialog.Builder(mActivity);
|
2015-11-01 22:58:44 +05:30
|
|
|
try {
|
2016-01-05 19:56:22 -08:00
|
|
|
applyOptions(mBuilder, options);
|
2015-11-01 22:58:44 +05:30
|
|
|
} catch (Exception e) {
|
|
|
|
|
callback.invoke("error", e.getMessage(), options.toString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (options.hasKey("onPositive")) {
|
2016-01-05 19:56:22 -08:00
|
|
|
mBuilder.onPositive(new MaterialDialog.SingleButtonCallback() {
|
2015-11-01 22:58:44 +05:30
|
|
|
@Override
|
|
|
|
|
public void onClick(MaterialDialog materialDialog, DialogAction dialogAction) {
|
|
|
|
|
callback.invoke("onPositive");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (options.hasKey("onNegative")) {
|
2016-01-05 19:56:22 -08:00
|
|
|
mBuilder.onNegative(new MaterialDialog.SingleButtonCallback() {
|
2015-11-01 22:58:44 +05:30
|
|
|
@Override
|
|
|
|
|
public void onClick(MaterialDialog materialDialog, DialogAction dialogAction) {
|
|
|
|
|
callback.invoke("onNegative");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (options.hasKey("onNeutral")) {
|
2016-01-05 19:56:22 -08:00
|
|
|
mBuilder.onNeutral(new MaterialDialog.SingleButtonCallback() {
|
2015-11-01 22:58:44 +05:30
|
|
|
@Override
|
|
|
|
|
public void onClick(MaterialDialog materialDialog, DialogAction dialogAction) {
|
|
|
|
|
callback.invoke("onNeutral");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (options.hasKey("onAny")) {
|
2016-01-05 19:56:22 -08:00
|
|
|
mBuilder.onAny(new MaterialDialog.SingleButtonCallback() {
|
2015-11-01 22:58:44 +05:30
|
|
|
@Override
|
|
|
|
|
public void onClick(MaterialDialog materialDialog, DialogAction dialogAction) {
|
|
|
|
|
callback.invoke("onAny");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (options.hasKey("itemsCallback")) {
|
2016-01-05 19:56:22 -08:00
|
|
|
mBuilder.itemsCallback(new MaterialDialog.ListCallback() {
|
2015-11-01 22:58:44 +05:30
|
|
|
@Override
|
|
|
|
|
public void onSelection(MaterialDialog materialDialog, View view, int i,
|
|
|
|
|
CharSequence charSequence) {
|
|
|
|
|
callback.invoke("itemsCallback", i, charSequence.toString());
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (options.hasKey("itemsCallbackSingleChoice")) {
|
2015-11-01 23:58:58 +05:30
|
|
|
// Check if there is a preselected index
|
2015-11-01 22:58:44 +05:30
|
|
|
int selectedIndex = options.hasKey("selectedIndex") ?
|
|
|
|
|
options.getInt("selectedIndex") : -1;
|
2016-01-05 19:56:22 -08:00
|
|
|
mBuilder.itemsCallbackSingleChoice(selectedIndex,
|
2015-11-01 22:58:44 +05:30
|
|
|
new MaterialDialog.ListCallbackSingleChoice() {
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onSelection(MaterialDialog materialDialog, View view, int i,
|
|
|
|
|
CharSequence charSequence) {
|
|
|
|
|
callback.invoke("itemsCallbackSingleChoice", i, charSequence.toString());
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (options.hasKey("itemsCallbackMultiChoice")) {
|
2015-11-01 23:58:58 +05:30
|
|
|
// Check if there are preselected indices
|
2015-11-01 22:58:44 +05:30
|
|
|
Integer[] selectedIndices = null;
|
|
|
|
|
if (options.hasKey("selectedIndices")) {
|
|
|
|
|
ReadableArray arr = options.getArray("selectedIndices");
|
|
|
|
|
selectedIndices = new Integer[arr.size()];
|
|
|
|
|
for (int i = 0; i < arr.size(); i++) {
|
|
|
|
|
selectedIndices[i] = arr.getInt(i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-05 19:56:22 -08:00
|
|
|
mBuilder.itemsCallbackMultiChoice(selectedIndices,
|
2015-11-01 22:58:44 +05:30
|
|
|
new MaterialDialog.ListCallbackMultiChoice() {
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onSelection(MaterialDialog materialDialog,
|
|
|
|
|
Integer[] integers, CharSequence[] charSequences) {
|
2015-11-01 23:58:58 +05:30
|
|
|
|
|
|
|
|
// Concatenate selected IDs into a string
|
2015-11-01 22:58:44 +05:30
|
|
|
StringBuilder selected = new StringBuilder("");
|
|
|
|
|
for (int i = 0; i < integers.length - 1; i++) {
|
|
|
|
|
selected.append(integers[i]).append(",");
|
|
|
|
|
}
|
|
|
|
|
if (integers.length > 0) {
|
|
|
|
|
selected.append(integers[integers.length - 1]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
callback.invoke("itemsCallbackMultiChoice", selected.toString());
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2015-11-01 23:58:58 +05:30
|
|
|
// Provide a 'Clear' button to unselect all choices
|
2015-11-01 22:58:44 +05:30
|
|
|
if (options.hasKey("multiChoiceClearButton") &&
|
|
|
|
|
options.getBoolean("multiChoiceClearButton")) {
|
2016-01-05 19:56:22 -08:00
|
|
|
mBuilder.neutralText("Clear").onNeutral(new MaterialDialog.SingleButtonCallback() {
|
2015-11-01 22:58:44 +05:30
|
|
|
@Override
|
|
|
|
|
public void onClick(MaterialDialog materialDialog, DialogAction dialogAction) {
|
|
|
|
|
materialDialog.clearSelectedIndices();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (options.hasKey("showListener")) {
|
2016-01-05 19:56:22 -08:00
|
|
|
mBuilder.showListener(new DialogInterface.OnShowListener() {
|
2015-11-01 22:58:44 +05:30
|
|
|
@Override
|
|
|
|
|
public void onShow(DialogInterface dialog) {
|
|
|
|
|
callback.invoke("showListener");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (options.hasKey("cancelListener")) {
|
2016-01-05 19:56:22 -08:00
|
|
|
mBuilder.cancelListener(new DialogInterface.OnCancelListener() {
|
2015-11-01 22:58:44 +05:30
|
|
|
@Override
|
|
|
|
|
public void onCancel(DialogInterface dialog) {
|
|
|
|
|
callback.invoke("cancelListener");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (options.hasKey("dismissListener")) {
|
2016-01-05 19:56:22 -08:00
|
|
|
mBuilder.dismissListener(new DialogInterface.OnDismissListener() {
|
2015-11-01 22:58:44 +05:30
|
|
|
@Override
|
|
|
|
|
public void onDismiss(DialogInterface dialog) {
|
|
|
|
|
callback.invoke("dismissListener");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (options.hasKey("input")) {
|
|
|
|
|
ReadableMap input = options.getMap("input");
|
2015-11-01 23:58:58 +05:30
|
|
|
|
|
|
|
|
// Check for hint and prefilled text
|
2015-11-01 22:58:44 +05:30
|
|
|
String hint = input.hasKey("hint") ? input.getString("hint") : null;
|
|
|
|
|
String prefill = input.hasKey("prefill") ? input.getString("prefill") : null;
|
2015-11-01 23:58:58 +05:30
|
|
|
|
|
|
|
|
// Check if empty input is allowed
|
2015-11-01 22:58:44 +05:30
|
|
|
boolean allowEmptyInput = !input.hasKey("allowEmptyInput") ||
|
|
|
|
|
input.getBoolean("allowEmptyInput");
|
|
|
|
|
|
2015-11-01 23:58:58 +05:30
|
|
|
// TODO : Provide pre-selected input types in Javascript
|
2015-11-01 22:58:44 +05:30
|
|
|
if (input.hasKey("type")) {
|
2016-01-05 19:56:22 -08:00
|
|
|
mBuilder.inputType(input.getInt("type"));
|
2015-11-01 22:58:44 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int minLength = input.hasKey("minLength") ? input.getInt("minLength") : 0;
|
|
|
|
|
int maxLength = input.hasKey("maxLength") ? input.getInt("maxLength") : -1;
|
|
|
|
|
|
2016-01-05 19:56:22 -08:00
|
|
|
mBuilder.inputRange(minLength, maxLength);
|
2015-11-01 22:58:44 +05:30
|
|
|
|
2016-01-05 19:56:22 -08:00
|
|
|
mBuilder.input(hint, prefill, allowEmptyInput, new MaterialDialog.InputCallback() {
|
2015-11-01 22:58:44 +05:30
|
|
|
@Override
|
|
|
|
|
public void onInput(MaterialDialog materialDialog, CharSequence charSequence) {
|
|
|
|
|
callback.invoke("input", charSequence.toString());
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-05 19:56:22 -08:00
|
|
|
if(mDialog != null)
|
|
|
|
|
mDialog.dismiss();
|
|
|
|
|
mDialog = mBuilder.build();
|
|
|
|
|
mDialog.show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ReactMethod
|
|
|
|
|
public void dismiss() {
|
|
|
|
|
if(mDialog != null)
|
|
|
|
|
mDialog.dismiss();
|
2015-11-01 22:58:44 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|