-
Notifications
You must be signed in to change notification settings - Fork 582
/
SearchPage.js
344 lines (320 loc) · 10.5 KB
/
SearchPage.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/**
* SearchPage
* @flow
* **/
'use strict';
import React, {Component} from "react";
import {
StyleSheet,
View,
Image,
Platform,
TextInput,
TouchableOpacity,
ActivityIndicator,
Text,
TouchableHighlight,
ListView,
} from "react-native";
import Toast, {DURATION} from 'react-native-easy-toast'
import LanguageDao, {FLAG_LANGUAGE} from '../expand/dao/LanguageDao'
import RepositoryCell from '../common/RepositoryCell'
import BaseCommon from '../common/BaseCommon'
import RepositoryDetail from './RepositoryDetail'
import {FLAG_TAB} from './HomePage'
import FavoriteDao from '../expand/dao/FavoriteDao'
import {FLAG_STORAGE} from '../expand/dao/DataRepository'
import Utils from '../util/Utils'
import ViewUtils from '../util/ViewUtils'
import GlobalStyles from '../../res/styles/GlobalStyles'
import makeCancelable from '../util/Cancelable'
import ProjectModel from '../model/ProjectModel'
const API_URL = 'https://api.github.com/search/repositories?q='
const QUERY_STR = '&sort=stars'
export default class SearchPage extends Component {
constructor(props) {
super(props);
this.baseCommon=new BaseCommon({...props,backPress:(e)=>this.onBackPress(e)});
this.languageDao = new LanguageDao(FLAG_LANGUAGE.flag_key);
this.favoriteDao = new FavoriteDao(FLAG_STORAGE.flag_popular);
this.favoritKeys = [];
this.state = {
inputKey: '',
isLoading: false,
showBottomButton: false,
rightButtonText: 'Go',
dataSource: new ListView.DataSource({
rowHasChanged: (row1, row2)=>row1 !== row2,
}),
};
}
componentDidMount() {
this.baseCommon.componentDidMount();
this.initKeys();
}
componentWillUnmount() {
this.baseCommon.componentWillUnmount();
if (this.isKeyChange)this.props.homeComponent.onReStart(FLAG_TAB.flag_popularTab);
}
onBackPress(e){
this.refs.input.blur();
this.props.navigator.pop();
return true;
}
async initKeys() {
this.keys = await this.languageDao.fetch();
}
onSelectRepository(projectModel) {
var item = projectModel.item;
this.props.navigator.push({
title: item.full_name,
component: RepositoryDetail,
params: {
projectModel: projectModel,
parentComponent: this,
flag: FLAG_STORAGE.flag_popular,
...this.props
},
});
}
onFavorite(item, isFavorite) {//favoriteIcon单击回调函数
if (isFavorite) {
this.favoriteDao.saveFavoriteItem(item.id.toString(), JSON.stringify(item));
} else {
this.favoriteDao.removeFavoriteItem(item.id.toString());
}
}
saveKey() {
let key = this.state.inputKey;
if (this.checkKeyIsExist(this.keys, key)) {
this.refs.toast.show(this.state.inputKey + ' already exists.', DURATION.LENGTH_SHORT)
} else {
key = {
"path": key,
"name": key,
"checked": true
};
this.keys.unshift(key);
this.languageDao.save(this.keys)
this.refs.toast.show(this.state.inputKey + ' saved successfully.', DURATION.LENGTH_SHORT);
this.isKeyChange = true;
}
}
checkKeyIsExist(keys, key) {
for (let i = 0, l = keys.length; i < l; i++) {
if (key.toLowerCase() === keys[i].name.toLowerCase())return true;
}
return false;
}
onRightButtonClick() {
if (this.state.rightButtonText === 'Go') {
this.updateState({rightButtonText: 'Cancel'});
this.loadData();
} else {
this.updateState({
rightButtonText: 'Go',
isLoading: false,
});
this.cancelable.cancel();
}
}
loadData() {
this.updateState({
isLoading: true,
showBottomButton: false,
});
this.cancelable = makeCancelable(fetch(this.genFetchUrl(this.state.inputKey)));
this.cancelable.promise
.then((response)=>response.json())
.then((responseData)=> {
if (!this || !responseData || !responseData.items || responseData.items.length === 0) {
this.refs.toast.show(this.state.inputKey + ' nothing found.', DURATION.LENGTH_SHORT);
this.updateState({isLoading: false, rightButtonText: 'Go',});
return;
}
this.items = responseData.items;
this.getFavoriteKeys();
if (!this.checkKeyIsExist(this.keys, this.state.inputKey)) {
this.updateState({showBottomButton: true,})
}
})
.catch((error)=> {
this.updateState({
isLoading: false,
rightButtonText: 'Go',
});
});
}
updateFavorite() {
this.getFavoriteKeys();
}
getFavoriteKeys() {//获取本地用户收藏的ProjectItem
this.favoriteDao.getFavoriteKeys().then((keys)=> {
this.favoritKeys = keys || [];
this.flushFavoriteState();
}).catch((error)=> {
this.flushFavoriteState();
console.log(error);
});
}
flushFavoriteState() {//更新ProjectItem的Favorite状态
let projectModels = [];
let items = this.items;
for (var i = 0, len = items.length; i < len; i++) {
projectModels.push(new ProjectModel(items[i], Utils.checkFavorite(items[i], this.favoritKeys)));
}
this.updateState({
isLoading: false,
dataSource: this.getDataSource(projectModels),
rightButtonText: 'Go',
});
}
updateState(dic) {
if (!this)return;
this.setState(dic);
}
genFetchUrl(category) {
return API_URL + category + QUERY_STR;
}
getDataSource(items) {
return this.state.dataSource.cloneWithRows(items);
}
renderRow(projectModel, sectionID, rowID) {
return (
<RepositoryCell
key={projectModel.item.id}
onSelect={()=>this.onSelectRepository(projectModel)}
theme={this.props.theme}
projectModel={projectModel}
onFavorite={(item, isFavorite)=>this.onFavorite(item, isFavorite)}/>
);
}
renderNavBar() {
let backButton =ViewUtils.getLeftButton(()=>this.onBackPress());
let inputView =
<TextInput
ref="input"
style={styles.textInput}
autoFocus={true}
underlineColorAndroid="white"
placeholder="Search repos"
placeholderTextColor="white"
clearTextOnFocus={true}
clearButtonMode="while-editing"
onChangeText={(inputKey) => this.setState({inputKey})}
></TextInput>;
let rightButton =
<TouchableOpacity
onPress={()=> {
this.refs.input.blur();
this.onRightButtonClick();
}}
>
<View style={{alignItems: 'center', marginRight: 10}}>
<Text style={styles.title}>{this.state.rightButtonText}</Text>
</View>
</TouchableOpacity>;
return (
<View style={{
backgroundColor: this.props.theme.themeColor,
height: Platform.OS==='ios'? GlobalStyles.nav_bar_height_ios:GlobalStyles.nav_bar_height_android,
flexDirection: 'row',
alignItems: 'center'
}}>
{backButton}
{inputView}
{rightButton}
</View>
)
}
render() {
let statusBar = null;
if (Platform.OS === 'ios') {
statusBar =
<View style={[styles.statusBar, {backgroundColor: this.props.theme.themeColor}]}/>;
}
let indicatorView = this.state.isLoading ?
<ActivityIndicator
animating={this.state.isLoading}
style={[styles.centering,]}
size="large"
/> : null;
let listView = !this.state.isLoading ?
<ListView
ref="listView"
style={styles.listView}
renderRow={(e)=>this.renderRow(e)}
enableEmptySections={true}
dataSource={this.state.dataSource}
/> : null;
let resultView =
<View style={{flex: 1}}>
{indicatorView}
{listView}
</View>
let bottomButton = this.state.showBottomButton ?
<TouchableOpacity
underlayColor="transparent"
style={[styles.bottomButton, {backgroundColor: this.props.theme.themeColor}]}
onPress={()=> {
this.saveKey();
}}
>
<View style={{justifyContent: 'center',}}>
<Text style={styles.title}>Add Key {this.state.inputKey} </Text>
</View>
</TouchableOpacity>
: null;
return (
<View style={[GlobalStyles.listView_container,]}>
{statusBar}
{this.renderNavBar()}
{resultView}
{bottomButton}
<Toast ref="toast" position='bottom'/>
</View>
)
}
}
const styles = StyleSheet.create({
statusBar: {
height: 20,
},
title: {
fontSize: 18,
color: '#FFFFFF',
fontWeight: '500',
},
textInput: {
flex: 1,
height: (Platform.OS === 'ios') ?30:40,
borderWidth: 1,
borderColor: 'white',
alignSelf: 'center',
paddingLeft: 5,
marginLeft: 5,
marginRight: 10,
borderRadius: 3,
opacity: 0.7,
color:'white'
},
centering: {
alignItems: 'center',
justifyContent: 'center',
padding: 8,
flex: 1,
},
bottomButton: {
alignItems: 'center',
justifyContent: 'center',
opacity: 0.9,
height: 40,
position: 'absolute',
left: 10,
top: GlobalStyles.window_height-45-(Platform.OS === 'ios' ? 0:25),
right: 10,
bottom: 0,
alignSelf: 'flex-end',
borderRadius: 3,
},
})