|
| 1 | +# Brownfield Navigation Native Integration |
| 2 | + |
| 3 | +## Discoverability triggers |
| 4 | + |
| 5 | +- "implement `BrownfieldNavigationDelegate`" |
| 6 | +- "where to call `BrownfieldNavigationManager.setDelegate(...)`" |
| 7 | +- "where to call `BrownfieldNavigationManager.shared.setDelegate(...)`" |
| 8 | +- "navigation call crashes at app startup or upon invocation" |
| 9 | + |
| 10 | +## Scope |
| 11 | + |
| 12 | +In scope: |
| 13 | +- Implementing generated `BrownfieldNavigationDelegate` methods in Android and iOS host code. |
| 14 | +- Registering the delegate with `BrownfieldNavigationManager` during app startup. |
| 15 | +- Enforcing startup/lifecycle ordering (delegate registered before JS calls). |
| 16 | +- Troubleshooting native wiring issues (crash/no-op/wrong route). |
| 17 | + |
| 18 | +Out of scope: |
| 19 | +- Authoring `brownfield.navigation.ts` and running codegen. For that, read [`setup-codegen.md`](setup-codegen.md) in this folder. |
| 20 | +- JavaScript call-site usage patterns in RN screens. For that, read [`javascript-usage.md`](javascript-usage.md) in this folder. |
| 21 | + |
| 22 | +## Procedure |
| 23 | + |
| 24 | +1. Confirm prerequisites |
| 25 | + - `BrownfieldNavigation.xcframework` is linked in the iOS host app. |
| 26 | + - If applicable, use the artifact produced by `npx brownfield package:ios` in the current project. The exact output path can vary by package manager and workspace layout. |
| 27 | + - `Gson` dependency is added to the Android host app. |
| 28 | + |
| 29 | +2. Implement Android delegate |
| 30 | + - Implement generated `BrownfieldNavigationDelegate` in the host `Activity` (or class with navigation context). |
| 31 | + - Wire each generated method to the native destination and map params exactly. |
| 32 | + - Typical implementation starts Android `Activity` instances with `Intent` extras. |
| 33 | + |
| 34 | +3. Register Android delegate during startup |
| 35 | + - Call `BrownfieldNavigationManager.setDelegate(...)` in startup flow (for example `onCreate`). |
| 36 | + - Registration must happen before any React Native screen can call `BrownfieldNavigation.*`. |
| 37 | + |
| 38 | +4. Implement iOS delegate |
| 39 | + - Create a class conforming to `BrownfieldNavigationDelegate`. |
| 40 | + - Wire each generated method to the intended native presentation flow (UIKit/SwiftUI). |
| 41 | + - Ensure screen presentation runs on the main/UI thread. |
| 42 | + |
| 43 | +5. Register iOS delegate during startup |
| 44 | + - Call `BrownfieldNavigationManager.shared.setDelegate(navigationDelegate: ...)` at app startup (for example in app `init`). |
| 45 | + - Registration must happen before RN-rendered routes can invoke module methods. |
| 46 | + |
| 47 | +6. Enforce lifecycle requirements |
| 48 | + - Delegate must be present before JS usage; missing delegate is a startup bug. |
| 49 | + - Re-register if the host object owning the delegate is recreated. |
| 50 | + - Keep navigation/presentation work on main thread. |
| 51 | + |
| 52 | +7. Triage runtime integration failures |
| 53 | + - Method added/changed in TS but missing natively: rerun `npx brownfield navigation:codegen` and rebuild. |
| 54 | + - Crash on launch or first method call: verify delegate registration order vs RN route rendering. |
| 55 | + - Method exists but wrong destination/no-op: verify delegate implementation wiring and parameter mapping. |
| 56 | + |
| 57 | +## Minimal native examples |
| 58 | + |
| 59 | +Assume the generated contract includes a method like `openNativeProfile(userId: string): void`. The generated delegate method name and parameter types come from the current project's `brownfield.navigation.ts`. |
| 60 | + |
| 61 | +### Kotlin example |
| 62 | + |
| 63 | +Use this pattern when the host screen or activity owns Android navigation context: |
| 64 | + |
| 65 | +```kotlin |
| 66 | +import android.content.Intent |
| 67 | +import android.os.Bundle |
| 68 | +import androidx.appcompat.app.AppCompatActivity |
| 69 | +import com.callstack.nativebrownfieldnavigation.BrownfieldNavigationDelegate |
| 70 | +import com.callstack.nativebrownfieldnavigation.BrownfieldNavigationManager |
| 71 | + |
| 72 | +class MainActivity : AppCompatActivity(), BrownfieldNavigationDelegate { |
| 73 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 74 | + super.onCreate(savedInstanceState) |
| 75 | + BrownfieldNavigationManager.setDelegate(this) |
| 76 | + } |
| 77 | + |
| 78 | + override fun openNativeProfile(userId: String) { |
| 79 | + val intent = Intent(this, ProfileActivity::class.java).apply { |
| 80 | + putExtra("userId", userId) |
| 81 | + } |
| 82 | + startActivity(intent) |
| 83 | + } |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +Portable takeaways: |
| 88 | +- Implement the generated `BrownfieldNavigationDelegate`. |
| 89 | +- Register the delegate before any React Native code can call `BrownfieldNavigation.*`. |
| 90 | +- Map each generated method to an explicit native destination and pass params through unchanged. |
| 91 | + |
| 92 | +### Swift example |
| 93 | + |
| 94 | +Use this pattern when an app-level object can own navigation setup and present UIKit or SwiftUI flows: |
| 95 | + |
| 96 | +```swift |
| 97 | +import BrownfieldNavigation |
| 98 | +import UIKit |
| 99 | + |
| 100 | +final class AppNavigationDelegate: BrownfieldNavigationDelegate { |
| 101 | + func openNativeProfile(userId: String) { |
| 102 | + DispatchQueue.main.async { |
| 103 | + guard let rootViewController = UIApplication.shared.connectedScenes |
| 104 | + .compactMap({ $0 as? UIWindowScene }) |
| 105 | + .flatMap(\.windows) |
| 106 | + .first(where: \.isKeyWindow)? |
| 107 | + .rootViewController else { |
| 108 | + return |
| 109 | + } |
| 110 | + |
| 111 | + let viewController = ProfileViewController(userId: userId) |
| 112 | + rootViewController.present(viewController, animated: true) |
| 113 | + } |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +final class AppDelegate: UIResponder, UIApplicationDelegate { |
| 118 | + private let navigationDelegate = AppNavigationDelegate() |
| 119 | + |
| 120 | + func application( |
| 121 | + _ application: UIApplication, |
| 122 | + didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil |
| 123 | + ) -> Bool { |
| 124 | + BrownfieldNavigationManager.shared.setDelegate(navigationDelegate: navigationDelegate) |
| 125 | + return true |
| 126 | + } |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +Portable takeaways: |
| 131 | +- Keep a strong reference to the delegate for as long as React Native might call it. |
| 132 | +- Register the delegate during startup, before RN screens that use Brownfield navigation are shown. |
| 133 | +- Perform presentation on the main thread and keep the implementation focused on routing plus param mapping. |
| 134 | + |
| 135 | +## Quick reference |
| 136 | + |
| 137 | +- Android delegate type: `BrownfieldNavigationDelegate` |
| 138 | +- Android registration: `BrownfieldNavigationManager.setDelegate(...)` |
| 139 | +- iOS delegate type: `BrownfieldNavigationDelegate` |
| 140 | +- iOS registration: `BrownfieldNavigationManager.shared.setDelegate(navigationDelegate: ...)` |
| 141 | +- Integration order: |
| 142 | + 1. Generate/update contract outputs |
| 143 | + 2. Implement delegate methods natively |
| 144 | + 3. Register delegate at startup |
| 145 | + 4. Render RN routes that call `BrownfieldNavigation.*` |
| 146 | +- Error cues this skill should address: |
| 147 | + - Crashes when JS calls navigation methods early |
| 148 | + - Missing delegate registration at startup |
| 149 | + - Wrong native screen opened from a JS call |
0 commit comments