Added .setItems() integration for Android AlertDialog

Reviewed By: dmmiller

Differential Revision: D2892199

fb-gh-sync-id: d052313a488d9dfa0ab23f76ea0a96a77260d6c2
This commit is contained in:
Oleg Lokhvitsky 2016-02-05 15:13:32 -08:00 committed by facebook-github-bot-6
parent 995b66db82
commit 4fd115ffa2
2 changed files with 20 additions and 2 deletions

View File

@ -28,6 +28,7 @@ import android.os.Bundle;
/* package */ static final String ARG_BUTTON_POSITIVE = "button_positive"; /* package */ static final String ARG_BUTTON_POSITIVE = "button_positive";
/* package */ static final String ARG_BUTTON_NEGATIVE = "button_negative"; /* package */ static final String ARG_BUTTON_NEGATIVE = "button_negative";
/* package */ static final String ARG_BUTTON_NEUTRAL = "button_neutral"; /* package */ static final String ARG_BUTTON_NEUTRAL = "button_neutral";
/* package */ static final String ARG_ITEMS = "items";
private final @Nullable DialogModule.AlertFragmentListener mListener; private final @Nullable DialogModule.AlertFragmentListener mListener;
@ -39,8 +40,7 @@ import android.os.Bundle;
public static Dialog createDialog( public static Dialog createDialog(
Context activityContext, Bundle arguments, DialogInterface.OnClickListener fragment) { Context activityContext, Bundle arguments, DialogInterface.OnClickListener fragment) {
AlertDialog.Builder builder = new AlertDialog.Builder(activityContext) AlertDialog.Builder builder = new AlertDialog.Builder(activityContext)
.setTitle(arguments.getString(ARG_TITLE)) .setTitle(arguments.getString(ARG_TITLE));
.setMessage(arguments.getString(ARG_MESSAGE));
if (arguments.containsKey(ARG_BUTTON_POSITIVE)) { if (arguments.containsKey(ARG_BUTTON_POSITIVE)) {
builder.setPositiveButton(arguments.getString(ARG_BUTTON_POSITIVE), fragment); builder.setPositiveButton(arguments.getString(ARG_BUTTON_POSITIVE), fragment);
@ -51,6 +51,14 @@ import android.os.Bundle;
if (arguments.containsKey(ARG_BUTTON_NEUTRAL)) { if (arguments.containsKey(ARG_BUTTON_NEUTRAL)) {
builder.setNeutralButton(arguments.getString(ARG_BUTTON_NEUTRAL), fragment); builder.setNeutralButton(arguments.getString(ARG_BUTTON_NEUTRAL), fragment);
} }
// if both message and items are set, Android will only show the message
// and ignore the items argument entirely
if (arguments.containsKey(ARG_MESSAGE)) {
builder.setMessage(arguments.getString(ARG_MESSAGE));
}
if (arguments.containsKey(ARG_ITEMS)) {
builder.setItems(arguments.getCharSequenceArray(ARG_ITEMS), fragment);
}
return builder.create(); return builder.create();
} }

View File

@ -26,6 +26,7 @@ import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod; import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap; import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.common.MapBuilder; import com.facebook.react.common.MapBuilder;
@ -43,6 +44,7 @@ public class DialogModule extends ReactContextBaseJavaModule implements Lifecycl
/* package */ static final String KEY_BUTTON_POSITIVE = "buttonPositive"; /* package */ static final String KEY_BUTTON_POSITIVE = "buttonPositive";
/* package */ static final String KEY_BUTTON_NEGATIVE = "buttonNegative"; /* package */ static final String KEY_BUTTON_NEGATIVE = "buttonNegative";
/* package */ static final String KEY_BUTTON_NEUTRAL = "buttonNeutral"; /* package */ static final String KEY_BUTTON_NEUTRAL = "buttonNeutral";
/* package */ static final String KEY_ITEMS = "items";
/* package */ static final Map<String, Object> CONSTANTS = MapBuilder.<String, Object>of( /* package */ static final Map<String, Object> CONSTANTS = MapBuilder.<String, Object>of(
ACTION_BUTTON_CLICKED, ACTION_BUTTON_CLICKED, ACTION_BUTTON_CLICKED, ACTION_BUTTON_CLICKED,
@ -230,6 +232,14 @@ public class DialogModule extends ReactContextBaseJavaModule implements Lifecycl
if (options.hasKey(KEY_BUTTON_NEUTRAL)) { if (options.hasKey(KEY_BUTTON_NEUTRAL)) {
args.putString(AlertFragment.ARG_BUTTON_NEUTRAL, options.getString(KEY_BUTTON_NEUTRAL)); args.putString(AlertFragment.ARG_BUTTON_NEUTRAL, options.getString(KEY_BUTTON_NEUTRAL));
} }
if (options.hasKey(KEY_ITEMS)) {
ReadableArray items = options.getArray(KEY_ITEMS);
CharSequence[] itemsArray = new CharSequence[items.size()];
for (int i = 0; i < items.size(); i ++) {
itemsArray[i] = items.getString(i);
}
args.putCharSequenceArray(AlertFragment.ARG_ITEMS, itemsArray);
}
fragmentManagerHelper.showNewAlert(mIsInForeground, args, actionCallback); fragmentManagerHelper.showNewAlert(mIsInForeground, args, actionCallback);
} }