hybrid-navigation 5.4.0 → 5.5.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 (150) hide show
  1. package/HybridNavigation.podspec +1 -0
  2. package/android/proguard-rules.pro +5 -0
  3. package/android/src/main/java/com/navigation/androidx/AppUtils.java +8 -1
  4. package/android/src/main/java/com/navigation/androidx/AwesomeActivity.java +115 -36
  5. package/android/src/main/java/com/navigation/androidx/AwesomeFragment.java +153 -100
  6. package/android/src/main/java/com/navigation/androidx/DefaultTabBarProvider.java +1 -1
  7. package/android/src/main/java/com/navigation/androidx/DrawableUtils.java +5 -1
  8. package/android/src/main/java/com/navigation/androidx/DrawerFragment.java +95 -39
  9. package/android/src/main/java/com/navigation/androidx/FragmentHelper.java +195 -29
  10. package/android/src/main/java/com/navigation/androidx/HybridDrawerLayout.java +225 -27
  11. package/android/src/main/java/com/navigation/androidx/LifecycleDelegate.java +33 -9
  12. package/android/src/main/java/com/navigation/androidx/PresentationDelegate.java +5 -107
  13. package/android/src/main/java/com/navigation/androidx/StackDelegate.java +37 -10
  14. package/android/src/main/java/com/navigation/androidx/StackFragment.java +82 -19
  15. package/android/src/main/java/com/navigation/androidx/Style.java +8 -4
  16. package/android/src/main/java/com/navigation/androidx/SystemUI.java +17 -5
  17. package/android/src/main/java/com/navigation/androidx/TabBar.java +69 -29
  18. package/android/src/main/java/com/navigation/androidx/TabBarFragment.java +48 -11
  19. package/android/src/main/java/com/navigation/androidx/TabBarItem.java +1 -2
  20. package/android/src/main/java/com/navigation/androidx/TabView.java +20 -10
  21. package/android/src/main/java/com/reactnative/hybridnavigation/Constants.java +1 -0
  22. package/android/src/main/java/com/reactnative/hybridnavigation/Garden.java +19 -15
  23. package/android/src/main/java/com/reactnative/hybridnavigation/GlobalStyle.java +10 -48
  24. package/android/src/main/java/com/reactnative/hybridnavigation/HBDRootView.java +22 -23
  25. package/android/src/main/java/com/reactnative/hybridnavigation/HybridFragment.java +54 -22
  26. package/android/src/main/java/com/reactnative/hybridnavigation/NativeEvent.java +4 -2
  27. package/android/src/main/java/com/reactnative/hybridnavigation/NativeNavigation.java +35 -64
  28. package/android/src/main/java/com/reactnative/hybridnavigation/NavigationCommandQueue.java +185 -121
  29. package/android/src/main/java/com/reactnative/hybridnavigation/Navigator.java +21 -5
  30. package/android/src/main/java/com/reactnative/hybridnavigation/NavigatorRegistry.java +27 -30
  31. package/android/src/main/java/com/reactnative/hybridnavigation/OrientationCoordinator.java +41 -4
  32. package/android/src/main/java/com/reactnative/hybridnavigation/Parameters.java +55 -154
  33. package/android/src/main/java/com/reactnative/hybridnavigation/ReactAppCompatActivity.java +30 -1
  34. package/android/src/main/java/com/reactnative/hybridnavigation/ReactAppCompatActivityDelegate.java +12 -37
  35. package/android/src/main/java/com/reactnative/hybridnavigation/ReactFragment.java +31 -190
  36. package/android/src/main/java/com/reactnative/hybridnavigation/ReactManager.java +115 -71
  37. package/android/src/main/java/com/reactnative/hybridnavigation/ReactStackFragment.java +27 -16
  38. package/android/src/main/java/com/reactnative/hybridnavigation/ReactSurfaceCleanup.java +50 -8
  39. package/android/src/main/java/com/reactnative/hybridnavigation/ReactTabBarFragment.java +34 -4
  40. package/android/src/main/java/com/reactnative/hybridnavigation/ReactTabBarProvider.java +39 -10
  41. package/android/src/main/java/com/reactnative/hybridnavigation/RemovalPlan.java +76 -0
  42. package/android/src/main/java/com/reactnative/hybridnavigation/SceneLifecycleStateMachine.java +72 -6
  43. package/android/src/main/java/com/reactnative/hybridnavigation/Utils.java +19 -3
  44. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/DrawerNavigator.java +11 -5
  45. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/ScreenNavigator.java +217 -99
  46. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/StackNavigator.java +122 -59
  47. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/TabNavigator.java +87 -24
  48. package/dist/DeepLink.d.ts +3 -1
  49. package/dist/DeepLink.js +39 -19
  50. package/dist/NativeNavigation.d.ts +8 -5
  51. package/dist/Navigation.d.ts +9 -24
  52. package/dist/Navigation.js +32 -97
  53. package/dist/NavigationComponentWrap.d.ts +1 -2
  54. package/dist/NavigationComponentWrap.js +0 -15
  55. package/dist/Navigator.d.ts +7 -5
  56. package/dist/Navigator.js +38 -33
  57. package/dist/Options.d.ts +1 -7
  58. package/dist/Options.js +1 -2
  59. package/dist/Route.d.ts +24 -12
  60. package/dist/handler/DispatchCommandHandler.d.ts +4 -5
  61. package/dist/handler/DispatchCommandHandler.js +37 -16
  62. package/dist/handler/LayoutCommandHandler.js +18 -3
  63. package/dist/handler/LeaveGuardHandler.d.ts +12 -8
  64. package/dist/handler/LeaveGuardHandler.js +174 -281
  65. package/dist/handler/LifecycleEventHandler.d.ts +7 -4
  66. package/dist/handler/LifecycleEventHandler.js +119 -32
  67. package/dist/handler/ResultEventHandler.d.ts +2 -2
  68. package/dist/handler/ResultEventHandler.js +72 -36
  69. package/dist/handler/SceneExitHandler.d.ts +6 -0
  70. package/dist/handler/SceneExitHandler.js +48 -8
  71. package/dist/hooks.d.ts +1 -0
  72. package/dist/hooks.js +39 -23
  73. package/dist/index.d.ts +9 -8
  74. package/dist/index.js +5 -8
  75. package/dist/routeGraph.d.ts +5 -0
  76. package/dist/routeGraph.js +47 -0
  77. package/dist/router/drawer.d.ts +2 -2
  78. package/dist/router/drawer.js +19 -9
  79. package/dist/router/helpers.d.ts +17 -0
  80. package/dist/router/helpers.js +90 -0
  81. package/dist/router/index.d.ts +2 -2
  82. package/dist/router/index.js +169 -97
  83. package/dist/router/screen.d.ts +2 -2
  84. package/dist/router/screen.js +16 -7
  85. package/dist/router/stack.d.ts +2 -2
  86. package/dist/router/stack.js +24 -49
  87. package/dist/router/tabs.js +5 -1
  88. package/dist/store.d.ts +2 -0
  89. package/dist/store.js +4 -0
  90. package/ios/HBDGlobalStyle.h +0 -1
  91. package/ios/HBDGlobalStyle.mm +27 -39
  92. package/ios/HBDUtils.h +0 -2
  93. package/ios/HBDUtils.mm +81 -41
  94. package/ios/Navigator/HBDDrawerNavigator.mm +31 -26
  95. package/ios/Navigator/HBDNavigatorRegistry.mm +23 -26
  96. package/ios/Navigator/HBDScreenNavigator.mm +240 -137
  97. package/ios/Navigator/HBDStackNavigator.mm +147 -97
  98. package/ios/Navigator/HBDTabNavigator.mm +99 -46
  99. package/ios/React/HBDNativeEvent.mm +38 -5
  100. package/ios/React/HBDNativeNavigation.mm +49 -51
  101. package/ios/React/HBDNavigationCommandQueue.h +55 -0
  102. package/ios/React/HBDNavigationCommandQueue.mm +341 -0
  103. package/ios/React/HBDNavigator.h +20 -0
  104. package/ios/React/HBDOrientationCoordinator.mm +26 -3
  105. package/ios/React/HBDReactBridgeManager.h +18 -6
  106. package/ios/React/HBDReactBridgeManager.mm +536 -315
  107. package/ios/React/HBDSceneLifecycleStateMachine.h +2 -0
  108. package/ios/React/HBDSceneLifecycleStateMachine.mm +18 -0
  109. package/ios/View/HBDRootView.h +1 -0
  110. package/ios/View/HBDRootView.mm +112 -4
  111. package/ios/View/UIImage+WithBadge.mm +9 -7
  112. package/ios/ViewController/HBDDrawerController.h +10 -0
  113. package/ios/ViewController/HBDDrawerController.mm +103 -71
  114. package/ios/ViewController/HBDNavigationController.mm +166 -58
  115. package/ios/ViewController/HBDReactViewController.h +0 -4
  116. package/ios/ViewController/HBDReactViewController.mm +13 -110
  117. package/ios/ViewController/HBDTabBarController.mm +67 -33
  118. package/ios/ViewController/HBDViewController.h +6 -0
  119. package/ios/ViewController/HBDViewController.mm +94 -37
  120. package/ios/ViewController/UINavigationController+HBD.h +2 -2
  121. package/ios/ViewController/UINavigationController+HBD.mm +13 -13
  122. package/ios/ViewController/UIViewController+HBD.h +23 -1
  123. package/ios/ViewController/UIViewController+HBD.mm +298 -40
  124. package/package.json +1 -1
  125. package/src/DeepLink.ts +39 -19
  126. package/src/NativeNavigation.ts +17 -5
  127. package/src/Navigation.ts +49 -170
  128. package/src/NavigationComponentWrap.tsx +1 -16
  129. package/src/Navigator.ts +50 -52
  130. package/src/Options.ts +4 -10
  131. package/src/Route.ts +37 -19
  132. package/src/handler/DispatchCommandHandler.ts +42 -20
  133. package/src/handler/LayoutCommandHandler.ts +15 -3
  134. package/src/handler/LeaveGuardHandler.ts +224 -329
  135. package/src/handler/LifecycleEventHandler.ts +131 -37
  136. package/src/handler/ResultEventHandler.ts +71 -37
  137. package/src/handler/SceneExitHandler.ts +60 -9
  138. package/src/hooks.ts +54 -26
  139. package/src/index.ts +77 -8
  140. package/src/routeGraph.ts +50 -0
  141. package/src/router/drawer.ts +21 -10
  142. package/src/router/helpers.ts +113 -0
  143. package/src/router/index.ts +187 -112
  144. package/src/router/screen.ts +35 -8
  145. package/src/router/stack.ts +40 -53
  146. package/src/router/tabs.ts +5 -1
  147. package/src/store.ts +5 -0
  148. package/dist/sceneCoverage.d.ts +0 -2
  149. package/dist/sceneCoverage.js +0 -25
  150. package/src/sceneCoverage.ts +0 -30
