hybrid-navigation 5.0.0 → 5.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. package/android/src/main/java/com/navigation/androidx/AwesomeActivity.java +2 -4
  2. package/android/src/main/java/com/navigation/androidx/AwesomeFragment.java +48 -2
  3. package/android/src/main/java/com/navigation/androidx/DefaultTabBarProvider.java +2 -2
  4. package/android/src/main/java/com/navigation/androidx/FragmentHelper.java +7 -0
  5. package/android/src/main/java/com/navigation/androidx/PresentationDelegate.java +2 -4
  6. package/android/src/main/java/com/navigation/androidx/StackFragment.java +3 -0
  7. package/android/src/main/java/com/navigation/androidx/TabBarFragment.java +50 -0
  8. package/android/src/main/java/com/navigation/androidx/TabBarItem.java +4 -0
  9. package/android/src/main/java/com/reactnative/hybridnavigation/DispatchResult.java +16 -4
  10. package/android/src/main/java/com/reactnative/hybridnavigation/GlobalStyle.java +19 -0
  11. package/android/src/main/java/com/reactnative/hybridnavigation/HBDRootView.java +17 -2
  12. package/android/src/main/java/com/reactnative/hybridnavigation/HybridFragment.java +66 -7
  13. package/android/src/main/java/com/reactnative/hybridnavigation/NativeNavigation.java +116 -18
  14. package/android/src/main/java/com/reactnative/hybridnavigation/NavigationCommandQueue.java +8 -0
  15. package/android/src/main/java/com/reactnative/hybridnavigation/ReactFragment.java +376 -8
  16. package/android/src/main/java/com/reactnative/hybridnavigation/ReactManager.java +72 -2
  17. package/android/src/main/java/com/reactnative/hybridnavigation/ReactTabBarFragment.java +26 -0
  18. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/ScreenNavigator.java +134 -12
  19. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/StackNavigator.java +35 -3
  20. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/TabNavigator.java +29 -4
  21. package/dist/NativeEvent.d.ts +15 -0
  22. package/dist/NativeNavigation.d.ts +8 -0
  23. package/dist/Navigation.d.ts +14 -1
  24. package/dist/Navigation.js +153 -12
  25. package/dist/NavigationComponentWrap.d.ts +3 -2
  26. package/dist/Navigator.d.ts +3 -2
  27. package/dist/Navigator.js +28 -9
  28. package/dist/Options.d.ts +12 -2
  29. package/dist/Route.d.ts +70 -5
  30. package/dist/defineRoutes.d.ts +19 -0
  31. package/dist/defineRoutes.js +6 -0
  32. package/dist/handler/DispatchCommandHandler.d.ts +16 -1
  33. package/dist/handler/DispatchCommandHandler.js +63 -5
  34. package/dist/handler/LayoutCommandHandler.d.ts +1 -0
  35. package/dist/handler/LayoutCommandHandler.js +3 -0
  36. package/dist/handler/LeaveGuardHandler.d.ts +17 -0
  37. package/dist/handler/LeaveGuardHandler.js +350 -0
  38. package/dist/handler/LifecycleEventHandler.d.ts +7 -3
  39. package/dist/handler/LifecycleEventHandler.js +36 -3
  40. package/dist/handler/PresentationCoordinator.d.ts +24 -0
  41. package/dist/handler/PresentationCoordinator.js +456 -0
  42. package/dist/handler/ResultEventHandler.d.ts +7 -0
  43. package/dist/handler/ResultEventHandler.js +13 -5
  44. package/dist/handler/TabEventHandler.d.ts +8 -0
  45. package/dist/handler/TabEventHandler.js +25 -0
  46. package/dist/hooks.d.ts +3 -1
  47. package/dist/hooks.js +20 -0
  48. package/dist/index.d.ts +11 -0
  49. package/dist/index.js +3 -0
  50. package/dist/reveal.d.ts +46 -0
  51. package/dist/reveal.js +431 -0
  52. package/dist/routeGraph.d.ts +12 -0
  53. package/dist/routeGraph.js +68 -0
  54. package/dist/router/index.js +5 -41
  55. package/dist/router/tabs.d.ts +1 -0
  56. package/dist/store.d.ts +2 -0
  57. package/dist/store.js +4 -0
  58. package/dist/testing.d.ts +79 -0
  59. package/dist/testing.js +251 -0
  60. package/ios/HBDGlobalStyle.h +3 -0
  61. package/ios/HBDGlobalStyle.mm +13 -0
  62. package/ios/HybridNavigation.h +1 -0
  63. package/ios/Navigator/HBDScreenNavigator.mm +250 -11
  64. package/ios/Navigator/HBDStackNavigator.mm +50 -7
  65. package/ios/Navigator/HBDTabNavigator.mm +33 -5
  66. package/ios/React/HBDNativeNavigation.mm +73 -12
  67. package/ios/React/HBDNavigator.h +8 -0
  68. package/ios/React/HBDOrientationCoordinator.h +2 -0
  69. package/ios/React/HBDOrientationCoordinator.mm +27 -0
  70. package/ios/React/HBDReactBridgeManager.h +10 -0
  71. package/ios/React/HBDReactBridgeManager.mm +48 -2
  72. package/ios/ViewController/HBDNavigationController.mm +14 -0
  73. package/ios/ViewController/HBDReactViewController.h +6 -0
  74. package/ios/ViewController/HBDReactViewController.mm +177 -1
  75. package/ios/ViewController/HBDTabBarController.h +11 -0
  76. package/ios/ViewController/HBDTabBarController.mm +170 -3
  77. package/ios/ViewController/HBDTabBarRenderer.h +28 -0
  78. package/ios/ViewController/HBDViewController.h +1 -0
  79. package/ios/ViewController/HBDViewController.mm +57 -9
  80. package/ios/ViewController/UIViewController+HBD.h +1 -0
  81. package/ios/ViewController/UIViewController+HBD.mm +17 -4
  82. package/package.json +3 -1
  83. package/src/NativeEvent.ts +17 -0
  84. package/src/NativeNavigation.ts +9 -0
  85. package/src/Navigation.ts +228 -12
  86. package/src/NavigationComponentWrap.tsx +11 -2
  87. package/src/Navigator.ts +38 -9
  88. package/src/Options.ts +9 -2
  89. package/src/Route.ts +102 -6
  90. package/src/defineRoutes.ts +46 -0
  91. package/src/handler/DispatchCommandHandler.ts +110 -11
  92. package/src/handler/LayoutCommandHandler.ts +4 -0
  93. package/src/handler/LeaveGuardHandler.ts +436 -0
  94. package/src/handler/LifecycleEventHandler.ts +85 -38
  95. package/src/handler/PresentationCoordinator.ts +566 -0
  96. package/src/handler/ResultEventHandler.ts +23 -9
  97. package/src/handler/TabEventHandler.ts +31 -0
  98. package/src/hooks.ts +37 -1
  99. package/src/index.ts +11 -0
  100. package/src/reveal.ts +565 -0
  101. package/src/routeGraph.ts +87 -0
  102. package/src/router/index.ts +5 -40
  103. package/src/router/tabs.ts +1 -0
  104. package/src/store.ts +5 -0
  105. package/src/testing.ts +395 -0
  106. package/testing.d.ts +1 -0
  107. package/testing.js +3 -0
