Skip to content

Commit 97ce240

Browse files
ryancatfacebook-github-bot
authored andcommitted
Update RedBoxDialog to separate content view and dialog
Summary: This diff separates the content view creation logic from existing `RedBoxDialog` logic. After the change, `RedBoxDialog` is no longer a subclass of `Dialog`, but behaves like a dialog with forwarding pattern to delegate dialog API to internal member. This will keep the APIs consistent with dependent components. The motivation of the change is to make the content view reusable. This is important in VR surface where we don't have activities and necessary implementations for Dialog to show. Changelog: [Android][Internal] Reviewed By: javache Differential Revision: D34016503 fbshipit-source-id: 261594bda9f6fb2d83764a1e5ec2e9e60d8d39a3
1 parent 99890bf commit 97ce240

File tree

1 file changed

+40
-16
lines changed

1 file changed

+40
-16
lines changed

ReactAndroid/src/main/java/com/facebook/react/devsupport/RedBoxDialog.java

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,15 @@
4242
import org.json.JSONObject;
4343

4444
/** Dialog for displaying JS errors in an eye-catching form (red box). */
45-
/* package */ class RedBoxDialog extends Dialog implements AdapterView.OnItemClickListener {
45+
/* package */ class RedBoxDialog implements AdapterView.OnItemClickListener {
4646

4747
private final DevSupportManager mDevSupportManager;
4848
private final DoubleTapReloadRecognizer mDoubleTapReloadRecognizer;
4949
private final @Nullable RedBoxHandler mRedBoxHandler;
50+
private final View mContentView;
51+
private final Context mContext;
5052

53+
private @Nullable Dialog mDialog;
5154
private ListView mStackView;
5255
private Button mReloadJsButton;
5356
private Button mDismissButton;
@@ -238,28 +241,25 @@ private static JSONObject stackFrameToJson(StackFrame frame) {
238241

239242
protected RedBoxDialog(
240243
Context context, DevSupportManager devSupportManager, @Nullable RedBoxHandler redBoxHandler) {
241-
super(context, R.style.Theme_Catalyst_RedBox);
242-
243-
requestWindowFeature(Window.FEATURE_NO_TITLE);
244-
245-
setContentView(R.layout.redbox_view);
244+
mContext = context;
245+
mContentView = (View) LayoutInflater.from(context).inflate(R.layout.redbox_view, null);
246246

247247
mDevSupportManager = devSupportManager;
248248
mDoubleTapReloadRecognizer = new DoubleTapReloadRecognizer();
249249
mRedBoxHandler = redBoxHandler;
250250

251-
mStackView = (ListView) findViewById(R.id.rn_redbox_stack);
251+
mStackView = (ListView) mContentView.findViewById(R.id.rn_redbox_stack);
252252
mStackView.setOnItemClickListener(this);
253253

254-
mReloadJsButton = (Button) findViewById(R.id.rn_redbox_reload_button);
254+
mReloadJsButton = (Button) mContentView.findViewById(R.id.rn_redbox_reload_button);
255255
mReloadJsButton.setOnClickListener(
256256
new View.OnClickListener() {
257257
@Override
258258
public void onClick(View v) {
259259
mDevSupportManager.handleReloadJS();
260260
}
261261
});
262-
mDismissButton = (Button) findViewById(R.id.rn_redbox_dismiss_button);
262+
mDismissButton = (Button) mContentView.findViewById(R.id.rn_redbox_dismiss_button);
263263
mDismissButton.setOnClickListener(
264264
new View.OnClickListener() {
265265
@Override
@@ -269,12 +269,12 @@ public void onClick(View v) {
269269
});
270270

271271
if (mRedBoxHandler != null && mRedBoxHandler.isReportEnabled()) {
272-
mLoadingIndicator = (ProgressBar) findViewById(R.id.rn_redbox_loading_indicator);
273-
mLineSeparator = (View) findViewById(R.id.rn_redbox_line_separator);
274-
mReportTextView = (TextView) findViewById(R.id.rn_redbox_report_label);
272+
mLoadingIndicator = (ProgressBar) mContentView.findViewById(R.id.rn_redbox_loading_indicator);
273+
mLineSeparator = (View) mContentView.findViewById(R.id.rn_redbox_line_separator);
274+
mReportTextView = (TextView) mContentView.findViewById(R.id.rn_redbox_report_label);
275275
mReportTextView.setMovementMethod(LinkMovementMethod.getInstance());
276276
mReportTextView.setHighlightColor(Color.TRANSPARENT);
277-
mReportButton = (Button) findViewById(R.id.rn_redbox_report_button);
277+
mReportButton = (Button) mContentView.findViewById(R.id.rn_redbox_report_button);
278278
mReportButton.setOnClickListener(mReportButtonOnClickListener);
279279
}
280280
}
@@ -303,15 +303,39 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
303303
AsyncTask.THREAD_POOL_EXECUTOR, (StackFrame) mStackView.getAdapter().getItem(position));
304304
}
305305

306-
@Override
307306
public boolean onKeyUp(int keyCode, KeyEvent event) {
308307
if (keyCode == KeyEvent.KEYCODE_MENU) {
309308
mDevSupportManager.showDevOptionsDialog();
310309
return true;
311310
}
312-
if (mDoubleTapReloadRecognizer.didDoubleTapR(keyCode, getCurrentFocus())) {
311+
if (mDoubleTapReloadRecognizer.didDoubleTapR(keyCode, mDialog.getCurrentFocus())) {
313312
mDevSupportManager.handleReloadJS();
314313
}
315-
return super.onKeyUp(keyCode, event);
314+
return mDialog.onKeyUp(keyCode, event);
315+
}
316+
317+
public Context getContext() {
318+
return mContext;
319+
}
320+
321+
public View getContentView() {
322+
return mContentView;
323+
}
324+
325+
public boolean isShowing() {
326+
return mDialog.isShowing();
327+
}
328+
329+
public void show() {
330+
if (mDialog == null) {
331+
mDialog = new Dialog(mContext, R.style.Theme_Catalyst_RedBox);
332+
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
333+
}
334+
mDialog.setContentView(mContentView);
335+
mDialog.show();
336+
}
337+
338+
public void dismiss() {
339+
mDialog.dismiss();
316340
}
317341
}

0 commit comments

Comments
 (0)