Skip to content

Commit

Permalink
Migration RN Alert Dialog to androidx (#44494)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #44494

Pull Request resolved: #44880

Migrates the `AlertFragment` from `android.app.AlertDialog` to `androidx.appcompat.app.AlertDialog`. This backports tons of fixes that have gone into the AlertDialog component over the years, including proper line wrapping of button text, alignment of buttons, etc.

## For consideration
- Alert dialog themes may no longer need the `android` namespace, meaning themes can now be specified as `alertDialogTheme` rather than `android:alertDialogTheme`.
- This change requires all implementing activities to have a theme that inherits from `Theme.AppCompat`. Creation of any activities which do not have a descendant of this style will result in an `IllegalStateException`: https://www.internalfb.com/intern/signalinfra/exception_owners/?mid=5ee93f6ecd59f3d8ad82a78c213ea016&result_id=16044073705339118.281475102518721.1715097866

## Changelog:

[Android] [Changed] - Migrated `AlertFragment` dialog builder to use `androidx.appcompat`

Reviewed By: zeyap

Differential Revision: D57019423

fbshipit-source-id: 84d8f69d896d32e72434149c0e31735d358370a9
  • Loading branch information
Peter Abbondanzo authored and facebook-github-bot committed Jun 12, 2024
1 parent ae7d543 commit 600d3f6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
package com.facebook.react.modules.dialog;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
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 @@ -7,10 +7,11 @@

package com.facebook.react.modules.dialog

import android.app.AlertDialog
import android.content.DialogInterface
import android.os.Looper.getMainLooper
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.FragmentActivity
import com.facebook.react.R
import com.facebook.react.bridge.Callback
import com.facebook.react.bridge.JavaOnlyMap
import com.facebook.react.bridge.ReactApplicationContext
Expand Down Expand Up @@ -48,6 +49,9 @@ class DialogModuleTest {
fun setUp() {
activityController = Robolectric.buildActivity(FragmentActivity::class.java)
activity = activityController.create().start().resume().get()
// We must set the theme to a descendant of AppCompat for the AlertDialog to show without
// raising an exception
activity.setTheme(APP_COMPAT_THEME)

val context: ReactApplicationContext = mock(ReactApplicationContext::class.java)
whenever(context.hasActiveReactInstance()).thenReturn(true)
Expand All @@ -62,6 +66,19 @@ class DialogModuleTest {
activityController.pause().stop().destroy()
}

@Test
fun testIllegalActivityTheme() {
val options = JavaOnlyMap()
activity.setTheme(NON_APP_COMPAT_THEME)

assertThrows(NullPointerException::class.java) {
dialogModule.showAlert(options, null, null)
shadowOf(getMainLooper()).idle()
}

activity.setTheme(APP_COMPAT_THEME)
}

@Test
fun testAllOptions() {
val options =
Expand Down Expand Up @@ -158,4 +175,9 @@ class DialogModuleTest {
return activity.supportFragmentManager.findFragmentByTag(DialogModule.FRAGMENT_TAG)
as? AlertFragment
}

companion object {
private val APP_COMPAT_THEME: Int = R.style.Theme_ReactNative_AppCompat_Light
private val NON_APP_COMPAT_THEME: Int = android.R.style.Theme_DeviceDefault_Light
}
}

0 comments on commit 600d3f6

Please sign in to comment.