@@ -0,0 +1,251 @@
1
+ import { useContext } from 'react';
2
+ import { TurboModuleRegistry } from 'react-native';
3
+ const DEFAULT_CONSTANTS = {
4
+ RESULT_OK: -1,
5
+ RESULT_CANCEL: 0,
6
+ RESULT_BLOCK: -2,
7
+ };
8
+ function createInitialState(options = {}) {
9
+ return {
10
+ dispatchCalls: [],
11
+ setResultCalls: [],
12
+ tabItemCalls: [],
13
+ beforeRemoveEnabled: new Set(),
14
+ currentRoute: null,
15
+ routeGraph: [],
16
+ rootGeneration: 0,
17
+ onDispatch: options.onDispatch,
18
+ onSetResult: options.onSetResult,
19
+ };
20
+ }
21
+ const nativeEventListeners = {
22
+ onResult: new Set(),
23
+ willSetRoot: new Set(),
24
+ didSetRoot: new Set(),
25
+ onSceneStateChange: new Set(),
26
+ onBeforeRemoveRequest: new Set(),
27
+ onTabPress: new Set(),
28
+ };
29
+ let activeState = createInitialState();
30
+ let bridgeInstalled = false;
31
+ function subscribe(set, listener) {
32
+ set.add(listener);
33
+ return {
34
+ remove: () => {
35
+ set.delete(listener);
36
+ },
37
+ };
38
+ }
39
+ function emit(set, payload) {
40
+ for (const listener of Array.from(set)) {
41
+ listener(payload);
42
+ }
43
+ }
44
+ export const mockNativeEvent = {
45
+ onResult: listener => subscribe(nativeEventListeners.onResult, listener),
46
+ willSetRoot: listener => subscribe(nativeEventListeners.willSetRoot, listener),
47
+ didSetRoot: listener => subscribe(nativeEventListeners.didSetRoot, listener),
48
+ onSceneStateChange: listener => subscribe(nativeEventListeners.onSceneStateChange, listener),
49
+ onBeforeRemoveRequest: listener => subscribe(nativeEventListeners.onBeforeRemoveRequest, listener),
50
+ onTabPress: listener => subscribe(nativeEventListeners.onTabPress, listener),
51
+ };
52
+ export const mockNativeNavigation = {
53
+ getConstants: () => DEFAULT_CONSTANTS,
54
+ startRegisterReactComponent: () => { },
55
+ endRegisterReactComponent: () => { },
56
+ registerReactComponent: () => { },
57
+ signalFirstRenderComplete: () => { },
58
+ setRoot: async () => {
59
+ activeState.rootGeneration += 1;
60
+ emit(nativeEventListeners.willSetRoot, undefined);
61
+ emit(nativeEventListeners.didSetRoot, undefined);
62
+ return { ok: true, code: 'ok' };
63
+ },
64
+ setResult: (sceneId, resultCode, data = null) => {
65
+ const normalizedData = (data ?? null);
66
+ activeState.setResultCalls.push({ sceneId, resultCode, data: normalizedData });
67
+ activeState.onSetResult?.(sceneId, resultCode, normalizedData);
68
+ },
69
+ dispatch: async (sceneId, action, params) => {
70
+ const typedParams = (params ?? {});
71
+ activeState.dispatchCalls.push({ sceneId, action, params: typedParams });
72
+ if (activeState.onDispatch) {
73
+ return await activeState.onDispatch(sceneId, action, typedParams);
74
+ }
75
+ return { ok: true, code: 'ok' };
76
+ },
77
+ currentTab: async () => 0,
78
+ isStackRoot: async () => true,
79
+ findSceneIdByModuleName: async () => null,
80
+ currentRoute: async () => activeState.currentRoute,
81
+ routeGraph: async () => activeState.routeGraph,
82
+ navigationSnapshot: async () => ({
83
+ rootGeneration: activeState.rootGeneration,
84
+ currentRoute: activeState.currentRoute,
85
+ layers: activeState.routeGraph,
86
+ }),
87
+ setDefaultOptions: () => { },
88
+ updateOptions: () => { },
89
+ setBeforeRemoveEnabled: (sceneId, enabled) => {
90
+ if (enabled) {
91
+ activeState.beforeRemoveEnabled.add(sceneId);
92
+ }
93
+ else {
94
+ activeState.beforeRemoveEnabled.delete(sceneId);
95
+ }
96
+ },
97
+ updateTabBar: () => { },
98
+ setTabItem: (sceneId, items) => {
99
+ activeState.tabItemCalls.push({ sceneId, items: items });
100
+ },
101
+ };
102
+ function installNativeBridge() {
103
+ if (bridgeInstalled) {
104
+ return;
105
+ }
106
+ bridgeInstalled = true;
107
+ const registry = TurboModuleRegistry;
108
+ const originalGet = registry.get?.bind(TurboModuleRegistry);
109
+ const originalGetEnforcing = registry.getEnforcing?.bind(TurboModuleRegistry);
110
+ registry.get = (name) => {
111
+ if (name === 'HBDNativeNavigation') {
112
+ return mockNativeNavigation;
113
+ }
114
+ if (name === 'HBDNativeEvent') {
115
+ return mockNativeEvent;
116
+ }
117
+ return originalGet ? originalGet(name) : null;
118
+ };
119
+ registry.getEnforcing = (name) => {
120
+ if (name === 'HBDNativeNavigation') {
121
+ return mockNativeNavigation;
122
+ }
123
+ if (name === 'HBDNativeEvent') {
124
+ return mockNativeEvent;
125
+ }
126
+ if (originalGetEnforcing) {
127
+ return originalGetEnforcing(name);
128
+ }
129
+ throw new Error(`TurboModule '${name}' could not be found.`);
130
+ };
131
+ }
132
+ /**
133
+ * Thin test harness that mocks only the two native TurboModule boundaries
134
+ * (`HBDNativeNavigation` and `HBDNativeEvent`) and runs the real JS navigation layer on top.
135
+ */
136
+ export function createMockNavigation(options = {}) {
137
+ installNativeBridge();
138
+ activeState = createInitialState(options);
139
+ // Require internal submodules directly instead of `./index` so Jest's
140
+ // `jest.mock('hybrid-navigation', () => require('hybrid-navigation/testing').createMockNavigation())`
141
+ // does not recursively re-enter its own factory.
142
+ const navMod = require('./Navigation');
143
+ const navigatorMod = require('./Navigator');
144
+ const hooksMod = require('./hooks');
145
+ const wrapMod = require('./NavigationComponentWrap');
146
+ const routerMod = require('./router');
147
+ const routeGraphMod = require('./routeGraph');
148
+ const revealMod = require('./reveal');
149
+ const defineRoutesMod = require('./defineRoutes');
150
+ const deepLinkMod = require('./DeepLink');
151
+ const optionsMod = require('./Options');
152
+ const { lifecycleHandler, } = require('./handler/LifecycleEventHandler');
153
+ const { dispatchHandler, } = require('./handler/DispatchCommandHandler');
154
+ const { tabEventHandler, } = require('./handler/TabEventHandler');
155
+ const { layoutHandler, } = require('./handler/LayoutCommandHandler');
156
+ const storeMod = require('./store');
157
+ const navigation = navMod.default;
158
+ const defaultSceneId = options.defaultSceneId ?? 'scene-1';
159
+ const defaultModuleName = options.defaultModuleName;
160
+ const getNavigator = (sceneId) => {
161
+ const nav = navigatorMod.Navigator.of(sceneId);
162
+ if (sceneId === defaultSceneId && defaultModuleName && nav.moduleName === undefined) {
163
+ nav.moduleName = defaultModuleName;
164
+ }
165
+ return nav;
166
+ };
167
+ const useNavigator = () => {
168
+ const nav = useContext(navigatorMod.NavigationContext);
169
+ return nav ?? getNavigator(defaultSceneId);
170
+ };
171
+ const controller = {
172
+ get dispatchCalls() {
173
+ return activeState.dispatchCalls;
174
+ },
175
+ get setResultCalls() {
176
+ return activeState.setResultCalls;
177
+ },
178
+ get tabItemCalls() {
179
+ return activeState.tabItemCalls;
180
+ },
181
+ get beforeRemoveEnabled() {
182
+ return activeState.beforeRemoveEnabled;
183
+ },
184
+ setCurrentRoute(route) {
185
+ activeState.currentRoute = route;
186
+ },
187
+ setRouteGraph(layers) {
188
+ activeState.routeGraph = layers;
189
+ },
190
+ setOnDispatch(fn) {
191
+ activeState.onDispatch = fn;
192
+ },
193
+ setOnSetResult(fn) {
194
+ activeState.onSetResult = fn;
195
+ },
196
+ emitSceneState(event) {
197
+ const prevState = event.prevState ?? navigation.getSceneState(event.sceneId);
198
+ emit(nativeEventListeners.onSceneStateChange, {
199
+ sceneId: event.sceneId,
200
+ state: event.state,
201
+ prevState,
202
+ moduleName: event.moduleName,
203
+ mode: event.mode,
204
+ presentingId: event.presentingId ?? undefined,
205
+ });
206
+ },
207
+ emitResult(event) {
208
+ emit(nativeEventListeners.onResult, event);
209
+ },
210
+ emitBeforeRemove(sceneId, action = 'back') {
211
+ emit(nativeEventListeners.onBeforeRemoveRequest, { sceneId, action });
212
+ },
213
+ emitTabPress(event) {
214
+ emit(nativeEventListeners.onTabPress, event);
215
+ },
216
+ emitRootReset() {
217
+ activeState.rootGeneration += 1;
218
+ emit(nativeEventListeners.willSetRoot, undefined);
219
+ emit(nativeEventListeners.didSetRoot, undefined);
220
+ },
221
+ getNavigator,
222
+ reset() {
223
+ activeState = createInitialState(options);
224
+ layoutHandler.clearListeners();
225
+ emit(nativeEventListeners.willSetRoot, undefined);
226
+ emit(nativeEventListeners.didSetRoot, undefined);
227
+ lifecycleHandler.resetForTesting();
228
+ storeMod.default.clear();
229
+ dispatchHandler.clearInterceptors();
230
+ tabEventHandler.clearListeners();
231
+ },
232
+ };
233
+ return {
234
+ __esModule: true,
235
+ ...optionsMod,
236
+ ...deepLinkMod,
237
+ ...defineRoutesMod,
238
+ ...revealMod,
239
+ ...routeGraphMod,
240
+ ...routerMod,
241
+ ...wrapMod,
242
+ ...hooksMod,
243
+ ...navigatorMod,
244
+ RESULT_OK: navMod.RESULT_OK,
245
+ RESULT_CANCEL: navMod.RESULT_CANCEL,
246
+ RESULT_BLOCK: navMod.RESULT_BLOCK,
247
+ default: navigation,
248
+ useNavigator: useNavigator,
249
+ __mock: controller,
250
+ };
251
+ }
@@ -10,6 +10,9 @@ NS_ASSUME_NONNULL_BEGIN
10
10
  + (HBDGlobalStyle *)globalStyle;
