-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTopicList.js
More file actions
258 lines (232 loc) · 7.2 KB
/
Copy pathTopicList.js
File metadata and controls
258 lines (232 loc) · 7.2 KB
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
import React from 'react-native';
let {
Text,
View,
StyleSheet,
ListView,
Image,
TouchableOpacity,
RefreshControl,
StatusBarIOS,
} = React;
let styles = StyleSheet.create({
container: {
flex: 1,
}
});
import Topic from './Topic';
import Fmt from './Fmt';
import SendTopic from './SendTopic';
var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
export default class TopicList extends React.Component {
constructor(props) {
super(props);
this.state = {
dataSource:this.newDs(),
isRefreshing:false,
refreshingTitle:'下拉刷新',
more:true,
page:1,
scrollsToTop: props.idx === 0,
};
}
static contextTypes = {
activeTab: React.PropTypes.number.isRequired
};
newDs() {
return new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
}
componentWillMount() {
StatusBarIOS.setStyle(0);
this.refresh();
console.log('componentWillMount on Content #' + this.props.idx);
}
buildUrl(page) {
let url = 'https://cnodejs.org/api/v1/topics';
switch (this.props.idx) {
case 1: url += '?tab=good'; break;
case 2: url += '?tab=share'; break;
case 3: url += '?tab=ask'; break;
case 4: url += '?tab=job'; break;
default: url += '?'
}
url += '&page='+page+'&limit=20';
return url;
}
refresh() {
this.setState({isRefreshing: true});
let url = this.buildUrl(1);
fetch(url)
.then(res => res.json())
.then(res => {
let data = res.data.map(i => {i.key = i.id; return i});
console.log(url);
this.setState({
data: data,
dataSource: this.state.dataSource.cloneWithRows(data),
isRefreshing: false,
page: 1,
more:data.length == 20,
});
})
.catch(error => {
console.log(error)
})
.done();
}
loadMore() {
if (!this.state.more) return;
console.log('MORE--------------');
let url = this.buildUrl(this.state.page + 1);
console.log("FETCH:", url);
fetch(url)
.then(res => res.json())
.then(res => {
let data = res.data.map(i => {i.key = i.id; return i});
console.log('FETCH-ROWS:', data.length);
let oldData = this.state.data;
for (let x of data) {
oldData.push(x);
}
this.setState({
dataSource: this.state.dataSource.cloneWithRows(oldData),
total: res.total,
page: this.state.page + 1,
more: res.data.length == 20,
});
})
.catch(error => {
console.log(error);
this.setState(
{
loadError: true,
});
})
.done();
}
componentDidMount() {
console.log('componentDidMount on Content #' + this.props.idx);
}
tag(topic) {
if (topic.top) {
return(
<Text style={{backgroundColor:'#80bd00', color:'white', fontSize:12, padding:2}}>置顶</Text>
);
} else {
let type;
if (topic.tab == 'ask' && this.props.idx != 3) {
type = '问答';
} else if (topic.tab == 'job' && this.props.idx != 4) {
type = '招聘';
} else {
return null;
}
return (
<Text style={{backgroundColor:'#d9ebb3', color:'white', fontSize:12, padding:2,}}>{type}</Text>
);
}
}
pick(topic) {
if (topic.good) {
return(
<Text style={{backgroundColor:'#eb6420', color:'white', fontSize:12, padding:2}}>精华</Text>
);
}
return null;
}
_openTopic(topic) {
this.props.navigator.push({
component: Topic,
passProps: { topic: topic },
whiteStatus: true,
});
}
_renderRow(topic) {
//{this.pick(topic)}
//<Text style={{color:'#999', fontSize:12, padding:2}}>{this.Fmt.dateDiff(topic.create_at)}</Text>
return (
<TouchableOpacity onPress={this._openTopic.bind(this, topic)}>
<View overflow='hidden' style={{flexDirection:'row', padding:8, flex:1, borderBottomWidth:1, borderBottomColor:'#f0f0f0'}}>
<View style={{marginRight:10, marginLeft:4}}>
<Image style={{width:36, height:36, borderRadius:18, marginBottom:8, backgroundColor:'#ddd'}} source={{uri:Fmt.fixFaceUrl(topic.author.avatar_url)}}/>
</View>
<View style={{flex:1}}>
<View style={{flexDirection:'row'}}>
<Text style={{color:'#999', fontSize:12, padding:0}}>{topic.author.loginname}</Text>
<View style={{flex:1}}/>
{this.pick(topic)}
{this.tag(topic)}
</View>
<View style={{flexDirection:'row', paddingTop:0, paddingBottom:5}}>
<Text numberOfLines={2} style={{fontSize:16, flex:1, }}>{topic.title}</Text>
</View>
<View style={{flexDirection:'row'}}>
<Text style={{color:'#999', fontSize:12, padding:0}}>{topic.visit_count}阅读</Text>
<Text style={{color:'#999', fontSize:12, padding:0}}>/ {topic.reply_count}回复</Text>
<View style={{flex:1}}/>
<Text style={{color:'#999', fontSize:12, padding:0}}>{Fmt.dateDiff(topic.last_reply_at)}</Text>
</View>
</View>
</View>
</TouchableOpacity>
);
}
_onRefresh() {
this.refresh();
}
_renderFooter() {
return (
<View style={{flex:1, alignItems:'center', justifyContent:'center', padding: 10}}>
<Text style={{color:'grey'}}>{this.state.more ? '查看更多' : '没有更多了'}</Text>
</View>
);
}
componentWillReceiveProps(nextProps, nextContext) {
if (nextContext.activeTab == this.props.idx) {
this.setState({scrollsToTop: true});
} else {
this.setState({scrollsToTop: false});
}
}
_onSendTopic() {
this.props.navigator.push({
component: SendTopic,
passProps: { idx : this.props.idx, parent: this},
whiteStatus: true,
});
}
render() {
console.log('render on Content #' + this.props.idx);
return (
<View style={[styles.container, {flex:1}]}>
<ListView
scrollsToTop={this.state.scrollsToTop}
scrollIndicatorInsets={{top: 0}}
dataSource={this.state.dataSource}
renderRow={this._renderRow.bind(this)}
onEndReached={(event) => this.loadMore() }
onEndReachedThreshold={0}
renderFooter={this._renderFooter.bind(this)}
refreshControl={
<RefreshControl
style={{backgroundColor:'rgba(10,10,10,0.06)'}}
onRefresh={this._onRefresh.bind(this)}
refreshing={this.state.isRefreshing}
title='下拉刷新'
//tintColor="#ff0000"
colors={['#ff000000','#00ff0000','#0000ff00']}
progressBackgroundColor="#ffff0000"
/>
}
>
</ListView>
<TouchableOpacity
onPress={this._onSendTopic.bind(this)}
style={{position:'absolute', right:20, bottom:20, width:60, height:60, borderRadius:30,
backgroundColor:'rgba(128,189,0,0.6)',}}>
<Image style={{width:30, height:30, marginTop:15, marginLeft:15, tintColor:'rgba(255,255,255,0.7)'}} source={require('./img/edit-512.png')}/>
</TouchableOpacity>
</View>
);
}
}