hybrid-navigation 4.1.0 → 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 (148) hide show
  1. package/HybridNavigation.podspec +9 -1
  2. package/README.md +12 -4
  3. package/android/build.gradle +4 -4
  4. package/android/proguard-rules.pro +0 -14
  5. package/android/src/main/java/com/navigation/androidx/AwesomeActivity.java +114 -23
  6. package/android/src/main/java/com/navigation/androidx/AwesomeFragment.java +115 -12
  7. package/android/src/main/java/com/navigation/androidx/DrawableUtils.java +83 -18
  8. package/android/src/main/java/com/navigation/androidx/DrawerFragment.java +185 -25
  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 +39 -12
  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 -89
  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/ReactTabBarProvider.java +8 -1
  36. package/android/src/main/java/com/reactnative/hybridnavigation/SceneLifecycleState.java +21 -0
  37. package/android/src/main/java/com/reactnative/hybridnavigation/SceneLifecycleStateMachine.java +116 -0
  38. package/android/src/main/java/com/reactnative/hybridnavigation/ScreenOrientation.java +42 -0
  39. package/android/src/main/java/com/reactnative/hybridnavigation/Utils.java +17 -0
  40. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/DrawerNavigator.java +14 -6
  41. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/ScreenNavigator.java +62 -22
  42. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/StackNavigator.java +35 -16
  43. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/TabNavigator.java +16 -13
  44. package/dist/DeepLink.d.ts +3 -2
  45. package/dist/DeepLink.js +37 -14
  46. package/dist/NativeEvent.d.ts +4 -12
  47. package/dist/NativeNavigation.d.ts +17 -19
  48. package/dist/Navigation.d.ts +21 -19
  49. package/dist/Navigation.js +83 -89
  50. package/dist/NavigationComponentWrap.d.ts +4 -3
  51. package/dist/NavigationComponentWrap.js +3 -6
  52. package/dist/Navigator.d.ts +31 -42
  53. package/dist/Navigator.js +97 -90
  54. package/dist/Options.d.ts +3 -1
  55. package/dist/Route.d.ts +85 -18
  56. package/dist/handler/DispatchCommandHandler.d.ts +10 -6
  57. package/dist/handler/DispatchCommandHandler.js +41 -17
  58. package/dist/handler/LayoutCommandHandler.d.ts +7 -7
  59. package/dist/handler/LayoutCommandHandler.js +21 -15
  60. package/dist/handler/LifecycleEventHandler.d.ts +26 -0
  61. package/dist/handler/LifecycleEventHandler.js +136 -0
  62. package/dist/handler/ResultEventHandler.d.ts +14 -9
  63. package/dist/handler/ResultEventHandler.js +88 -13
  64. package/dist/hooks.d.ts +5 -2
  65. package/dist/hooks.js +26 -4
  66. package/dist/index.d.ts +13 -0
  67. package/dist/index.js +0 -2
  68. package/dist/routeConfigRegistry.d.ts +6 -0
  69. package/dist/routeConfigRegistry.js +19 -0
  70. package/dist/router/drawer.d.ts +2 -2
  71. package/dist/router/drawer.js +6 -3
  72. package/dist/router/index.d.ts +8 -5
  73. package/dist/router/index.js +127 -43
  74. package/dist/router/screen.d.ts +2 -2
  75. package/dist/router/screen.js +1 -1
  76. package/dist/router/stack.d.ts +2 -2
  77. package/dist/router/stack.js +59 -30
  78. package/dist/router/tabs.d.ts +2 -2
  79. package/dist/router/tabs.js +4 -1
  80. package/dist/store.d.ts +2 -2
  81. package/ios/HBDGlobalStyle.h +26 -0
  82. package/ios/{GlobalStyle.mm → HBDGlobalStyle.mm} +30 -29
  83. package/ios/HybridNavigation.h +4 -1
  84. package/ios/Navigator/HBDDrawerNavigator.mm +24 -8
  85. package/ios/Navigator/HBDNavigatorRegistry.mm +1 -0
  86. package/ios/Navigator/HBDScreenNavigator.mm +106 -52
  87. package/ios/Navigator/HBDStackNavigator.mm +109 -61
  88. package/ios/Navigator/HBDTabNavigator.mm +32 -14
  89. package/ios/React/HBDNativeNavigation.mm +109 -131
  90. package/ios/React/HBDNavigator.h +29 -5
  91. package/ios/React/HBDOrientationCoordinator.h +27 -0
  92. package/ios/React/HBDOrientationCoordinator.mm +282 -0
  93. package/ios/React/HBDReactBridgeManager.h +36 -20
  94. package/ios/React/HBDReactBridgeManager.mm +450 -108
  95. package/ios/React/HBDSceneLifecycleStateMachine.h +38 -0
  96. package/ios/React/HBDSceneLifecycleStateMachine.mm +180 -0
  97. package/ios/View/HBDRootView.h +2 -0
  98. package/ios/View/HBDRootView.mm +148 -0
  99. package/ios/View/UIImage+WithBadge.h +3 -3
  100. package/ios/View/UIImage+WithBadge.mm +10 -15
  101. package/ios/ViewController/HBDDrawerController.mm +126 -5
  102. package/ios/ViewController/HBDNavigationController.mm +17 -4
  103. package/ios/ViewController/HBDReactViewController.mm +59 -121
  104. package/ios/ViewController/HBDTabBarController.h +4 -0
  105. package/ios/ViewController/HBDTabBarController.mm +59 -44
  106. package/ios/ViewController/HBDViewController.h +10 -3
  107. package/ios/ViewController/HBDViewController.mm +161 -159
  108. package/ios/ViewController/UINavigationController+HBD.h +2 -2
  109. package/ios/ViewController/UINavigationController+HBD.mm +3 -3
  110. package/ios/ViewController/UIViewController+HBD.h +17 -10
  111. package/ios/ViewController/UIViewController+HBD.mm +85 -69
  112. package/package.json +1 -1
  113. package/src/DeepLink.ts +41 -14
  114. package/src/NativeEvent.ts +4 -14
  115. package/src/NativeNavigation.ts +22 -33
  116. package/src/Navigation.ts +162 -121
  117. package/src/NavigationComponentWrap.tsx +9 -9
  118. package/src/Navigator.ts +191 -153
  119. package/src/Options.ts +3 -1
  120. package/src/Route.ts +134 -19
  121. package/src/handler/DispatchCommandHandler.ts +55 -21
  122. package/src/handler/LayoutCommandHandler.ts +36 -17
  123. package/src/handler/LifecycleEventHandler.ts +172 -0
  124. package/src/handler/ResultEventHandler.ts +112 -19
  125. package/src/hooks.ts +39 -7
  126. package/src/index.ts +26 -2
  127. package/src/routeConfigRegistry.ts +26 -0
  128. package/src/router/drawer.ts +8 -5
  129. package/src/router/index.ts +161 -50
  130. package/src/router/screen.ts +3 -3
  131. package/src/router/stack.ts +80 -29
  132. package/src/router/tabs.ts +15 -7
  133. package/src/store.ts +1 -1
  134. package/android/src/main/java/androidx/fragment/app/InternalFragment.java +0 -22
  135. package/android/src/main/java/com/navigation/androidx/SystemUI30.java +0 -87
  136. package/android/src/main/java/com/reactnative/hybridnavigation/NativeGarden.java +0 -176
  137. package/android/src/main/java/com/reactnative/hybridnavigation/ReactTabBar.java +0 -130
  138. package/android/src/main/res/layout/nav_tab_bar_react.xml +0 -32
  139. package/android/src/main/res/layout/nav_tab_bar_react_indeterminate.xml +0 -32
  140. package/dist/NativeGarden.d.ts +0 -13
  141. package/dist/NativeGarden.js +0 -2
  142. package/dist/handler/VisibilityEventHandler.d.ts +0 -13
  143. package/dist/handler/VisibilityEventHandler.js +0 -39
  144. package/ios/GlobalStyle.h +0 -15
  145. package/ios/React/HBDNativeGarden.h +0 -10
  146. package/ios/React/HBDNativeGarden.mm +0 -111
  147. package/src/NativeGarden.ts +0 -16
  148. package/src/handler/VisibilityEventHandler.ts +0 -52
