import React, {Component} from 'react'; import {StyleSheet, Text, View, InteractionManager, FlatList, Alert} from 'react-native'; import {Navigation} from 'react-native-navigation'; import Ionicons from 'react-native-vector-icons/Ionicons'; import Touchable from '../touchable'; import Color from '../../style/color'; import UserIcon from '../userIcon'; import { ListFooterLoading, ListFooterEnd, ListFooterFailed } from '../listFooter'; export default class FollowUserList extends Component { constructor(props) { super(props); this.dataSource = props.dataSource; this.listType = props.listType || 'undefined'; this.state = { users: [], refreshing: false, refreshFailed: false, hasMore: true, loadingMore: false, loadFailed: false }; } componentDidMount() { InteractionManager.runAfterInteractions(() => { this.refresh(); }); } _onItemPress(user) { Navigation.push(this.props.componentId, { component: { name: 'User', options: { bottomTabs: { visible: false, // hide bottom tab for android drawBehind: true, animate: true } }, passProps: { user: user } } }); } _onDeletePress(user) { Alert.alert('提示', '确定删除关注?', [ {text: '删除', style: 'destructive', onPress: () => { this.props.onDeletePress(user.id) .done(() => { let filterUsers = this.state.users.filter((it) => it.id !== user.id); this.setState({ users: filterUsers }); }); }}, {text: '取消', onPress: () => {}} ]); } refresh() { if (this.state.refreshing) { return; } this.setState({refreshing: true, refreshFailed: false}); this.dataSource.refresh() .then(result => { if(!result) { throw { message: 'refresh ' + this.listType + ' no result' } } else { console.log('refresh ' + this.listType + ' result:', result); this.setState({ users: result.list ? result.list : [], hasMore: result.more, refreshFailed: false }); } }).catch(e => { this.setState({ refreshFailed: true }); }).done(() => { this.setState({ refreshing: false }); }); } loadMore() { if (this.state.loadingMore) { return; } this.setState({loadingMore: true, loadFailed: false}); this.dataSource.refresh(true) .then(result => { if(!result) { throw { message: 'refresh ' + this.listType + ' no result' } } else { console.log('refresh ' + this.listType + ' result:', result); this.setState({ users: result.list ? result.list : [], hasMore: result.more, loadFailed: false }); } }).catch(e => { this.setState({ hasMore: false, loadFailed: true }); }).done(() => { this.setState({ loadingMore: false }); }); } render() { return ( { return item.id ? item.id.toString() : index; }} renderItem={({item}) => { return ( this._onItemPress(item)}> this._onItemPress(item)}> {item.name} this._onDeletePress(item)}> ); }} ListFooterComponent={() => { if (this.state.refreshing || this.state.loadingMore || this.state.users.length == 0) { return null; } if (this.state.loadFailed) { return ; } if (!this.state.hasMore) { return ; } return ; }} refreshing={this.state.refreshing} onRefresh={this.refresh.bind(this)} onEndReachedThreshold={2} onEndReached={this.state.hasMore ? this.loadMore.bind(this) : null} /> ); } } const localStyle = StyleSheet.create({ container: { flex: 1 }, list: { height: '100%' }, box: { flexDirection: 'row', borderBottomWidth: 1, borderColor: Color.line, alignItems: 'center', backgroundColor: 'white', paddingLeft: 25 }, userName: { flex: 1, color: Color.text, fontSize: 16 }, removeIcon: { padding: 20 } });