ContactsScreen.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. import React, {Component} from 'react';
  2. import TitleBar from '../views/TitleBar';
  3. import SideBar from '../views/SideBar';
  4. import CommonLoadingView from '../views/CommonLoadingView';
  5. import Global from '../utils/Global';
  6. import Utils from '../utils/Utils';
  7. import UserInfoUtil from '../utils/UserInfoUtil';
  8. import Toast from '@remobile/react-native-toast';
  9. import {
  10. Dimensions,
  11. FlatList,
  12. Image,
  13. PixelRatio,
  14. StyleSheet,
  15. Text,
  16. TouchableHighlight,
  17. View
  18. } from 'react-native';
  19. const {width} = Dimensions.get('window');
  20. export default class ContactsScreen extends Component {
  21. static navigationOptions = {
  22. tabBarLabel: '联系人',
  23. tabBarIcon: ({focused, tintColor}) => {
  24. if (focused) {
  25. return (
  26. <Image style={styles.tabBarIcon} source={require('../../images/ic_contacts_selected.png')}/>
  27. );
  28. }
  29. return (
  30. <Image style={styles.tabBarIcon} source={require('../../images/ic_contacts_normal.png')}/>
  31. );
  32. },
  33. };
  34. constructor(props) {
  35. super(props);
  36. this.state = {
  37. loadingState: Global.loading,
  38. contactData: null,
  39. }
  40. }
  41. getContacts() {
  42. var url = "http://app.yubo725.top/friends"; // 新接口
  43. fetch(url).then((res) => res.json())
  44. .then((json) => {
  45. UserInfoUtil.setUserInfo(json);
  46. this.setState({
  47. loadingState: Global.loadSuccess,
  48. contactData: json
  49. })
  50. })
  51. }
  52. render() {
  53. switch (this.state.loadingState) {
  54. case Global.loading:
  55. this.getContacts();
  56. return this.renderLoadingView();
  57. case Global.loadSuccess:
  58. return this.renderSuccessView();
  59. case Global.loadError:
  60. return this.renderErrorView();
  61. default:
  62. }
  63. }
  64. renderLoadingView() {
  65. return (
  66. <View style={styles.container}>
  67. <TitleBar nav={this.props.navigation}/>
  68. <View style={styles.content}>
  69. <CommonLoadingView hintText={"正在获取联系人数据..."}/>
  70. </View>
  71. </View>
  72. );
  73. }
  74. renderErrorView() {
  75. return (
  76. <View style={{justifyContent: 'center', alignItems: 'center', flex: 1, flexDirection: 'column'}}>
  77. <Text style={{fontSize: 16, color: '#000000'}}>加载数据出错!</Text>
  78. </View>
  79. );
  80. }
  81. renderSuccessView() {
  82. var listData = [];
  83. var headerListData = [];
  84. var headerImages = [require('../../images/ic_new_friends.png'), require('../../images/ic_group_chat.png'),
  85. require('../../images/ic_tag.png'), require('../../images/ic_common.png')];
  86. var headerTitles = ['新的朋友', '群聊', '标签', '公众号'];
  87. var index = 0;
  88. for (var i = 0; i < headerTitles.length; i++) {
  89. headerListData.push({
  90. key: index++,
  91. title: headerTitles[i],
  92. nick: '',
  93. icon: headerImages[i],
  94. sectionStart: false,
  95. });
  96. }
  97. var contacts = this.state.contactData;
  98. for (var i = 0; i < contacts.length; i++) {
  99. // var pinyin = PinyinUtil.getFullChars(contacts[i].name);
  100. var pinyin = contacts[i].pinyin.toUpperCase();
  101. var firstLetter = pinyin.substring(0, 1);
  102. if (firstLetter < 'A' || firstLetter > 'Z') {
  103. firstLetter = '#';
  104. }
  105. let icon = require('../../images/avatar.png');
  106. if (!Utils.isEmpty(contacts[i].avatar)) {
  107. icon = {uri: contacts[i].avatar};
  108. }
  109. listData.push({
  110. key: index++,
  111. icon: icon,
  112. title: contacts[i].name,
  113. nick: contacts[i].nick,
  114. pinyin: pinyin,
  115. firstLetter: firstLetter,
  116. sectionStart: false,
  117. })
  118. }
  119. // 按拼音排序
  120. listData.sort(function (a, b) {
  121. if (a.pinyin === undefined || b.pinyin === undefined) {
  122. return 1;
  123. }
  124. if (a.pinyin > b.pinyin) {
  125. return 1;
  126. }
  127. if (a.pinyin < b.pinyin) {
  128. return -1;
  129. }
  130. return 0;
  131. });
  132. listData = headerListData.concat(listData);
  133. // 根据首字母分区
  134. for (var i = 0; i < listData.length; i++) {
  135. var obj = listData[i];
  136. if (obj.pinyin === undefined) {
  137. continue;
  138. }
  139. if (i > 0 && i < listData.length) {
  140. var preObj = listData[i - 1];
  141. if (preObj.pinyin === undefined && obj.pinyin !== undefined) {
  142. obj.sectionStart = true;
  143. } else if (preObj.pinyin !== undefined && obj.pinyin !== undefined && preObj.firstLetter !== obj.firstLetter) {
  144. obj.sectionStart = true;
  145. }
  146. }
  147. }
  148. this.listData = listData;
  149. return (
  150. <View style={styles.container}>
  151. <TitleBar nav={this.props.navigation}/>
  152. <View style={styles.divider}></View>
  153. <View style={styles.content}>
  154. <FlatList
  155. ref={'list'}
  156. data={listData}
  157. renderItem={this._renderItem}
  158. getItemLayout={this._getItemLayout}
  159. />
  160. <SideBar onLetterSelectedListener={this.onSideBarSelected.bind(this)}/>
  161. </View>
  162. <View style={styles.divider}></View>
  163. </View>
  164. );
  165. }
  166. _getItemLayout = (data, index) => {
  167. let len = data.sectionStart ? (58) : (51);
  168. let dividerHeight = 1 / PixelRatio.get();
  169. return {
  170. length: len,
  171. offset: (len + dividerHeight) * index,
  172. index
  173. };
  174. }
  175. onSideBarSelected(letter) {
  176. if (this.listData) {
  177. for (let i = 0; i < this.listData.length; i++) {
  178. let item = this.listData[i];
  179. if (item.firstLetter == letter && item.sectionStart) {
  180. Toast.showShortCenter(letter);
  181. this.refs.list.scrollToIndex({viewPosition: 0, index: i});
  182. break;
  183. }
  184. }
  185. }
  186. }
  187. onListItemClick(item) {
  188. let index = item.item.key;
  189. if (index == 0) {
  190. // 新的朋友
  191. this.props.navigation.navigate('NewFriend', {title: '新的朋友', data: item.item})
  192. } else if (index >= 1 && index <= 3) {
  193. Toast.showShortCenter('功能未实现');
  194. } else {
  195. this.props.navigation.navigate('ContactDetail', {title: '详细资料', data: item.item});
  196. }
  197. }
  198. _renderItem = (item) => {
  199. var section = [];
  200. if (item.item.sectionStart) {
  201. section.push(<Text key={"section" + item.item.key}
  202. style={listItemStyle.sectionView}>{item.item.firstLetter}</Text>);
  203. }
  204. return (
  205. <View>
  206. {section}
  207. <TouchableHighlight underlayColor={Global.touchableHighlightColor} onPress={() => {
  208. this.onListItemClick(item)
  209. }}>
  210. <View style={listItemStyle.container} key={item.item.key}>
  211. <Image style={listItemStyle.image} source={item.item.icon}/>
  212. <Text style={listItemStyle.itemText}>{item.item.title}</Text>
  213. <Text style={listItemStyle.subText}>{Utils.isEmpty(item.item.nick) ? "" : "(" + item.item.nick + ")"}</Text>
  214. </View>
  215. </TouchableHighlight>
  216. <View style={{width: width, height: 1 / PixelRatio.get(), backgroundColor: Global.dividerColor}}/>
  217. </View>
  218. );
  219. }
  220. }
  221. const listItemStyle = StyleSheet.create({
  222. container: {
  223. width: width,
  224. flexDirection: 'row',
  225. alignItems: 'center',
  226. backgroundColor: '#FFFFFF'
  227. },
  228. image: {
  229. marginLeft: 15,
  230. marginRight: 15,
  231. marginTop: 8,
  232. marginBottom: 8,
  233. width: 35,
  234. height: 35,
  235. },
  236. itemText: {
  237. fontSize: 15,
  238. color: '#000000'
  239. },
  240. subText: {
  241. fontSize: 15,
  242. color: '#999999'
  243. },
  244. sectionView: {
  245. width: width,
  246. backgroundColor: 'rgba(0, 0, 0, 0)',
  247. paddingLeft: 10,
  248. paddingRight: 10,
  249. paddingTop: 2,
  250. paddingBottom: 2,
  251. color: '#999999'
  252. }
  253. });
  254. const styles = StyleSheet.create({
  255. container: {
  256. flex: 1,
  257. flexDirection: 'column',
  258. justifyContent: 'center',
  259. alignItems: 'center'
  260. },
  261. divider: {
  262. width: width,
  263. height: 1 / PixelRatio.get(),
  264. backgroundColor: '#D3D3D3'
  265. },
  266. content: {
  267. flex: 1,
  268. width: width,
  269. flexDirection: 'row',
  270. backgroundColor: Global.pageBackgroundColor
  271. },
  272. tabBarIcon: {
  273. width: 24,
  274. height: 24,
  275. },
  276. });