11
11
 
12
12
  @property(nonatomic, strong, readonly) UIColor *screenBackgroundColor;
13
+ @property(nonatomic, strong, readonly) UIColor *modalBackgroundColor;
14
+ @property(nonatomic, assign, readonly) BOOL injectInitialMetrics;
15
+ @property(nonatomic, assign, readonly) BOOL prewarmPush;
13
16
  @property(nonatomic, assign, readonly) UIBarStyle statusBarStyle;
14
17
  @property(nonatomic, strong, readonly) UIColor *tabBarBackgroundColor;
15
18
  @property(nonatomic, strong, readonly, nullable) UIImage *tabBarShadowImage;
@@ -41,6 +41,19 @@
41
41
  _screenBackgroundColor = UIColor.whiteColor;
42
42
  }
43
43
 
44
+ NSString *modalBackgroundColor = _options[@"modalBackgroundColor"];
45
+ if (modalBackgroundColor) {
46
+ _modalBackgroundColor = [HBDUtils colorWithHexString:modalBackgroundColor];
47
+ } else {
48
+ _modalBackgroundColor = UIColor.clearColor;
49
+ }
50
+
51
+ id injectInitialMetrics = _options[@"injectInitialMetrics"];
52
+ _injectInitialMetrics = [injectInitialMetrics isKindOfClass:[NSNumber class]] ? [injectInitialMetrics boolValue] : NO;
53
+
54
+ id prewarmPush = _options[@"prewarmPush"];
55
+ _prewarmPush = [prewarmPush isKindOfClass:[NSNumber class]] ? [prewarmPush boolValue] : NO;
56
+
44
57
  NSString *statusBarStyle = _options[@"statusBarStyle"];
