forked from race604/ZhiHuDaily-React-Native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StoryScreen.js
168 lines (160 loc) · 3.66 KB
/
StoryScreen.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
'use strict';
var React = require('react-native');
var {
AppRegistry,
PixelRatio,
StyleSheet,
Text,
View,
Image,
ToolbarAndroid,
TouchableHighlight,
} = React;
var precomputeStyle = require('precomputeStyle');
var MyWebView = require('./WebView');
var DetailToolbar = require('./DetailToolbar');
var BASE_URL = 'http://news.at.zhihu.com/api/4/news/';
var REF_HEADER = 'header';
var PIXELRATIO = PixelRatio.get();
var StoryScreen = React.createClass({
getInitialState: function() {
return({
isLoading: false,
detail: null,
scrollY: 0,
});
},
componentDidMount: function() {
this.fetchStroyDetail();
},
fetchStroyDetail: function() {
var reqUrl = BASE_URL + this.props.story.id;
this.setState({
isLoading: true,
detail: null,
});
fetch(reqUrl)
.then((response) => response.json())
.catch((error) => {
this.setState({
isLoading: false,
detail: null,
});
})
.then((responseData) => {
this.setState({
isLoading: false,
detail: responseData,
});
})
.done();
},
onWebViewScroll: function(event) {
//console.log('ScrollY: ' + event);
var scrollY = -event / PIXELRATIO;
var nativeProps = precomputeStyle({transform: [{translateY: scrollY}]});
this.refs[REF_HEADER].setNativeProps(nativeProps);
},
render: function() {
var toolbar = <DetailToolbar navigator={this.props.navigator} style={styles.toolbar}
story={this.props.story}/>;
if (this.state.isLoading) {
return (
<View style={[styles.container, styles.center]}>
<Text>
正在加载...
</Text>
{toolbar}
</View>
);
} else {
if (this.state.detail) {
// var headerStyle = {
// height: 200,
// flexDirection: 'row',
// transform: [{translateY: this.state.scrollY}],
// };
var toolbar
return (
<View style={styles.container}>
<MyWebView
style={styles.content}
html={this.state.detail.body}
css={this.state.detail.css[0]}
onScrollChange={this.onWebViewScroll}/>
<Image
ref={REF_HEADER}
source={{uri: this.state.detail.image}}
style={styles.headerImage} >
<View style={styles.titleContainer}>
<Text style={styles.title}>
{this.props.story.title}
</Text>
</View>
</Image>
{toolbar}
</View>
);
} else {
return (
<View style={[styles.container, styles.center]}>
<Text>
加载失败
</Text>
{toolbar}
</View>
);
}
}
}
});
var styles = StyleSheet.create({
toolbar: {
backgroundColor: '#00a2ed',
height: 56,
position: 'absolute',
left: 0,
right: 0,
bottom: 0,
top: 0,
},
headerImage: {
height: 200,
flexDirection: 'row',
backgroundColor: '#DDDDDD',
position: 'absolute',
left: 0,
right: 0,
bottom: 0,
top: 56,
},
titleContainer: {
flex: 1,
alignSelf: 'flex-end',
padding: 10,
backgroundColor: 'rgba(0,0,0,0.3)',
},
title: {
flex: 1,
fontSize: 18,
fontWeight: '500',
color: 'white',
},
container: {
flex: 1,
backgroundColor: '#F5FCFF',
},
center: {
justifyContent: 'center',
alignItems: 'center',
},
content: {
flex: 1,
position: 'absolute',
left: 0,
right: 0,
bottom: 0,
top:56,
},
});
module.exports = StoryScreen;