hybrid-navigation 4.0.8 → 5.0.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 (149) hide show
  1. package/HybridNavigation.podspec +9 -1
  2. package/README.md +12 -4
  3. package/android/build.gradle +5 -4
  4. package/android/proguard-rules.pro +3 -0
  5. package/android/src/main/java/com/navigation/androidx/AwesomeActivity.java +114 -23
  6. package/android/src/main/java/com/navigation/androidx/AwesomeFragment.java +116 -13
  7. package/android/src/main/java/com/navigation/androidx/DrawableUtils.java +83 -18
  8. package/android/src/main/java/com/navigation/androidx/DrawerFragment.java +192 -28
  9. package/android/src/main/java/com/navigation/androidx/FragmentHelper.java +11 -0
  10. package/android/src/main/java/com/navigation/androidx/HybridDrawerLayout.java +563 -143
  11. package/android/src/main/java/com/navigation/androidx/LifecycleDelegate.java +47 -15
  12. package/android/src/main/java/com/navigation/androidx/PresentableActivity.java +3 -0
  13. package/android/src/main/java/com/navigation/androidx/PresentationDelegate.java +8 -3
  14. package/android/src/main/java/com/navigation/androidx/StackDelegate.java +29 -9
  15. package/android/src/main/java/com/navigation/androidx/StackFragment.java +28 -3
  16. package/android/src/main/java/com/navigation/androidx/SystemUI.java +59 -90
  17. package/android/src/main/java/com/navigation/androidx/TabBar.java +21 -2
  18. package/android/src/main/java/com/navigation/androidx/TabBarFragment.java +24 -7
  19. package/android/src/main/java/com/navigation/androidx/TabView.java +11 -2
  20. package/android/src/main/java/com/reactnative/hybridnavigation/DispatchResult.java +59 -0
  21. package/android/src/main/java/com/reactnative/hybridnavigation/Garden.java +18 -10
  22. package/android/src/main/java/com/reactnative/hybridnavigation/GlobalStyle.java +15 -2
  23. package/android/src/main/java/com/reactnative/hybridnavigation/HBDRootView.java +3 -8
  24. package/android/src/main/java/com/reactnative/hybridnavigation/HybridFragment.java +113 -6
  25. package/android/src/main/java/com/reactnative/hybridnavigation/HybridNavigationPackage.java +13 -24
  26. package/android/src/main/java/com/reactnative/hybridnavigation/NativeEvent.java +5 -1
  27. package/android/src/main/java/com/reactnative/hybridnavigation/NativeNavigation.java +196 -194
  28. package/android/src/main/java/com/reactnative/hybridnavigation/NavigationCommandQueue.java +499 -0
  29. package/android/src/main/java/com/reactnative/hybridnavigation/Navigator.java +4 -1
  30. package/android/src/main/java/com/reactnative/hybridnavigation/OrientationCoordinator.java +162 -0
  31. package/android/src/main/java/com/reactnative/hybridnavigation/ReactAppCompatActivity.java +103 -66
  32. package/android/src/main/java/com/reactnative/hybridnavigation/ReactFragment.java +50 -87
  33. package/android/src/main/java/com/reactnative/hybridnavigation/ReactManager.java +156 -77
  34. package/android/src/main/java/com/reactnative/hybridnavigation/ReactStackFragment.java +6 -6
  35. package/android/src/main/java/com/reactnative/hybridnavigation/ReactSurfaceCleanup.java +3 -1
  36. package/android/src/main/java/com/reactnative/hybridnavigation/ReactTabBarProvider.java +8 -1
  37. package/android/src/main/java/com/reactnative/hybridnavigation/SceneLifecycleState.java +21 -0
  38. package/android/src/main/java/com/reactnative/hybridnavigation/SceneLifecycleStateMachine.java +116 -0
  39. package/android/src/main/java/com/reactnative/hybridnavigation/ScreenOrientation.java +42 -0
  40. package/android/src/main/java/com/reactnative/hybridnavigation/Utils.java +17 -0
  41. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/DrawerNavigator.java +14 -6
  42. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/ScreenNavigator.java +62 -22
  43. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/StackNavigator.java +35 -16
  44. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/TabNavigator.java +16 -13
  45. package/dist/DeepLink.d.ts +3 -2
  46. package/dist/DeepLink.js +37 -14
  47. package/dist/NativeEvent.d.ts +4 -12
  48. package/dist/NativeNavigation.d.ts +17 -19
  49. package/dist/Navigation.d.ts +21 -19
  50. package/dist/Navigation.js +83 -89
  51. package/dist/NavigationComponentWrap.d.ts +4 -3
  52. package/dist/NavigationComponentWrap.js +3 -6
  53. package/dist/Navigator.d.ts +31 -42
  54. package/dist/Navigator.js +97 -90
  55. package/dist/Options.d.ts +3 -1
  56. package/dist/Route.d.ts +85 -18
  57. package/dist/handler/DispatchCommandHandler.d.ts +10 -6
  58. package/dist/handler/DispatchCommandHandler.js +41 -17
  59. package/dist/handler/LayoutCommandHandler.d.ts +7 -7
  60. package/dist/handler/LayoutCommandHandler.js +21 -15
  61. package/dist/handler/LifecycleEventHandler.d.ts +26 -0
  62. package/dist/handler/LifecycleEventHandler.js +136 -0
  63. package/dist/handler/ResultEventHandler.d.ts +14 -9
  64. package/dist/handler/ResultEventHandler.js +88 -13
  65. package/dist/hooks.d.ts +5 -2
  66. package/dist/hooks.js +26 -4
  67. package/dist/index.d.ts +13 -0
  68. package/dist/index.js +0 -2
  69. package/dist/routeConfigRegistry.d.ts +6 -0
  70. package/dist/routeConfigRegistry.js +19 -0
  71. package/dist/router/drawer.d.ts +2 -2
  72. package/dist/router/drawer.js +6 -3
  73. package/dist/router/index.d.ts +8 -5
  74. package/dist/router/index.js +127 -43
  75. package/dist/router/screen.d.ts +2 -2
  76. package/dist/router/screen.js +1 -1
  77. package/dist/router/stack.d.ts +2 -2
  78. package/dist/router/stack.js +59 -30
  79. package/dist/router/tabs.d.ts +2 -2
  80. package/dist/router/tabs.js +4 -1
  81. package/dist/store.d.ts +2 -2
  82. package/ios/HBDGlobalStyle.h +26 -0
  83. package/ios/{GlobalStyle.mm → HBDGlobalStyle.mm} +30 -27
  84. package/ios/HybridNavigation.h +4 -1
  85. package/ios/Navigator/HBDDrawerNavigator.mm +24 -8
  86. package/ios/Navigator/HBDNavigatorRegistry.mm +1 -0
  87. package/ios/Navigator/HBDScreenNavigator.mm +106 -52
  88. package/ios/Navigator/HBDStackNavigator.mm +109 -61
  89. package/ios/Navigator/HBDTabNavigator.mm +32 -14
  90. package/ios/React/HBDNativeNavigation.mm +109 -131
  91. package/ios/React/HBDNavigator.h +29 -5
  92. package/ios/React/HBDOrientationCoordinator.h +27 -0
  93. package/ios/React/HBDOrientationCoordinator.mm +282 -0
  94. package/ios/React/HBDReactBridgeManager.h +37 -19
  95. package/ios/React/HBDReactBridgeManager.mm +490 -96
  96. package/ios/React/HBDSceneLifecycleStateMachine.h +38 -0
  97. package/ios/React/HBDSceneLifecycleStateMachine.mm +180 -0
  98. package/ios/View/HBDRootView.h +2 -0
  99. package/ios/View/HBDRootView.mm +148 -0
  100. package/ios/View/UIImage+WithBadge.h +3 -3
  101. package/ios/View/UIImage+WithBadge.mm +10 -15
  102. package/ios/ViewController/HBDDrawerController.mm +127 -6
  103. package/ios/ViewController/HBDNavigationController.mm +17 -4
  104. package/ios/ViewController/HBDReactViewController.mm +59 -121
  105. package/ios/ViewController/HBDTabBarController.h +4 -0
  106. package/ios/ViewController/HBDTabBarController.mm +59 -44
  107. package/ios/ViewController/HBDViewController.h +10 -3
  108. package/ios/ViewController/HBDViewController.mm +161 -147
  109. package/ios/ViewController/UINavigationController+HBD.h +2 -2
  110. package/ios/ViewController/UINavigationController+HBD.mm +3 -3
  111. package/ios/ViewController/UIViewController+HBD.h +17 -10
  112. package/ios/ViewController/UIViewController+HBD.mm +85 -69
  113. package/package.json +1 -1
  114. package/src/DeepLink.ts +41 -14
  115. package/src/NativeEvent.ts +4 -14
  116. package/src/NativeNavigation.ts +22 -33
  117. package/src/Navigation.ts +162 -121
  118. package/src/NavigationComponentWrap.tsx +9 -9
  119. package/src/Navigator.ts +191 -153
  120. package/src/Options.ts +3 -1
  121. package/src/Route.ts +134 -19
  122. package/src/handler/DispatchCommandHandler.ts +55 -21
  123. package/src/handler/LayoutCommandHandler.ts +36 -17
  124. package/src/handler/LifecycleEventHandler.ts +172 -0
  125. package/src/handler/ResultEventHandler.ts +112 -19
  126. package/src/hooks.ts +39 -7
  127. package/src/index.ts +26 -2
  128. package/src/routeConfigRegistry.ts +26 -0
  129. package/src/router/drawer.ts +8 -5
  130. package/src/router/index.ts +161 -50
  131. package/src/router/screen.ts +3 -3
  132. package/src/router/stack.ts +80 -29
  133. package/src/router/tabs.ts +15 -7
  134. package/src/store.ts +1 -1
  135. package/android/src/main/java/androidx/fragment/app/InternalFragment.java +0 -22
  136. package/android/src/main/java/com/navigation/androidx/SystemUI30.java +0 -87
  137. package/android/src/main/java/com/reactnative/hybridnavigation/NativeGarden.java +0 -176
  138. package/android/src/main/java/com/reactnative/hybridnavigation/ReactTabBar.java +0 -130
  139. package/android/src/main/res/layout/nav_tab_bar_react.xml +0 -32
  140. package/android/src/main/res/layout/nav_tab_bar_react_indeterminate.xml +0 -32
  141. package/dist/NativeGarden.d.ts +0 -13
  142. package/dist/NativeGarden.js +0 -2
  143. package/dist/handler/VisibilityEventHandler.d.ts +0 -13
  144. package/dist/handler/VisibilityEventHandler.js +0 -39
  145. package/ios/GlobalStyle.h +0 -15
  146. package/ios/React/HBDNativeGarden.h +0 -10
  147. package/ios/React/HBDNativeGarden.mm +0 -111
  148. package/src/NativeGarden.ts +0 -16
  149. package/src/handler/VisibilityEventHandler.ts +0 -52