45
58
  if (statusBarStyle && [statusBarStyle isEqualToString:@"light-content"]) {
46
59
  _statusBarStyle = UIBarStyleBlack;
@@ -8,6 +8,7 @@
8
8
  #import "HBDViewController.h"
9
9
  #import "HBDNavigationController.h"
10
10
  #import "HBDTabBarController.h"
11
+ #import "HBDTabBarRenderer.h"
11
12
  #import "HBDDrawerController.h"
12
13
  #import "UIViewController+HBD.h"
13
14
  #import "UINavigationController+HBD.h"
@@ -15,7 +15,7 @@
15
15
  }
16
16
 
17
17
  - (NSArray<NSString *> *)supportActions {
18
- return @[@"present", @"presentLayout", @"dismiss", @"showModal", @"showModalLayout", @"hideModal"];
18
+ return @[@"present", @"presentLayout", @"dismiss", @"showModal", @"showModalLayout", @"hideModal", @"close"];
19
19
  }
20
20
 
21
21
  - (UIViewController *)viewControllerWithLayout:(NSDictionary *)layout {
@@ -58,7 +58,7 @@
58
58
  }
59
59
 
60
60
  if ([action isEqualToString:@"dismiss"]) {
61
- [self handleDismissWithViewController:vc completion:completion];
61
+ [self handleDismissWithViewController:vc extras:extras completion:completion];
62
62
  return;
63
63
  }