package/src/Navigator.ts CHANGED
@@ -1,226 +1,264 @@
1
1
  import { createContext } from 'react';
2
2
 
3
3
  import store from './store';
4
- import type { Visibility, Layout, BuildInLayout } from './Route';
5
- import type { NavigationItem } from './Options';
6
- import Navigation, { ResultType, RESULT_BLOCK } from './Navigation';
7
-
8
- export const NavigationContext = createContext<any>(null);
9
-
10
- interface NavigationState {
11
- params: { readonly [index: string]: any };
12
- }
13
-
14
- interface PropsType {
15
- [index: string]: any;
16
- }
4
+ import NativeNavigation from './NativeNavigation';
5
+ import { dispatchHandler } from './handler/DispatchCommandHandler';
6
+ import { lifecycleHandler } from './handler/LifecycleEventHandler';
7
+ import { RESULT_BLOCK, resultHandler } from './handler/ResultEventHandler';
8
+ import type { NavigationOption, TabBarStyle, TabItemInfo } from './Options';
9
+ import type {
10
+ BuildInLayout,
11
+ DispatchResult,
12
+ Layout,
13
+ NavigationResult,
14
+ RouteName,
15
+ RouteResultOf,
16
+ SceneLifecycleState,
17
+ ScreenPropsArgs,
18
+ SetResultData,
19
+ Visibility,
20
+ } from './Route';
21
+
22
+ export const NavigationContext = createContext<Navigator<any> | null>(null);
17
23
 
