hybrid-navigation 5.0.0 → 5.1.0

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 +449 -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 +177 -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 +13 -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 +560 -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,52 @@
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)waitForDismissalIfNeededForViewController:(UIViewController *)presenting retry:(dispatch_block_t)retry {
113
+ UIViewController *presented = presenting.presentedViewController;
114
+ if (!presented || !presented.isBeingDismissed) {
115
+ return NO;
116
+ }
117
+ HBDReactBridgeManager *manager = [HBDReactBridgeManager get];
118
+ NSUInteger commandId = manager.activeCommandId;
119
+ id<UIViewControllerTransitionCoordinator> coordinator = presented.transitionCoordinator;
120
+ if (coordinator) {
121
+ [coordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
122
+ dispatch_async(dispatch_get_main_queue(), ^{
123
+ if (commandId != 0 && manager.activeCommandId != commandId) {
124
+ return;
125
+ }
126
+ retry();
127
+ });
128
+ }];
129
+ } else {
130
+ dispatch_async(dispatch_get_main_queue(), ^{
131
+ if (commandId != 0 && manager.activeCommandId != commandId) {
132
+ return;
133
+ }
134
+ retry();
135
+ });
136
+ }
137
+ return YES;
138
+ }
139
+
99
140
  - (void)handlePresentWithViewController:(UIViewController *)presenting extras:(NSDictionary *)extras completion:(HBDDispatchCompletion)completion {
100
141
  if ([self isInsideModalViewController:presenting]) {
101
142
  completion(HBDDispatchResultFail(HBDDispatchCodeInvalidAction, @"Cannot present a scene on top of a modal."));
102
143
  return;
103
144
  }
145
+ if ([self waitForDismissalIfNeededForViewController:presenting retry:^{
146
+ [self handlePresentWithViewController:presenting extras:extras completion:completion];
147
+ }]) {
148
+ return;
149
+ }
104
150
  if (![presenting hbd_canPresentViewController]) {
105
151
  completion(HBDDispatchResultFail(HBDDispatchCodeAlreadyPresenting, @"Target scene is already presenting another scene."));
106
152
  return;
@@ -118,8 +164,9 @@
118
164
  [vc setHbd_requestCode:requestCode];
119
165
  [vc setHbd_presentingSceneId:presenting.hbd_sceneId];
120
166
 
167
+ NSString *sceneId = [self resolvedSceneIdForViewController:vc];
121
168
  [presenting presentViewController:nav animated:YES completion:^{
122
- completion(HBDDispatchResultOk());
169
+ completion(HBDDispatchResultOkWithSceneId(sceneId));
123
170
  }];
124
171
  }
125
172
 
@@ -131,8 +178,80 @@
131
178
  return host;
132
179
  }
133
180
 