64
64
 
@@ -68,7 +68,7 @@
68
68
  }
69
69
 
70
70
  if ([action isEqualToString:@"hideModal"]) {
71
- [self handleDismissWithViewController:vc completion:completion];
71
+ [self handleDismissWithViewController:vc extras:extras completion:completion];
72
72
  return;
73
73
  }
74
74
 
@@ -82,6 +82,11 @@
82
82
  return;
83
83
  }
84
84
 
85
+ if ([action isEqualToString:@"close"]) {
86
+ [self handleCloseWithViewController:vc extras:extras completion:completion];
87
+ return;
88
+ }
89
+
85
90
  completion(HBDDispatchResultFail(HBDDispatchCodeInvalidAction, [NSString stringWithFormat:@"Unsupported screen action: %@", action]));
86
91
  }
87
92
 
@@ -96,11 +101,84 @@
96
101
  return NO;
97
102
  }
98
103
 
104
+ - (NSString *)resolvedSceneIdForViewController:(UIViewController *)vc {
105
+ HBDViewController *primary = [[HBDReactBridgeManager get] primaryViewControllerWithViewController:vc];
106
+ if (primary.hbd_sceneId.length > 0) {
107
+ return primary.hbd_sceneId;
108
+ }
109
+ return vc.hbd_sceneId;
110
+ }
111
+
112
+ - (BOOL)waitForTransitionIfNeededForViewController:(nullable UIViewController *)vc
113
+ retryCount:(NSUInteger)retryCount
114
+ retry:(void (^)(NSUInteger nextRetryCount))retry {
115
+ if (!vc || (!vc.isBeingPresented && !vc.isBeingDismissed)) {
116
+ return NO;
117
+ }
118
+ HBDReactBridgeManager *manager = [HBDReactBridgeManager get];
119
+ NSUInteger commandId = manager.activeCommandId;
120
+ id<UIViewControllerTransitionCoordinator> coordinator = vc.transitionCoordinator;
121
+ if (coordinator) {
122
+ [coordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
123
+ dispatch_async(dispatch_get_main_queue(), ^{
124
+ if (commandId != 0 && manager.activeCommandId != commandId) {
125
+ return;
126
+ }
127
+ retry(0);
128
+ });
129
+ }];
130
+ return YES;
131
+ }
132
+ if (retryCount >= 3) {
133
+ return NO;
134
+ }
135
+ dispatch_async(dispatch_get_main_queue(), ^{
136
+ if (commandId != 0 && manager.activeCommandId != commandId) {
137
+ return;
138
+ }
139
+ retry(retryCount + 1);
140
+ });
141
+ return YES;
142
+ }
143
+
144
+ - (BOOL)waitForDismissalIfNeededForViewController:(UIViewController *)presenting retry:(dispatch_block_t)retry {
145
+ UIViewController *presented = presenting.presentedViewController;
146
+ if (!presented || !presented.isBeingDismissed) {
147
+ return NO;
148
+ }
149
+ HBDReactBridgeManager *manager = [HBDReactBridgeManager get];
150
+ NSUInteger commandId = manager.activeCommandId;
151
+ id<UIViewControllerTransitionCoordinator> coordinator = presented.transitionCoordinator;
152
+ if (coordinator) {
153
+ [coordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
154
+ dispatch_async(dispatch_get_main_queue(), ^{
155
+ if (commandId != 0 && manager.activeCommandId != commandId) {
156
+ return;
157
+ }
158
+ retry();
159
+ });
160
+ }];
161
+ } else {
162
+ dispatch_async(dispatch_get_main_queue(), ^{
163
+ if (commandId != 0 && manager.activeCommandId != commandId) {
164
+ return;
165
+ }
166
+ retry();
167
+ });
168
+ }
169
+ return YES;
170
+ }
171
+
99
172
  - (void)handlePresentWithViewController:(UIViewController *)presenting extras:(NSDictionary *)extras completion:(HBDDispatchCompletion)completion {
100
173
  if ([self isInsideModalViewController:presenting]) {
101
174
  completion(HBDDispatchResultFail(HBDDispatchCodeInvalidAction, @"Cannot present a scene on top of a modal."));
102
175
  return;
103
176
  }
177
+ if ([self waitForDismissalIfNeededForViewController:presenting retry:^{
178
+ [self handlePresentWithViewController:presenting extras:extras completion:completion];
179
+ }]) {
180
+ return;
181
+ }
104
182
  if (![presenting hbd_canPresentViewController]) {
105
183
  completion(HBDDispatchResultFail(HBDDispatchCodeAlreadyPresenting, @"Target scene is already presenting another scene."));
106
184
  return;
@@ -118,8 +196,9 @@
118
196
  [vc setHbd_requestCode:requestCode];
119
197
  [vc setHbd_presentingSceneId:presenting.hbd_sceneId];
120
198
 
199
+ NSString *sceneId = [self resolvedSceneIdForViewController:vc];
121
200
  [presenting presentViewController:nav animated:YES completion:^{
122
- completion(HBDDispatchResultOk());
201
+ completion(HBDDispatchResultOkWithSceneId(sceneId));
123
202
  }];
124
203
  }
125
204
 
@@ -131,8 +210,100 @@
131
210
  return host;
132
211
  }
