SearchScreen.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import React, {Component} from 'react';
  2. import SearchTitleBar from '../views/SearchTitleBar';
  3. import CommonLoadingView from '../views/CommonLoadingView';
  4. import Global from '../utils/Global';
  5. import {Dimensions, FlatList, Image, PixelRatio, StyleSheet, Text, TouchableOpacity, View} from 'react-native';
  6. const {width} = Dimensions.get('window');
  7. const stateNormal = -1;
  8. const stateNoData = -2;
  9. export default class SearchScreen extends Component {
  10. constructor(props) {
  11. super(props);
  12. this.state = {
  13. loadingState: stateNormal,
  14. searchResult: null,
  15. listItemIndex: 0
  16. };
  17. }
  18. render() {
  19. switch (this.state.loadingState) {
  20. case stateNormal:
  21. return this.renderDefaultView();
  22. case Global.loading:
  23. return this.renderLoadingView();
  24. case Global.loadSuccess:
  25. if (this.state.searchResult == null || this.state.searchResult.length == 0) {
  26. return this.renderEmptyView();
  27. }
  28. return this.renderSearchResultView();
  29. }
  30. }
  31. renderDefaultView() {
  32. return (
  33. <View style={styles.container}>
  34. <SearchTitleBar nav={this.props.navigation} handleSearchClick={this.handleSearchClick}/>
  35. <View style={styles.content}>
  36. <Text style={styles.searchHintText}>搜索指定内容</Text>
  37. <View style={styles.row}>
  38. <View style={styles.rowItem}><Text style={styles.rowItemText}>朋友圈</Text></View>
  39. <View style={styles.rowItemDivider}/>
  40. <View style={styles.rowItem}><Text style={styles.rowItemText}>文章</Text></View>
  41. <View style={styles.rowItemDivider}/>
  42. <View style={styles.rowItem}><Text style={styles.rowItemText}>公众号</Text></View>
  43. </View>
  44. <View style={styles.row}>
  45. <View style={styles.rowItem}><Text style={styles.rowItemText}>小说</Text></View>
  46. <View style={styles.rowItemDivider}/>
  47. <View style={styles.rowItem}><Text style={styles.rowItemText}>音乐</Text></View>
  48. <View style={styles.rowItemDivider}/>
  49. <View style={styles.rowItem}><Text style={styles.rowItemText}>表情</Text></View>
  50. </View>
  51. </View>
  52. </View>
  53. );
  54. }
  55. renderSearchResultView() {
  56. return (
  57. <View style={styles.container}>
  58. <SearchTitleBar nav={this.props.navigation} handleSearchClick={this.handleSearchClick}/>
  59. <View style={styles.content}>
  60. <FlatList
  61. data={this.state.searchResult}
  62. renderItem={this.renderItem}
  63. />
  64. </View>
  65. </View>
  66. );
  67. }
  68. renderItem = (item) => {
  69. var title = item.item.name;
  70. var icon = item.item.avatar;
  71. var param = {
  72. title: title,
  73. icon: {uri: icon},
  74. };
  75. return (
  76. <View key={item.item.key}>
  77. <TouchableOpacity activeOpacity={0.5} onPress={() => {
  78. this.props.navigation.navigate('ContactDetail', {title: "详细资料", data: param})
  79. }} style={{flexDirection: 'column', flex: 1}}>
  80. <View style={listItemStyle.container}>
  81. <Image style={listItemStyle.listItemAvatar} source={{uri: item.item.avatar}}/>
  82. <Text style={listItemStyle.listItemText}>{item.item.name}</Text>
  83. </View>
  84. </TouchableOpacity>
  85. <View style={listItemStyle.divider}/>
  86. </View>
  87. );
  88. }
  89. renderEmptyView() {
  90. return (
  91. <View style={styles.container}>
  92. <SearchTitleBar nav={this.props.navigation} handleSearchClick={this.handleSearchClick}/>
  93. <View style={styles.content}>
  94. <Text style={{marginTop: 50}}>没有搜索到结果</Text>
  95. </View>
  96. </View>
  97. );
  98. }
  99. renderLoadingView() {
  100. return (
  101. <View style={styles.container}>
  102. <SearchTitleBar nav={this.props.navigation} handleSearchClick={this.handleSearchClick}/>
  103. <View style={styles.content}>
  104. <CommonLoadingView hintText={"正在搜索..."}/>
  105. </View>
  106. </View>
  107. );
  108. }
  109. renderErrorView() {
  110. return (
  111. <View><Text>Error!</Text></View>
  112. );
  113. }
  114. startSearch = (key) => {
  115. const URL = "http://app.yubo725.top/search";
  116. var data = {key: key};
  117. let params = new FormData();
  118. params.append("key", key);
  119. fetch(URL, {
  120. method: 'POST',
  121. body: params
  122. }).then((res) => res.json())
  123. .then(json => {
  124. for (var i = 0; i < json.length; i++) {
  125. var item = json[i];
  126. item.key = item.id;
  127. }
  128. this.setState({
  129. loadingState: Global.loadSuccess,
  130. searchResult: json
  131. })
  132. })
  133. }
  134. handleSearchClick = (str) => {
  135. this.setState({loadingState: Global.loading});
  136. this.startSearch(str);
  137. }
  138. }
  139. const listItemStyle = StyleSheet.create({
  140. container: {
  141. flex: 1,
  142. width: width,
  143. flexDirection: 'row',
  144. alignItems: 'center',
  145. padding: 10,
  146. },
  147. listItemAvatar: {
  148. width: 45,
  149. height: 45,
  150. },
  151. listItemText: {
  152. color: '#000000',
  153. fontSize: 15,
  154. marginLeft: 15
  155. },
  156. divider: {
  157. width: width,
  158. height: 1 / PixelRatio.get(),
  159. backgroundColor: Global.dividerColor
  160. }
  161. })
  162. const styles = StyleSheet.create({
  163. container: {
  164. flex: 1,
  165. flexDirection: 'column',
  166. },
  167. content: {
  168. flex: 1,
  169. flexDirection: 'column',
  170. alignItems: 'center',
  171. backgroundColor: '#FFFFFF',
  172. },
  173. searchHintText: {
  174. marginTop: 50,
  175. marginBottom: 50,
  176. fontSize: 16,
  177. color: '#999999'
  178. },
  179. row: {
  180. justifyContent: 'center',
  181. flexDirection: 'row',
  182. paddingTop: 15,
  183. paddingBottom: 15,
  184. paddingLeft: 20,
  185. paddingRight: 20,
  186. },
  187. rowItem: {
  188. flex: 1,
  189. flexDirection: 'row',
  190. justifyContent: 'center',
  191. },
  192. rowItemText: {
  193. color: '#49BC1C',
  194. },
  195. rowItemDivider: {
  196. width: 1 / PixelRatio.get(),
  197. height: 20,
  198. backgroundColor: '#000000'
  199. }
  200. })