This repository was archived by the owner on Jan 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRouter.native.js
161 lines (131 loc) · 4.63 KB
/
Router.native.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import React from 'react'
import { findKey, merge } from 'lodash'
import {
NavigationProvider,
NavigationContext,
StackNavigation,
createRouter,
NavigationStyles,
NavigationActions,
} from '@expo/ex-navigation'
import RouterBase from './RouterBase'
const rootNavigatorID = 'root'
const appNavigatorID = 'app'
export {
NavigationStyles,
createNavigationEnabledStore,
NavigationReducer,
} from '@expo/ex-navigation'
export default class TipsiRouter extends RouterBase {
/* eslint-disable no-unused-vars */
constructor(initialRoute, routes, useMemoryHistory = false, defaultRouteConfig = {}) {
super(initialRoute, routes, useMemoryHistory, defaultRouteConfig)
const App = this.createAppComponent(initialRoute, defaultRouteConfig)
const expoRouter = this.createRoutes({ ...routes, app: { component: App } })
this.title = defaultRouteConfig.title || ''
this.routes = routes
this.observers = []
this.navigationContext = new NavigationContext({ router: expoRouter })
this.navigationProvider = this.stackNavigationProvider(initialRoute, expoRouter)
}
createRoutes = (appRoutes) => {
const routes = Object.keys(appRoutes).reduce((memo, key) => ({
...memo,
[key]: () => appRoutes[key].component,
}), {})
return createRouter(() => routes, { ignoreSerializableWarnings: true })
}
/* eslint-disable react/no-this-in-sfc */
stackNavigationProvider = () => (
<NavigationProvider context={this.navigationContext}>
<StackNavigation
id={rootNavigatorID}
initialRoute="app"
defaultRouteConfig={{ styles: NavigationStyles.SlideVertical }}
/>
</NavigationProvider>
)
createAppComponent = (initialRoute, defaultRouteConfig) => () => (
<StackNavigation
id={appNavigatorID}
initialRoute={initialRoute}
defaultRouteConfig={defaultRouteConfig}
/>
)
getNavigator = () => (
this.navigationContext.getNavigator(appNavigatorID)
)
getCurrentRoute = () => this.getNavigator().getCurrentRoute()
config = (params) => {
const currentRoute = this.getCurrentRoute()
currentRoute.config = merge(currentRoute.config, params)
}
setTitle(title) {
super.setTitle(title)
this.config({ navigationBar: { title } })
}
updateParams = (params = {}) => {
const navigator = this.getNavigator()
navigator.updateCurrentRouteParams(params)
}
updateTitle = (title) => {
this.setTitle(title)
this.updateParams()
}
/* eslint-disable no-underscore-dangle */
push = (e, route, paramsOrOptions = {}) => {
const { config = {}, ...params } = paramsOrOptions
const { transitionGroup, ...restConfig } = config
const navigator = this.getNavigator()
const expoRoute = this.navigationContext._router.getRoute(this.routeName(route), params)
expoRoute.config = { ...restConfig }
this.navigationContext.performAction(({ stacks }) => {
stacks(navigator.navigatorUID).push(expoRoute)
})
}
pop = () => {
this.getNavigator().pop()
}
popToTop = () => {
this.getNavigator().popToTop()
}
replace = (e, route, paramsOrOptions = {}) => {
const { config = {}, ...params } = paramsOrOptions
const navigator = this.getNavigator()
const expoRoute = this.navigationContext._router.getRoute(this.routeName(route), params)
expoRoute.config = { ...config }
navigator.componentInstance._useAnimation = false
this.navigationContext.performAction(({ stacks }) => {
stacks(navigator.navigatorUID).replace(expoRoute)
})
requestAnimationFrame(() => {
navigator.componentInstance._useAnimation = true
})
}
showModal = (e, route, paramsOrOptions = {}, delay = 0) => {
const { config = {}, ...params } = paramsOrOptions
const navigatorApp = this.getNavigator()
const navigatorMaster = this.navigationContext.getNavigatorByUID(
navigatorApp.parentNavigatorUID
)
const expoRoute = this.navigationContext._router.getRoute(this.routeName(route), params)
expoRoute.config = { ...config }
const pushScene = () => this.navigationContext.performAction(({ stacks }) => {
stacks(navigatorMaster.navigatorUID).push(expoRoute)
})
delay ? setTimeout(pushScene, delay) : pushScene()
}
dismissModal = () => {
const navigatorApp = this.getNavigator()
const navigatorMaster = this.navigationContext.getNavigatorByUID(
navigatorApp.parentNavigatorUID
)
navigatorMaster.pop()
this.navigationContext._store.dispatch(
NavigationActions.setCurrentNavigator(navigatorApp.navigatorUID)
)
}
/* eslint-disable-next-line */
redirect() {}
routeName = route => findKey(this.routes, { path: route.path })
}