Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DialogModule supports only FragmentActivity #23365

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
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;
import androidx.fragment.app.DialogFragment;

/**
* A fragment used to display the dialog.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import android.content.DialogInterface.OnDismissListener;
import android.os.Bundle;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;

import com.facebook.common.logging.FLog;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.LifecycleEventListener;
Expand All @@ -26,6 +28,8 @@
import com.facebook.react.common.MapBuilder;
import com.facebook.react.module.annotations.ReactModule;
import java.util.Map;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

@ReactModule(name = DialogModule.NAME)
Expand Down Expand Up @@ -60,35 +64,17 @@ public DialogModule(ReactApplicationContext reactContext) {
}

@Override
public String getName() {
public @Nonnull String getName() {
return NAME;
}

/**
* Helper to allow this module to work with both the standard FragmentManager
* and the Support FragmentManager (for apps that need to use it for legacy reasons).
* Since the two APIs don't share a common interface there's unfortunately some
* code duplication.
*/
private class FragmentManagerHelper {

// Exactly one of the two is null
private final @Nullable android.app.FragmentManager mFragmentManager;
private final @Nullable androidx.fragment.app.FragmentManager mSupportFragmentManager;
private final @Nonnull FragmentManager mFragmentManager;

private @Nullable Object mFragmentToShow;

private boolean isUsingSupportLibrary() {
return mSupportFragmentManager != null;
}

public FragmentManagerHelper(androidx.fragment.app.FragmentManager supportFragmentManager) {
mFragmentManager = null;
mSupportFragmentManager = supportFragmentManager;
}
public FragmentManagerHelper(android.app.FragmentManager fragmentManager) {
public FragmentManagerHelper(@Nonnull FragmentManager fragmentManager) {
mFragmentManager = fragmentManager;
mSupportFragmentManager = null;
}

public void showPendingAlert() {
Expand All @@ -99,30 +85,18 @@ public void showPendingAlert() {
}

dismissExisting();
if (isUsingSupportLibrary()) {
((SupportAlertFragment) mFragmentToShow).show(mSupportFragmentManager, FRAGMENT_TAG);
} else {
((AlertFragment) mFragmentToShow).show(mFragmentManager, FRAGMENT_TAG);
}
((AlertFragment) mFragmentToShow).show(mFragmentManager, FRAGMENT_TAG);
mFragmentToShow = null;
}

private void dismissExisting() {
if (!mIsInForeground) {
return;
}
if (isUsingSupportLibrary()) {
SupportAlertFragment oldFragment =
(SupportAlertFragment) mSupportFragmentManager.findFragmentByTag(FRAGMENT_TAG);
if (oldFragment != null && oldFragment.isResumed()) {
oldFragment.dismiss();
}
} else {
AlertFragment oldFragment =
(AlertFragment) mFragmentManager.findFragmentByTag(FRAGMENT_TAG);
if (oldFragment != null && oldFragment.isResumed()) {
oldFragment.dismiss();
}
AlertFragment oldFragment =
(AlertFragment) mFragmentManager.findFragmentByTag(FRAGMENT_TAG);
if (oldFragment != null && oldFragment.isResumed()) {
oldFragment.dismiss();
}
}

Expand All @@ -134,26 +108,14 @@ public void showNewAlert(Bundle arguments, Callback actionCallback) {
AlertFragmentListener actionListener =
actionCallback != null ? new AlertFragmentListener(actionCallback) : null;

if (isUsingSupportLibrary()) {
SupportAlertFragment alertFragment = new SupportAlertFragment(actionListener, arguments);
if (mIsInForeground && !mSupportFragmentManager.isStateSaved()) {
if (arguments.containsKey(KEY_CANCELABLE)) {
alertFragment.setCancelable(arguments.getBoolean(KEY_CANCELABLE));
}
alertFragment.show(mSupportFragmentManager, FRAGMENT_TAG);
} else {
mFragmentToShow = alertFragment;
AlertFragment alertFragment = new AlertFragment(actionListener, arguments);
if (mIsInForeground && !mFragmentManager.isStateSaved()) {
if (arguments.containsKey(KEY_CANCELABLE)) {
alertFragment.setCancelable(arguments.getBoolean(KEY_CANCELABLE));
}
alertFragment.show(mFragmentManager, FRAGMENT_TAG);
} else {
AlertFragment alertFragment = new AlertFragment(actionListener, arguments);
if (mIsInForeground) {
if (arguments.containsKey(KEY_CANCELABLE)) {
alertFragment.setCancelable(arguments.getBoolean(KEY_CANCELABLE));
}
alertFragment.show(mFragmentManager, FRAGMENT_TAG);
} else {
mFragmentToShow = alertFragment;
}
mFragmentToShow = alertFragment;
}
}
}
Expand Down Expand Up @@ -269,8 +231,8 @@ public void run() {
}

/**
* Creates a new helper to work with either the FragmentManager or the legacy support
* FragmentManager transparently. Returns null if we're not attached to an Activity.
* Creates a new helper to work with FragmentManager.
* Returns null if we're not attached to an Activity.
*
* DO NOT HOLD LONG-LIVED REFERENCES TO THE OBJECT RETURNED BY THIS METHOD, AS THIS WILL CAUSE
* MEMORY LEAKS.
Expand All @@ -280,10 +242,6 @@ public void run() {
if (activity == null) {
return null;
}
if (activity instanceof FragmentActivity) {
return new FragmentManagerHelper(((FragmentActivity) activity).getSupportFragmentManager());
} else {
return new FragmentManagerHelper(activity.getFragmentManager());
}
return new FragmentManagerHelper(((FragmentActivity) activity).getSupportFragmentManager());
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.app.Activity;


import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
Expand All @@ -25,15 +25,18 @@
import org.robolectric.RobolectricTestRunner;
import org.robolectric.util.ActivityController;

import androidx.fragment.app.FragmentActivity;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;

@RunWith(RobolectricTestRunner.class)
@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "androidx.*", "android.*"})
public class DialogModuleTest {

private ActivityController<Activity> mActivityController;
private Activity mActivity;
private ActivityController<FragmentActivity> mActivityController;
private FragmentActivity mActivity;
private DialogModule mDialogModule;

final static class SimpleCallback implements Callback {
Expand All @@ -57,7 +60,7 @@ public Object[] getArgs() {

@Before
public void setUp() throws Exception {
mActivityController = Robolectric.buildActivity(Activity.class);
mActivityController = Robolectric.buildActivity(FragmentActivity.class);
mActivity = mActivityController
.create()
.start()
Expand Down Expand Up @@ -94,7 +97,7 @@ public void testAllOptions() {

final AlertFragment fragment = getFragment();
assertNotNull("Fragment was not displayed", fragment);
assertEquals(false, fragment.isCancelable());
assertFalse(fragment.isCancelable());

final AlertDialog dialog = (AlertDialog) fragment.getDialog();
assertEquals("OK", dialog.getButton(DialogInterface.BUTTON_POSITIVE).getText().toString());
Expand Down Expand Up @@ -164,7 +167,7 @@ public void testCallbackDismiss() {
}

private AlertFragment getFragment() {
return (AlertFragment) mActivity.getFragmentManager()
return (AlertFragment) mActivity.getSupportFragmentManager()
.findFragmentByTag(DialogModule.FRAGMENT_TAG);
}
}