-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollect.js
279 lines (261 loc) · 8.18 KB
/
Collect.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
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
import React, {Component} from 'react';
import {
StyleSheet,
Text,
View,
FlatList,
ActivityIndicator, TouchableOpacity, Image, ToastAndroid
} from 'react-native';
import StorageOpt from "./StorageOpt"
let pageNo = 0;//当前第几页
let cookie = "";
export default class Collect extends Component {
static navigationOptions = {
title: "收藏列表",
headerStyle: {
backgroundColor: '#549cf8',
},
headerTintColor: '#fff',
};
constructor(props) {
super(props);
this.state = {
isLoading: true,
isRefreshing: false,
//网络请求状态
error: false,
errorInfo: "",
dataArray: [],
showFoot: 0, // 控制foot, 0:隐藏footer 1:已加载完成,没有更多数据 2 :显示加载中
};
}
componentWillMount() {
StorageOpt.loaddata("cookie", (result) => {
cookie = result;
pageNo = 0;
this.fetchData(pageNo);
});
}
fetchData(pageNo) {
const reqUrl = "https://www.wanandroid.com/lg/collect/list/" + pageNo + "/json";
const request = new Request(reqUrl, {method: 'GET', headers: {'Cookie': cookie}});
fetch(request)
.then((response) => {
return response.json();
})
.then((responseData) => {
let data = responseData.data.datas;
/**
* 这里改变dataArray的值是因为防止下拉刷新数据的时候,屏幕闪烁
*/
if (this.state.isRefreshing) {
this.setState({
dataArray:[]
})
}
let foot = 0;
if (data.length === 0) {
foot = 1;
}
this.setState({
//复制数据源
dataArray: this.state.dataArray.concat(data),
showFoot: foot,
isLoading: false,
isRefreshing: false,
});
data = null;
})
.catch((error) => {
console.log(error)
});
}
render() {
if (this.state.isLoading) {
return this.renderLoadingView();
}
return (
<FlatList
data={this.state.dataArray}
renderItem={this.renderData.bind(this)}
ListFooterComponent={this._renderFooter.bind(this)}
onEndReached={this._onEndReached.bind(this)}
onRefresh={this._onRefresh.bind(this)}
refreshing={this.state.isRefreshing}
ItemSeparatorComponent={ItemDivideComponent}
onEndReachedThreshold={0.1}
keyExtractor={item => item.id} />
);
}
renderLoadingView() {
return (
<View style={styles.loading}>
<ActivityIndicator
animating={true}
color='#549cf8'
size="large"
/>
</View>
);
}
renderData({item}) {
return (
<View style={styles.container}>
<View style={styles.authorTime}>
<Text></Text>
<Text style={{margin: 5}}>{item.chapterName}</Text>
</View>
<View style={styles.authorTime}>
<Text style={{margin: 5}} onPress={this._clickItem.bind(this, item)}>{item.title}</Text>
</View>
<View style={styles.authorTime}>
<Text style={styles.author}>{item.niceDate}.{item.author}</Text>
<TouchableOpacity onPress={this.unCollectFetchData.bind(this, item)}>
<Image source={{uri: 'ic_collected'}} style={{width:25,height:25,marginRight: 5, marginBottom: 5}}/>
</TouchableOpacity>
</View>
</View>
);
}
_clickItem = (item) => {
this.props.navigation.navigate("MyWeb", {url: item.link, desc: item.title});
};
unCollectFetchData(item) {
const url = "https://www.wanandroid.com/lg/uncollect/" + item.id + "/json";
let formData = new FormData();
if (item.originId === null || item.originId === "") {
formData.append('originId', "-1");
} else {
formData.append('originId', item.originId);
}
fetch(url, {method : 'POST', headers: {'Cookie': cookie}, body: formData})
.then((response) => {
return response.json();
})
.then((responseData) => {
if (responseData.errorCode === 0) {//执行成功
ToastAndroid.show("已取消收藏", ToastAndroid.SHORT);
let data = this.removeArray(this.state.dataArray, item);
this.setState({
dataArray: data
});
data = null;
} else {
ToastAndroid.show("取消收藏失败", ToastAndroid.SHORT);
}
})
.catch((error) => {
console.log(error)
});
}
_renderFooter() {
if (this.state.showFoot === 1) {
return (
<View style={styles.noMoreData}>
<Text>没有更多数据了</Text>
</View>
);
} else if (this.state.showFoot === 2) {
return (
<View style={styles.footer}>
<ActivityIndicator animating={true}
color='#549cf8'
size="small"/>
<Text>正在加载更多数据...</Text>
</View>
);
} else if (this.state.showFoot === 0) {
return (
<View style={styles.footer}>
<Text></Text>
</View>
);
}
}
_onEndReached() {
//如果是正在加载中或没有更多数据了,则返回
if (this.state.showFoot !== 0) {
return;
}
pageNo++;
this.setState({
showFoot: 2,
});
//获取数据
this.fetchData(pageNo);
}
_onRefresh() {
this.setState({
isRefreshing:true,
});
pageNo = 0;
this.fetchData(pageNo);
}
removeArray(_arr, _obj) {
let length = _arr.length;
for (let i = 0; i < length; i++) {
if (_arr[i].id === _obj.id) {
if (i === 0) {
_arr.shift(); //删除并返回数组的第一个元素
return _arr;
}
else if (i === length - 1) {
_arr.pop(); //删除并返回数组的最后一个元素
return _arr;
}
else {
_arr.splice(i, 1); //删除下标为i的元素
return _arr;
}
break;
}
}
return _arr;
}
}
class ItemDivideComponent extends Component {
render() {
return (
<View style={{height: 1, backgroundColor: '#549cf8'}}/>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
},
loading: {
flex: 1,
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'center'
},
authorTime: {
flexDirection: 'row',
justifyContent: 'space-between',
},
author: {
margin: 5,
color: 'gray',
fontSize: 10,
},
footer: {
flexDirection: 'row',
height: 24,
justifyContent: 'center',
alignItems: 'center',
marginTop: 10,
marginBottom: 10,
},
noMoreData: {
flexDirection: 'row',
height: 30,
justifyContent: 'center',
alignItems: 'center',
marginTop: 10,
marginBottom: 10,
color: '#999999',
fontSize: 14
},
});