133
212
 
134
- - (void)handleDismissWithViewController:(UIViewController *)vc completion:(HBDDispatchCompletion)completion {
213
+ - (BOOL)applyPendingResultIfPresentToViewController:(UIViewController *)vc extras:(NSDictionary *)extras {
214
+ NSDictionary *pendingResult = [extras[@"result"] isKindOfClass:[NSDictionary class]] ? extras[@"result"] : nil;
215
+ if (!pendingResult) {
216
+ return NO;
217
+ }
218
+ id codeObj = pendingResult[@"code"];
219
+ if (![codeObj isKindOfClass:[NSNumber class]]) {
220
+ return NO;
221
+ }
222
+ NSInteger resultCode = [codeObj integerValue];
223
+ id rawData = pendingResult[@"data"];
224
+ NSDictionary *resultData = [rawData isKindOfClass:[NSDictionary class]] ? rawData : nil;
225
+ [vc hbd_setResultCode:resultCode resultData:resultData];
226
+ return YES;
227
+ }
228
+
229
+ - (BOOL)viewController:(UIViewController *)viewController hasAncestorOrSelf:(nullable UIViewController *)ancestor {
230
+ if (!viewController || !ancestor) {
231
+ return NO;
232
+ }
233
+ for (UIViewController *current = viewController; current != nil; current = current.parentViewController) {
234
+ if (current == ancestor) {
235
+ return YES;
236
+ }
237
+ }
238
+ return NO;
239
+ }
240
+
241
+ - (void)handleCloseWithViewController:(UIViewController *)vc extras:(NSDictionary *)extras completion:(HBDDispatchCompletion)completion {
242
+ [self handleCloseWithViewController:vc extras:extras retryCount:0 completion:completion];
243
+ }
244
+
245
+ - (void)handleCloseWithViewController:(UIViewController *)vc extras:(NSDictionary *)extras retryCount:(NSUInteger)retryCount completion:(HBDDispatchCompletion)completion {
246
+ UIViewController *modalHost = [self modalHostViewControllerForViewController:vc];
247
+ if ([self waitForDismissalIfNeededForViewController:modalHost retry:^{
248
+ [self handleCloseWithViewController:vc extras:extras retryCount:0 completion:completion];
249
+ }]) {
250
+ return;
251
+ }
252
+ if ([self waitForTransitionIfNeededForViewController:modalHost retryCount:retryCount retry:^(NSUInteger nextRetryCount) {
253
+ [self handleCloseWithViewController:vc extras:extras retryCount:nextRetryCount completion:completion];
254
+ }]) {
255
+ return;
256
+ }
257
+
258
+ UIViewController *presented = modalHost.presentedViewController;
259
+ if (presented != nil) {
260
+ completion(HBDDispatchResultFail(HBDDispatchCodeCovered, @"Target scene is covered by another presented scene."));
261
+ return;
262
+ }
263
+
264
+ UINavigationController *nav = vc.navigationController;
265
+ if (nav != nil && nav.viewControllers.count > 1) {
266
+ UIViewController *topVC = nav.topViewController;
267
+ if (![self viewController:vc hasAncestorOrSelf:topVC]) {
268
+ completion(HBDDispatchResultFail(HBDDispatchCodeCovered, @"Target scene is covered by another screen in the stack."));
269
+ return;
270
+ }
271
+ UIViewController *stackRoot = nav.viewControllers.firstObject;
272
+ if (![self viewController:vc hasAncestorOrSelf:stackRoot]) {
273
+ UIViewController *resultTarget = topVC ?: vc;
274
+ [self applyPendingResultIfPresentToViewController:resultTarget extras:extras];
275
+ [[HBDReactBridgeManager get] handleNavigationWithViewController:vc action:@"pop" extras:extras completion:completion];
276
+ return;
277
+ }
278
+ }
279
+
280
+ if (modalHost.presentingViewController != nil) {
281
+ NSMutableDictionary *dismissExtras = extras ? [extras mutableCopy] : [NSMutableDictionary dictionary];
282
+ dismissExtras[@"target"] = @"self";
283
+ [self handleDismissWithViewController:vc extras:[dismissExtras copy] completion:completion];
284
+ return;
285
+ }
286
+
287
+ completion(HBDDispatchResultFail(HBDDispatchCodeInvalidAction, @"Target scene cannot be closed."));
288
+ }
289
+
290
+ - (void)handleDismissWithViewController:(UIViewController *)vc extras:(NSDictionary *)extras completion:(HBDDispatchCompletion)completion {
291
+ [self handleDismissWithViewController:vc extras:extras retryCount:0 completion:completion];
292
+ }
293
+
294
+ - (void)handleDismissWithViewController:(UIViewController *)vc extras:(NSDictionary *)extras retryCount:(NSUInteger)retryCount completion:(HBDDispatchCompletion)completion {
135
295
  UIViewController *modalHost = [self modalHostViewControllerForViewController:vc];
296
+ if ([self waitForDismissalIfNeededForViewController:modalHost retry:^{
297
+ [self handleDismissWithViewController:vc extras:extras retryCount:0 completion:completion];
298
+ }]) {
299
+ return;
300
+ }
301
+ if ([self waitForTransitionIfNeededForViewController:modalHost retryCount:retryCount retry:^(NSUInteger nextRetryCount) {
302
+ [self handleDismissWithViewController:vc extras:extras retryCount:nextRetryCount completion:completion];
303
+ }]) {
304
+ return;
305
+ }
306
+
136
307
  BOOL isModal = modalHost.modalPresentationStyle == UIModalPresentationOverFullScreen;
137
308
  BOOL animated = !isModal;
138
309
  if (!isModal && [vc isKindOfClass:[HBDViewController class]]) {
@@ -141,15 +312,65 @@
141
312
  }
142
313
 
143
314
  UIViewController *presenting = modalHost.presentingViewController;
315
+ UIViewController *presented = modalHost.presentedViewController;
316
+ BOOL hasActivePresented = presented != nil && !presented.isBeingDismissed;
317
+ NSString *target = [extras[@"target"] isKindOfClass:[NSString class]] ? extras[@"target"] : nil;
318
+
319
+ if ([target isEqualToString:@"self"]) {
320
+ if (hasActivePresented) {
321
+ completion(HBDDispatchResultFail(HBDDispatchCodeCovered, @"Target scene is covered by another presented scene."));
322
+ return;
323
+ }
324
+ if (!presenting) {
325
+ completion(HBDDispatchResultFail(HBDDispatchCodeInvalidAction, @"Target scene is not presented."));
326
+ return;
327
+ }
328
+ } else if ([target isEqualToString:@"presented"]) {
329
+ if (!hasActivePresented) {
330
+ completion(HBDDispatchResultFail(HBDDispatchCodeInvalidAction, @"No presented scene to dismiss."));
331
+ return;
332
+ }
333
+ }
334
+
144
335
  if (!presenting && !modalHost.presentedViewController) {
145
336
  RCTLogInfo(@"[Navigation] Nothing to dismiss for sceneId: %@", vc.hbd_sceneId);
146
337
  completion(HBDDispatchResultFail(HBDDispatchCodeInvalidAction, @"Nothing to dismiss."));
147
338
  return;
148
339
  }
149
340
 
150
- [modalHost dismissViewControllerAnimated:animated completion:^{
151
- if (presenting != nil) {
152
- HBDViewController *hbdvc = [[HBDReactBridgeManager get] primaryViewControllerWithViewController:presenting];
341
+ BOOL dismissPresented = [target isEqualToString:@"presented"] || (target == nil && hasActivePresented);
342
+ if (dismissPresented && [self waitForTransitionIfNeededForViewController:presented retryCount:retryCount retry:^(NSUInteger nextRetryCount) {
343
+ [self handleDismissWithViewController:vc extras:extras retryCount:nextRetryCount completion:completion];
344
+ }]) {
345
+ return;
346
+ }
347
+
348
+ UIViewController *dismissedTarget = dismissPresented ? presented : modalHost;
349
+ UIViewController *dismissReceiver = dismissPresented ? modalHost : ((presenting.presentedViewController == modalHost) ? presenting : modalHost);
350
+ UIViewController *revealed = dismissPresented ? modalHost : presenting;
351
+ BOOL dismissAnimated = dismissPresented ? (presented.modalPresentationStyle != UIModalPresentationOverFullScreen) : animated;
352
+
353
+ NSInteger previousResultCode = vc.hbd_resultCode;
354
+ NSDictionary *previousResultData = vc.hbd_resultData;
355
+ BOOL appliedPendingResult = NO;
356
+ if (!dismissPresented) {
357
+ appliedPendingResult = [self applyPendingResultIfPresentToViewController:vc extras:extras];
358
+ }
359
+
360
+ __weak UIViewController *weakDismissedTarget = dismissedTarget;
361
+ [dismissReceiver dismissViewControllerAnimated:dismissAnimated completion:^{
362
+ UIViewController *remainingTarget = weakDismissedTarget;
363
+ if (remainingTarget != nil && remainingTarget.presentingViewController != nil) {
364
+ if (appliedPendingResult) {
365
+ [vc hbd_setResultCode:previousResultCode resultData:previousResultData];
366
+ }
367
+ RCTLogWarn(@"[Navigation] dismissViewControllerAnimated completed but target scene is still presented. sceneId=%@", vc.hbd_sceneId);
368
+ NSString *code = remainingTarget.presentedViewController != nil ? HBDDispatchCodeCovered : HBDDispatchCodeNotReady;
369
+ completion(HBDDispatchResultFail(code, @"Target scene was not dismissed."));
370
+ return;
371
+ }
372
+ if (revealed != nil) {
373
+ HBDViewController *hbdvc = [[HBDReactBridgeManager get] primaryViewControllerWithViewController:revealed];
153
374
  if (hbdvc != nil) {
154
375
  [[HBDOrientationCoordinator shared] applyOrientationForViewController:hbdvc];
155
376
  }
@@ -159,6 +380,11 @@
159
380
  }
160
381
 
161
382
  - (void)handleShowModalWithViewController:(UIViewController *)presenting extras:(NSDictionary *)extras completion:(HBDDispatchCompletion)completion {
383
+ if ([self waitForDismissalIfNeededForViewController:presenting retry:^{
384
+ [self handleShowModalWithViewController:presenting extras:extras completion:completion];
385
+ }]) {
386
+ return;
387
+ }
162
388
  if (![presenting hbd_canPresentViewController]) {
163
389
  completion(HBDDispatchResultFail(HBDDispatchCodeAlreadyPresenting, @"Target scene is already presenting another scene."));
164
390
  return;
@@ -175,8 +401,9 @@
175
401
  [vc setHbd_requestCode:requestCode];
176
402
  [vc setHbd_presentingSceneId:presenting.hbd_sceneId];
177
403
 
404
+ NSString *sceneId = [self resolvedSceneIdForViewController:vc];
178
405
  [presenting presentViewController:vc animated:NO completion:^{
179
- completion(HBDDispatchResultOk());
406
+ completion(HBDDispatchResultOkWithSceneId(sceneId));
180
407
  }];
181
408
  }
182
409
 
@@ -185,6 +412,11 @@
185
412
  completion(HBDDispatchResultFail(HBDDispatchCodeInvalidAction, @"Cannot present a scene on top of a modal."));
186
413
  return;
187
414
  }
415
+ if ([self waitForDismissalIfNeededForViewController:presenting retry:^{
416
+ [self handlePresentLayoutWithViewController:presenting extras:extras completion:completion];
417
+ }]) {
418
+ return;
419
+ }
188
420
  if (![presenting hbd_canPresentViewController]) {
189
421
  completion(HBDDispatchResultFail(HBDDispatchCodeAlreadyPresenting, @"Target scene is already presenting another scene."));
190
422
  return;
@@ -201,12 +433,18 @@
201
433
  [vc setHbd_presentingSceneId:presenting.hbd_sceneId];
202
434
  vc.modalPresentationStyle = UIModalPresentationCurrentContext;
203
435
 
436
+ NSString *sceneId = [self resolvedSceneIdForViewController:vc];
204
437
  [presenting presentViewController:vc animated:YES completion:^{
205
- completion(HBDDispatchResultOk());
438
+ completion(HBDDispatchResultOkWithSceneId(sceneId));
206
439
  }];
207
440
  }
208
441
 
209
442
  - (void)handleShowModalLayoutWithViewController:(UIViewController *)presenting extras:(NSDictionary *)extras completion:(HBDDispatchCompletion)completion {
443
+ if ([self waitForDismissalIfNeededForViewController:presenting retry:^{
444
+ [self handleShowModalLayoutWithViewController:presenting extras:extras completion:completion];
445
+ }]) {
446
+ return;
447
+ }
210
448
  if (![presenting hbd_canPresentViewController]) {
211
449
  completion(HBDDispatchResultFail(HBDDispatchCodeAlreadyPresenting, @"Target scene is already presenting another scene."));
212
450
  return;
@@ -224,8 +462,9 @@
224
462
  [vc setHbd_requestCode:requestCode];
225
463
  [vc setHbd_presentingSceneId:presenting.hbd_sceneId];
226
464
 
465
+ NSString *sceneId = [self resolvedSceneIdForViewController:vc];
227
466
  [presenting presentViewController:vc animated:NO completion:^{
228
- completion(HBDDispatchResultOk());
467
+ completion(HBDDispatchResultOkWithSceneId(sceneId));
229
468
  }];
230
469
  }
231
470