@@ -2,6 +2,7 @@
2
2
 
3
3
  #import "HBDGlobalStyle.h"
4
4
  #import "HBDNativeEvent.h"
5
+ #import "HBDNavigationCommandQueue.h"
5
6
  #import "HBDNavigatorRegistry.h"
6
7
  #import "HBDReactViewController.h"
7
8
  #import "HBDTabBarController.h"
@@ -15,53 +16,18 @@ NSString *const HBDReactModuleRegistryDidCompletedNotification = @"HBDReactModul
15
16
  NSString *const HBDReactBridgeWillReloadNotification = @"HBDReactBridgeWillReloadNotification";
16
17
  const NSInteger HBDResultOK = -1;
17
18
  const NSInteger HBDResultCancel = 0;
18
- const NSInteger HBDResultBlock = -2;
19
-
20
- static const NSTimeInterval HBDCommandTimeoutSeconds = 3.0;
21
-
22
- typedef NS_ENUM(NSInteger, HBDCommandType) {
23
- HBDCommandTypeDispatch,
24
- HBDCommandTypeSetRoot,
25
- };
26
-
27
- @interface HBDNavigationCommand : NSObject
28
- @property(nonatomic, assign) HBDCommandType type;
29
- @property(nonatomic, copy, nullable) NSString *sceneId;
30
- @property(nonatomic, copy, nullable) NSString *action;
31
- @property(nonatomic, copy, nullable) NSDictionary *extras;
32
- @property(nonatomic, copy, nullable) NSDictionary *layout;
33
- @property(nonatomic, copy, nullable) HBDDispatchCompletion completion;
34
- @end
35
-
36
- @implementation HBDNavigationCommand
37
- @end
38
-
39
- @interface HBDReadyTask : NSObject
40
- @property(nonatomic, copy) dispatch_block_t task;
41
- @property(nonatomic, copy, nullable) dispatch_block_t onCancelled;
42
- @end
43
19
 
44
- @implementation HBDReadyTask
45
- @end
46
-
47
- @interface HBDReactBridgeManager ()
20
+ @interface HBDReactBridgeManager () <HBDNavigationCommandQueueDelegate>
48
21
 
49
22
  @property(nonatomic, strong) NSMutableDictionary *nativeModules;
50
23
  @property(nonatomic, strong) NSMutableDictionary *reactModules;
51
24
  @property(nonatomic, assign, readwrite, getter=isReactModuleRegisterCompleted) BOOL reactModuleRegisterCompleted;
52
25
  @property(nonatomic, strong) HBDNavigatorRegistry *navigatorRegistry;
26
+ @property(nonatomic, strong) HBDNavigationCommandQueue *commandQueue;
53
27
 
54
- @property(nonatomic, strong, nullable) UIWindow *window;
55
- @property(nonatomic, strong) NSMutableArray<HBDReadyTask *> *viewHierarchyReadyTasks;
56
- @property(nonatomic, assign) BOOL flushingViewHierarchyReadyTasks;
57
- @property(nonatomic, strong) NSMutableArray<HBDNavigationCommand *> *commandQueue;
58
- @property(nonatomic, strong) NSMapTable<NSString *, UIViewController *> *prewarmingViewControllers;
59
- @property(nonatomic, assign) BOOL executingCommand;
60
- @property(nonatomic, assign) NSUInteger commandQueueGeneration;
61
- @property(nonatomic, assign, readwrite) NSUInteger rootGeneration;
62
- @property(nonatomic, strong, nullable) HBDNavigationCommand *activeCommand;
63
- @property(nonatomic, assign, readwrite) NSUInteger activeCommandId;
64
- @property(nonatomic, assign) NSUInteger nextCommandId;
28
+ @property(nonatomic, weak, nullable) UIWindow *window;
29
+ @property(nonatomic, strong, nullable) UIWindow *ownedFallbackWindow;
30
+ @property(nonatomic, weak, nullable) id activeNavigationModule;
65
31
 
66
32
  @end
67
33
 
@@ -83,23 +49,225 @@ typedef NS_ENUM(NSInteger, HBDCommandType) {
83
49
  _reactModuleRegisterCompleted = NO;
84
50
  _navigatorRegistry = [[HBDNavigatorRegistry alloc] init];
85
51
  _globalStyle = [[HBDGlobalStyle alloc] initWithOptions:@{}];
86
- _viewHierarchyReadyTasks = [NSMutableArray array];
87
- _commandQueue = [NSMutableArray array];
88
- _prewarmingViewControllers = [NSMapTable strongToWeakObjectsMapTable];
52
+ _commandQueue = [[HBDNavigationCommandQueue alloc] init];
53
+ _commandQueue.delegate = self;
54
+ NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
55
+ [center addObserver:self selector:@selector(handleHostWindowAvailabilityDidChange:) name:UISceneWillConnectNotification object:nil];
56
+ [center addObserver:self selector:@selector(handleHostWindowAvailabilityDidChange:) name:UISceneDidActivateNotification object:nil];
57
+ [center addObserver:self selector:@selector(handleHostWindowAvailabilityDidChange:) name:UIWindowDidBecomeKeyNotification object:nil];
89
58
  }
90
59
  return self;
91
60
  }
92
61
 
