forked from race604/ZhiHuDaily-React-Native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWelcomeScreen.js
103 lines (96 loc) · 1.99 KB
/
WelcomeScreen.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
'use strict';
var React = require('react-native');
var {
AsyncStorage,
Image,
StyleSheet,
Text,
View,
} = React;
var REQUEST_URL = "http://news-at.zhihu.com/api/4/start-image/1080*1776";
var COVER_KEY = '@WelcomeScreen:cover';
var WelcomeScreen = React.createClass({
fetchData: function() {
fetch(REQUEST_URL)
.then((response) => response.json())
.then((responseData) => {
//console.log(responseData);
this.setState({cover: responseData});
//this._onCoverLoaded(responseData);
})
.done();
},
async _onCoverLoaded(value) {
try {
await AsyncStorage.setItem(COVER_KEY, value);
} catch (error) {
console.log(error);
}
},
componentDidMount: function() {
this.fetchData();
//this._loadInitialState().done();
},
async _loadInitialState() {
try {
var value = await AsyncStorage.getItem(COVER_KEY);
if (value !== null){
this.setState({cover: value});
}
} catch (error) {
}
},
getInitialState: function() {
return {
cover: null,
};
},
renderEmpty: function() {
return(
<View style={styles.container}>
<Text style={styles.title}>
知乎日报
</Text>
</View>
);
},
render: function() {
if (!this.state.cover) {
return this.renderEmpty();
}
return(
<Image
source={{uri: this.state.cover.img}}
style={styles.cover}>
<Text style={styles.text}>
{this.state.cover.text}
</Text>
</Image>
);
}
});
var styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
cover: {
flex: 1,
flexDirection: 'row',
backgroundColor: '#F5FCFF',
},
title: {
fontSize: 32,
fontWeight: '500',
},
text: {
flex: 1,
fontSize: 16,
alignSelf: 'flex-end',
textAlign: 'center',
marginBottom: 10,
}
});
module.exports = WelcomeScreen;