diff --git a/apps/AndroidApp/app/src/main/AndroidManifest.xml b/apps/AndroidApp/app/src/main/AndroidManifest.xml index b7c0735e8..c6f1665df 100644 --- a/apps/AndroidApp/app/src/main/AndroidManifest.xml +++ b/apps/AndroidApp/app/src/main/AndroidManifest.xml @@ -18,6 +18,10 @@ android:name=".ReferralsActivity" android:exported="false" android:theme="@style/Theme.AndroidBrownfieldApp" /> + promise.resolve(true) } - .setNegativeButton("Cancel") { _, _ -> promise.resolve(false) } - .setCancelable(false) - .show() - } - } - - override fun showNativeBanner(message: String, onDismiss: Callback) { - runOnUiThread { - AlertDialog.Builder(this) - .setMessage(message) - .setPositiveButton("Dismiss") { _, _ -> onDismiss.invoke() } - .setCancelable(false) - .show() - } - } } @Composable @@ -177,20 +119,28 @@ private fun MainScreen(modifier: Modifier = Modifier) { name = ReactNativeConstants.APP_NAME, ) + EspressoTagAnchor(tag = E2eTestIds.nativeAppOpenReactNative) { + Button( + onClick = { + activity?.let { + it.startActivity( + Intent(it, ReactNativeActivity::class.java) + // Forward the Detox flag so the RN host keeps E2E behaviour. + .putExtra("DetoxE2E", it.intent?.getStringExtra("DetoxE2E")) + ) + } + } + ) { + Text("Open React Native") + } + } + PostMessageCard( onMessageReceived = { message -> postMessageToastText = message }, textInputEnabled = !isDetoxE2E, ) - Spacer(modifier = Modifier.height(1.dp)) - - ReactNativeView( - modifier = Modifier - .fillMaxWidth() - .height(520.dp) - .clip(RoundedCornerShape(16.dp)) - .background(MaterialTheme.colorScheme.surface) - ) + Spacer(modifier = Modifier.height(16.dp)) } postMessageToastText?.let { message -> @@ -202,58 +152,6 @@ private fun MainScreen(modifier: Modifier = Modifier) { } } -@Composable -fun ReactNativeView( - modifier: Modifier = Modifier -) { - val activity = LocalContext.current as? Activity - val brownfieldE2E = remember(activity) { - activity?.intent?.getStringExtra("DetoxE2E") == "YES" - } - - AndroidFragment( - modifier = modifier, - arguments = Bundle().apply { - putString( - ReactNativeFragmentArgNames.ARG_MODULE_NAME, - ReactNativeConstants.MAIN_MODULE_NAME - ) - putBoolean( - ReactNativeFragmentArgNames.ARG_WAIT_FOR_FULL_DISPLAY, - ReactNativeConstants.WAIT_FOR_FULL_DISPLAY - ) - putBoolean( - ReactNativeFragmentArgNames.ARG_COLLECT_THREAD_METRICS, - true - ) - putBundle( - ReactNativeFragmentArgNames.ARG_LAUNCH_OPTIONS, - Bundle().apply { - putString( - "nativeOsVersionLabel", - "Android ${Build.VERSION.RELEASE}" - ) - putBoolean("brownfieldE2E", brownfieldE2E) - } - ) - }, - onUpdate = { fragment -> - fragment.onMetrics = OnDisplayMetrics(BrownfieldMetricsLogger::logDisplay) - } - ) -} - -private class ReactNativeFragmentFactory : FragmentFactory() { - override fun instantiate(classLoader: ClassLoader, className: String): Fragment { - if (className == ReactNativeFragment::class.java.name) { - return ReactNativeFragment().apply { - onMetrics = OnDisplayMetrics(BrownfieldMetricsLogger::logDisplay) - } - } - return super.instantiate(classLoader, className) - } -} - @Preview(showBackground = true) @Composable fun GreetingPreview() { diff --git a/apps/AndroidApp/app/src/main/java/com/callstack/brownfield/android/example/ReactNativeActivity.kt b/apps/AndroidApp/app/src/main/java/com/callstack/brownfield/android/example/ReactNativeActivity.kt new file mode 100644 index 000000000..d942ef86b --- /dev/null +++ b/apps/AndroidApp/app/src/main/java/com/callstack/brownfield/android/example/ReactNativeActivity.kt @@ -0,0 +1,171 @@ +package com.callstack.brownfield.android.example + +import android.app.Activity +import android.content.Intent +import android.content.res.Configuration +import android.os.Build +import android.os.Bundle +import androidx.activity.compose.setContent +import androidx.activity.enableEdgeToEdge +import androidx.appcompat.app.AlertDialog +import androidx.appcompat.app.AppCompatActivity +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.navigationBarsPadding +import androidx.compose.runtime.Composable +import androidx.compose.runtime.remember +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.semantics.semantics +import androidx.compose.ui.semantics.testTagsAsResourceId +import androidx.fragment.app.Fragment +import androidx.fragment.app.FragmentFactory +import androidx.fragment.compose.AndroidFragment +import com.callstack.brownfield.android.example.ui.theme.AndroidBrownfieldAppTheme +import com.callstack.nativebrownfieldnavigation.BrownfieldNavigationDelegate +import com.callstack.nativebrownfieldnavigation.BrownfieldNavigationManager +import com.callstack.nativebrownfieldnavigation.UserType +import com.callstack.reactnativebrownfield.OnDisplayMetrics +import com.callstack.reactnativebrownfield.ReactNativeFragment +import com.callstack.reactnativebrownfield.constants.ReactNativeFragmentArgNames +import com.facebook.react.bridge.Callback +import com.facebook.react.bridge.Promise + +/** + * Full-screen React Native host, pushed from [MainActivity] (mirrors RNAppScreen on iOS). + * Each launch creates a new fragment, so every presentation gets its own display metrics. + */ +class ReactNativeActivity : AppCompatActivity(), BrownfieldNavigationDelegate { + private val isDetoxE2E: Boolean + get() = intent?.getStringExtra("DetoxE2E") == "YES" + + override fun onConfigurationChanged(newConfig: Configuration) { + super.onConfigurationChanged(newConfig) + + ReactNativeHostManager.onConfigurationChanged(application, newConfig) + } + + override fun onResume() { + super.onResume() + // Own Brownfield navigation only while this activity is foregrounded. + BrownfieldNavigationManager.setDelegate(this) + if (isDetoxE2E) { + window.decorView.post { window.decorView.requestFocus() } + } + } + + override fun onPause() { + // Release ownership before another host can become the active delegate. + BrownfieldNavigationManager.clearDelegate() + super.onPause() + } + + override fun onWindowFocusChanged(hasFocus: Boolean) { + super.onWindowFocusChanged(hasFocus) + if (!hasFocus && isDetoxE2E) { + window.decorView.post { window.decorView.requestFocus() } + } + } + + override fun onCreate(savedInstanceState: Bundle?) { + supportFragmentManager.fragmentFactory = ReactNativeFragmentFactory() + super.onCreate(null) + enableEdgeToEdge() + + setContent { + AndroidBrownfieldAppTheme { + ReactNativeView( + modifier = Modifier + .fillMaxSize() + .navigationBarsPadding() + .semantics { testTagsAsResourceId = true } + ) + } + } + } + + override fun navigateToSettings(user: UserType) { + startActivity(Intent(this, SettingsActivity::class.java)) + } + + override fun navigateToReferrals(userId: String) { + startActivity( + Intent(this, ReferralsActivity::class.java).putExtra( + ReferralsActivity.EXTRA_USER_ID, + userId + ) + ) + } + + override fun requestNativeConfirmation(title: String, promise: Promise) { + runOnUiThread { + AlertDialog.Builder(this) + .setTitle(title) + .setPositiveButton("OK") { _, _ -> promise.resolve(true) } + .setNegativeButton("Cancel") { _, _ -> promise.resolve(false) } + .setCancelable(false) + .show() + } + } + + override fun showNativeBanner(message: String, onDismiss: Callback) { + runOnUiThread { + AlertDialog.Builder(this) + .setMessage(message) + .setPositiveButton("Dismiss") { _, _ -> onDismiss.invoke() } + .setCancelable(false) + .show() + } + } +} + +@Composable +private fun ReactNativeView( + modifier: Modifier = Modifier +) { + val activity = LocalContext.current as? Activity + val brownfieldE2E = remember(activity) { + activity?.intent?.getStringExtra("DetoxE2E") == "YES" + } + + AndroidFragment( + modifier = modifier, + arguments = Bundle().apply { + putString( + ReactNativeFragmentArgNames.ARG_MODULE_NAME, + ReactNativeConstants.MAIN_MODULE_NAME + ) + putBoolean( + ReactNativeFragmentArgNames.ARG_WAIT_FOR_FULL_DISPLAY, + ReactNativeConstants.WAIT_FOR_FULL_DISPLAY + ) + putBoolean( + ReactNativeFragmentArgNames.ARG_COLLECT_THREAD_METRICS, + true + ) + putBundle( + ReactNativeFragmentArgNames.ARG_LAUNCH_OPTIONS, + Bundle().apply { + putString( + "nativeOsVersionLabel", + "Android ${Build.VERSION.RELEASE}" + ) + putBoolean("brownfieldE2E", brownfieldE2E) + } + ) + }, + onUpdate = { fragment -> + fragment.onMetrics = OnDisplayMetrics(BrownfieldMetricsLogger::logDisplay) + } + ) +} + +private class ReactNativeFragmentFactory : FragmentFactory() { + override fun instantiate(classLoader: ClassLoader, className: String): Fragment { + if (className == ReactNativeFragment::class.java.name) { + return ReactNativeFragment().apply { + onMetrics = OnDisplayMetrics(BrownfieldMetricsLogger::logDisplay) + } + } + return super.instantiate(classLoader, className) + } +} diff --git a/apps/AppleApp/Brownfield Apple App/E2eTestIds.swift b/apps/AppleApp/Brownfield Apple App/E2eTestIds.swift index c4fe83d8b..bcccc7f2b 100644 --- a/apps/AppleApp/Brownfield Apple App/E2eTestIds.swift +++ b/apps/AppleApp/Brownfield Apple App/E2eTestIds.swift @@ -3,6 +3,7 @@ import Foundation /// Keep in sync with `@callstack/brownfield-example-shared-tests` `e2eTestIds`. enum E2eTestIds { static let appleAppGreeting = "brownfield-e2e-appleapp-greeting" + static let appleAppOpenReactNative = "brownfield-e2e-appleapp-open-react-native" static let appleAppPostMessageSend = "brownfield-e2e-appleapp-post-message-send" static let appleAppPostMessageToast = "brownfield-e2e-appleapp-post-message-toast" static let appleAppNativeSettings = "brownfield-e2e-appleapp-native-settings" diff --git a/apps/AppleApp/Brownfield Apple App/components/ContentView.swift b/apps/AppleApp/Brownfield Apple App/components/ContentView.swift index c9cdefb14..4ab83153c 100644 --- a/apps/AppleApp/Brownfield Apple App/components/ContentView.swift +++ b/apps/AppleApp/Brownfield Apple App/components/ContentView.swift @@ -35,8 +35,9 @@ struct ContentView: View { @State private var postMessageToastText = "" var body: some View { - NavigationView { - ZStack { + // Toast sits above NavigationView so RN postMessage stays visible on the pushed RN screen. + ZStack { + NavigationView { ScrollView { VStack(spacing: 16) { GreetingCard(name: hostAppName) @@ -45,19 +46,20 @@ struct ContentView: View { Text("Open React Native") } .buttonStyle(.borderedProminent) + .accessibilityIdentifier(E2eTestIds.appleAppOpenReactNative) MessagesView() } .frame(maxWidth: .infinity) .padding(16) } + } - if showPostMessageToast { - Toast( - message: postMessageToastText, - isShowing: $showPostMessageToast - ) - } + if showPostMessageToast { + Toast( + message: postMessageToastText, + isShowing: $showPostMessageToast + ) } } .onAppear { diff --git a/apps/ExpoApp56/RNApp.tsx b/apps/ExpoApp56/RNApp.tsx index 559c3afc0..505052d58 100644 --- a/apps/ExpoApp56/RNApp.tsx +++ b/apps/ExpoApp56/RNApp.tsx @@ -4,12 +4,19 @@ import BrownfieldNavigation from '@callstack/brownfield-navigation'; import { checkAndFetchUpdate } from './src/utils/expo-rn-updates'; import Counter from './src/components/counter'; +import { useMarkFullyDisplayed } from './src/hooks/use-mark-fully-displayed'; type RNAppProps = { nativeOsVersionLabel?: string; + brownfieldPresentationID?: string; }; -export default function RNApp({ nativeOsVersionLabel }: RNAppProps) { +export default function RNApp({ + nativeOsVersionLabel, + brownfieldPresentationID, +}: RNAppProps) { + useMarkFullyDisplayed(brownfieldPresentationID); + const requestNativeConfirmation = async () => { try { await BrownfieldNavigation.requestNativeConfirmation( @@ -57,15 +64,12 @@ export default function RNApp({ nativeOsVersionLabel }: RNAppProps) { title="Navigate to Referrals" onPress={() => BrownfieldNavigation.navigateToReferrals('123')} /> -