62
+ - (void)dealloc {
63
+ [[NSNotificationCenter defaultCenter] removeObserver:self];
64
+ }
65
+
66
+ - (void)handleHostWindowAvailabilityDidChange:(NSNotification *)notification {
67
+ dispatch_async(dispatch_get_main_queue(), ^{
68
+ if ([self mainWindow] != nil) {
69
+ [self.commandQueue drain];
70
+ [self markViewHierarchyReadyIfManagedNativeRootExists];
71
+ }
72
+ });
73
+ }
74
+
93
75
  - (void)setGlobalStyle:(HBDGlobalStyle *)globalStyle {
94
76
  _globalStyle = globalStyle ?: [[HBDGlobalStyle alloc] initWithOptions:@{}];
95
77
  [self applyGlobalStyleToHierarchy];
96
78
  }
97
79
 
80
+ #pragma mark - Command Queue Forwarding
81
+
82
+ - (BOOL)isViewHierarchyReady {
83
+ return self.commandQueue.isViewHierarchyReady;
84
+ }
85
+
86
+ - (void)setViewHierarchyReady:(BOOL)viewHierarchyReady {
87
+ self.commandQueue.viewHierarchyReady = viewHierarchyReady;
88
+ }
89
+
90
+ - (NSUInteger)activeCommandId {
91
+ return self.commandQueue.activeCommandId;
92
+ }
93
+
94
+ - (NSUInteger)rootGeneration {
95
+ return self.commandQueue.rootGeneration;
96
+ }
97
+
98
+ - (void)runWhenViewHierarchyReady:(dispatch_block_t)task {
99
+ [self runWhenViewHierarchyReady:task onCancelled:nil];
100
+ }
101
+
102
+ - (void)runWhenViewHierarchyReady:(dispatch_block_t)task onCancelled:(nullable dispatch_block_t)onCancelled {
103
+ [self.commandQueue runWhenViewHierarchyReady:task onCancelled:onCancelled];
104
+ }
105
+
106
+ - (void)enqueueNavigationWithSceneId:(NSString *)sceneId
107
+ action:(NSString *)action
108
+ extras:(NSDictionary *)extras
109
+ completion:(nullable HBDDispatchCompletion)completion {
110
+ [self.commandQueue enqueueNavigationWithSceneId:sceneId action:action extras:extras completion:completion];
111
+ }
112
+
113
+ - (void)enqueueSetRootWithLayout:(NSDictionary *)layout completion:(nullable HBDDispatchCompletion)completion {
114
+ [self.commandQueue enqueueSetRootWithLayout:layout completion:completion];
115
+ }
116
+
117
+ #pragma mark - HBDNavigationCommandQueueDelegate
118
+
119
+ - (BOOL)commandQueueIsReactModuleRegisterCompleted:(HBDNavigationCommandQueue *)queue {
120
+ return self.isReactModuleRegisterCompleted;
121
+ }
122
+
123
+ - (BOOL)commandQueueCanInstallRoot:(HBDNavigationCommandQueue *)queue {
124
+ return [self mainWindow] != nil;
125
+ }
126
+
127
+ - (BOOL)commandQueueHasRootLayout:(HBDNavigationCommandQueue *)queue {
128
+ return self.hasRootLayout;
129
+ }
130
+
131
+ - (BOOL)commandQueueHasInstalledRoot:(HBDNavigationCommandQueue *)queue {
132
+ UIViewController *root = [self mainWindow].rootViewController;
133
+ if (!root) {
134
+ return NO;
135
+ }
136
+ return [self hasRegisteredLayoutForViewController:root] || [self primaryViewControllerWithViewController:root] != nil;
137
+ }
138
+
139
+ - (BOOL)commandQueueHasInteractiveTransition:(HBDNavigationCommandQueue *)queue {
140
+ UIViewController *root = [self mainWindow].rootViewController;
141
+ if (!root) {
142
+ return NO;
143
+ }
144
+ NSArray<UIViewController *> *layers = [self presentationLayersFromRoot:root];
145
+ for (UIViewController *layer in layers) {
146
+ if ([self hasInteractiveTransitionInHierarchy:layer visited:[NSMutableSet set]]) {
147
+ return YES;
148
+ }
149
+ }
150
+ return NO;
151
+ }
152
+
153
+ - (BOOL)hasInteractiveTransitionInHierarchy:(nullable UIViewController *)vc visited:(NSMutableSet<UIViewController *> *)visited {
154
+ if (!vc || [visited containsObject:vc]) {
155
+ return NO;
156
+ }
157
+ [visited addObject:vc];
158
+ if (vc.transitionCoordinator.isInteractive) {
159
+ return YES;
160
+ }
161
+ for (UIViewController *child in vc.childViewControllers) {
162
+ if ([self hasInteractiveTransitionInHierarchy:child visited:visited]) {
163
+ return YES;
164
+ }
165
+ }
166
+ if ([vc isKindOfClass:[HBDDrawerController class]]) {
167
+ HBDDrawerController *drawer = (HBDDrawerController *)vc;
168
+ if ([self hasInteractiveTransitionInHierarchy:drawer.contentController visited:visited] ||
169
+ [self hasInteractiveTransitionInHierarchy:drawer.menuController visited:visited]) {
170
+ return YES;
171
+ }
172
+ }
173
+ return NO;
174
+ }
175
+
176
+ - (void)waitForTransitionOnViewController:(nullable UIViewController *)transitioning
177
+ commandId:(NSUInteger)commandId
178
+ retryCount:(NSUInteger)retryCount
179
+ action:(dispatch_block_t)action
180
+ onTimeout:(nullable dispatch_block_t)onTimeout {
181
+ if (!transitioning) {
182
+ if (action) {
183
+ action();
184
+ }
185
+ return;
186
+ }
187
+ __weak HBDReactBridgeManager *weakSelf = self;
188
+ dispatch_block_t runIfActive = ^{
189
+ HBDReactBridgeManager *strongSelf = weakSelf;
190
+ if (!strongSelf) {
191
+ return;
192
+ }
193
+ if (commandId != 0 && strongSelf.activeCommandId != commandId) {
194
+ return;
195
+ }
196
+ if (action) {
197
+ action();
198
+ }
199
+ };
200
+ id<UIViewControllerTransitionCoordinator> coordinator = transitioning.transitionCoordinator;
201
+ if (coordinator) {
202
+ [coordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
203
+ dispatch_async(dispatch_get_main_queue(), runIfActive);
204
+ }];
205
+ return;
206
+ }
207
+ if (retryCount < 3) {
208
+ dispatch_async(dispatch_get_main_queue(), runIfActive);
209
+ return;
210
+ }
211
+ if (onTimeout) {
212
+ onTimeout();
213
+ }
214
+ }
215
+
216
+ - (void)markViewHierarchyReadyIfManagedNativeRootExists {
217
+ if (!self.isReactModuleRegisterCompleted || self.hasRootLayout || self.isViewHierarchyReady || self.commandQueue.hasQueuedSetRoot) {
218
+ return;
219
+ }
220
+ UIViewController *root = [self mainWindow].rootViewController;
221
+ if (!root) {
222
+ return;
223
+ }
224
+ HBDViewController *primary = [self primaryViewControllerWithViewController:root];
225
+ if (primary && primary.lifecycleStateMachine.isFirstRendered) {
226
+ self.viewHierarchyReady = YES;
227
+ }
228
+ }
229
+
230
+ - (void)commandQueue:(HBDNavigationCommandQueue *)queue
231
+ executeSetRootWithLayout:(NSDictionary *)layout
232
+ completion:(HBDDispatchCompletion)completion {
233
+ UIViewController *vc = [self viewControllerWithLayout:layout];
234
+ if (!vc) {
235
+ RCTLogError(@"[Navigation] 无法创建 ViewController for layout: %@", layout);
236
+ completion(HBDDispatchResultFail(HBDDispatchCodeInvalidAction, @"Unable to create ViewController for layout."));
237
+ return;
238
+ }
239
+ if (![self mainWindow]) {
240
+ completion(HBDDispatchResultFail(HBDDispatchCodeHostUnavailable, @"Navigation host window is unavailable."));
241
+ return;
242
+ }
243
+ self.hasRootLayout = YES;
244
+ [self installRootViewController:vc completion:completion];
245
+ }
246
+
247
+ - (void)commandQueue:(HBDNavigationCommandQueue *)queue
248
+ executeDispatchWithSceneId:(NSString *)sceneId
249
+ action:(NSString *)action
250
+ extras:(NSDictionary *)extras
251
+ completion:(HBDDispatchCompletion)completion {
252
+ UIViewController *vc = [self viewControllerBySceneId:sceneId];
253
+ if (!vc) {
254
+ RCTLogInfo(@"[Navigation] Can't find target scene for action: %@", action);
255
+ completion(HBDDispatchResultFail(HBDDispatchCodeSceneNotFound, [NSString stringWithFormat:@"Can't find target scene for action: %@", action]));
256
+ return;
257
+ }
258
+
259
+ [self handleNavigationWithViewController:vc action:action extras:extras completion:completion];
260
+ }
261
+
98
262
  #pragma mark - Window Resolution