18
24
  let tagGenerator = 0;
19
25
 
20
- Navigation.addGlobalVisibilityEventListener((sceneId: string, visibility: Visibility) => {
21
- const navigator = store.getNavigator(sceneId);
22
- if (navigator) {
23
- navigator.visibility = visibility;
24
- }
25
- });
26
-
27
- export interface Navigator {}
28
-
29
- export class Navigator implements Navigator {
30
- static of(sceneId: string) {
26
+ export class Navigator<CurrentRoute extends RouteName = RouteName> {
27
+ static of<Name extends RouteName = RouteName>(sceneId: string): Navigator<Name> {
31
28
  let navigator = store.getNavigator(sceneId);
32
29
  if (!navigator) {
33
30
  navigator = new Navigator(sceneId);
34
- store.addNavigator(sceneId, navigator);
31
+ if (lifecycleHandler.getSceneState(sceneId) !== 'destroyed') {
32
+ store.addNavigator(sceneId, navigator);
33
+ }
35
34
  }
36
- return navigator;
35
+ return navigator as unknown as Navigator<Name>;
37
36
  }
38
37
 
39
- static unmount = (sceneId: string) => {
40
- store.removeNavigator(sceneId);
41
- Navigation.unmount(sceneId);
42
- };
43
-
44
- static async find(moduleName: string) {
45
- const sceneId = await Navigation.findSceneIdByModuleName(moduleName);
38
+ static async find<Name extends RouteName = RouteName>(
39
+ moduleName: Name,
40
+ ): Promise<Navigator<Name> | undefined> {
41
+ const sceneId = await NativeNavigation.findSceneIdByModuleName(moduleName);
46
42
  if (sceneId) {
47
- return Navigator.of(sceneId);
43
+ return Navigator.of<Name>(sceneId);
48
44
  }
49
45
  }
50
46
 
51
- static async current(): Promise<Navigator> {
52
- const route = await Navigation.currentRoute();
47
+ static async current(): Promise<Navigator | undefined> {
48
+ const route = await NativeNavigation.currentRoute();
49
+ if (!route) {
50
+ return undefined;
51
+ }
53
52
  return Navigator.of(route.sceneId);
54
53
  }
55
54
 
56
55
  constructor(public sceneId: string, public moduleName?: string) {}
57
56
 
58
- visibility: Visibility = 'pending';
57
+ get visibility(): Visibility {
58
+ const state = lifecycleHandler.getSceneState(this.sceneId);
59
+ if (state === 'visible') {
60
+ return 'visible';
61
+ }
62
+ if (state === 'invisible' || state === 'destroyed') {
63
+ return 'invisible';
64
+ }
65
+ return 'pending';
66
+ }
59
67
 
60
- readonly state: NavigationState = {
61
- params: {},
62
- };
68
+ get lifecycleState(): SceneLifecycleState {
69
+ return lifecycleHandler.getSceneState(this.sceneId);
70
+ }
63
71
 
64
- setParams = (params: { [index: string]: any }) => {
65
- this.state.params = { ...this.state.params, ...params };
72
+ private dispatchForResult = async <T = unknown>(
73
+ action: string,
74
+ params: Record<string, any>,
75
+ warnOnFailure?: (code: DispatchResult['code']) => string,
76
+ ): Promise<NavigationResult<T>> => {
77
+ const requestCode = --tagGenerator;
78
+ const { promise, cancel } = resultHandler.prepareResult<T>(this.sceneId, requestCode);
79
+ let result: DispatchResult;
80
+ try {
81
+ result = await dispatchHandler.dispatch(this.sceneId, action, {
82
+ ...params,
83
+ requestCode,
84
+ });
85
+ } catch (error) {
86
+ cancel();
87
+ throw error;
88
+ }
89
+ if (!result.ok) {
90
+ cancel();
91
+ if (warnOnFailure) {
92
+ console.warn(warnOnFailure(result.code));
93
+ }
94
+ return { status: 'blocked', code: RESULT_BLOCK, reason: result.code };
95
+ }
96
+ return promise;
66
97
  };
67
98
 
68
- push = async <T extends ResultType, P extends PropsType = {}>(
69
- moduleName: string,
70
- props: P = {} as P,
71
- options: NavigationItem = {},
72
- ) => {
73
- const success = await Navigation.dispatch(this.sceneId, 'push', {
99
+ push = async <Name extends RouteName = RouteName, T = RouteResultOf<Name>>(
100
+ moduleName: Name,
101
+ ...args: ScreenPropsArgs<Name>
102
+ ): Promise<NavigationResult<T>> => {
103
+ const [props = {}, options = {}] = args;
104
+ return this.dispatchForResult<T>('push', {
74
105
  moduleName,
75
106
  from: this.moduleName,
76
107
  to: moduleName,
77
- props,
108
+ props: props as object,
78
109
  options,
79
110
  });
80
- if (!success) {
81
- return [RESULT_BLOCK, null] as [number, T];
82
- }
83
- return Navigation.result<T>(this.sceneId, 0);
84
111
  };
85
112
 
86
- pushLayout = async <T extends ResultType>(layout: BuildInLayout | Layout) => {
87
- const success = await Navigation.dispatch(this.sceneId, 'pushLayout', {
113
+ pushLayout = async <T = unknown>(
114
+ layout: BuildInLayout | Layout,
115
+ ): Promise<NavigationResult<T>> => {
116
+ return this.dispatchForResult<T>('pushLayout', {
88
117
  from: this.moduleName,
89
118
  layout,
90
119
  });
91
- if (!success) {
92
- return [RESULT_BLOCK, null] as [number, T];
93
- }
94
- return Navigation.result<T>(this.sceneId, 0);
95
120
  };
96
121
 
97
- pop = () => Navigation.dispatch(this.sceneId, 'pop', { from: this.moduleName });
122
+ pop = (): Promise<DispatchResult> =>
123
+ dispatchHandler.dispatch(this.sceneId, 'pop', { from: this.moduleName });
98
124
 
99
- popTo = (moduleName: string, inclusive: boolean = false) =>
100
- Navigation.dispatch(this.sceneId, 'popTo', {
125
+ popTo = (
126
+ moduleName: RouteName | (string & {}),
127
+ inclusive: boolean = false,
128
+ ): Promise<DispatchResult> =>
129
+ dispatchHandler.dispatch(this.sceneId, 'popTo', {
101
130
  moduleName,
102
131
  inclusive,
103
132
  from: this.moduleName,
104
133
  to: moduleName,
105
134
  });
106
135
 
107
- popToRoot = () => Navigation.dispatch(this.sceneId, 'popToRoot', { from: this.moduleName });
108
-
109
- redirectTo = <P extends PropsType>(
110
- moduleName: string,
111
- props: P = {} as P,
112
- options: NavigationItem = {},
113
- ) =>
114
- Navigation.dispatch(this.sceneId, 'redirectTo', {
115
- moduleName,
116
- from: this.moduleName,
117
- to: moduleName,
118
- props,
119
- options,
120
- });
136
+ popToRoot = (): Promise<DispatchResult> =>
137
+ dispatchHandler.dispatch(this.sceneId, 'popToRoot', { from: this.moduleName });
121
138
 
122
- present = async <T extends ResultType, P extends PropsType = {}>(
123
- moduleName: string,
124
- props: P = {} as P,
125
- options: NavigationItem = {},
126
- ) => {
127
- const requestCode = --tagGenerator;
128
- const success = await Navigation.dispatch(this.sceneId, 'present', {
139
+ redirectTo = <Name extends RouteName = RouteName>(
140
+ moduleName: Name,
141
+ ...args: ScreenPropsArgs<Name>
142
+ ): Promise<DispatchResult> => {
143
+ const [props = {}, options = {}] = args;
144
+ return dispatchHandler.dispatch(this.sceneId, 'redirectTo', {
129
145
  moduleName,
130
146
  from: this.moduleName,
131
147
  to: moduleName,
132
- props,
148
+ props: props as object,
133
149
  options,
134
- requestCode,
135
150
  });
136
- if (!success) {
137
- console.warn(
138
- `present from:${this.moduleName} to:${moduleName} failed. Maybe the screen (${this.moduleName}) already shows or present another screen.`,
139
- );
140
- return [RESULT_BLOCK, null] as [number, T];
141
- }
142
- return Navigation.result<T>(this.sceneId, requestCode);
143
151
  };
144
152
 
145
- presentLayout = async <T extends ResultType>(layout: BuildInLayout | Layout) => {
146
- const requestCode = --tagGenerator;
147
- const success = await Navigation.dispatch(this.sceneId, 'presentLayout', {
148
- layout,
149
- from: this.moduleName,
150
- requestCode,
151
- });
152
- if (!success) {
153
- console.warn(
154
- `presentLayout from:${this.moduleName} failed. Maybe the screen (${this.moduleName}) already shows or present another screen.`,
155
- );
156
- return [RESULT_BLOCK, null] as [number, T];
157
- }
158
- return Navigation.result<T>(this.sceneId, requestCode);
153
+ present = async <Name extends RouteName = RouteName, T = RouteResultOf<Name>>(
154
+ moduleName: Name,
155
+ ...args: ScreenPropsArgs<Name>
156
+ ): Promise<NavigationResult<T>> => {
157
+ const [props = {}, options = {}] = args;
158
+ return this.dispatchForResult<T>(
159
+ 'present',
160
+ {
161
+ moduleName,
162
+ from: this.moduleName,
163
+ to: moduleName,
164
+ props: props as object,
165
+ options,
166
+ },
167
+ code => `present from:${this.moduleName} to:${moduleName} failed (${code}).`,
168
+ );
159
169
  };
160
170
 
161
- dismiss = () => Navigation.dispatch(this.sceneId, 'dismiss', { from: this.moduleName });
171
+ presentLayout = async <T = unknown>(
172
+ layout: BuildInLayout | Layout,
173
+ ): Promise<NavigationResult<T>> => {
174
+ return this.dispatchForResult<T>(
175
+ 'presentLayout',
176
+ {
177
+ layout,
178
+ from: this.moduleName,
179
+ },
180
+ code => `presentLayout from:${this.moduleName} failed (${code}).`,
181
+ );
182
+ };
162
183
 
163
- showModal = async <T extends ResultType, P extends PropsType = {}>(
164
- moduleName: string,
165
- props: P = {} as P,
166
- options: NavigationItem = {},
167
- ) => {
168
- const requestCode = --tagGenerator;
169
- const success = await Navigation.dispatch(this.sceneId, 'showModal', {
170
- moduleName,
171
- from: this.moduleName,
172
- to: moduleName,
173
- props,
174
- options,
175
- requestCode,
176
- });
177
- if (!success) {
178
- console.warn(
179
- `showModal from:${this.moduleName} to:${moduleName} failed. Maybe the screen (${this.moduleName}) already shows or present another screen.`,
180
- );
181
- return [RESULT_BLOCK, null] as [number, T];
182
- }
183
- return Navigation.result<T>(this.sceneId, requestCode);
184
+ dismiss = (): Promise<DispatchResult> =>
185
+ dispatchHandler.dispatch(this.sceneId, 'dismiss', { from: this.moduleName });
186
+
187
+ showModal = async <Name extends RouteName = RouteName, T = RouteResultOf<Name>>(
188
+ moduleName: Name,
189
+ ...args: ScreenPropsArgs<Name>
190
+ ): Promise<NavigationResult<T>> => {
191
+ const [props = {}, options = {}] = args;
192
+ return this.dispatchForResult<T>(
193
+ 'showModal',
194
+ {
195
+ moduleName,
196
+ from: this.moduleName,
197
+ to: moduleName,
198
+ props: props as object,
199
+ options,
200
+ },
201
+ code => `showModal from:${this.moduleName} to:${moduleName} failed (${code}).`,
202
+ );
184
203
  };
185
204
 
186
- showModalLayout = async <T extends ResultType>(layout: BuildInLayout | Layout) => {
187
- const requestCode = --tagGenerator;
188
- const success = await Navigation.dispatch(this.sceneId, 'showModalLayout', {
189
- from: this.moduleName,
190
- layout,
191
- requestCode,
192
- });
193
- if (!success) {
194
- console.warn(
195
- `showModal from:${this.moduleName} failed. Maybe the screen (${this.moduleName}) already shows or present another screen.`,
196
- );
197
- return [RESULT_BLOCK, null] as [number, T];
198
- }
199
- return Navigation.result<T>(this.sceneId, requestCode);
205
+ showModalLayout = async <T = unknown>(
206
+ layout: BuildInLayout | Layout,
207
+ ): Promise<NavigationResult<T>> => {
208
+ return this.dispatchForResult<T>(
209
+ 'showModalLayout',
210
+ {
211
+ from: this.moduleName,
212
+ layout,
213
+ },
214
+ code => `showModalLayout from:${this.moduleName} failed (${code}).`,
215
+ );
200
216
  };
201
217
 
202
- hideModal = () => Navigation.dispatch(this.sceneId, 'hideModal', { from: this.moduleName });
218
+ hideModal = (): Promise<DispatchResult> =>
219
+ dispatchHandler.dispatch(this.sceneId, 'hideModal', { from: this.moduleName });
203
220
 
204
- setResult = <T extends ResultType>(resultCode: number, data: T = null as any): void =>
205
- Navigation.setResult(this.sceneId, resultCode, data);
221
+ setResult = (
222
+ resultCode: number,
223
+ data: SetResultData<CurrentRoute> | null = null,
224
+ ): void => resultHandler.setResult(this.sceneId, resultCode, data);
206
225
 
207
- switchTab = async (index: number, popToRoot: boolean = false) => {
208
- const from = await Navigation.currentTab(this.sceneId);
209
- return Navigation.dispatch(this.sceneId, 'switchTab', {
226
+ switchTab = async (index: number, popToRoot: boolean = false): Promise<DispatchResult> => {
227
+ const from = await NativeNavigation.currentTab(this.sceneId);
228
+ return dispatchHandler.dispatch(this.sceneId, 'switchTab', {
210
229
  from,
211
230
  to: index,
212
231
  popToRoot,
213
232
  });
214
233
  };
215
234
 
216
- toggleMenu = () =>
217
- Navigation.dispatch(this.sceneId, 'toggleMenu', {
235
+ toggleMenu = (): Promise<DispatchResult> =>
236
+ dispatchHandler.dispatch(this.sceneId, 'toggleMenu', {
218
237
  from: this.moduleName,
219
238
  });
220
239
 
221
- openMenu = () => Navigation.dispatch(this.sceneId, 'openMenu', { from: this.moduleName });
240
+ openMenu = (): Promise<DispatchResult> =>
241
+ dispatchHandler.dispatch(this.sceneId, 'openMenu', { from: this.moduleName });
242
+
243
+ closeMenu = (): Promise<DispatchResult> =>
244
+ dispatchHandler.dispatch(this.sceneId, 'closeMenu', { from: this.moduleName });
222
245
 
223
- closeMenu = () => Navigation.dispatch(this.sceneId, 'closeMenu', { from: this.moduleName });
246
+ setMenuInteractive = (enabled: boolean): Promise<DispatchResult> =>
247
+ dispatchHandler.dispatch(this.sceneId, 'setMenuInteractive', {
248
+ enabled,
249
+ from: this.moduleName,
250
+ });
251
+
252
+ updateOptions = (options: NavigationOption): void =>
253
+ NativeNavigation.updateOptions(this.sceneId, options);
254
+
255
+ updateTabBar = (options: TabBarStyle): void =>
256
+ NativeNavigation.updateTabBar(this.sceneId, options);
257
+
258
+ setTabItem = (item: TabItemInfo | TabItemInfo[]): void => {
259
+ const items = Array.isArray(item) ? item : [item];
260
+ NativeNavigation.setTabItem(this.sceneId, items);
261
+ };
224
262
 
225
- isStackRoot = () => Navigation.isStackRoot(this.sceneId);
263
+ isStackRoot = () => NativeNavigation.isStackRoot(this.sceneId);
226
264
  }
package/src/Options.ts CHANGED
@@ -10,6 +10,7 @@ export type BarStyleLightContent = typeof BarStyleLightContent;
10
10
  export 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
 
14
15
  export interface NavigationOption {
15
16
  screenBackgroundColor?: Color; // 页面背景,默认是白色
@@ -21,11 +22,11 @@ export interface NavigationOption {
21
22
  homeIndicatorAutoHiddenIOS?: boolean; // 是否隐藏 Home 指示器,默认 false
22
23
 
23
24
  backInteractive?: boolean; // 是否允许侧滑返回或通过返回键返回
25
+ orientation?: ScreenOrientation; // 页面方向策略:portrait | landscape | auto
24
26
  }
25
27
 
26
28
  export interface NavigationItem extends NavigationOption {
27
29
  animatedTransition?: boolean;
28
- forceScreenLandscape?: boolean; // 是否强制横屏
29
30
  tabItem?: TabItem;
30
31
  }
31
32
 
@@ -45,6 +46,7 @@ export interface DefaultOptions {
45
46
  statusBarStyle?: BarStyle; // 状态栏样式,可选项有 `BarStyleLightContent` 和 `BarStyleDarkContent`
46
47
  navigationBarColorAndroid?: Color; // 底部虚拟键背景颜色,仅对 Android 8.0 以上版本生效
47
48
  displayCutoutWhenLandscapeAndroid?: boolean; // 横屏时,是否将界面延伸至刘海区域,默认 true
49
+ orientation?: ScreenOrientation; // 默认全局方向策略,默认为 portrait
48
50
 
49
51
  tabBarBackgroundColor?: Color; // 底部 TabBar 背景颜色,请勿使用带透明度的颜色。
50
52
  tabBarShadowImage?: ShadowImage; // 底部 TabBar 阴影图片。对于 iOS, 只有同时设置了 tabBarBackgroundColor 才会生效
package/src/Route.ts CHANGED
@@ -1,32 +1,137 @@
1
+ import type { NavigationItem } from './Options';
2
+ import type { CustomLayoutMap, NavigationParamList } from './index';
3
+
1
4
  export type Visibility = 'visible' | 'invisible' | 'pending';
2
5
 
3
- export interface Screen extends Layout {
4
- screen: {
5
- moduleName: string;
6
- props?: object;
7
- options?: {};
8
- };
6
+ export type SceneLifecycleState = 'created' | 'visible' | 'invisible' | 'destroyed';
7
+
8
+ export type DispatchResultCode =
9
+ | 'ok'
10
+ | 'intercepted'
11
+ | 'scene_not_found'
12
+ | 'already_presenting'
13
+ | 'not_ready'
14
+ | 'superseded'
15
+ | 'invalid_action'
16
+ | 'host_unavailable';
17
+
18
+ export interface DispatchResult {
19
+ ok: boolean;
20
+ code: DispatchResultCode;
21
+ message?: string;
22
+ }
23
+
24
+ export interface SetRootOptions {
25
+ stickyAndroid?: boolean;
9
26
  }
10
27
 
11
- export interface Stack extends Layout {
28
+ export type CancelReason = 'dismissed' | 'unmounted' | 'root_reset';
29
+
30
+ export type NavigationResult<T = unknown> =
31
+ | { status: 'ok'; code: number; data: T }
32
+ | { status: 'cancel'; code: number; reason: CancelReason }
33
+ | { status: 'blocked'; code: number; reason: DispatchResultCode };
34
+
35
+ export type ScreenRoute<
36
+ Props extends Record<string, any> = Record<never, never>,
37
+ Result = unknown,
38
+ > = {
39
+ props: Props;
40
+ result: Result;
41
+ };
42
+
43
+ export type HasTypedRoutes = keyof NavigationParamList extends never ? false : true;
44
+
45
+ export type RouteName = HasTypedRoutes extends true
46
+ ? Extract<keyof NavigationParamList, string>
47
+ : string;
48
+
49
+ type RawRouteEntry<Name extends string> = Name extends keyof NavigationParamList
50
+ ? NavigationParamList[Name]
51
+ : ScreenRoute<Record<string, any>, unknown>;
52
+
53
+ export type RoutePropsOf<Name extends string> =
54
+ RawRouteEntry<Name> extends { props: infer P }
55
+ ? P
56
+ : RawRouteEntry<Name> extends { props?: infer P }
57
+ ? [Exclude<P, undefined>] extends [never]
58
+ ? Record<never, never>
59
+ : Exclude<P, undefined>
60
+ : Record<string, any>;
61
+
62
+ export type RouteResultOf<Name extends string> =
63
+ RawRouteEntry<Name> extends { result: infer R }
64
+ ? R
65
+ : RawRouteEntry<Name> extends { result?: infer R }
66
+ ? unknown extends R
67
+ ? unknown
68
+ : [Exclude<R, undefined>] extends [never]
69
+ ? unknown
70
+ : Exclude<R, undefined>
71
+ : unknown;
72
+
73
+ export type ResultMap = Record<string, any> & { [Symbol.iterator]?: never };
74
+
75
+ type IsAny<T> = 0 extends 1 & T ? true : false;
76
+
77
+ export type SetResultData<Name extends string> =
78
+ IsAny<Name> extends true
79
+ ? any
80
+ : unknown extends RouteResultOf<Name>
81
+ ? ResultMap
82
+ : Extract<RouteResultOf<Name>, ResultMap>;
83
+
84
+ type RequiredKeysOf<T> = {
85
+ [K in keyof T]-?: {} extends Pick<T, K> ? never : K;
86
+ }[keyof T];
87
+
88
+ export type IsPropsRequired<P> = [RequiredKeysOf<P>] extends [never] ? false : true;
89
+
90
+ export type ScreenPropsArgs<Name extends string> = HasTypedRoutes extends true
91
+ ? IsPropsRequired<RoutePropsOf<Name>> extends true
92
+ ? [props: RoutePropsOf<Name>, options?: NavigationItem]
93
+ : [props?: RoutePropsOf<Name>, options?: NavigationItem]
94
+ : [props?: Record<string, any>, options?: NavigationItem];
95
+
96
+ export type Screen<Name extends RouteName = RouteName> = HasTypedRoutes extends true
97
+ ? Name extends RouteName
98
+ ? {
99
+ screen: {
100
+ moduleName: Name;
101
+ props?: RoutePropsOf<Name>;
102
+ options?: NavigationItem;
103
+ };
104
+ }
105
+ : never
106
+ : {
107
+ screen: {
108
+ moduleName: Name;
109
+ props?: Record<string, any>;
110
+ options?: NavigationItem;
111
+ };
112
+ };
113
+
114
+ export type TypedScreen = Screen<RouteName>;
115
+
116
+ export interface Stack {
12
117
  stack: {
13
- children: Array<BuildInLayout | Layout>;
118
+ children: Array<Layout>;
14
119
  options?: {};
15
120
  };
16
121
  }
17
122
 
18
- export interface Tabs extends Layout {
123
+ export interface Tabs {
19
124
  tabs: {
20
- children: Array<BuildInLayout | Layout>;
125
+ children: Array<Layout>;
21
126
  options?: {
22
127
  selectedIndex?: number;
23
128
  };
24
129
  };
25
130
  }
26
131
 
27
- export interface Drawer extends Layout {
132
+ export interface Drawer {
28
133
  drawer: {
29
- children: [BuildInLayout | Layout, BuildInLayout | Layout];
134
+ children: [Layout, Layout];
30
135
  options?: {
31
136
  maxDrawerWidth?: number;
32
137
  minDrawerMargin?: number;
@@ -35,11 +140,17 @@ export interface Drawer extends Layout {
35
140
  };
36
141
  }
37
142
 
38
- export type BuildInLayout = Screen | Stack | Tabs | Drawer;
143
+ export type HasCustomLayouts = keyof CustomLayoutMap extends never ? false : true;
39
144
 
40
- export interface Layout {
41
- [index: string]: {};
42
- }
145
+ export type CustomLayout = HasCustomLayouts extends true
146
+ ? {
147
+ [K in Extract<keyof CustomLayoutMap, string>]: Record<K, CustomLayoutMap[K]>;
148
+ }[Extract<keyof CustomLayoutMap, string>]
149
+ : never;
150
+
151
+ export type BuiltInLayout = TypedScreen | Stack | Tabs | Drawer;
152
+ export type BuildInLayout = BuiltInLayout;
153
+ export type Layout = BuiltInLayout | CustomLayout;
43
154
 
44
155
  export type RouteMode = 'modal' | 'present' | 'push';
45
156
  export type LayoutMode = 'modal' | 'present' | 'normal';
@@ -61,8 +172,10 @@ export interface RouteGraph {
61
172
 
62
173
  export interface RouteConfig {
63
174
  path?: string;
64
- dependency?: string;
175
+ dependency?: RouteName;
65
176
  mode?: RouteMode;
177
+ options?: NavigationItem;
178
+ lazy?: boolean;
66
179
  }
67
180
 
68
181
  export interface RouteData {
@@ -81,13 +194,15 @@ export interface RouteInfo {
81
194
  options: object;
82
195
  }
83
196
 
197
+ export type RouteHandleResult = [boolean, RouteGraph | null, DispatchResult?];
198
+
84
199
  export interface RouteHandler {
85
200
  /**
86
201
  * @param graph - current RouteGraph to process
87
202
  * @param target - route may add to the graph
88
- * @returns [boolean, RouteGraph] - if the route is handled, return true and childGraph for next Handler to process, [false, graph] otherwise
203
+ * @returns [boolean, RouteGraph | null, DispatchResult?] - if the route is handled, return true and childGraph for next Handler to process, [false, graph] otherwise
89
204
  */
90
- process: (graph: RouteGraph, target: RouteInfo) => Promise<[boolean, RouteGraph | null]>;
205
+ process: (graph: RouteGraph, target: RouteInfo) => Promise<RouteHandleResult>;
91
206
  }
92
207
 
93
208
  export type RouteInterceptor = (path: string) => boolean | Promise<boolean>;