-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
199 lines (183 loc) · 5.33 KB
/
index.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
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
import React from 'react'
import {
StyleSheet,
View,
StatusBar,
Animated,
AsyncStorage,
Alert,
} from 'react-native'
import { AppLoading, SplashScreen } from 'expo'
import { Asset } from 'expo-asset'
import { connect } from 'react-redux'
import { NativeRouter } from 'react-router-native'
import get from 'lodash/get'
import { createStructuredSelector } from 'reselect'
import * as FileSystem from 'expo-file-system'
import RouterWithDrawer from './Navigation'
import api from './api'
import {
flatten,
fetchAllSuccess,
fetchAdsSuccess,
fetchAdsReq,
fetchAllPostsReq,
fetchAllFail,
} from './Pages/Posts/reducer'
import { cacheFolder, cacheDateStorageKey } from './constants/cacheFolder'
import { logo, defaultPostsLength } from './constants/miscConsts'
class AppIndex extends React.Component {
constructor(props) {
super(props)
SplashScreen.preventAutoHide()
this.state = {
fadeAnim: new Animated.Value(0), // Initial value for opacity: 0
isSplashReady: false,
isAppReady: false,
}
}
async componentDidMount() {
const folder = await FileSystem.getInfoAsync(cacheFolder)
if (!folder.exists) await FileSystem.makeDirectoryAsync(cacheFolder)
AsyncStorage.getItem(cacheDateStorageKey, async (err, result) => {
if (!result) return AsyncStorage.setItem(cacheDateStorageKey, (new Date()).toString())
const diff = (new Date(result) - new Date()) / 1000 / 24 / 60 / 60
if (diff < -1) {
try {
await FileSystem.deleteAsync(cacheFolder, { idempotent: true })
await FileSystem.makeDirectoryAsync(cacheFolder)
} catch (e) {
console.log(e)
}
return AsyncStorage.setItem(cacheDateStorageKey, (new Date()).toString())
}
})
}
_cacheSplashResourcesAsync = async () => Asset.fromModule(logo).downloadAsync()
_cacheResourcesAsync = async () => {
SplashScreen.hide()
const {
posts,
fetchPostsCustom,
fetchAdsCustom,
fetchAdsSuccessCustom,
setPostsCustom,
fetchFail,
} = this.props
const { fadeAnim } = this.state
if (!posts || posts.length === 0) {
fetchPostsCustom()
fetchAdsCustom()
fetch(api.getPosts(defaultPostsLength))
.then((r) => r.json())
.then((fetchedPosts) => {
fetch(api.getPromoCards(defaultPostsLength))
.then((r) => r.json())
.then((ads) => {
const result = fetchedPosts
.filter((post) => !post.categories.includes(617))
.map(
(post, index) => {
const count = index + 1
const shouldRenderPromo = count % 3 === 0
const pointer = count / 3 - 1
const ad = ads[pointer]
return index > 0 && shouldRenderPromo && ad
? [ad, post] : post
},
)
const flatResult = flatten(result)
fetchAdsSuccessCustom()
setPostsCustom(flatResult)
})
.catch((e) => fetchFail(e))
})
.then(async () => {
Animated.timing(
fadeAnim,
{
toValue: 1,
duration: 500,
useNativeDriver: false,
},
).start(() => {
this.setState({ isAppReady: true })
})
})
.catch((e) => console.error(e))
}
}
cacheResourcesAsync = async () => {
const images = [
logo,
]
const cacheImages = images.map((image) => Asset.fromModule(image).downloadAsync())
return Promise.all(cacheImages)
}
render() {
const { isSplashReady, isAppReady, fadeAnim } = this.state
if (!isSplashReady) {
return (
<AppLoading
startAsync={this._cacheSplashResourcesAsync}
onFinish={() => this.setState({ isSplashReady: true })}
onError={(e) => Alert(e)}
autoHideSplash={false}
/>
)
}
if (isSplashReady && !isAppReady) {
return (
<Animated.View
style={{
opacity: fadeAnim,
justifyContent: 'center',
alignItems: 'center',
flex: 1,
}}
>
<Animated.Image
source={logo}
style={{
aspectRatio: 2.3,
resizeMode: 'contain',
}}
onLoad={this._cacheResourcesAsync}
/>
</Animated.View>
)
}
return (
<NativeRouter>
<View style={styles.container}>
<StatusBar
style={{ zIndex: 10 }}
barStyle="light-content"
backgroundColor="#333376"
/>
<RouterWithDrawer />
</View>
</NativeRouter>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
},
})
const mapStateToProps = createStructuredSelector({
posts: (state) => get(state, 'posts.posts'),
})
const mapDispatchToProps = (dispatch) => ({
fetchPostsCustom: () => dispatch(fetchAllPostsReq(20)),
fetchAdsCustom: () => dispatch(fetchAdsReq(20)),
setPostsCustom: (data) => dispatch(fetchAllSuccess(data)),
fetchAdsSuccessCustom: () => dispatch(fetchAdsSuccess()),
fetchFail: (e) => dispatch(fetchAllFail(e)),
})
export default connect(
mapStateToProps,
mapDispatchToProps,
)(AppIndex)