99
263
 
100
264
  - (nullable UIWindow *)mainWindow {
101
- if (self.window) {
102
- return self.window;
265
+ UIWindow *cached = self.window;
266
+ if (cached) {
267
+ UIWindowScene *scene = cached.windowScene;
268
+ if (!scene || scene.activationState != UISceneActivationStateUnattached) {
269
+ return cached;
270
+ }
103
271
  }
104
272
  UIWindow *resolved = [self discoverDefaultWindow];
105
273
  self.window = resolved;
@@ -131,30 +299,41 @@ typedef NS_ENUM(NSInteger, HBDCommandType) {
131
299
  if ([sceneDelegate respondsToSelector:@selector(window)]) {
132
300
  UIWindow *delegateWindow = [(id<UIWindowSceneDelegate>)sceneDelegate window];
133
301
  if (delegateWindow) {
302
+ self.ownedFallbackWindow = nil;
134
303
  return delegateWindow;
135
304
  }
136
305
  }
137
306
  if (@available(iOS 15.0, *)) {
138
307
  if (windowScene.keyWindow) {
308
+ self.ownedFallbackWindow = nil;
139
309
  return windowScene.keyWindow;
140
310
  }
141
311
  }
142
312
  for (UIWindow *window in windowScene.windows) {
143
313
  if (window.isKeyWindow) {
314
+ self.ownedFallbackWindow = nil;
144
315
  return window;
145
316
  }
146
317
  }
147
318
  if (windowScene.windows.firstObject) {
319
+ self.ownedFallbackWindow = nil;
148
320
  return windowScene.windows.firstObject;
149
321
  }
150
322
  UIWindow *newWindow = [[UIWindow alloc] initWithWindowScene:windowScene];
151
323
  newWindow.backgroundColor = UIColor.whiteColor;
152
324
  if ([sceneDelegate respondsToSelector:@selector(setWindow:)]) {
153
325
  [(id<UIWindowSceneDelegate>)sceneDelegate setWindow:newWindow];
326
+ self.ownedFallbackWindow = nil;
327
+ } else {
328
+ self.ownedFallbackWindow = newWindow;
154
329
  }
155
330
  return newWindow;
156
331
  }
157
332
 
333
+ if (NSBundle.mainBundle.infoDictionary[@"UIApplicationSceneManifest"] != nil) {
334
+ return nil;
335
+ }
336
+
158
337
  id<UIApplicationDelegate> appDelegate = RCTSharedApplication().delegate;
159
338
  UIWindow *appWindow = [appDelegate respondsToSelector:@selector(window)] ? appDelegate.window : nil;
160
339
  if (!appWindow) {
@@ -162,220 +341,136 @@ typedef NS_ENUM(NSInteger, HBDCommandType) {
162
341
  appWindow.backgroundColor = UIColor.whiteColor;
163
342
  if ([appDelegate respondsToSelector:@selector(setWindow:)]) {
164
343
  appDelegate.window = appWindow;
344
+ self.ownedFallbackWindow = nil;
345
+ } else {
346
+ self.ownedFallbackWindow = appWindow;
165
347
  }
348
+ } else {
349
+ self.ownedFallbackWindow = nil;
166
350
  }
167
351
  return appWindow;
168
352
  }
169
353
 
170
- #pragma mark - Readiness Gate & Command Queue
171
-
172
- - (void)setViewHierarchyReady:(BOOL)viewHierarchyReady {
173
- _viewHierarchyReady = viewHierarchyReady;
174
- if (viewHierarchyReady) {
175
- [self flushViewHierarchyReadyTasks];
176
- }
177
- }
178
-
179
- - (void)runWhenViewHierarchyReady:(dispatch_block_t)task {
180
- [self runWhenViewHierarchyReady:task onCancelled:nil];
181
- }
182
-
183
- - (void)runWhenViewHierarchyReady:(dispatch_block_t)task onCancelled:(nullable dispatch_block_t)onCancelled {
184
- if (!task) {
185
- return;
186
- }
187
- HBDReadyTask *entry = [HBDReadyTask new];
188
- entry.task = task;
189
- entry.onCancelled = onCancelled;
190
- [self.viewHierarchyReadyTasks addObject:entry];
191
- if (self.isViewHierarchyReady) {
192
- [self flushViewHierarchyReadyTasks];
193
- }
194
- }
354
+ #pragma mark - Root Presentation & Invalidation
195
355
 
196
- - (void)flushViewHierarchyReadyTasks {
197
- if (!self.isViewHierarchyReady || self.flushingViewHierarchyReadyTasks) {
198
- return;
199
- }
200
-
201
- self.flushingViewHierarchyReadyTasks = YES;
202
- while (self.isViewHierarchyReady && self.viewHierarchyReadyTasks.count > 0) {
203
- HBDReadyTask *entry = self.viewHierarchyReadyTasks.firstObject;
204
- [self.viewHierarchyReadyTasks removeObjectAtIndex:0];
205
- if (entry.task) {
206
- entry.task();
207
- }
208
- }
209
- self.flushingViewHierarchyReadyTasks = NO;
210
- }
211
-
212
- - (void)enqueueNavigationWithSceneId:(NSString *)sceneId
213
- action:(NSString *)action
214
- extras:(NSDictionary *)extras
215
- completion:(nullable HBDDispatchCompletion)completion {
216
- HBDNavigationCommand *command = [HBDNavigationCommand new];
217
- command.type = HBDCommandTypeDispatch;
218
- command.sceneId = sceneId;
219
- command.action = action;
220
- command.extras = extras;
221
- command.completion = completion;
222
- [self.commandQueue addObject:command];
223
- [self drainCommandQueue];
356
+ - (void)setRootViewController:(UIViewController *)rootViewController {
357
+ self.hasRootLayout = NO;
358
+ [self.commandQueue supersedeAllCommandsWithReason:@"Navigation command was superseded by a native setRootViewController call."];
359
+ [self installRootViewController:rootViewController completion:nil];
224
360
  }
225
361
 
226
- - (void)enqueueSetRootWithLayout:(NSDictionary *)layout completion:(nullable HBDDispatchCompletion)completion {
227
- NSArray<HBDNavigationCommand *> *superseded = [self.commandQueue copy];
228
- [self.commandQueue removeAllObjects];
229
- for (HBDNavigationCommand *cmd in superseded) {
230
- if (cmd.completion) {
231
- HBDDispatchCompletion cb = cmd.completion;
232
- cmd.completion = nil;
233
- cb(HBDDispatchResultFail(HBDDispatchCodeSuperseded, @"Navigation command was superseded by a newer setRoot call."));
362
+ - (void)installRootViewController:(UIViewController *)rootViewController
363
+ completion:(nullable HBDDispatchCompletion)completion {
364
+ NSUInteger generation = self.commandQueue.commandQueueGeneration;
365
+ UIWindow *targetWindow = [self mainWindow];
366
+ if (!targetWindow) {
367
+ if (completion) {
368
+ completion(HBDDispatchResultFail(HBDDispatchCodeHostUnavailable, @"Navigation host window is unavailable."));
234
369
  }
235
- }
236
-
237
- HBDNavigationCommand *command = [HBDNavigationCommand new];
238
- command.type = HBDCommandTypeSetRoot;
239
- command.layout = layout;
240
- command.completion = completion;
241
- [self.commandQueue addObject:command];
242
- [self drainCommandQueue];
243
- }
244
-
245
- - (void)drainCommandQueue {
246
- if (self.executingCommand || self.commandQueue.count == 0) {
247
370
  return;
248
371
  }
372
+ NSUInteger rootGen = [self.commandQueue nextRootGeneration];
249
373
 
250
- HBDNavigationCommand *peek = self.commandQueue.firstObject;
251
- if (peek.type == HBDCommandTypeSetRoot && !self.isReactModuleRegisterCompleted) {
252
- return;
253
- }
254
-
255
- HBDNavigationCommand *command = self.commandQueue.firstObject;
256
- [self.commandQueue removeObjectAtIndex:0];
257
- self.executingCommand = YES;
258
- self.activeCommand = command;
259
- self.activeCommandId = ++self.nextCommandId;
260
-
261
- NSUInteger generation = self.commandQueueGeneration;
262
- __block BOOL completed = NO;
263
- HBDDispatchCompletion finishCommand = ^(NSDictionary *result) {
264
- if (completed) {
265
- return;
266
- }
267
- completed = YES;
268
- HBDDispatchCompletion callback = command.completion;
269
- command.completion = nil;
270
- if (self.activeCommand == command) {
271
- self.activeCommand = nil;
272
- self.activeCommandId = 0;
273
- }
274
- if (generation != self.commandQueueGeneration) {
275
- return;
276
- }
277
- if (callback) {
278
- callback(result ?: HBDDispatchResultFail(HBDDispatchCodeInvalidAction, nil));
279
- }
280
- self.executingCommand = NO;
281
- dispatch_async(dispatch_get_main_queue(), ^{
282
- [self drainCommandQueue];
283
- });
284
- };
285
-
286
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(HBDCommandTimeoutSeconds * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
287
- if (!completed && generation == self.commandQueueGeneration) {
288
- RCTLogWarn(@"[Navigation] Navigation command timed out, unlocking command queue. action=%@", command.action ?: @"setRoot");
289
- if (command.type == HBDCommandTypeSetRoot) {
290
- self.rootGeneration += 1;
291
- if (self.didSetRoot == finishCommand) {
292
- self.didSetRoot = NULL;
293
- }
294
- }
295
- finishCommand(HBDDispatchResultFail(HBDDispatchCodeNotReady, @"Navigation command timed out."));
296
- }
297
- });
374
+ [self continueSetRootViewController:rootViewController
375
+ onWindow:targetWindow
376
+ generation:generation
377
+ rootGeneration:rootGen
378
+ retryCount:0
379
+ completion:completion];
380
+ }
298
381
 
299
- if (command.type == HBDCommandTypeSetRoot) {
300
- UIViewController *vc = [self viewControllerWithLayout:command.layout];
301
- if (!vc) {
302
- RCTLogError(@"[Navigation] 无法创建 ViewController for layout: %@", command.layout);
303
- finishCommand(HBDDispatchResultFail(HBDDispatchCodeInvalidAction, @"Unable to create ViewController for layout."));
304
- return;
305
- }
306
- if (![self mainWindow]) {
307
- finishCommand(HBDDispatchResultFail(HBDDispatchCodeHostUnavailable, @"Navigation host window is unavailable."));
308
- return;
309
- }
310
- self.viewHierarchyReady = NO;
311
- [self.prewarmingViewControllers removeAllObjects];
312
- self.hasRootLayout = YES;
313
- if (self.didSetRoot) {
314
- self.didSetRoot(HBDDispatchResultFail(HBDDispatchCodeSuperseded, @"setRoot was superseded by a newer call."));
315
- }
316
- self.didSetRoot = finishCommand;
317
- [self setRootViewController:vc];
382
+ - (void)continueSetRootViewController:(UIViewController *)rootViewController
383
+ onWindow:(UIWindow *)targetWindow
384
+ generation:(NSUInteger)generation
385
+ rootGeneration:(NSUInteger)rootGen
386
+ retryCount:(NSUInteger)retryCount
387
+ completion:(nullable HBDDispatchCompletion)completion {
388
+ if (generation != self.commandQueue.commandQueueGeneration || rootGen != self.rootGeneration) {
318
389
  return;
319
390
  }
320
391
 
321
- UIViewController *vc = [self viewControllerBySceneId:command.sceneId];
322
- if (!vc) {
323
- RCTLogInfo(@"[Navigation] Can't find target scene for action: %@", command.action);
324
- finishCommand(HBDDispatchResultFail(HBDDispatchCodeSceneNotFound, [NSString stringWithFormat:@"Can't find target scene for action: %@", command.action]));
392
+ UIViewController *currentRoot = targetWindow.rootViewController;
393
+ UIViewController *transitioning = nil;
394
+ NSMutableSet<UIViewController *> *visited = [NSMutableSet set];
395
+ UIViewController *cursor = currentRoot;
396
+ while (cursor != nil && ![visited containsObject:cursor]) {
397
+ [visited addObject:cursor];
398
+ if (cursor.transitionCoordinator != nil || cursor.isBeingDismissed || cursor.isBeingPresented) {
399
+ transitioning = cursor;
400
+ break;
401
+ }
402
+ cursor = cursor.presentedViewController;
403
+ }
404
+
405
+ if (transitioning && (transitioning.transitionCoordinator != nil || retryCount < 3)) {
406
+ __weak HBDReactBridgeManager *weakSelf = self;
407
+ [self waitForTransitionOnViewController:transitioning
408
+ commandId:self.activeCommandId
409
+ retryCount:retryCount
410
+ action:^{
411
+ [weakSelf continueSetRootViewController:rootViewController
412
+ onWindow:targetWindow
413
+ generation:generation
414
+ rootGeneration:rootGen
415
+ retryCount:retryCount + 1
416
+ completion:completion];
417
+ } onTimeout:nil];
325
418
  return;
326
419
  }
327
420
 
328
- [self handleNavigationWithViewController:vc action:command.action extras:command.extras completion:finishCommand];
329
- }
330
-
331
- - (void)setRootViewController:(UIViewController *)rootViewController {
332
- NSUInteger generation = self.commandQueueGeneration;
333
- NSUInteger rootGen = ++self.rootGeneration;
334
- UIWindow *targetWindow = [self mainWindow];
335
- if (!targetWindow) {
336
- HBDDispatchCompletion didSetRoot = self.didSetRoot;
337
- self.didSetRoot = NULL;
338
- if (didSetRoot) {
339
- didSetRoot(HBDDispatchResultFail(HBDDispatchCodeHostUnavailable, @"Navigation host window is unavailable."));
340
- }
341
- return;
342
- }
421
+ self.viewHierarchyReady = NO;
422
+ [[HBDNativeEvent getInstance] emitWillSetRoot];
343
423
 
344
- UIViewController *presentedViewController = targetWindow.rootViewController.presentedViewController;
424
+ UIViewController *presentedViewController = currentRoot.presentedViewController;
345
425
  if (presentedViewController && !presentedViewController.isBeingDismissed) {
346
- [targetWindow.rootViewController dismissViewControllerAnimated:NO completion:^{
347
- if (generation != self.commandQueueGeneration || rootGen != self.rootGeneration) {
426
+ if (currentRoot != rootViewController) {
427
+ [currentRoot hbd_markDestroyedRecursively];
428
+ }
429
+ [currentRoot hbd_dismissForRootResetAnimated:NO completion:^{
430
+ if (generation != self.commandQueue.commandQueueGeneration || rootGen != self.rootGeneration) {
348
431
  return;
349
432
  }
350
- [self performSetRootViewController:rootViewController onWindow:targetWindow animated:NO rootGeneration:rootGen];
433
+ [self performSetRootViewController:rootViewController
434
+ onWindow:targetWindow
435
+ animated:NO
436
+ generation:generation
437
+ rootGeneration:rootGen
438
+ completion:completion];
351
439
  }];
352
440
  } else {
353
- [self performSetRootViewController:rootViewController onWindow:targetWindow animated:YES rootGeneration:rootGen];
441
+ [self performSetRootViewController:rootViewController
442
+ onWindow:targetWindow
443
+ animated:YES
444
+ generation:generation
445
+ rootGeneration:rootGen
446
+ completion:completion];
354
447
  }
355
448
  }
356
449
 
357
450
  - (void)performSetRootViewController:(UIViewController *)rootViewController
358
451
  onWindow:(UIWindow *)targetWindow
359
452
  animated:(BOOL)animated
360
- rootGeneration:(NSUInteger)rootGen {
361
- NSUInteger generation = self.commandQueueGeneration;
362
- [[HBDNativeEvent getInstance] emitWillSetRoot];
453
+ generation:(NSUInteger)generation
454
+ rootGeneration:(NSUInteger)rootGen
455
+ completion:(nullable HBDDispatchCompletion)completion {
363
456
  void (^applyRoot)(void) = ^{
457
+ UIViewController *oldRoot = targetWindow.rootViewController;
364
458
  targetWindow.rootViewController = rootViewController;
365
459
  targetWindow.windowLevel = UIWindowLevelNormal;
366
460
  if (!targetWindow.isKeyWindow) {
367
461
  [targetWindow makeKeyAndVisible];
368
462
  }
463
+ if (oldRoot && oldRoot != rootViewController) {
464
+ [oldRoot hbd_markDestroyedRecursively];
465
+ }
369
466
  };
370
467
  void (^completeRoot)(void) = ^{
371
- if (generation != self.commandQueueGeneration || rootGen != self.rootGeneration) {
468
+ if (generation != self.commandQueue.commandQueueGeneration || rootGen != self.rootGeneration) {
372
469
  return;
373
470
  }
374
- HBDDispatchCompletion didSetRoot = self.didSetRoot;
375
- self.didSetRoot = NULL;
376
471
  self.viewHierarchyReady = YES;
377
- if (didSetRoot) {
378
- didSetRoot(HBDDispatchResultOk());
472
+ if (completion) {
473
+ completion(HBDDispatchResultOk());
379
474
  }
380
475
  [[HBDNativeEvent getInstance] emitDidSetRoot];
381
476
  };
@@ -394,50 +489,41 @@ typedef NS_ENUM(NSInteger, HBDCommandType) {
394
489
  }
395
490
  }
396
491
 
492
+ - (void)bindNavigationModule:(id)module {
493
+ if (!module) {
494
+ return;
495
+ }
496
+ dispatch_block_t bindBlock = ^{
497
+ id previous = self.activeNavigationModule;
498
+ if (previous && previous != module) {
499
+ [self invalidate];
500
+ }
501
+ self.activeNavigationModule = module;
502
+ };
503
+ if ([NSThread isMainThread]) {
504
+ bindBlock();
505
+ } else {
506
+ dispatch_async(dispatch_get_main_queue(), bindBlock);
507
+ }
508
+ }
509
+
510
+ - (void)invalidateForModule:(id)module {
511
+ if (!module || self.activeNavigationModule != module) {
512
+ return;
513
+ }
514
+ self.activeNavigationModule = nil;
515
+ [self invalidate];
516
+ }
517
+
397
518
  - (void)invalidate {
398
519
  RCTLogInfo(@"[Navigation] HBDReactBridgeManager#invalidate");
399
520
  [NSObject cancelPreviousPerformRequestsWithTarget:self];
400
521
 
401
522
  [[NSNotificationCenter defaultCenter] postNotificationName:HBDReactBridgeWillReloadNotification object:self];
402
523
 
403
- self.commandQueueGeneration += 1;
404
- self.rootGeneration += 1;
405
- self.executingCommand = NO;
406
- self.activeCommandId = 0;
407
-
408
- HBDNavigationCommand *activeCommand = self.activeCommand;
409
- self.activeCommand = nil;
410
- NSArray<HBDNavigationCommand *> *pendingCommands = [self.commandQueue copy];
411
- [self.commandQueue removeAllObjects];
412
- HBDDispatchCompletion didSetRoot = self.didSetRoot;
413
- self.didSetRoot = NULL;
414
- NSArray<HBDReadyTask *> *pendingReadyTasks = [self.viewHierarchyReadyTasks copy];
415
- [self.viewHierarchyReadyTasks removeAllObjects];
416
- self.viewHierarchyReady = NO;
417
- [self.prewarmingViewControllers removeAllObjects];
418
-
419
- NSDictionary *unavailable = HBDDispatchResultFail(HBDDispatchCodeHostUnavailable, @"Navigation host was invalidated.");
420
- if (activeCommand) {
421
- HBDDispatchCompletion completion = activeCommand.completion;
422
- activeCommand.completion = nil;
423
- if (completion) {
424
- completion(unavailable);
425
- }
426
- } else if (didSetRoot) {
427
- didSetRoot(unavailable);
428
- }
429
- for (HBDNavigationCommand *command in pendingCommands) {
430
- HBDDispatchCompletion completion = command.completion;
431
- command.completion = nil;
432
- if (completion) {
433
- completion(unavailable);
434
- }
435
- }
436
- for (HBDReadyTask *entry in pendingReadyTasks) {
437
- if (entry.onCancelled) {
438
- entry.onCancelled();
439
- }
440
- }
524
+ self.window = nil;
525
+ self.ownedFallbackWindow = nil;
526
+ [self.commandQueue invalidateWithReason:@"Navigation host was invalidated."];
441
527
 
442
528
  NSArray<id<HBDNavigator>> *navigators = [self.navigatorRegistry allNavigators];
443
529
  for (id<HBDNavigator> navigator in navigators) {
@@ -449,28 +535,54 @@ typedef NS_ENUM(NSInteger, HBDCommandType) {
449
535
 
450
536
  #pragma mark - Global Style & Registry
451
537
 
538
+ - (NSArray<UIViewController *> *)presentationLayersFromRoot:(nullable UIViewController *)root {
539
+ if (!root) {
540
+ return @[];
541
+ }
542
+ NSMutableArray<UIViewController *> *layers = [NSMutableArray array];
543
+ UIViewController *current = root;
544
+ while (current != nil && ![layers containsObject:current]) {
545
+ [layers addObject:current];
546
+ UIViewController *presented = current.presentedViewController;
547
+ if (presented && !presented.isBeingDismissed) {
548
+ current = presented;
549
+ } else {
550
+ break;
551
+ }
552
+ }
553
+ return layers;
554
+ }
555
+
452
556
  - (void)applyGlobalStyleToHierarchy {
453
- UIWindow *targetWindow = self.window;
557
+ UIWindow *targetWindow = [self mainWindow];
454
558
  if (!targetWindow.rootViewController) {
455
559
  return;
456
560
  }
457
561
  HBDGlobalStyle *style = self.globalStyle ?: [HBDGlobalStyle globalStyle];
458
- [self applyGlobalStyle:style toViewController:targetWindow.rootViewController];
562
+ NSArray<UIViewController *> *layers = [self presentationLayersFromRoot:targetWindow.rootViewController];
563
+ NSMutableSet<UIViewController *> *visited = [NSMutableSet set];
564
+ for (UIViewController *layer in layers) {
565
+ [self applyGlobalStyle:style toViewController:layer visited:visited];
566
+ }
459
567
  }
460
568
 
461
- - (void)applyGlobalStyle:(HBDGlobalStyle *)style toViewController:(UIViewController *)vc {
462
- if (!vc) {
569
+ - (void)applyGlobalStyle:(HBDGlobalStyle *)style
570
+ toViewController:(UIViewController *)vc
571
+ visited:(NSMutableSet<UIViewController *> *)visited {
572
+ if (!vc || [visited containsObject:vc]) {
463
573
  return;
464
574
  }
575
+ [visited addObject:vc];
465
576
  if ([vc isKindOfClass:[HBDTabBarController class]]) {
466
577
  [(HBDTabBarController *)vc applyGlobalStyle:style];
467
578
  }
468
579
  for (UIViewController *child in vc.childViewControllers) {
469
- [self applyGlobalStyle:style toViewController:child];
580
+ [self applyGlobalStyle:style toViewController:child visited:visited];
470
581
  }
471
- UIViewController *presented = vc.presentedViewController;
472
- if (presented && presented != vc && !presented.isBeingDismissed) {
473
- [self applyGlobalStyle:style toViewController:presented];
582
+ if ([vc isKindOfClass:[HBDDrawerController class]]) {
583
+ HBDDrawerController *drawer = (HBDDrawerController *)vc;
584
+ [self applyGlobalStyle:style toViewController:drawer.contentController visited:visited];
585
+ [self applyGlobalStyle:style toViewController:drawer.menuController visited:visited];
474
586
  }
475
587
  }
476
588
 
@@ -491,7 +603,10 @@ typedef NS_ENUM(NSInteger, HBDCommandType) {
491
603
  }
492
604
 
493
605
  - (void)registerReactModule:(NSString *)moduleName options:(NSDictionary *)options {
494
- _reactModules[moduleName] = options;
606
+ if (![moduleName isKindOfClass:[NSString class]] || moduleName.length == 0) {
607
+ return;
608
+ }
609
+ _reactModules[moduleName] = [options isKindOfClass:[NSDictionary class]] ? options : @{};
495
610
  }
496
611
 
497
612
  - (NSDictionary *)reactModuleOptions:(NSString *)moduleName {
@@ -509,16 +624,55 @@ typedef NS_ENUM(NSInteger, HBDCommandType) {
509
624
 
510
625
  - (void)endRegisterReactModule {
511
626
  _reactModuleRegisterCompleted = YES;
512
- [self drainCommandQueue];
627
+ [self.commandQueue drain];
513
628
  if (self.delegate != nil) {
514
629
  [self.delegate reactModuleRegisterDidCompleted:self];
515
630
  }
631
+ [self markViewHierarchyReadyIfManagedNativeRootExists];
632
+ [self syncPendingVisibleEmissions];
516
633
  [[NSNotificationCenter defaultCenter] postNotificationName:HBDReactModuleRegistryDidCompletedNotification object:nil];
517
634
  }
518
635
 
636
+ - (void)syncPendingVisibleEmissions {
637
+ UIViewController *root = [self mainWindow].rootViewController;
638
+ if (!root) {
639
+ return;
640
+ }
641
+ NSArray<UIViewController *> *layers = [self presentationLayersFromRoot:root];
642
+ NSMutableSet<UIViewController *> *visited = [NSMutableSet set];
643
+ for (UIViewController *layer in layers) {
644
+ [self syncPendingVisibleEmissionInHierarchy:layer visited:visited];
645
+ }
646
+ }
647
+
648
+ - (void)syncPendingVisibleEmissionInHierarchy:(nullable UIViewController *)vc visited:(NSMutableSet<UIViewController *> *)visited {
649
+ if (!vc || [visited containsObject:vc]) {
650
+ return;
651
+ }
652
+ [visited addObject:vc];
653
+ if ([vc isKindOfClass:[HBDViewController class]]) {
654
+ [(HBDViewController *)vc syncPendingVisibleEmissionIfNeeded];
655
+ }
656
+ for (UIViewController *child in vc.childViewControllers) {
657
+ [self syncPendingVisibleEmissionInHierarchy:child visited:visited];
658
+ }
659
+ if ([vc isKindOfClass:[HBDDrawerController class]]) {
660
+ HBDDrawerController *drawer = (HBDDrawerController *)vc;
661
+ [self syncPendingVisibleEmissionInHierarchy:drawer.contentController visited:visited];
662
+ [self syncPendingVisibleEmissionInHierarchy:drawer.menuController visited:visited];
663
+ }
664
+ }
665
+
666
+ - (BOOL)hasRegisteredLayoutForViewController:(UIViewController *)vc {
667
+ return [self.navigatorRegistry layoutForViewController:vc] != nil;
668
+ }
669
+
519
670
  #pragma mark - ViewController Factory & Hierarchy Queries
520
671
 
521
672
  - (UIViewController *)createViewControllerWithLayout:(NSDictionary *)layout {
673
+ if (![layout isKindOfClass:[NSDictionary class]]) {
674
+ return nil;
675
+ }
522
676
  NSArray<NSString *> *layouts = [self.navigatorRegistry allLayouts];
523
677
  for (NSString *name in layouts) {
524
678
  if (![[layout allKeys] containsObject:name]) {
@@ -551,16 +705,22 @@ typedef NS_ENUM(NSInteger, HBDCommandType) {
551
705
  return vc;
552
706
  }
553
707
 
554
- - (HBDViewController *)viewControllerWithModuleName:(NSString *)moduleName props:(NSDictionary *)props options:(NSDictionary *)options {
708
+ - (nullable HBDViewController *)viewControllerWithModuleName:(NSString *)moduleName props:(NSDictionary *)props options:(NSDictionary *)options {
555
709
  if (!self.isReactModuleRegisterCompleted) {
556
- @throw [NSException exceptionWithName:@"IllegalStateException" reason:@"React module hasn't register completed." userInfo:@{}];
710
+ RCTLogError(@"[Navigation] React module registration hasn't completed yet; cannot create module '%@'.", moduleName);
711
+ return nil;
557
712
  }
558
713
 
559
- if (!props) {
714
+ if (![moduleName isKindOfClass:[NSString class]] || moduleName.length == 0) {
715
+ RCTLogError(@"[Navigation] Module name is empty.");
716
+ return nil;
717
+ }
718
+
719
+ if (![props isKindOfClass:[NSDictionary class]]) {
560
720
  props = @{};
561
721
  }
562
722
 
563
- if (!options) {
723
+ if (![options isKindOfClass:[NSDictionary class]]) {
564
724
  options = @{};
565
725
  }
566
726
 
@@ -570,7 +730,10 @@ typedef NS_ENUM(NSInteger, HBDCommandType) {
570
730
  return [[HBDReactViewController alloc] initWithModuleName:moduleName props:props options:options];
571
731
  } else {
572
732
  Class clazz = [self nativeModuleClass:moduleName];
573
- NSCAssert([self hasNativeModule:moduleName], @"Can't find module named with %@ , do you forget to register?", moduleName);
733
+ if (!clazz) {
734
+ RCTLogError(@"[Navigation] Can't find module named '%@'. Did you forget to register it?", moduleName);
735
+ return nil;
736
+ }
574
737
  return [[clazz alloc] initWithModuleName:moduleName props:props options:options];
575
738
  }
576
739
  }
@@ -580,49 +743,46 @@ typedef NS_ENUM(NSInteger, HBDCommandType) {
580
743
  return nil;
581
744
  }
582
745
  UIWindow *targetWindow = [self mainWindow];
583
- UIViewController *vc = [self viewControllerWithSceneId:sceneId viewController:targetWindow.rootViewController];
584
- if (vc) {
585
- return vc;
586
- }
587
- return [self.prewarmingViewControllers objectForKey:sceneId];
588
- }
589
-
590
- - (void)registerPrewarmingViewController:(UIViewController *)vc {
591
- NSString *sceneId = vc.hbd_sceneId;
592
- if (sceneId.length == 0) {
593
- return;
594
- }
595
- [self.prewarmingViewControllers setObject:vc forKey:sceneId];
596
- }
597
-
598
- - (void)unregisterPrewarmingViewController:(UIViewController *)vc {
599
- NSString *sceneId = vc.hbd_sceneId;
600
- if (sceneId.length == 0) {
601
- return;
602
- }
603
- if ([self.prewarmingViewControllers objectForKey:sceneId] == vc) {
604
- [self.prewarmingViewControllers removeObjectForKey:sceneId];
746
+ NSArray<UIViewController *> *layers = [self presentationLayersFromRoot:targetWindow.rootViewController];
747
+ NSMutableSet<UIViewController *> *visited = [NSMutableSet set];
748
+ for (NSInteger i = (NSInteger)layers.count - 1; i >= 0; i--) {
749
+ UIViewController *found = [self viewControllerWithSceneId:sceneId
750
+ viewController:layers[(NSUInteger)i]
751
+ visited:visited];
752
+ if (found) {
753
+ return found;
754
+ }
605
755
  }
756
+ return nil;
606
757
  }
607
758
 
608
- - (nullable UIViewController *)viewControllerWithSceneId:(NSString *)sceneId viewController:(nullable UIViewController *)vc {
609
- if (!vc) {
759
+ - (nullable UIViewController *)viewControllerWithSceneId:(NSString *)sceneId
760
+ viewController:(nullable UIViewController *)vc
761
+ visited:(NSMutableSet<UIViewController *> *)visited {
762
+ if (!vc || [visited containsObject:vc]) {
610
763
  return nil;
611
764
  }
765
+ [visited addObject:vc];
612
766
  if ([vc.hbd_sceneId isEqualToString:sceneId]) {
613
767
  return vc;
614
768
  }
615
769
 
616
- UIViewController *presentedViewController = vc.presentedViewController;
617
- if (presentedViewController && ![presentedViewController isBeingDismissed]) {
618
- UIViewController *target = [self viewControllerWithSceneId:sceneId viewController:presentedViewController];
770
+ NSArray<UIViewController *> *children = vc.childViewControllers;
771
+ for (NSInteger i = (NSInteger)children.count - 1; i >= 0; i--) {
772
+ UIViewController *target = [self viewControllerWithSceneId:sceneId
773
+ viewController:children[(NSUInteger)i]
774
+ visited:visited];
619
775
  if (target) {
620
776
  return target;
621
777
  }
622
778
  }
623
-
624
- for (UIViewController *child in vc.childViewControllers) {
625
- UIViewController *target = [self viewControllerWithSceneId:sceneId viewController:child];
779
+ if ([vc isKindOfClass:[HBDDrawerController class]]) {
780
+ HBDDrawerController *drawer = (HBDDrawerController *)vc;
781
+ UIViewController *target = [self viewControllerWithSceneId:sceneId viewController:drawer.menuController visited:visited];
782
+ if (target) {
783
+ return target;
784
+ }
785
+ target = [self viewControllerWithSceneId:sceneId viewController:drawer.contentController visited:visited];
626
786
  if (target) {
627
787
  return target;
628
788
  }
@@ -632,17 +792,30 @@ typedef NS_ENUM(NSInteger, HBDCommandType) {
632
792
  }
633
793
 
634
794
  - (nullable NSString *)findSceneIdByModuleName:(NSString *)moduleName {
635
- UIWindow *targetWindow = [self mainWindow];
636
- if (!targetWindow.rootViewController) {
795
+ if (moduleName.length == 0) {
637
796
  return nil;
638
797
  }
639
- return [self findSceneIdByModuleName:moduleName withViewController:targetWindow.rootViewController];
798
+ UIWindow *targetWindow = [self mainWindow];
799
+ NSArray<UIViewController *> *layers = [self presentationLayersFromRoot:targetWindow.rootViewController];
800
+ NSMutableSet<UIViewController *> *visited = [NSMutableSet set];
801
+ for (NSInteger i = (NSInteger)layers.count - 1; i >= 0; i--) {
802
+ NSString *sceneId = [self findSceneIdByModuleName:moduleName
803
+ withViewController:layers[(NSUInteger)i]
804
+ visited:visited];
805
+ if (sceneId) {
806
+ return sceneId;
807
+ }
808
+ }
809
+ return nil;
640
810
  }
641
811
 
642
- - (nullable NSString *)findSceneIdByModuleName:(NSString *)moduleName withViewController:(nullable UIViewController *)vc {
643
- if (!vc) {
812
+ - (nullable NSString *)findSceneIdByModuleName:(NSString *)moduleName
813
+ withViewController:(nullable UIViewController *)vc
814
+ visited:(NSMutableSet<UIViewController *> *)visited {
815
+ if (!vc || [visited containsObject:vc]) {
644
816
  return nil;
645
817
  }
818
+ [visited addObject:vc];
646
819
  if ([vc isKindOfClass:[HBDViewController class]]) {
647
820
  HBDViewController *hbd = (HBDViewController *)vc;
648
821
  if ([moduleName isEqualToString:hbd.moduleName]) {
@@ -650,16 +823,22 @@ typedef NS_ENUM(NSInteger, HBDCommandType) {
650
823
  }
651
824
  }
652
825
 
653
- UIViewController *presented = vc.presentedViewController;
654
- if (presented && vc != presented && !presented.isBeingDismissed) {
655
- NSString *sceneId = [self findSceneIdByModuleName:moduleName withViewController:presented];
826
+ NSArray<UIViewController *> *children = vc.childViewControllers;
827
+ for (NSInteger i = (NSInteger)children.count - 1; i >= 0; i--) {
828
+ NSString *sceneId = [self findSceneIdByModuleName:moduleName
829
+ withViewController:children[(NSUInteger)i]
830
+ visited:visited];
656
831
  if (sceneId) {
657
832
  return sceneId;
658
833
  }
659
834
  }
660
-
661
- for (UIViewController *child in vc.childViewControllers) {
662
- NSString *sceneId = [self findSceneIdByModuleName:moduleName withViewController:child];
835
+ if ([vc isKindOfClass:[HBDDrawerController class]]) {
836
+ HBDDrawerController *drawer = (HBDDrawerController *)vc;
837
+ NSString *sceneId = [self findSceneIdByModuleName:moduleName withViewController:drawer.menuController visited:visited];
838
+ if (sceneId) {
839
+ return sceneId;
840
+ }
841
+ sceneId = [self findSceneIdByModuleName:moduleName withViewController:drawer.contentController visited:visited];
663
842
  if (sceneId) {
664
843
  return sceneId;
665
844
  }
@@ -674,17 +853,41 @@ typedef NS_ENUM(NSInteger, HBDCommandType) {
674
853
  }
675
854
 
676
855
  - (HBDViewController *)primaryViewControllerWithViewController:(UIViewController *)vc {
677
- UIViewController *presented = vc.presentedViewController;
678
- if (presented && !presented.beingDismissed && ![presented isKindOfClass:[UIAlertController class]]) {
679
- return [self primaryViewControllerWithViewController:presented];
856
+ if (!vc) {
857
+ return nil;
680
858
  }
859
+ NSArray<UIViewController *> *layers = [self presentationLayersFromRoot:vc];
860
+ for (NSInteger i = (NSInteger)layers.count - 1; i >= 0; i--) {
861
+ UIViewController *layer = layers[(NSUInteger)i];
862
+ if ([layer isKindOfClass:[UIAlertController class]]) {
863
+ continue;
864
+ }
865
+ HBDViewController *primary = [self primaryViewControllerInHierarchy:layer];
866
+ if (primary) {
867
+ return primary;
868
+ }
869
+ }
870
+ return nil;
871
+ }
681
872
 
873
+ - (nullable HBDViewController *)primaryViewControllerInHierarchy:(nullable UIViewController *)vc {
874
+ if (!vc) {
875
+ return nil;
876
+ }
682
877
  NSString *layout = [self.navigatorRegistry layoutForViewController:vc];
683
878
  if (layout) {
684
879
  id<HBDNavigator> navigator = [self.navigatorRegistry navigatorForLayout:layout];
685
880
  return [navigator primaryViewControllerWithViewController:vc];
686
881
  }
687
-
882
+ if ([vc isKindOfClass:[HBDViewController class]]) {
883
+ return (HBDViewController *)vc;
884
+ }
885
+ if ([vc isKindOfClass:[UINavigationController class]]) {
886
+ return [self primaryViewControllerInHierarchy:((UINavigationController *)vc).topViewController];
887
+ }
888
+ if ([vc isKindOfClass:[UITabBarController class]]) {
889
+ return [self primaryViewControllerInHierarchy:((UITabBarController *)vc).selectedViewController];
890
+ }
688
891
  return nil;
689
892
  }
690
893
 
@@ -735,6 +938,24 @@ typedef NS_ENUM(NSInteger, HBDCommandType) {
735
938
  [navigator handleNavigationWithViewController:vc action:action extras:extras completion:completion];
736
939
  }
737
940
 
941
+ - (NSDictionary *)removalPlanWithSceneId:(NSString *)sceneId action:(NSString *)action extras:(NSDictionary *)extras {
942
+ UIViewController *vc = [self viewControllerBySceneId:sceneId];
943
+ if (!vc) {
944
+ return HBDRemovalPlan(HBDDispatchCodeSceneNotFound, @[], nil, nil);
945
+ }
946
+ id<HBDNavigator> navigator = [self.navigatorRegistry navigatorForAction:action];
947
+ if (!navigator) {
948
+ return HBDRemovalPlan(HBDDispatchCodeInvalidAction, @[], nil, nil);
949
+ }
950
+ if ([navigator respondsToSelector:@selector(removalPlanWithViewController:action:extras:)]) {
951
+ NSDictionary *plan = [navigator removalPlanWithViewController:vc action:action extras:extras ?: @{}];
952
+ if (plan) {
953
+ return plan;
954
+ }
955
+ }
956
+ return HBDRemovalPlan(@"noop", @[], nil, nil);
957
+ }
958
+
738
959
  - (void)registerNavigator:(id<HBDNavigator>)navigator {
739
960
  [self.navigatorRegistry registerNavigator:navigator];
740
961
  }