-
Notifications
You must be signed in to change notification settings - Fork 36
/
App.tsx
283 lines (241 loc) · 8.74 KB
/
App.tsx
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
import 'react-native-gesture-handler'
import AsyncStorage from '@react-native-community/async-storage'
import NetInfo, { NetInfoSubscription } from '@react-native-community/netinfo'
import React, { Component } from 'react'
import { Image, LogBox, Platform, StatusBar, View } from 'react-native'
import Config from 'react-native-config'
import { getFontScale } from 'react-native-device-info'
import { GestureHandlerRootView } from 'react-native-gesture-handler'
import Orientation from 'react-native-orientation-locker'
import { initialWindowMetrics, SafeAreaProvider } from 'react-native-safe-area-context'
import TrackPlayer from 'react-native-track-player'
import { setGlobal } from 'reactn'
import {
UpdateRequiredOverlay,
OverlayAlert,
ImageFullView,
BoostDropdownBanner,
LoadingInterstitialView,
NotificationBanner,
MaintenanceScheduledOverlay
} from './src/components'
import { checkIfFDroidAppVersion, pvIsTablet } from './src/lib/deviceDetection'
import {
registerAndroidAutoModule,
requestDrawOverAppsPermission,
unregisterAndroidAutoModule
} from './src/lib/carplay/PVCarPlay.android'
import { refreshDownloads } from './src/lib/downloader'
import { PV } from './src/resources'
import { determineFontScaleMode } from './src/resources/Fonts'
import { GlobalTheme } from './src/resources/Interfaces'
import Router from './src/Router'
import { isInitialLoadPodcastsScreen } from './src/screens/PodcastsScreen'
import { downloadCategoriesList } from './src/services/category'
import PVEventEmitter from './src/services/eventEmitter'
import { playerHandlePauseWithUpdate } from './src/services/player'
import { PlayerAudioSetupService } from './src/services/playerAudioSetup'
import { getLastMaintenanceScheduledStartTime, getMetaAppInfo,
updateLastMaintenanceScheduledStartTime } from './src/services/meta'
import { pauseDownloadingEpisodesAll } from './src/state/actions/downloads'
import { settingsRunEveryStartup } from './src/state/actions/settings'
import initialState from './src/state/initialState'
import { darkTheme, lightTheme } from './src/styles'
import { hasValidDownloadingConnection } from './src/lib/network'
import {
handleCarPlayPodcastsUpdate,
handleCarPlayQueueUpdate,
registerCarModule,
showRootView,
unregisterCarModule
} from './src/lib/carplay/PVCarPlay'
LogBox.ignoreLogs(['EventEmitter.removeListener', 'Require cycle'])
type Props = any
type State = {
appReady: boolean
minVersionMismatch: boolean
maintenanceScheduled?: {
endTime: Date
startTime: Date
} | null
}
setGlobal(initialState)
let ignoreHandleNetworkChange = Platform.OS === 'ios'
let carplayEventsInitialized = false
class App extends Component<Props, State> {
unsubscribeNetListener: NetInfoSubscription | null
constructor(props: Props) {
super(props)
if (!pvIsTablet()) {
Orientation.lockToPortrait()
}
this.state = {
appReady: false,
minVersionMismatch: false
}
this.unsubscribeNetListener = null
downloadCategoriesList()
}
async componentDidMount() {
// Android Auto
if (Platform.OS === 'android' && !checkIfFDroidAppVersion()) {
// initialize Android Auto Tabs with no content. Content will be updated as they are loaded to the global state.
registerAndroidAutoModule()
await requestDrawOverAppsPermission()
}
TrackPlayer.registerPlaybackService(() => require('./src/services/playerAudioEvents'))
await PlayerAudioSetupService()
StatusBar.setBarStyle('light-content')
Platform.OS === 'android' && StatusBar.setBackgroundColor(PV.Colors.ink, true)
const darkModeEnabled = await AsyncStorage.getItem(PV.Keys.DARK_MODE_ENABLED)
let globalTheme = darkTheme
if (darkModeEnabled === null) {
globalTheme = Config.DEFAULT_THEME_DARK ? darkTheme : lightTheme
} else if (darkModeEnabled === 'FALSE') {
globalTheme = lightTheme
}
await this.setupGlobalState(globalTheme)
await settingsRunEveryStartup()
this.unsubscribeNetListener = NetInfo.addEventListener(this.handleNetworkChange)
// iOS CarPlay
Platform.OS === 'ios' && registerCarModule(this.onConnect, this.onDisconnect)
}
componentWillUnmount() {
this.unsubscribeNetListener && this.unsubscribeNetListener()
// iOS CarPlay
Platform.OS === 'ios' && unregisterCarModule(this.onConnect, this.onDisconnect)
// Android Auto
if (Platform.OS === 'android' && !checkIfFDroidAppVersion()) {
unregisterAndroidAutoModule()
}
}
onConnect = () => {
// Do things now that carplay is connected
showRootView()
if (!carplayEventsInitialized) {
carplayEventsInitialized = true
PVEventEmitter.on(PV.Events.QUEUE_HAS_UPDATED, handleCarPlayQueueUpdate)
PVEventEmitter.on(PV.Events.APP_FINISHED_INITALIZING_FOR_CARPLAY, handleCarPlayPodcastsUpdate)
/*
This code is intended to correct a race condition when the mobile app is already initialized,
then CarPlay is connected later.
*/
if (!isInitialLoadPodcastsScreen) {
handleCarPlayPodcastsUpdate()
}
}
}
onDisconnect = () => {
playerHandlePauseWithUpdate()
// Do things now that carplay is disconnected
PVEventEmitter.removeListener(PV.Events.QUEUE_HAS_UPDATED, handleCarPlayQueueUpdate)
PVEventEmitter.removeListener(PV.Events.APP_FINISHED_INITALIZING_FOR_CARPLAY, handleCarPlayPodcastsUpdate)
}
handleNetworkChange = () => {
;(async () => {
this.setState({ appReady: true })
// Don't continue handleNetworkChange when internet is first reachable on initial app launch
if (ignoreHandleNetworkChange) {
ignoreHandleNetworkChange = false
return
}
await this.getMetaAppInfo()
const skipCannotDownloadAlert = true
if (await hasValidDownloadingConnection(skipCannotDownloadAlert)) {
refreshDownloads()
} else {
pauseDownloadingEpisodesAll()
}
})()
}
setupGlobalState = async (theme: GlobalTheme) => {
const fontScale = await getFontScale()
const fontScaleMode = determineFontScaleMode(fontScale)
setGlobal({
globalTheme: theme,
fontScaleMode,
fontScale
})
}
getMetaAppInfo = async () => {
const result = await getMetaAppInfo()
const { versionValid } = result
let { maintenanceScheduled } = result
const currentUTCTime = new Date(new Date().getTime())
const startTime = maintenanceScheduled?.startTime ? new Date(maintenanceScheduled.startTime) : null
const endTime = maintenanceScheduled?.endTime ? new Date(maintenanceScheduled.endTime) : null
const lastStartTime = await getLastMaintenanceScheduledStartTime()
const hasNewStartTime = startTime && lastStartTime !== startTime?.toString()
if (hasNewStartTime && endTime && currentUTCTime < startTime) {
await updateLastMaintenanceScheduledStartTime(startTime)
maintenanceScheduled = {
startTime,
endTime
}
} else {
await updateLastMaintenanceScheduledStartTime()
maintenanceScheduled = null
}
this.setState({
minVersionMismatch: !versionValid,
maintenanceScheduled
})
}
_handleMaintenanceScheduledConfirm = () => {
this.setState({ maintenanceScheduled: null })
}
_renderIntersitial = () => {
if (Platform.OS === 'ios') {
return null
}
return (
<View style={{ backgroundColor: PV.Colors.ink, flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Image source={PV.Images.BANNER} resizeMode='contain' />
</View>
)
}
render() {
const { maintenanceScheduled, minVersionMismatch } = this.state
// Prevent white screen flash on navigation on Android
const wrapperStyle =
Platform.OS === 'android'
? {
backgroundColor: PV.Colors.ink,
borderColor: PV.Colors.ink,
shadowOpacity: 1,
opacity: 1
}
: {
flex: 1
}
if (minVersionMismatch) {
return <UpdateRequiredOverlay />
}
if (maintenanceScheduled) {
return (
<MaintenanceScheduledOverlay
endTime={maintenanceScheduled.endTime}
handleConfirmPress={this._handleMaintenanceScheduledConfirm}
startTime={maintenanceScheduled.startTime}
/>
)
}
return this.state.appReady ? (
<GestureHandlerRootView style={{ flex: 1 }}>
<SafeAreaProvider initialMetrics={initialWindowMetrics} style={wrapperStyle}>
<View style={{ flex: 1 }}>
<Router />
<OverlayAlert />
</View>
<ImageFullView />
<BoostDropdownBanner />
<NotificationBanner />
</SafeAreaProvider>
<LoadingInterstitialView />
</GestureHandlerRootView>
) : (
this._renderIntersitial()
)
}
}
export default App