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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
|
import React, {useState, useRef, useEffect} from 'react'
import {observer} from 'mobx-react-lite'
import {
useWindowDimensions,
GestureResponderEvent,
Image,
SafeAreaView,
StyleSheet,
Text,
TouchableOpacity,
View,
} from 'react-native'
import {ScreenContainer, Screen} from 'react-native-screens'
import {GestureDetector, Gesture} from 'react-native-gesture-handler'
import Animated, {
useSharedValue,
useAnimatedStyle,
withTiming,
runOnJS,
interpolate,
} from 'react-native-reanimated'
import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome'
import {IconProp} from '@fortawesome/fontawesome-svg-core'
import {useStores} from '../../../state'
import {NavigationModel} from '../../../state/models/navigation'
import {TabsSelectorModel} from '../../../state/models/shell'
import {match, MatchResult} from '../../routes'
import {Modal} from '../../com/modals/Modal'
import {LocationNavigator} from './location-navigator'
import {createBackMenu, createForwardMenu} from './history-menu'
import {createAccountsMenu} from './accounts-menu'
import {createLocationMenu} from './location-menu'
import {s, colors} from '../../lib/styles'
import {AVIS} from '../../lib/assets'
const locationIconNeedsNudgeUp = (icon: IconProp) => icon === 'house'
const SWIPE_GESTURE_DIST_TRIGGER = 0.5
const SWIPE_GESTURE_VEL_TRIGGER = 2500
const Location = ({
icon,
title,
onPress,
}: {
icon: IconProp
title?: string
onPress?: (event: GestureResponderEvent) => void
}) => {
const nudgeUp = locationIconNeedsNudgeUp(icon)
return (
<TouchableOpacity style={styles.location} onPress={onPress}>
{title ? (
<FontAwesomeIcon
size={12}
style={[
styles.locationIcon,
nudgeUp ? styles.locationIconNudgeUp : undefined,
]}
icon={icon}
/>
) : (
<FontAwesomeIcon
size={12}
style={styles.locationIconLight}
icon="magnifying-glass"
/>
)}
<Text style={title ? styles.locationText : styles.locationTextLight}>
{title || 'Search'}
</Text>
</TouchableOpacity>
)
}
const Btn = ({
icon,
inactive,
onPress,
onLongPress,
}: {
icon: IconProp
inactive?: boolean
onPress?: (event: GestureResponderEvent) => void
onLongPress?: (event: GestureResponderEvent) => void
}) => {
if (inactive) {
return (
<View style={styles.ctrl}>
<FontAwesomeIcon
size={21}
style={[styles.ctrlIcon, styles.inactive]}
icon={icon}
/>
</View>
)
}
return (
<TouchableOpacity
style={styles.ctrl}
onPress={onPress}
onLongPress={onLongPress}>
<FontAwesomeIcon size={21} style={styles.ctrlIcon} icon={icon} />
</TouchableOpacity>
)
}
export const MobileShell: React.FC = observer(() => {
const store = useStores()
const [isLocationMenuActive, setLocationMenuActive] = useState(false)
const winDim = useWindowDimensions()
const swipeGestureInterp = useSharedValue<number>(0)
const screenRenderDesc = constructScreenRenderDesc(store.nav)
const onPressAvi = () =>
createAccountsMenu({
debug_onPressItem: () => store.nav.navigate('/profile/alice.com'),
})
const onPressLocation = () => setLocationMenuActive(true)
const onPressEllipsis = () => createLocationMenu()
const onNavigateLocation = (url: string) => {
setLocationMenuActive(false)
store.nav.navigate(url)
}
const onDismissLocationNavigator = () => setLocationMenuActive(false)
const onPressBack = () => store.nav.tab.goBack()
const onPressForward = () => store.nav.tab.goForward()
const onPressHome = () => store.nav.navigate('/')
const onPressNotifications = () => store.nav.navigate('/notifications')
const onPressTabs = () => store.shell.openModal(new TabsSelectorModel())
const onLongPressBack = () => createBackMenu(store.nav.tab)
const onLongPressForward = () => createForwardMenu(store.nav.tab)
const goBack = () => store.nav.tab.goBack()
const swipeGesture = Gesture.Pan()
.onUpdate(e => {
if (store.nav.tab.canGoBack) {
swipeGestureInterp.value = Math.max(e.translationX / winDim.width, 0)
}
})
.onEnd(e => {
if (
swipeGestureInterp.value >= SWIPE_GESTURE_DIST_TRIGGER ||
e.velocityX > SWIPE_GESTURE_VEL_TRIGGER
) {
swipeGestureInterp.value = withTiming(1, {duration: 100}, () => {
runOnJS(goBack)()
})
} else {
swipeGestureInterp.value = withTiming(0, {duration: 100})
}
})
useEffect(() => {
// reset the swipe interopolation when the page changes
swipeGestureInterp.value = 0
}, [swipeGestureInterp, store.nav.tab.current])
const swipeTransform = useAnimatedStyle(() => ({
transform: [{translateX: swipeGestureInterp.value * winDim.width}],
}))
const swipeOpacity = useAnimatedStyle(() => ({
opacity: interpolate(swipeGestureInterp.value, [0, 1.0], [0.6, 0.0]),
}))
return (
<View style={styles.outerContainer}>
<View style={styles.topBar}>
<TouchableOpacity onPress={onPressAvi}>
<Image style={styles.avi} source={AVIS['alice.com']} />
</TouchableOpacity>
<Location
icon={screenRenderDesc.icon}
title={store.nav.tab.current.title}
onPress={onPressLocation}
/>
<TouchableOpacity style={styles.topBarBtn} onPress={onPressEllipsis}>
<FontAwesomeIcon icon="ellipsis" />
</TouchableOpacity>
</View>
<SafeAreaView style={styles.innerContainer}>
<GestureDetector gesture={swipeGesture}>
<ScreenContainer style={styles.screenContainer}>
{screenRenderDesc.screens.map(
({Com, params, key, current, previous}) => {
return (
<Screen
key={key}
style={[StyleSheet.absoluteFill]}
activityState={current ? 2 : previous ? 1 : 0}>
<Animated.View
style={
current ? [styles.screenMask, swipeOpacity] : undefined
}
/>
<Animated.View
style={[
s.flex1,
styles.screen,
current ? swipeTransform : undefined,
]}>
<Com params={params} visible={true} />
</Animated.View>
</Screen>
)
},
)}
</ScreenContainer>
</GestureDetector>
</SafeAreaView>
<View style={styles.bottomBar}>
<Btn
icon="angle-left"
inactive={!store.nav.tab.canGoBack}
onPress={onPressBack}
onLongPress={onLongPressBack}
/>
<Btn
icon="angle-right"
inactive={!store.nav.tab.canGoForward}
onPress={onPressForward}
onLongPress={onLongPressForward}
/>
<Btn icon="house" onPress={onPressHome} />
<Btn icon={['far', 'bell']} onPress={onPressNotifications} />
<Btn icon={['far', 'clone']} onPress={onPressTabs} />
</View>
<Modal />
{isLocationMenuActive && (
<LocationNavigator
url={store.nav.tab.current.url}
onNavigate={onNavigateLocation}
onDismiss={onDismissLocationNavigator}
/>
)}
</View>
)
})
/**
* This method produces the information needed by the shell to
* render the current screens with screen-caching behaviors.
*/
type ScreenRenderDesc = MatchResult & {
key: string
current: boolean
previous: boolean
}
function constructScreenRenderDesc(nav: NavigationModel): {
icon: IconProp
screens: ScreenRenderDesc[]
} {
let icon: IconProp = 'magnifying-glass'
let screens: ScreenRenderDesc[] = []
for (const tab of nav.tabs) {
const tabScreens = [
...tab.getBackList(5),
Object.assign({}, tab.current, {index: tab.index}),
]
const parsedTabScreens = tabScreens.map(screen => {
const isCurrent = nav.isCurrentScreen(tab.id, screen.index)
const isPrevious = nav.isCurrentScreen(tab.id, screen.index + 1)
const matchRes = match(screen.url)
if (isCurrent) {
icon = matchRes.icon
}
return Object.assign(matchRes, {
key: `t${tab.id}-s${screen.index}`,
current: isCurrent,
previous: isPrevious,
}) as ScreenRenderDesc
})
screens = screens.concat(parsedTabScreens)
}
return {
icon,
screens,
}
}
const styles = StyleSheet.create({
outerContainer: {
height: '100%',
},
innerContainer: {
flex: 1,
},
screenContainer: {
flex: 1,
},
screen: {
backgroundColor: colors.gray1,
},
screenMask: {
position: 'absolute',
top: 0,
bottom: 0,
left: 0,
right: 0,
backgroundColor: '#000',
opacity: 0.5,
},
topBar: {
flexDirection: 'row',
backgroundColor: colors.white,
borderBottomWidth: 1,
borderBottomColor: colors.gray2,
paddingLeft: 10,
paddingRight: 10,
paddingTop: 40,
paddingBottom: 5,
},
avi: {
width: 34,
height: 34,
marginRight: 8,
borderRadius: 17,
},
location: {
flex: 1,
flexDirection: 'row',
borderRadius: 6,
paddingLeft: 12,
paddingRight: 6,
paddingTop: 9,
paddingBottom: 9,
backgroundColor: colors.gray1,
},
locationIcon: {
color: colors.gray5,
marginTop: 3,
marginRight: 6,
},
locationIconNudgeUp: {
marginTop: 2,
},
locationIconLight: {
color: colors.gray5,
marginTop: 2,
marginRight: 8,
},
locationText: {
color: colors.black,
},
locationTextLight: {
color: colors.gray4,
},
topBarBtn: {
marginLeft: 8,
justifyContent: 'center',
borderRadius: 6,
paddingHorizontal: 6,
},
bottomBar: {
flexDirection: 'row',
backgroundColor: colors.white,
borderTopWidth: 1,
borderTopColor: colors.gray2,
paddingLeft: 5,
paddingRight: 15,
paddingBottom: 20,
},
ctrl: {
flex: 1,
paddingTop: 15,
paddingBottom: 15,
},
ctrlIcon: {
marginLeft: 'auto',
marginRight: 'auto',
},
inactive: {
color: colors.gray3,
},
})
|