hybrid-navigation 2.13.0-beta.2 → 2.13.0-beta.3

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 (58) hide show
  1. package/ios/HybridNavigation/HBDUtils.h +3 -6
  2. package/ios/HybridNavigation/HBDUtils.m +10 -0
  3. package/ios/HybridNavigation/Navigator/HBDStackNavigator.m +7 -2
  4. package/ios/HybridNavigation/ReactBridge/HBDNavigationModule.m +1 -1
  5. package/ios/HybridNavigation/View/HBDRootView.m +9 -3
  6. package/ios/HybridNavigation/ViewController/HBDNavigationController.m +0 -30
  7. package/ios/HybridNavigation/ViewController/HBDViewController.m +7 -3
  8. package/ios/HybridNavigation/ViewController/UINavigationController+HBD.m +0 -3
  9. package/lib/Event.d.ts +22 -0
  10. package/lib/Event.js +117 -0
  11. package/lib/Garden.d.ts +2 -64
  12. package/lib/Garden.js +9 -47
  13. package/lib/GardenModule.d.ts +20 -0
  14. package/lib/GardenModule.js +3 -0
  15. package/lib/NavigationModule.d.ts +33 -22
  16. package/lib/NavigationModule.js +79 -22
  17. package/lib/Navigator.d.ts +33 -17
  18. package/lib/Navigator.js +106 -170
  19. package/lib/{typing.d.ts → Options.d.ts} +46 -50
  20. package/lib/{typing.js → Options.js} +0 -0
  21. package/lib/ReactRegistry.d.ts +2 -3
  22. package/lib/ReactRegistry.js +8 -28
  23. package/lib/Route.d.ts +74 -0
  24. package/lib/{router/typing.js → Route.js} +0 -0
  25. package/lib/hooks.d.ts +0 -1
  26. package/lib/hooks.js +18 -24
  27. package/lib/index.d.ts +3 -2
  28. package/lib/index.js +4 -2
  29. package/lib/router/drawer.d.ts +1 -1
  30. package/lib/router/index.d.ts +4 -7
  31. package/lib/router/index.js +1 -6
  32. package/lib/router/screen.d.ts +1 -1
  33. package/lib/router/stack.d.ts +1 -1
  34. package/lib/router/tabs.d.ts +1 -1
  35. package/lib/store.d.ts +0 -9
  36. package/lib/store.js +0 -22
  37. package/package.json +1 -1
  38. package/src/Event.ts +188 -0
  39. package/src/Garden.ts +18 -122
  40. package/src/GardenModule.ts +24 -0
  41. package/src/NavigationModule.ts +118 -29
  42. package/src/Navigator.ts +164 -209
  43. package/src/{typing.ts → Options.ts} +51 -57
  44. package/src/ReactRegistry.tsx +17 -40
  45. package/src/Route.ts +88 -0
  46. package/src/hooks.ts +23 -32
  47. package/src/index.ts +5 -2
  48. package/src/router/drawer.ts +1 -1
  49. package/src/router/index.ts +21 -12
  50. package/src/router/screen.ts +1 -1
  51. package/src/router/stack.ts +1 -1
  52. package/src/router/tabs.ts +1 -1
  53. package/src/store.ts +0 -28
  54. package/lib/router/typing.d.ts +0 -37
  55. package/lib/utils.d.ts +0 -9
  56. package/lib/utils.js +0 -50
  57. package/src/router/typing.ts +0 -45
  58. package/src/utils.ts +0 -83