134
- - (void)handleDismissWithViewController:(UIViewController *)vc completion:(HBDDispatchCompletion)completion {
181
+ - (void)applyPendingResultIfPresentToViewController:(UIViewController *)vc extras:(NSDictionary *)extras {
182
+ NSDictionary *pendingResult = [extras[@"result"] isKindOfClass:[NSDictionary class]] ? extras[@"result"] : nil;
183
+ if (!pendingResult) {
184
+ return;
185
+ }
186
+ id codeObj = pendingResult[@"code"];
187
+ if (![codeObj isKindOfClass:[NSNumber class]]) {
188
+ return;
189
+ }
190
+ NSInteger resultCode = [codeObj integerValue];
191
+ id rawData = pendingResult[@"data"];
192
+ NSDictionary *resultData = [rawData isKindOfClass:[NSDictionary class]] ? rawData : nil;
193
+ [vc hbd_setResultCode:resultCode resultData:resultData];
194
+ }
195
+
196
+ - (BOOL)viewController:(UIViewController *)viewController hasAncestorOrSelf:(nullable UIViewController *)ancestor {
197
+ if (!viewController || !ancestor) {
198
+ return NO;
199
+ }
200
+ for (UIViewController *current = viewController; current != nil; current = current.parentViewController) {
201
+ if (current == ancestor) {
202
+ return YES;
203
+ }
204
+ }
205
+ return NO;
206
+ }
207
+
208
+ - (void)handleCloseWithViewController:(UIViewController *)vc extras:(NSDictionary *)extras completion:(HBDDispatchCompletion)completion {
209
+ UIViewController *modalHost = [self modalHostViewControllerForViewController:vc];
210
+ if ([self waitForDismissalIfNeededForViewController:modalHost retry:^{
211
+ [self handleCloseWithViewController:vc extras:extras completion:completion];
212
+ }]) {
213
+ return;
214
+ }
215
+
216
+ UIViewController *presented = modalHost.presentedViewController;
217
+ if (presented != nil) {
218
+ completion(HBDDispatchResultFail(HBDDispatchCodeCovered, @"Target scene is covered by another presented scene."));
219
+ return;
220
+ }
221
+
222
+ UINavigationController *nav = vc.navigationController;
223
+ if (nav != nil && nav.viewControllers.count > 1) {
224
+ UIViewController *topVC = nav.topViewController;
225
+ if (![self viewController:vc hasAncestorOrSelf:topVC]) {
226
+ completion(HBDDispatchResultFail(HBDDispatchCodeCovered, @"Target scene is covered by another screen in the stack."));
227
+ return;
228
+ }
229
+ UIViewController *stackRoot = nav.viewControllers.firstObject;
230
+ if (![self viewController:vc hasAncestorOrSelf:stackRoot]) {
231
+ UIViewController *resultTarget = topVC ?: vc;
232
+ [self applyPendingResultIfPresentToViewController:resultTarget extras:extras];
233
+ [[HBDReactBridgeManager get] handleNavigationWithViewController:vc action:@"pop" extras:extras completion:completion];
234
+ return;
235
+ }
236
+ }
237
+
238
+ if (modalHost.presentingViewController != nil) {
239
+ NSMutableDictionary *dismissExtras = extras ? [extras mutableCopy] : [NSMutableDictionary dictionary];
240
+ dismissExtras[@"target"] = @"self";
241
+ [self handleDismissWithViewController:vc extras:[dismissExtras copy] completion:completion];
242
+ return;
243
+ }
244
+
245
+ completion(HBDDispatchResultFail(HBDDispatchCodeInvalidAction, @"Target scene cannot be closed."));
246
+ }
247
+
248
+ - (void)handleDismissWithViewController:(UIViewController *)vc extras:(NSDictionary *)extras completion:(HBDDispatchCompletion)completion {
135
249
  UIViewController *modalHost = [self modalHostViewControllerForViewController:vc];
250
+ if ([self waitForDismissalIfNeededForViewController:modalHost retry:^{
251
+ [self handleDismissWithViewController:vc extras:extras completion:completion];
252
+ }]) {
253
+ return;
254
+ }
136
255
  BOOL isModal = modalHost.modalPresentationStyle == UIModalPresentationOverFullScreen;
137
256
  BOOL animated = !isModal;
138
257
  if (!isModal && [vc isKindOfClass:[HBDViewController class]]) {
@@ -141,15 +260,44 @@
141
260
  }
142
261
 
143
262
  UIViewController *presenting = modalHost.presentingViewController;
263
+ UIViewController *presented = modalHost.presentedViewController;
264
+ BOOL hasActivePresented = presented != nil && !presented.isBeingDismissed;
265
+ NSString *target = [extras[@"target"] isKindOfClass:[NSString class]] ? extras[@"target"] : nil;
266
+
267
+ if ([target isEqualToString:@"self"]) {
268
+ if (hasActivePresented) {
269
+ completion(HBDDispatchResultFail(HBDDispatchCodeCovered, @"Target scene is covered by another presented scene."));
270
+ return;
271
+ }
272
+ if (!presenting) {
273
+ completion(HBDDispatchResultFail(HBDDispatchCodeInvalidAction, @"Target scene is not presented."));
274
+ return;
275
+ }
276
+ } else if ([target isEqualToString:@"presented"]) {
277
+ if (!hasActivePresented) {
278
+ completion(HBDDispatchResultFail(HBDDispatchCodeInvalidAction, @"No presented scene to dismiss."));
279
+ return;
280
+ }
281
+ }
282
+
144
283
  if (!presenting && !modalHost.presentedViewController) {
145
284
  RCTLogInfo(@"[Navigation] Nothing to dismiss for sceneId: %@", vc.hbd_sceneId);
146
285
  completion(HBDDispatchResultFail(HBDDispatchCodeInvalidAction, @"Nothing to dismiss."));
147
286
  return;
148
287
  }
149
288
 
150
- [modalHost dismissViewControllerAnimated:animated completion:^{
151
- if (presenting != nil) {
152
- HBDViewController *hbdvc = [[HBDReactBridgeManager get] primaryViewControllerWithViewController:presenting];
289
+ UIViewController *dismissReceiver = [target isEqualToString:@"presented"] ? presented : modalHost;
290
+ UIViewController *revealed = [target isEqualToString:@"presented"] ? modalHost : presenting;
291
+ BOOL receiverIsModal = dismissReceiver.modalPresentationStyle == UIModalPresentationOverFullScreen;
292
+ BOOL dismissAnimated = [target isEqualToString:@"presented"] ? !receiverIsModal : animated;
293
+
294
+ if (![target isEqualToString:@"presented"] && !hasActivePresented) {
295
+ [self applyPendingResultIfPresentToViewController:vc extras:extras];
296
+ }
297
+
298
+ [dismissReceiver dismissViewControllerAnimated:dismissAnimated completion:^{
299
+ if (revealed != nil) {
300
+ HBDViewController *hbdvc = [[HBDReactBridgeManager get] primaryViewControllerWithViewController:revealed];
153
301
  if (hbdvc != nil) {
154
302
  [[HBDOrientationCoordinator shared] applyOrientationForViewController:hbdvc];
155
303
  }
@@ -159,6 +307,11 @@
159
307
  }
160
308
 
161
309
  - (void)handleShowModalWithViewController:(UIViewController *)presenting extras:(NSDictionary *)extras completion:(HBDDispatchCompletion)completion {
310
+ if ([self waitForDismissalIfNeededForViewController:presenting retry:^{
311
+ [self handleShowModalWithViewController:presenting extras:extras completion:completion];
312
+ }]) {
313
+ return;
314
+ }
162
315
  if (![presenting hbd_canPresentViewController]) {
163
316
  completion(HBDDispatchResultFail(HBDDispatchCodeAlreadyPresenting, @"Target scene is already presenting another scene."));
164
317
  return;
@@ -175,8 +328,9 @@
175
328
  [vc setHbd_requestCode:requestCode];
176
329
  [vc setHbd_presentingSceneId:presenting.hbd_sceneId];
177
330
 
331
+ NSString *sceneId = [self resolvedSceneIdForViewController:vc];
178
332
  [presenting presentViewController:vc animated:NO completion:^{
179
- completion(HBDDispatchResultOk());
333
+ completion(HBDDispatchResultOkWithSceneId(sceneId));
180
334
  }];
181
335
  }
182
336
 
@@ -185,6 +339,11 @@
185
339
  completion(HBDDispatchResultFail(HBDDispatchCodeInvalidAction, @"Cannot present a scene on top of a modal."));
186
340
  return;
187
341
  }
342
+ if ([self waitForDismissalIfNeededForViewController:presenting retry:^{
343
+ [self handlePresentLayoutWithViewController:presenting extras:extras completion:completion];
344
+ }]) {
345
+ return;
346
+ }
188
347
  if (![presenting hbd_canPresentViewController]) {
189
348
  completion(HBDDispatchResultFail(HBDDispatchCodeAlreadyPresenting, @"Target scene is already presenting another scene."));
190
349
  return;
@@ -201,12 +360,18 @@
201
360
  [vc setHbd_presentingSceneId:presenting.hbd_sceneId];
202
361
  vc.modalPresentationStyle = UIModalPresentationCurrentContext;
203
362
 
363
+ NSString *sceneId = [self resolvedSceneIdForViewController:vc];
204
364
  [presenting presentViewController:vc animated:YES completion:^{
205
- completion(HBDDispatchResultOk());
365
+ completion(HBDDispatchResultOkWithSceneId(sceneId));
206
366
  }];
207
367
  }
208
368
 
209
369
  - (void)handleShowModalLayoutWithViewController:(UIViewController *)presenting extras:(NSDictionary *)extras completion:(HBDDispatchCompletion)completion {
370
+ if ([self waitForDismissalIfNeededForViewController:presenting retry:^{
371
+ [self handleShowModalLayoutWithViewController:presenting extras:extras completion:completion];
372
+ }]) {
373
+ return;
374
+ }
210
375
  if (![presenting hbd_canPresentViewController]) {
211
376
  completion(HBDDispatchResultFail(HBDDispatchCodeAlreadyPresenting, @"Target scene is already presenting another scene."));
212
377
  return;
@@ -224,8 +389,9 @@
224
389
  [vc setHbd_requestCode:requestCode];
225
390
  [vc setHbd_presentingSceneId:presenting.hbd_sceneId];
226
391
 
392
+ NSString *sceneId = [self resolvedSceneIdForViewController:vc];
227
393
  [presenting presentViewController:vc animated:NO completion:^{
228
- completion(HBDDispatchResultOk());
394
+ completion(HBDDispatchResultOkWithSceneId(sceneId));
229
395
  }];
230
396
  }
231
397