@@ -1,29 +1,30 @@
1
1
  import { AppRegistry } from 'react-native';
2
- import LayoutCommandHandler from './handler/LayoutCommandHandler';
3
- import DispatchCommandHandler from './handler/DispatchCommandHandler';
4
- import ResultEventHandler from './handler/ResultEventHandler';
5
- import VisibilityEventHandler from './handler/VisibilityEventHandler';
6
- import NativeEvent from './NativeEvent';
2
+ import { layoutHandler } from './handler/LayoutCommandHandler';
3
+ import { dispatchHandler } from './handler/DispatchCommandHandler';
4
+ import { RESULT_BLOCK, RESULT_CANCEL, RESULT_OK, resultHandler, } from './handler/ResultEventHandler';
5
+ import { lifecycleHandler, } from './handler/LifecycleEventHandler';
7
6
  import NativeNavigation from './NativeNavigation';
8
- import NativeGarden from './NativeGarden';
9
- export const { RESULT_CANCEL, RESULT_OK, RESULT_BLOCK } = NativeNavigation.getConstants();
7
+ import store from './store';
8
+ import { withNavigation } from './NavigationComponentWrap';
9
+ import { bumpRouteConfigVersion, clearRouteConfigs, setRouteConfig, } from './routeConfigRegistry';
10
+ export { RESULT_CANCEL, RESULT_OK, RESULT_BLOCK };
10
11
  class NavigationImpl {
11
- dispatchHandler = new DispatchCommandHandler();
12
- resultHandler = new ResultEventHandler();
13
- layoutHandler = new LayoutCommandHandler();
14
- visibilityHandler = new VisibilityEventHandler();
15
12
  constructor() {
16
- this.handleTabSwitch();
17
- this.layoutHandler.handleRootLayoutChange();
18
- this.visibilityHandler.handleComponentVisibility();
19
- this.resultHandler.handleComponentResult();
13
+ layoutHandler.handleRootLayoutChange(() => {
14
+ resultHandler.invalidateAll('root_reset');
15
+ });
16
+ lifecycleHandler.handleSceneLifecycle(sceneId => {
17
+ resultHandler.invalidateResultEventListener(sceneId, 'unmounted');
18
+ store.removeNavigator(sceneId);
19
+ });
20
+ resultHandler.handleComponentResult();
20
21
  }
21
- wrap;
22
22
  hoc;
23
23
  registerEnded = false;
24
24
  startRegisterComponent(hoc) {
25
25
  this.hoc = hoc;
26
26
  this.registerEnded = false;
27
+ clearRouteConfigs();
27
28
  NativeNavigation.startRegisterReactComponent();
28
29
  }
29
30
  endRegisterComponent() {
@@ -32,127 +33,120 @@ class NavigationImpl {
32
33
  return;
33
34
  }
34
35
  this.registerEnded = true;
36
+ bumpRouteConfigVersion();
35
37
  NativeNavigation.endRegisterReactComponent();
36
38
  }
37
39
  registerComponent(appKey, getComponentFunc, routeConfig) {
38
- if (routeConfig) {
39
- this.registerRoute(appKey, routeConfig);
40
+ if (routeConfig && (routeConfig.path || routeConfig.dependency || routeConfig.mode)) {
41
+ setRouteConfig(appKey, routeConfig);
42
+ }
43
+ const hoc = this.hoc;
44
+ if (routeConfig?.options !== undefined || routeConfig?.lazy === true) {
45
+ NativeNavigation.registerReactComponent(appKey, routeConfig.options ?? {});
46
+ let CachedRootComponent;
47
+ AppRegistry.registerComponent(appKey, () => {
48
+ if (!CachedRootComponent) {
49
+ let WrappedComponent = getComponentFunc();
50
+ if (__DEV__ && WrappedComponent?.navigationItem) {
51
+ console.warn(`[hybrid-navigation] "${appKey}" enabled lazy registration via registerComponent(), so withNavigationItem() on the component is ignored.`);
52
+ }
53
+ if (hoc) {
54
+ WrappedComponent = hoc(WrappedComponent);
55
+ }
56
+ CachedRootComponent = withNavigation(appKey)(WrappedComponent);
57
+ }
58
+ return CachedRootComponent;
59
+ });
60
+ return;
40
61
  }
41
62
  let WrappedComponent = getComponentFunc();
42
- if (this.hoc) {
43
- WrappedComponent = this.hoc(WrappedComponent);
63
+ if (hoc) {
64
+ WrappedComponent = hoc(WrappedComponent);
44
65
  }
45
66
  const options = WrappedComponent.navigationItem || {};
46
67
  NativeNavigation.registerReactComponent(appKey, options);
47
- let RootComponent = this.wrap(appKey)(WrappedComponent);
68
+ const RootComponent = withNavigation(appKey)(WrappedComponent);
48
69
  AppRegistry.registerComponent(appKey, () => RootComponent);
49
70
  }
50
- _routeConfigs = new Map();
51
- registerRoute(moduleName, route) {
52
- this._routeConfigs.set(moduleName, route);
71
+ addLifecycleListener(sceneId, listener) {
72
+ lifecycleHandler.addLifecycleListener(sceneId, listener);
73
+ return {
74
+ remove: () => lifecycleHandler.removeLifecycleListener(sceneId, listener),
75
+ };
53
76
  }
54
- routeConfigs() {
55
- return this._routeConfigs;
77
+ addGlobalLifecycleListener(listener) {
78
+ lifecycleHandler.addGlobalLifecycleListener(listener);
79
+ return {
80
+ remove: () => lifecycleHandler.removeGlobalLifecycleListener(listener),
81
+ };
56
82
  }
57
- setNavigationComponentWrap(wrap) {
58
- this.wrap = wrap;
83
+ getSceneState(sceneId) {
84
+ return lifecycleHandler.getSceneState(sceneId);
59
85
  }
60
86
  addGlobalVisibilityEventListener(listener) {
61
- this.visibilityHandler.addGlobalVisibilityEventListener(listener);
87
+ lifecycleHandler.addGlobalVisibilityEventListener(listener);
62
88
  return {
63
- remove: () => this.visibilityHandler.removeGlobalVisibilityEventListener(listener),
89
+ remove: () => lifecycleHandler.removeGlobalVisibilityEventListener(listener),
64
90
  };
65
91
  }
66
92
  addVisibilityEventListener(sceneId, listener) {
67
- this.visibilityHandler.addVisibilityEventListener(sceneId, listener);
93
+ lifecycleHandler.addVisibilityEventListener(sceneId, listener);
68
94
  return {
69
- remove: () => this.visibilityHandler.removeVisibilityEventListener(sceneId, listener),
95
+ remove: () => lifecycleHandler.removeVisibilityEventListener(sceneId, listener),
70
96
  };
71
97
  }
72
98
  dispatch(sceneId, action, params = {}) {
73
- return this.dispatchHandler.dispatch(sceneId, action, params);
74
- }
75
- handleTabSwitch() {
76
- NativeEvent.onSwitchTab(({ sceneId, from, to }) => {
77
- this.dispatch(sceneId, 'switchTab', { from, to });
78
- });
99
+ return dispatchHandler.dispatch(sceneId, action, params);
79
100
  }
80
- setInterceptor(interceptor) {
81
- this.dispatchHandler.setInterceptor(interceptor);
101
+ addInterceptor(interceptor) {
102
+ dispatchHandler.addInterceptor(interceptor);
103
+ return {
104
+ remove: () => dispatchHandler.removeInterceptor(interceptor),
105
+ };
82
106
  }
83
107
  setResult = (sceneId, resultCode, data = null) => {
84
- if (resultCode < RESULT_OK) {
85
- console.warn('`resultCode` can only be `RESULT_OK`, `RESULT_CANCEL` or an integer greater than 0.');
86
- }
87
- NativeNavigation.setResult(sceneId, resultCode, data);
108
+ resultHandler.setResult(sceneId, resultCode, data);
88
109
  };
89
- result(sceneId, resultCode) {
90
- return this.resultHandler.waitResult(sceneId, resultCode);
110
+ result(sceneId, requestCode) {
111
+ return resultHandler.waitResult(sceneId, requestCode);
91
112
  }
92
- unmount(sceneId) {
93
- this.resultHandler.invalidateResultEventListener(sceneId);
113
+ setRoot(layout, options) {
114
+ return layoutHandler.setRoot(layout, options);
94
115
  }
95
- setRoot(layout, sticky = false) {
96
- return this.layoutHandler.setRoot(layout, sticky);
97
- }
98
- setRootLayoutUpdateListener(willSetRoot = () => { }, didSetRoot = () => { }) {
99
- this.layoutHandler.setRootLayoutUpdateListener(willSetRoot, didSetRoot);
116
+ addRootLayoutUpdateListener(willSetRoot, didSetRoot) {
117
+ const remove = layoutHandler.addRootLayoutUpdateListener(willSetRoot, didSetRoot);
118
+ return { remove };
100
119
  }
101
120
  currentTab(sceneId) {
102
- return new Promise(resolve => {
103
- NativeNavigation.currentTab(sceneId, (_, index) => {
104
- resolve(index);
105
- });
106
- });
121
+ return NativeNavigation.currentTab(sceneId);
107
122
  }
108
123
  findSceneIdByModuleName(moduleName) {
109
- return new Promise(resolve => {
110
- NativeNavigation.findSceneIdByModuleName(moduleName, (_, sceneId) => {
111
- resolve(sceneId);
112
- });
113
- });
124
+ return NativeNavigation.findSceneIdByModuleName(moduleName);
114
125
  }
115
126
  isStackRoot(sceneId) {
116
- return new Promise(resolve => {
117
- NativeNavigation.isStackRoot(sceneId, (_, result) => {
118
- resolve(result);
119
- });
120
- });
121
- }
122
- signalFirstRenderComplete(sceneId) {
123
- NativeNavigation.signalFirstRenderComplete(sceneId);
127
+ return NativeNavigation.isStackRoot(sceneId);
124
128
  }
125
129
  currentRoute() {
126
- return new Promise(resolve => {
127
- NativeNavigation.currentRoute((_, route) => {
128
- resolve(route);
129
- });
130
- });
130
+ return NativeNavigation.currentRoute();
131
131
  }
132
132
  routeGraph() {
133
- return new Promise(resolve => {
134
- NativeNavigation.routeGraph((_, graph) => {
135
- resolve(graph);
136
- });
137
- });
133
+ return NativeNavigation.routeGraph();
138
134
  }
139
135
  setDefaultOptions(options) {
140
- NativeGarden.setStyle(options);
136
+ NativeNavigation.setDefaultOptions(options);
141
137
  }
142
138
  updateOptions(sceneId, options) {
143
- NativeGarden.updateOptions(sceneId, options);
139
+ NativeNavigation.updateOptions(sceneId, options);
144
140
  }
145
141
  updateTabBar(sceneId, options) {
146
- NativeGarden.updateTabBar(sceneId, options);
142
+ NativeNavigation.updateTabBar(sceneId, options);
147
143
  }
148
144
  setTabItem(sceneId, item) {
149
- if (!Array.isArray(item)) {
150
- item = [item];
151
- }
152
- NativeGarden.setTabItem(sceneId, item);
145
+ const items = Array.isArray(item) ? item : [item];
146
+ NativeNavigation.setTabItem(sceneId, items);
153
147
  }
154
148
  setMenuInteractive(sceneId, enabled) {
155
- NativeGarden.setMenuInteractive(sceneId, enabled);
149
+ return this.dispatch(sceneId, 'setMenuInteractive', { enabled });
156
150
  }
157
151
  }
158
152
  export default new NavigationImpl();
@@ -1,10 +1,11 @@
1
1
  import React from 'react';
2
2
  import type { NavigationItem } from './Options';
3
+ import type { HasTypedRoutes, RouteName, RoutePropsOf } from './Route';
3
4
  import { Navigator } from './Navigator';
4
- export interface NavigationProps {
5
- navigator: Navigator;
5
+ export type NavigationProps<Name extends RouteName = RouteName> = (HasTypedRoutes extends true ? RoutePropsOf<Name> : Record<string, any>) & {
6
+ navigator: Navigator<Name>;
6
7
  sceneId: string;
7
- }
8
+ };
8
9
  interface NativeProps {
9
10
  sceneId: string;
10
11
  }
@@ -1,6 +1,6 @@
1
1
  import React, { useEffect } from 'react';
2
2
  import { Navigator, NavigationContext } from './Navigator';
3
- import Navigation from './Navigation';
3
+ import NativeNavigation from './NativeNavigation';
4
4
  function getDisplayName(WrappedComponent) {
5
5
  return WrappedComponent.displayName || WrappedComponent.name || 'Component';
6
6
  }
@@ -13,10 +13,7 @@ export function withNavigation(moduleName) {
13
13
  navigator.moduleName = moduleName;
14
14
  }
15
15
  useEffect(() => {
16
- Navigation.signalFirstRenderComplete(sceneId);
17
- return () => {
18
- Navigator.unmount(sceneId);
19
- };
16
+ NativeNavigation.signalFirstRenderComplete(sceneId);
20
17
  }, [sceneId]);
21
18
  const injected = {
22
19
  navigator,
@@ -31,7 +28,7 @@ export function withNavigation(moduleName) {
31
28
  }
32
29
  export function withNavigationItem(item) {
33
30
  return function (WrappedComponent) {
34
- let navigationItem = WrappedComponent.navigationItem;
31
+ const navigationItem = WrappedComponent.navigationItem;
35
32
  if (navigationItem) {
36
33
  WrappedComponent.navigationItem = {
37
34
  ...navigationItem,
@@ -1,47 +1,36 @@
1
- import type { Visibility, Layout, BuildInLayout } from './Route';
2
- import type { NavigationItem } from './Options';
3
- import { ResultType } from './Navigation';
4
- export declare const NavigationContext: import("react").Context<any>;
5
- interface NavigationState {
6
- params: {
7
- readonly [index: string]: any;
8
- };
9
- }
10
- interface PropsType {
11
- [index: string]: any;
12
- }
13
- export interface Navigator {
14
- }
15
- export declare class Navigator implements Navigator {
1
+ import type { NavigationOption, TabBarStyle, TabItemInfo } from './Options';
2
+ import type { BuildInLayout, DispatchResult, Layout, NavigationResult, RouteName, RouteResultOf, SceneLifecycleState, ScreenPropsArgs, SetResultData, Visibility } from './Route';
3
+ export declare const NavigationContext: import("react").Context<Navigator<any> | null>;
4
+ export declare class Navigator<CurrentRoute extends RouteName = RouteName> {
16
5
  sceneId: string;
17
6
  moduleName?: string | undefined;
18
- static of(sceneId: string): Navigator;
19
- static unmount: (sceneId: string) => void;
20
- static find(moduleName: string): Promise<Navigator | undefined>;
21
- static current(): Promise<Navigator>;
7
+ static of<Name extends RouteName = RouteName>(sceneId: string): Navigator<Name>;
8
+ static find<Name extends RouteName = RouteName>(moduleName: Name): Promise<Navigator<Name> | undefined>;
9
+ static current(): Promise<Navigator | undefined>;
22
10
  constructor(sceneId: string, moduleName?: string | undefined);
23
- visibility: Visibility;
24
- readonly state: NavigationState;
25
- setParams: (params: {
26
- [index: string]: any;
27
- }) => void;
28
- push: <T extends ResultType, P extends PropsType = {}>(moduleName: string, props?: P, options?: NavigationItem) => Promise<[number, T]>;
29
- pushLayout: <T extends ResultType>(layout: BuildInLayout | Layout) => Promise<[number, T]>;
30
- pop: () => Promise<boolean>;
31
- popTo: (moduleName: string, inclusive?: boolean) => Promise<boolean>;
32
- popToRoot: () => Promise<boolean>;
33
- redirectTo: <P extends PropsType>(moduleName: string, props?: P, options?: NavigationItem) => Promise<boolean>;
34
- present: <T extends ResultType, P extends PropsType = {}>(moduleName: string, props?: P, options?: NavigationItem) => Promise<[number, T]>;
35
- presentLayout: <T extends ResultType>(layout: BuildInLayout | Layout) => Promise<[number, T]>;
36
- dismiss: () => Promise<boolean>;
37
- showModal: <T extends ResultType, P extends PropsType = {}>(moduleName: string, props?: P, options?: NavigationItem) => Promise<[number, T]>;
38
- showModalLayout: <T extends ResultType>(layout: BuildInLayout | Layout) => Promise<[number, T]>;
39
- hideModal: () => Promise<boolean>;
40
- setResult: <T extends ResultType>(resultCode: number, data?: T) => void;
41
- switchTab: (index: number, popToRoot?: boolean) => Promise<boolean>;
42
- toggleMenu: () => Promise<boolean>;
43
- openMenu: () => Promise<boolean>;
44
- closeMenu: () => Promise<boolean>;
11
+ get visibility(): Visibility;
12
+ get lifecycleState(): SceneLifecycleState;
13
+ private dispatchForResult;
14
+ push: <Name extends RouteName = RouteName, T = RouteResultOf<Name>>(moduleName: Name, ...args: ScreenPropsArgs<Name>) => Promise<NavigationResult<T>>;
15
+ pushLayout: <T = unknown>(layout: BuildInLayout | Layout) => Promise<NavigationResult<T>>;
16
+ pop: () => Promise<DispatchResult>;
17
+ popTo: (moduleName: RouteName | (string & {}), inclusive?: boolean) => Promise<DispatchResult>;
18
+ popToRoot: () => Promise<DispatchResult>;
19
+ redirectTo: <Name extends RouteName = RouteName>(moduleName: Name, ...args: ScreenPropsArgs<Name>) => Promise<DispatchResult>;
20
+ present: <Name extends RouteName = RouteName, T = RouteResultOf<Name>>(moduleName: Name, ...args: ScreenPropsArgs<Name>) => Promise<NavigationResult<T>>;
21
+ presentLayout: <T = unknown>(layout: BuildInLayout | Layout) => Promise<NavigationResult<T>>;
22
+ dismiss: () => Promise<DispatchResult>;
23
+ showModal: <Name extends RouteName = RouteName, T = RouteResultOf<Name>>(moduleName: Name, ...args: ScreenPropsArgs<Name>) => Promise<NavigationResult<T>>;
24
+ showModalLayout: <T = unknown>(layout: BuildInLayout | Layout) => Promise<NavigationResult<T>>;
25
+ hideModal: () => Promise<DispatchResult>;
26
+ setResult: (resultCode: number, data?: SetResultData<CurrentRoute> | null) => void;
27
+ switchTab: (index: number, popToRoot?: boolean) => Promise<DispatchResult>;
28
+ toggleMenu: () => Promise<DispatchResult>;
29
+ openMenu: () => Promise<DispatchResult>;
30
+ closeMenu: () => Promise<DispatchResult>;
31
+ setMenuInteractive: (enabled: boolean) => Promise<DispatchResult>;
32
+ updateOptions: (options: NavigationOption) => void;
33
+ updateTabBar: (options: TabBarStyle) => void;
34
+ setTabItem: (item: TabItemInfo | TabItemInfo[]) => void;
45
35
  isStackRoot: () => Promise<boolean>;
46
36
  }
47
- export {};
package/dist/Navigator.js CHANGED
@@ -1,14 +1,11 @@
1
1
  import { createContext } from 'react';
2
2
  import store from './store';
3
- import Navigation, { RESULT_BLOCK } from './Navigation';
3
+ import NativeNavigation from './NativeNavigation';
4
+ import { dispatchHandler } from './handler/DispatchCommandHandler';
5
+ import { lifecycleHandler } from './handler/LifecycleEventHandler';
6
+ import { RESULT_BLOCK, resultHandler } from './handler/ResultEventHandler';
4
7
  export const NavigationContext = createContext(null);
5
8
  let tagGenerator = 0;
6
- Navigation.addGlobalVisibilityEventListener((sceneId, visibility) => {
7
- const navigator = store.getNavigator(sceneId);
8
- if (navigator) {
9
- navigator.visibility = visibility;
10
- }
11
- });
12
9
  export class Navigator {
13
10
  sceneId;
14
11
  moduleName;
@@ -16,146 +13,156 @@ export class Navigator {
16
13
  let navigator = store.getNavigator(sceneId);
17
14
  if (!navigator) {
18
15
  navigator = new Navigator(sceneId);
19
- store.addNavigator(sceneId, navigator);
16
+ if (lifecycleHandler.getSceneState(sceneId) !== 'destroyed') {
17
+ store.addNavigator(sceneId, navigator);
18
+ }
20
19
  }
21
20
  return navigator;
22
21
  }
23
- static unmount = (sceneId) => {
24
- store.removeNavigator(sceneId);
25
- Navigation.unmount(sceneId);
26
- };
27
22
  static async find(moduleName) {
28
- const sceneId = await Navigation.findSceneIdByModuleName(moduleName);
23
+ const sceneId = await NativeNavigation.findSceneIdByModuleName(moduleName);
29
24
  if (sceneId) {
30
25
  return Navigator.of(sceneId);
31
26
  }
32
27
  }
33
28
  static async current() {
34
- const route = await Navigation.currentRoute();
29
+ const route = await NativeNavigation.currentRoute();
30
+ if (!route) {
31
+ return undefined;
32
+ }
35
33
  return Navigator.of(route.sceneId);
36
34
  }
37
35
  constructor(sceneId, moduleName) {
38
36
  this.sceneId = sceneId;
39
37
  this.moduleName = moduleName;
40
38
  }
41
- visibility = 'pending';
42
- state = {
43
- params: {},
44
- };
45
- setParams = (params) => {
46
- this.state.params = { ...this.state.params, ...params };
39
+ get visibility() {
40
+ const state = lifecycleHandler.getSceneState(this.sceneId);
41
+ if (state === 'visible') {
42
+ return 'visible';
43
+ }
44
+ if (state === 'invisible' || state === 'destroyed') {
45
+ return 'invisible';
46
+ }
47
+ return 'pending';
48
+ }
49
+ get lifecycleState() {
50
+ return lifecycleHandler.getSceneState(this.sceneId);
51
+ }
52
+ dispatchForResult = async (action, params, warnOnFailure) => {
53
+ const requestCode = --tagGenerator;
54
+ const { promise, cancel } = resultHandler.prepareResult(this.sceneId, requestCode);
55
+ let result;
56
+ try {
57
+ result = await dispatchHandler.dispatch(this.sceneId, action, {
58
+ ...params,
59
+ requestCode,
60
+ });
61
+ }
62
+ catch (error) {
63
+ cancel();
64
+ throw error;
65
+ }
66
+ if (!result.ok) {
67
+ cancel();
68
+ if (warnOnFailure) {
69
+ console.warn(warnOnFailure(result.code));
70
+ }
71
+ return { status: 'blocked', code: RESULT_BLOCK, reason: result.code };
72
+ }
73
+ return promise;
47
74
  };
48
- push = async (moduleName, props = {}, options = {}) => {
49
- const success = await Navigation.dispatch(this.sceneId, 'push', {
75
+ push = async (moduleName, ...args) => {
76
+ const [props = {}, options = {}] = args;
77
+ return this.dispatchForResult('push', {
50
78
  moduleName,
51
79
  from: this.moduleName,
52
80
  to: moduleName,
53
- props,
81
+ props: props,
54
82
  options,
55
83
  });
56
- if (!success) {
57
- return [RESULT_BLOCK, null];
58
- }
59
- return Navigation.result(this.sceneId, 0);
60
84
  };
61
85
  pushLayout = async (layout) => {
62
- const success = await Navigation.dispatch(this.sceneId, 'pushLayout', {
86
+ return this.dispatchForResult('pushLayout', {
63
87
  from: this.moduleName,
64
88
  layout,
65
89
  });
66
- if (!success) {
67
- return [RESULT_BLOCK, null];
68
- }
69
- return Navigation.result(this.sceneId, 0);
70
90
  };
71
- pop = () => Navigation.dispatch(this.sceneId, 'pop', { from: this.moduleName });
72
- popTo = (moduleName, inclusive = false) => Navigation.dispatch(this.sceneId, 'popTo', {
91
+ pop = () => dispatchHandler.dispatch(this.sceneId, 'pop', { from: this.moduleName });
92
+ popTo = (moduleName, inclusive = false) => dispatchHandler.dispatch(this.sceneId, 'popTo', {
73
93
  moduleName,
74
94
  inclusive,
75
95
  from: this.moduleName,
76
96
  to: moduleName,
77
97
  });
78
- popToRoot = () => Navigation.dispatch(this.sceneId, 'popToRoot', { from: this.moduleName });
79
- redirectTo = (moduleName, props = {}, options = {}) => Navigation.dispatch(this.sceneId, 'redirectTo', {
80
- moduleName,
81
- from: this.moduleName,
82
- to: moduleName,
83
- props,
84
- options,
85
- });
86
- present = async (moduleName, props = {}, options = {}) => {
87
- const requestCode = --tagGenerator;
88
- const success = await Navigation.dispatch(this.sceneId, 'present', {
98
+ popToRoot = () => dispatchHandler.dispatch(this.sceneId, 'popToRoot', { from: this.moduleName });
99
+ redirectTo = (moduleName, ...args) => {
100
+ const [props = {}, options = {}] = args;
101
+ return dispatchHandler.dispatch(this.sceneId, 'redirectTo', {
89
102
  moduleName,
90
103
  from: this.moduleName,
91
104
  to: moduleName,
92
- props,
105
+ props: props,
93
106
  options,
94
- requestCode,
95
107
  });
96
- if (!success) {
97
- console.warn(`present from:${this.moduleName} to:${moduleName} failed. Maybe the screen (${this.moduleName}) already shows or present another screen.`);
98
- return [RESULT_BLOCK, null];
99
- }
100
- return Navigation.result(this.sceneId, requestCode);
108
+ };
109
+ present = async (moduleName, ...args) => {
110
+ const [props = {}, options = {}] = args;
111
+ return this.dispatchForResult('present', {
112
+ moduleName,
113
+ from: this.moduleName,
114
+ to: moduleName,
115
+ props: props,
116
+ options,
117
+ }, code => `present from:${this.moduleName} to:${moduleName} failed (${code}).`);
101
118
  };
102
119
  presentLayout = async (layout) => {
103
- const requestCode = --tagGenerator;
104
- const success = await Navigation.dispatch(this.sceneId, 'presentLayout', {
120
+ return this.dispatchForResult('presentLayout', {
105
121
  layout,
106
122
  from: this.moduleName,
107
- requestCode,
108
- });
109
- if (!success) {
110
- console.warn(`presentLayout from:${this.moduleName} failed. Maybe the screen (${this.moduleName}) already shows or present another screen.`);
111
- return [RESULT_BLOCK, null];
112
- }
113
- return Navigation.result(this.sceneId, requestCode);
123
+ }, code => `presentLayout from:${this.moduleName} failed (${code}).`);
114
124
  };
115
- dismiss = () => Navigation.dispatch(this.sceneId, 'dismiss', { from: this.moduleName });
116
- showModal = async (moduleName, props = {}, options = {}) => {
117
- const requestCode = --tagGenerator;
118
- const success = await Navigation.dispatch(this.sceneId, 'showModal', {
125
+ dismiss = () => dispatchHandler.dispatch(this.sceneId, 'dismiss', { from: this.moduleName });
126
+ showModal = async (moduleName, ...args) => {
127
+ const [props = {}, options = {}] = args;
128
+ return this.dispatchForResult('showModal', {
119
129
  moduleName,
120
130
  from: this.moduleName,
121
131
  to: moduleName,
122
- props,
132
+ props: props,
123
133
  options,
124
- requestCode,
125
- });
126
- if (!success) {
127
- console.warn(`showModal from:${this.moduleName} to:${moduleName} failed. Maybe the screen (${this.moduleName}) already shows or present another screen.`);
128
- return [RESULT_BLOCK, null];
129
- }
130
- return Navigation.result(this.sceneId, requestCode);
134
+ }, code => `showModal from:${this.moduleName} to:${moduleName} failed (${code}).`);
131
135
  };
132
136
  showModalLayout = async (layout) => {
133
- const requestCode = --tagGenerator;
134
- const success = await Navigation.dispatch(this.sceneId, 'showModalLayout', {
137
+ return this.dispatchForResult('showModalLayout', {
135
138
  from: this.moduleName,
136
139
  layout,
137
- requestCode,
138
- });
139
- if (!success) {
140
- console.warn(`showModal from:${this.moduleName} failed. Maybe the screen (${this.moduleName}) already shows or present another screen.`);
141
- return [RESULT_BLOCK, null];
142
- }
143
- return Navigation.result(this.sceneId, requestCode);
140
+ }, code => `showModalLayout from:${this.moduleName} failed (${code}).`);
144
141
  };
145
- hideModal = () => Navigation.dispatch(this.sceneId, 'hideModal', { from: this.moduleName });
146
- setResult = (resultCode, data = null) => Navigation.setResult(this.sceneId, resultCode, data);
142
+ hideModal = () => dispatchHandler.dispatch(this.sceneId, 'hideModal', { from: this.moduleName });
143
+ setResult = (resultCode, data = null) => resultHandler.setResult(this.sceneId, resultCode, data);
147
144
  switchTab = async (index, popToRoot = false) => {
148
- const from = await Navigation.currentTab(this.sceneId);
149
- return Navigation.dispatch(this.sceneId, 'switchTab', {
145
+ const from = await NativeNavigation.currentTab(this.sceneId);
146
+ return dispatchHandler.dispatch(this.sceneId, 'switchTab', {
150
147
  from,
151
148
  to: index,
152
149
  popToRoot,
153
150
  });
154
151
  };
155
- toggleMenu = () => Navigation.dispatch(this.sceneId, 'toggleMenu', {
152
+ toggleMenu = () => dispatchHandler.dispatch(this.sceneId, 'toggleMenu', {
153
+ from: this.moduleName,
154
+ });
155
+ openMenu = () => dispatchHandler.dispatch(this.sceneId, 'openMenu', { from: this.moduleName });
156
+ closeMenu = () => dispatchHandler.dispatch(this.sceneId, 'closeMenu', { from: this.moduleName });
157
+ setMenuInteractive = (enabled) => dispatchHandler.dispatch(this.sceneId, 'setMenuInteractive', {
158
+ enabled,
156
159
  from: this.moduleName,
157
160
  });
158
- openMenu = () => Navigation.dispatch(this.sceneId, 'openMenu', { from: this.moduleName });
159
- closeMenu = () => Navigation.dispatch(this.sceneId, 'closeMenu', { from: this.moduleName });
160
- isStackRoot = () => Navigation.isStackRoot(this.sceneId);
161
+ updateOptions = (options) => NativeNavigation.updateOptions(this.sceneId, options);
162
+ updateTabBar = (options) => NativeNavigation.updateTabBar(this.sceneId, options);
163
+ setTabItem = (item) => {
164
+ const items = Array.isArray(item) ? item : [item];
165
+ NativeNavigation.setTabItem(this.sceneId, items);
166
+ };
167
+ isStackRoot = () => NativeNavigation.isStackRoot(this.sceneId);
161
168
  }
package/dist/Options.d.ts CHANGED
@@ -10,6 +10,7 @@ export type BarStyleLightContent = typeof BarStyleLightContent;
10
10
  export declare const BarStyleDarkContent = "dark-content";
11
11
  export type BarStyleDarkContent = typeof BarStyleDarkContent;
12
12
  export type BarStyle = BarStyleLightContent | BarStyleDarkContent;
13
+ export type ScreenOrientation = 'portrait' | 'landscape' | 'auto';
13
14
  export interface NavigationOption {
14
15
  screenBackgroundColor?: Color;
15
16
  statusBarHidden?: boolean;
@@ -19,10 +20,10 @@ export interface NavigationOption {
19
20
  displayCutoutWhenLandscapeAndroid?: boolean;
20
21
  homeIndicatorAutoHiddenIOS?: boolean;
21
22
  backInteractive?: boolean;
23
+ orientation?: ScreenOrientation;
22
24
  }
23
25
  export interface NavigationItem extends NavigationOption {
24
26
  animatedTransition?: boolean;
25
- forceScreenLandscape?: boolean;
26
27
  tabItem?: TabItem;
27
28
  }
28
29
  export interface TabItem {
@@ -39,6 +40,7 @@ export interface DefaultOptions {
39
40
  statusBarStyle?: BarStyle;
40
41
  navigationBarColorAndroid?: Color;
41
42
  displayCutoutWhenLandscapeAndroid?: boolean;
43
+ orientation?: ScreenOrientation;
42
44
  tabBarBackgroundColor?: Color;
43
45
  tabBarShadowImage?: ShadowImage;
44
46
  tabBarItemSelectedColor?: Color;