package/src/Event.ts ADDED
@@ -0,0 +1,188 @@
1
+ import { NativeModules, NativeEventEmitter, NativeModule, EventSubscription } from 'react-native'
2
+ import GardenModule from './GardenModule'
3
+
4
+ function listenStatusBarHeightChanged(listener: (statusBarHeight: number) => void) {
5
+ const GardenEventReceiver = new NativeEventEmitter(GardenModule)
6
+ const { EVENT_STATUSBAR_FRAME_CHANGE } = GardenModule.getConstants()
7
+ return GardenEventReceiver.addListener(EVENT_STATUSBAR_FRAME_CHANGE, ({ statusBarHeight }) => {
8
+ listener(statusBarHeight)
9
+ })
10
+ }
11
+
12
+ interface HBDEventEmitter extends NativeModule {
13
+ getConstants: () => {
14
+ EVENT_NAVIGATION: string
15
+ EVENT_SWITCH_TAB: string
16
+ EVENT_DID_SET_ROOT: string
17
+ EVENT_WILL_SET_ROOT: string
18
+ ON_COMPONENT_RESULT: string
19
+ ON_BAR_BUTTON_ITEM_CLICK: string
20
+ ON_COMPONENT_APPEAR: string
21
+ ON_COMPONENT_DISAPPEAR: string
22
+ KEY_REQUEST_CODE: string
23
+ KEY_RESULT_CODE: string
24
+ KEY_RESULT_DATA: string
25
+ KEY_SCENE_ID: string
26
+ KEY_MODULE_NAME: string
27
+ KEY_INDEX: string
28
+ KEY_ACTION: string
29
+ KEY_ON: string
30
+ }
31
+ }
32
+
33
+ const HBDEventEmitter: HBDEventEmitter = NativeModules.HBDEventEmitter
34
+ const HBDEventReceiver = new NativeEventEmitter(HBDEventEmitter)
35
+
36
+ function listenComponentResult(
37
+ listener: (sceneId: string, requestCode: number, resultCode: number, resultData: object) => void,
38
+ ) {
39
+ const {
40
+ EVENT_NAVIGATION,
41
+ KEY_ON,
42
+ ON_COMPONENT_RESULT,
43
+ KEY_REQUEST_CODE,
44
+ KEY_RESULT_CODE,
45
+ KEY_RESULT_DATA,
46
+ KEY_SCENE_ID,
47
+ } = HBDEventEmitter.getConstants()
48
+
49
+ return HBDEventReceiver.addListener(EVENT_NAVIGATION, data => {
50
+ if (data[KEY_ON] === ON_COMPONENT_RESULT) {
51
+ const requestCode = data[KEY_REQUEST_CODE]
52
+ const resultCode = data[KEY_RESULT_CODE]
53
+ const resultData = data[KEY_RESULT_DATA]
54
+ const sceneId = data[KEY_SCENE_ID]
55
+
56
+ listener(sceneId, requestCode, resultCode, resultData)
57
+ }
58
+ })
59
+ }
60
+
61
+ function listenComponentVisibility(
62
+ componentDidAppear: (sceneId: string) => void,
63
+ componentDidDisappear: (sceneId: string) => void,
64
+ ) {
65
+ const { EVENT_NAVIGATION, KEY_ON, ON_COMPONENT_APPEAR, ON_COMPONENT_DISAPPEAR, KEY_SCENE_ID } =
66
+ HBDEventEmitter.getConstants()
67
+
68
+ return HBDEventReceiver.addListener(EVENT_NAVIGATION, data => {
69
+ if (data[KEY_ON] === ON_COMPONENT_APPEAR) {
70
+ componentDidAppear(data[KEY_SCENE_ID])
71
+ } else if (data[KEY_ON] === ON_COMPONENT_DISAPPEAR) {
72
+ componentDidDisappear(data[KEY_SCENE_ID])
73
+ }
74
+ })
75
+ }
76
+
77
+ function listenBarButtonItemClick(listener: (sceneId: string, action: string) => void) {
78
+ const { EVENT_NAVIGATION, KEY_ON, ON_BAR_BUTTON_ITEM_CLICK, KEY_ACTION, KEY_SCENE_ID } =
79
+ HBDEventEmitter.getConstants()
80
+
81
+ return HBDEventReceiver.addListener(
82
+ EVENT_NAVIGATION,
83
+ data => {
84
+ if (data[KEY_ON] === ON_BAR_BUTTON_ITEM_CLICK) {
85
+ listener(data[KEY_SCENE_ID], data[KEY_ACTION])
86
+ }
87
+ },
88
+ {},
89
+ )
90
+ }
91
+
92
+ function listenSwitchTab(listener: (sceneId: string, from: number, to: number) => void) {
93
+ const { EVENT_SWITCH_TAB, KEY_INDEX, KEY_SCENE_ID } = HBDEventEmitter.getConstants()
94
+ return HBDEventReceiver.addListener(EVENT_SWITCH_TAB, event => {
95
+ const index = event[KEY_INDEX]
96
+ const [from, to] = index.split('-')
97
+ listener(event[KEY_SCENE_ID], parseInt(from, 10), parseInt(to, 10))
98
+ })
99
+ }
100
+
101
+ function listenSetRoot(willSetRoot: () => void, didSetRoot: (tag: number) => void) {
102
+ const { EVENT_WILL_SET_ROOT, EVENT_DID_SET_ROOT } = HBDEventEmitter.getConstants()
103
+
104
+ const subscriptions: EventSubscription[] = []
105
+
106
+ subscriptions.push(
107
+ HBDEventReceiver.addListener(EVENT_WILL_SET_ROOT, _ => {
108
+ willSetRoot()
109
+ }),
110
+ )
111
+
112
+ subscriptions.push(
113
+ HBDEventReceiver.addListener(EVENT_DID_SET_ROOT, (event: { tag: number }) => {
114
+ didSetRoot(event.tag)
115
+ }),
116
+ )
117
+
118
+ return {
119
+ remove: () => {
120
+ subscriptions.forEach(subscription => subscription.remove())
121
+ },
122
+ }
123
+ }
124
+
125
+ const actions: Record<string, Function> = {}
126
+ const keyPairs: Record<string, string[]> = {}
127
+
128
+ let actionIdGenerator = 0
129
+ let barButtonClickHandlerFactory = () => {
130
+ return (sceneId: string, value: Function) => {
131
+ console.warn(`barButtonClickHandlerFactory 没有正确绑定`, sceneId, value)
132
+ }
133
+ }
134
+
135
+ function setBarButtonClickHandlerFactory(generator: typeof barButtonClickHandlerFactory) {
136
+ barButtonClickHandlerFactory = generator
137
+ }
138
+
139
+ function bindBarButtonClickEvent(sceneId: string, item: object | null | undefined): object | null {
140
+ if (item === null || item === undefined) {
141
+ return null
142
+ }
143
+
144
+ return JSON.parse(
145
+ JSON.stringify(item, (key, value) => {
146
+ if (key !== 'action' || typeof value !== 'function') {
147
+ return value
148
+ }
149
+
150
+ const actionKey = 'ON_BAR_BUTTON_ITEM_CLICK_' + actionIdGenerator++
151
+
152
+ if (!keyPairs[sceneId]) {
153
+ keyPairs[sceneId] = []
154
+ }
155
+ keyPairs[sceneId].push(actionKey)
156
+ actions[actionKey] = value
157
+
158
+ return actionKey
159
+ }),
160
+ )
161
+ }
162
+
163
+ function removeBarButtonClickEvent(sceneId: string): void {
164
+ const keys = keyPairs[sceneId]
165
+ delete keyPairs[sceneId]
166
+ if (keys) {
167
+ keys.forEach(key => delete actions[key])
168
+ }
169
+ }
170
+
171
+ listenBarButtonItemClick((sceneId: string, action: string) => {
172
+ const listeners = actions
173
+ if (listeners[action]) {
174
+ const handler = barButtonClickHandlerFactory()
175
+ handler(sceneId, listeners[action])
176
+ }
177
+ })
178
+
179
+ export default {
180
+ listenStatusBarHeightChanged,
181
+ listenComponentResult,
182
+ listenComponentVisibility,
183
+ listenSwitchTab,
184
+ listenSetRoot,
185
+ bindBarButtonClickEvent,
186
+ removeBarButtonClickEvent,
187
+ setBarButtonClickHandlerFactory,
188
+ }
package/src/Garden.ts CHANGED
@@ -1,94 +1,30 @@
1
- import { NativeEventEmitter, NativeModules, Platform } from 'react-native'
2
- import { bindBarButtonItemClickEvent } from './utils'
3
- import { NavigationOption, ImageSource, Color, BarStyle, TitleAlignment, BarButtonItem, TitleItem } from './typing'
4
-
5
- export interface ShadowImage {
6
- image?: ImageSource
7
- color?: Color
8
- }
9
-
10
- export interface Style {
11
- screenBackgroundColor?: Color // 页面背景,默认是白色
12
- topBarStyle?: BarStyle // TopBar 样式,决定了状态栏的颜色,可选项有 `BarStyleLightContent` 和 `BarStyleDarkContent`
13
- topBarColor?: Color // TopBar 背景颜色,默认根据 topBarStyle 来计算
14
- topBarColorDarkContent?: Color // TopBar 背景颜色,当 topBarStyle 的值为 BarStyleDarkContent 时生效,覆盖 topBarColor 的值
15
- topBarColorLightContent?: Color // TopBar 背景颜色,当 topBarStyle 的值为 BarStyleLightContent 时生效,覆盖 topBarColor 的值
16
- statusBarColorAndroid?: Color // 状态栏背景颜色,默认取 topBarColor 的值
17
- navigationBarColorAndroid?: Color // 底部虚拟键背景颜色,仅对 Android 8.0 以上版本生效
18
- hideBackTitleIOS?: boolean // 是否隐藏返回按钮旁边的文字,默认是 false, 仅对 iOS 生效
19
- elevationAndroid?: number // TopBar 阴影高度,默认值为 4 dp
20
- shadowImage?: ShadowImage // TopBar 阴影图片,仅对 iOS 生效
21
- backIcon?: ImageSource // 返回按钮图片
22
- topBarTintColor?: Color // TopBar 按钮的颜色。默认根据 topBarStyle 来计算
23
- topBarTintColorDarkContent?: Color // TopBar 按钮颜色,当 topBarStyle 的值为 BarStyleDarkContent 时生效,覆盖 topBarTintColor 的值
24
- topBarTintColorLightContent?: Color // TopBar 按钮颜色,当 topBarStyle 的值为 BarStyleLightContent 时生效,覆盖 topBarTintColor 的值
25
- titleTextColor?: Color // TopBar 标题颜色,默认根据 topBarStyle 来计算
26
- titleTextColorDarkContent?: Color // TopBar 标题颜色,当 topBarStyle 的值为 BarStyleDarkContent 时生效,覆盖 titleTextColor 的值
27
- titleTextColorLightContent?: Color // TopBar 标题颜色,当 topBarStyle 的值为 BarStyleLightContent 时生效,覆盖 titleTextColor 的值
28
- titleTextSize?: number // TopBar 标题字体大小,默认是 17 dp(pt)
29
- titleAlignmentAndroid?: TitleAlignment // TopBar 标题的位置,可选项有 `TitleAlignmentLeft` 和 `TitleAlignmentCenter` ,仅对 Android 生效
30
- barButtonItemTextSize?: number // TopBar 按钮字体大小,默认是 15 dp(pt)
31
- swipeBackEnabledAndroid?: boolean // Android 是否开启右滑返回,默认是 false
32
- splitTopBarTransitionIOS?: boolean // iOS 侧滑返回时,是否总是割裂导航栏背景
33
- scrimAlphaAndroid?: number // Android 侧滑返回遮罩效果 [0 - 255]
34
- displayCutoutWhenLandscapeAndroid?: boolean // 横屏时,是否将界面延伸至刘海区域,默认 true
35
-
36
- tabBarColor?: Color // 底部 TabBar 背景颜色,请勿使用带透明度的颜色。
37
- tabBarShadowImage?: ShadowImage // 底部 TabBar 阴影图片。对于 iOS, 只有同时设置了 tabBarColor 才会生效
38
- tabBarItemColor?: Color // 底部 TabBarItem icon 选中颜色
39
- tabBarUnselectedItemColor?: Color // 底部 TabBarItem icon 未选中颜色,默认为 #BDBDBD
40
- tabBarBadgeColor?: Color // Tab badge 颜色
41
- }
42
-
43
- export type TabBarStyle = Pick<
44
- Style,
45
- 'tabBarColor' | 'tabBarShadowImage' | 'tabBarItemColor' | 'tabBarUnselectedItemColor'
46
- >
47
-
48
- export interface TabBadge {
49
- index: number
50
- text?: string
51
- hidden: boolean
52
- dot?: boolean
53
- }
54
-
55
- export interface TabItemInfo {
56
- index: number
57
- title?: string
58
- badge?: {
59
- text?: string
60
- hidden: boolean
61
- dot?: boolean
62
- }
63
- icon?: {
64
- selected: ImageSource
65
- unselected?: ImageSource
66
- }
67
- }
68
-
69
- export interface TabIcon {
70
- index: number
71
- icon: ImageSource
72
- unselectedIcon?: ImageSource
73
- }
1
+ import { Platform } from 'react-native'
2
+ import GardenModule from './GardenModule'
3
+ import Event from './Event'
4
+ import {
5
+ BarButtonItem,
6
+ DefaultOptions,
7
+ NavigationOption,
8
+ TabBarStyle,
9
+ TabItemInfo,
10
+ TitleItem,
11
+ } from './Options'
74
12
 
