forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
An essential revert of '243070a#diff-8e3b74ad18aa49187acd4240321bc9b9'…
… to bring back support for showing dialogs over standard Android activities (#222)
- Loading branch information
1 parent
a978d6b
commit e91e19f
Showing
2 changed files
with
158 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
ReactAndroid/src/main/java/com/facebook/react/modules/dialog/PlatformAlertFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
package com.facebook.react.modules.dialog; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import android.annotation.SuppressLint; | ||
import android.app.AlertDialog; | ||
import android.app.Dialog; | ||
import android.app.DialogFragment; | ||
import android.content.DialogInterface; | ||
import android.content.Context; | ||
import android.os.Bundle; | ||
|
||
/** | ||
* A fragment used to display the dialog based on the platform DialogFragment control. Please note that the usage of this control is deprecated in API v28 in favor of the newer Jetpack/Support controls. | ||
*/ | ||
public class PlatformAlertFragment extends DialogFragment implements DialogInterface.OnClickListener { | ||
/* package */ static final String ARG_TITLE = "title"; | ||
/* package */ static final String ARG_MESSAGE = "message"; | ||
/* package */ static final String ARG_BUTTON_POSITIVE = "button_positive"; | ||
/* package */ static final String ARG_BUTTON_NEGATIVE = "button_negative"; | ||
/* package */ static final String ARG_BUTTON_NEUTRAL = "button_neutral"; | ||
/* package */ static final String ARG_ITEMS = "items"; | ||
|
||
private final @Nullable DialogModule.AlertFragmentListener mListener; | ||
|
||
public PlatformAlertFragment() { | ||
mListener = null; | ||
} | ||
|
||
@SuppressLint("ValidFragment") | ||
public PlatformAlertFragment(@Nullable DialogModule.AlertFragmentListener listener, Bundle arguments) { | ||
mListener = listener; | ||
setArguments(arguments); | ||
} | ||
|
||
|
||
public static Dialog createDialog( | ||
Context activityContext, Bundle arguments, DialogInterface.OnClickListener fragment) { | ||
AlertDialog.Builder builder = new AlertDialog.Builder(activityContext) | ||
.setTitle(arguments.getString(ARG_TITLE)); | ||
|
||
if (arguments.containsKey(ARG_BUTTON_POSITIVE)) { | ||
builder.setPositiveButton(arguments.getString(ARG_BUTTON_POSITIVE), fragment); | ||
} | ||
|
||
if (arguments.containsKey(ARG_BUTTON_NEGATIVE)) { | ||
builder.setNegativeButton(arguments.getString(ARG_BUTTON_NEGATIVE), fragment); | ||
} | ||
|
||
if (arguments.containsKey(ARG_BUTTON_NEUTRAL)) { | ||
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(); | ||
} | ||
|
||
@Override | ||
public Dialog onCreateDialog(Bundle savedInstanceState) { | ||
return createDialog(getActivity(), getArguments(), this); | ||
} | ||
|
||
@Override | ||
public void onClick(DialogInterface dialog, int which) { | ||
if (mListener != null) { | ||
mListener.onClick(dialog, which); | ||
} | ||
} | ||
|
||
@Override | ||
public void onDismiss(DialogInterface dialog) { | ||
super.onDismiss(dialog); | ||
if (mListener != null) { | ||
mListener.onDismiss(dialog); | ||
} | ||
} | ||
} |