75
13
  type Nullable<T> = {
76
14
  [P in keyof T]: T[P] extends T[P] | undefined ? T[P] | null : T[P]
77
15
  }
78
16
 
79
- const GardenModule = NativeModules.GardenModule
80
- const { STATUSBAR_HEIGHT, EVENT_STATUSBAR_FRAME_CHANGE, TOOLBAR_HEIGHT } = GardenModule.getConstants()
17
+ const { STATUSBAR_HEIGHT, TOOLBAR_HEIGHT } = GardenModule.getConstants()
81
18
 
82
19
  if (Platform.OS === 'ios') {
83
- const GardenEventEmitter: NativeEventEmitter = new NativeEventEmitter(GardenModule)
84
- GardenEventEmitter.addListener(EVENT_STATUSBAR_FRAME_CHANGE, ({ statusBarHeight }) => {
20
+ Event.listenStatusBarHeightChanged(statusBarHeight => {
85
21
  Garden.statusBarHeight = statusBarHeight
86
22
  Garden.topBarHeight = statusBarHeight + TOOLBAR_HEIGHT
87
23
  })
88
24
  }
89
25
 
90
26
  export class Garden {
91
- static setStyle(style: Style = {}) {
27
+ static setStyle(style: DefaultOptions = {}) {
92
28
  GardenModule.setStyle(style)
93
29
  }
94
30
 
@@ -101,22 +37,22 @@ export class Garden {
101
37
  // --------------- instance method --------------
102
38
 
103
39
  setLeftBarButtonItem(buttonItem: Nullable<BarButtonItem> | null) {
104
- const options = bindBarButtonItemClickEvent(buttonItem, { sceneId: this.sceneId })
40
+ const options = Event.bindBarButtonClickEvent(this.sceneId, buttonItem)
105
41
  GardenModule.setLeftBarButtonItem(this.sceneId, options)
106
42
  }
107
43
 
108
44
  setRightBarButtonItem(buttonItem: Nullable<BarButtonItem> | null) {
109
- const options = bindBarButtonItemClickEvent(buttonItem, { sceneId: this.sceneId })
45
+ const options = Event.bindBarButtonClickEvent(this.sceneId, buttonItem)
110
46
  GardenModule.setRightBarButtonItem(this.sceneId, options)
111
47
  }
112
48
 
113
49
  setLeftBarButtonItems(buttonItems: Array<Nullable<BarButtonItem>> | null) {
114
- const options = bindBarButtonItemClickEvent(buttonItems, { sceneId: this.sceneId })
50
+ const options = Event.bindBarButtonClickEvent(this.sceneId, buttonItems)
115
51
  GardenModule.setLeftBarButtonItems(this.sceneId, options)
116
52
  }
117
53
 
118
54
  setRightBarButtonItems(buttonItems: Array<Nullable<BarButtonItem>> | null) {
119
- const options = bindBarButtonItemClickEvent(buttonItems, { sceneId: this.sceneId })
55
+ const options = Event.bindBarButtonClickEvent(this.sceneId, buttonItems)
120
56
  GardenModule.setRightBarButtonItems(this.sceneId, options)
121
57
  }
122
58
 
@@ -139,46 +75,6 @@ export class Garden {
139
75
  GardenModule.setTabItem(this.sceneId, item)
140
76
  }
141
77
 
142
- setTabIcon(icon: TabIcon | TabIcon[]) {
143
- console.warn(`Garden#setTabIcon is deprecated, use Garden#setTabItem instead.`)
144
- if (!Array.isArray(icon)) {
145
- icon = [icon]
146
- }
147
- const icons = icon
148
- const items: Array<TabItemInfo> = []
149
- for (const i of icons) {
150
- items.push({
151
- index: i.index,
152
- icon: {
153
- selected: i.icon,
154
- unselected: i.unselectedIcon,
155
- },
156
- })
157
- }
158
- this.setTabItem(items)
159
- }
160
-
161
- setTabBadge(badge: TabBadge | TabBadge[]) {
162
- console.warn(`Garden#setTabBadge is deprecated, use Garden#setTabItem instead.`)
163
- if (!Array.isArray(badge)) {
164
- badge = [badge]
165
- }
166
-
167
- const badges = badge
168
- const items: Array<TabItemInfo> = []
169
- for (const b of badges) {
170
- items.push({
171
- index: b.index,
172
- badge: {
173
- hidden: b.hidden,
174
- text: b.text,
175
- dot: b.dot,
176
- },
177
- })
178
- }
179
- this.setTabItem(items)
180
- }
181
-
182
78
  setMenuInteractive(enabled: boolean) {
183
79
  GardenModule.setMenuInteractive(this.sceneId, enabled)
184
80
  }
@@ -0,0 +1,24 @@
1
+ import { NativeModules, NativeModule } from 'react-native'
2
+
3
+ interface GardenModule extends NativeModule {
4
+ getConstants: () => {
5
+ TOOLBAR_HEIGHT: number
6
+ STATUSBAR_HEIGHT: number
7
+ EVENT_STATUSBAR_FRAME_CHANGE: string
8
+ }
9
+
10
+ setStyle: (style: object) => void
11
+ setTitleItem: (sceneId: string, item: object) => void
12
+ setLeftBarButtonItem: (sceneId: string, item: object | null) => void
13
+ setRightBarButtonItem: (sceneId: string, item: object | null) => void
14
+ setLeftBarButtonItems: (sceneId: string, item: object | null) => void
15
+ setRightBarButtonItems: (sceneId: string, item: object | null) => void
16
+ updateOptions: (sceneId: string, options: object) => void
17
+ updateTabBar: (sceneId: string, item: object) => void
18
+ setTabItem: (sceneId: string, item: object) => void
19
+ setMenuInteractive: (sceneId: string, enabled: boolean) => void
20
+ }
21
+
22
+ const GardenModule: GardenModule = NativeModules.GardenModule
23
+
24
+ export default GardenModule
@@ -1,29 +1,118 @@
1
- import { NativeModules, NativeEventEmitter } from 'react-native'
2
-
3
- const NavigationModule = NativeModules.NavigationModule
4
- const HBDEventEmitter = NativeModules.HBDEventEmitter
5
-
6
- const EventEmitter: NativeEventEmitter = new NativeEventEmitter(HBDEventEmitter)
7
- export const EVENT_SWITCH_TAB: string = HBDEventEmitter.EVENT_SWITCH_TAB
8
- export const EVENT_NAVIGATION: string = HBDEventEmitter.EVENT_NAVIGATION
9
- export const EVENT_WILL_SET_ROOT: string = HBDEventEmitter.EVENT_WILL_SET_ROOT
10
- export const EVENT_DID_SET_ROOT: string = HBDEventEmitter.EVENT_DID_SET_ROOT
11
-
12
- export const ON_COMPONENT_RESULT: string = HBDEventEmitter.ON_COMPONENT_RESULT
13
- export const ON_BAR_BUTTON_ITEM_CLICK: string = HBDEventEmitter.ON_BAR_BUTTON_ITEM_CLICK
14
- export const ON_COMPONENT_APPEAR: string = HBDEventEmitter.ON_COMPONENT_APPEAR
15
- export const ON_COMPONENT_DISAPPEAR: string = HBDEventEmitter.ON_COMPONENT_DISAPPEAR
16
-
17
- export const KEY_ON: string = HBDEventEmitter.KEY_ON
18
- export const KEY_REQUEST_CODE: string = HBDEventEmitter.KEY_REQUEST_CODE
19
- export const KEY_RESULT_CODE: string = HBDEventEmitter.KEY_RESULT_CODE
20
- export const KEY_RESULT_DATA: string = HBDEventEmitter.KEY_RESULT_DATA
21
- export const KEY_SCENE_ID: string = HBDEventEmitter.KEY_SCENE_ID
22
- export const KEY_MODULE_NAME: string = HBDEventEmitter.KEY_MODULE_NAME
23
- export const KEY_INDEX: string = HBDEventEmitter.KEY_INDEX
24
- export const KEY_ACTION: string = HBDEventEmitter.KEY_ACTION
25
-
26
- export const RESULT_OK: number = NavigationModule.RESULT_OK
27
- export const RESULT_CANCEL: number = NavigationModule.RESULT_CANCEL
28
-
29
- export { EventEmitter, NavigationModule }
1
+ import { NativeModules } from 'react-native'
2
+ import { Route, RouteGraph } from './Route'
3
+
4
+ type Callback = (error: never, result: any) => void
5
+
6
+ interface NavigationModule {
7
+ getConstants: () => {
8
+ RESULT_OK: number
9
+ RESULT_CANCEL: number
10
+ }
11
+
12
+ startRegisterReactComponent: () => void
13
+ endRegisterReactComponent: () => void
14
+ registerReactComponent: (appKey: string, options: object) => void
15
+ signalFirstRenderComplete: (sceneId: string) => void
16
+ setRoot: (layout: object, sticky: boolean, tag: number) => void
17
+ setResult: (sceneId: string, resultCode: number, data: object | null) => void
18
+ dispatch: (sceneId: string, action: string, params: object, callback: Callback) => void
19
+ currentTab: (sceneId: string, callback: Callback) => void
20
+ isStackRoot: (sceneId: string, callback: Callback) => void
21
+ findSceneIdByModuleName: (moduleName: string, callback: Callback) => void
22
+ currentRoute: (callback: Callback) => void
23
+ routeGraph: (callback: Callback) => void
24
+ }
25
+
26
+ const NavigationModule: NavigationModule = NativeModules.NavigationModule
27
+
28
+ function getConstants() {
29
+ return NavigationModule.getConstants()
30
+ }
31
+
32
+ function startRegisterReactComponent() {
33
+ NavigationModule.startRegisterReactComponent()
34
+ }
35
+
36
+ function endRegisterReactComponent() {
37
+ NavigationModule.endRegisterReactComponent()
38
+ }
39
+
40
+ function registerReactComponent(appKey: string, options: object) {
41
+ NavigationModule.registerReactComponent(appKey, options)
42
+ }
43
+
44
+ function signalFirstRenderComplete(sceneId: string) {
45
+ NavigationModule.signalFirstRenderComplete(sceneId)
46
+ }
47
+
48
+ function setResult(sceneId: string, resultCode: number, data: object | null) {
49
+ NavigationModule.setResult(sceneId, resultCode, data)
50
+ }
51
+
52
+ function setRoot(layout: object, sticky: boolean, tag: number) {
53
+ NavigationModule.setRoot(layout, sticky, tag)
54
+ }
55
+
56
+ function dispatch(sceneId: string, action: string, params: object) {
57
+ return new Promise<boolean>(resolve => {
58
+ NavigationModule.dispatch(sceneId, action, params, (_, result) => {
59
+ resolve(result)
60
+ })
61
+ })
62
+ }
63
+
64
+ function currentTab(sceneId: string) {
65
+ return new Promise<number>(resolve => {
66
+ NavigationModule.currentTab(sceneId, (_, result) => {
67
+ resolve(result)
68
+ })
69
+ })
70
+ }
71
+
72
+ function isStackRoot(sceneId: string) {
73
+ return new Promise<boolean>(resolve => {
74
+ NavigationModule.isStackRoot(sceneId, (_, result) => {
75
+ resolve(result)
76
+ })
77
+ })
78
+ }
79
+
80
+ function findSceneIdByModuleName(moduleName: string) {
81
+ return new Promise<string | null>(resolve => {
82
+ NavigationModule.findSceneIdByModuleName(moduleName, (_, result) => {
83
+ resolve(result)
84
+ })
85
+ })
86
+ }
87
+
88
+ function currentRoute() {
89
+ return new Promise<Route>(resolve => {
90
+ NavigationModule.currentRoute((_, result) => {
91
+ resolve(result)
92
+ })
93
+ })
94
+ }
95
+
96
+ function routeGraph() {
97
+ return new Promise<RouteGraph[]>(resolve => {
98
+ NavigationModule.routeGraph((_, result) => {
99
+ resolve(result)
100
+ })
101
+ })
102
+ }
103
+
104
+ export default {
105
+ getConstants,
106
+ startRegisterReactComponent,
107
+ endRegisterReactComponent,
108
+ registerReactComponent,
109
+ signalFirstRenderComplete,
110
+ setResult,
111
+ setRoot,
112
+ dispatch,
113
+ currentTab,
114
+ isStackRoot,
115
+ findSceneIdByModuleName,
116
+ currentRoute,
117
+ routeGraph,
118
+ }