mirror of
https://github.com/timepill/timepill-app.git
synced 2025-04-30 09:59:31 +08:00
1. 去掉customedList组件,避免增加代码复杂度
This commit is contained in:
parent
762c46eead
commit
beb5c0ec21
2 changed files with 90 additions and 119 deletions
|
@ -1,97 +0,0 @@
|
|||
import React, {Component} from 'react';
|
||||
import {StyleSheet, Text, View, InteractionManager, FlatList} from 'react-native';
|
||||
|
||||
|
||||
export default class CustomedList extends Component {
|
||||
|
||||
constructor(props) {
|
||||
super(props);
|
||||
|
||||
this.listType = props.listType || 'undefined';
|
||||
this.dataSource = props.dataSource;
|
||||
|
||||
this.state = {
|
||||
listData: [],
|
||||
hasMore: false,
|
||||
|
||||
refreshing: false,
|
||||
refreshFailed: false
|
||||
};
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
InteractionManager.runAfterInteractions(() => {
|
||||
this.refresh();
|
||||
});
|
||||
}
|
||||
|
||||
refresh(loadMore = false) {
|
||||
if (this.state.refreshing) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.setState({hasMore: false, refreshing: true, refreshFailed: false});
|
||||
this.dataSource.refresh(loadMore)
|
||||
.then(result => {
|
||||
if(!result) {
|
||||
throw {
|
||||
message: 'refresh ' + this.listType + ' no result'
|
||||
}
|
||||
|
||||
} else {
|
||||
console.log('refresh ' + this.listType + ' result:', result);
|
||||
|
||||
this.setState({
|
||||
listData: result.list ? result.list : [],
|
||||
hasMore: result.more,
|
||||
refreshFailed: false
|
||||
});
|
||||
}
|
||||
|
||||
}).catch(e => {
|
||||
this.setState({
|
||||
listData: [],
|
||||
hasMore: false,
|
||||
refreshFailed: true
|
||||
});
|
||||
|
||||
}).done(() => {
|
||||
this.setState({
|
||||
refreshing: false
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
loadMore() {
|
||||
if (this.state.refreshing) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.refresh(true);
|
||||
}
|
||||
|
||||
render() {
|
||||
return (
|
||||
<FlatList
|
||||
|
||||
data={this.state.listData}
|
||||
|
||||
keyExtractor={(item, index) => {
|
||||
return item.id ? item.id.toString() : index;
|
||||
}}
|
||||
|
||||
renderItem={this.props.renderItem}
|
||||
|
||||
refreshing={this.state.refreshing}
|
||||
onRefresh={this.refresh.bind(this)}
|
||||
|
||||
onEndReachedThreshold={5}
|
||||
onEndReached={this.state.hasMore ? this.loadMore.bind(this) : null}
|
||||
/>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const localStyle = StyleSheet.create({
|
||||
|
||||
});
|
|
@ -6,7 +6,6 @@ import Touchable from '../touchable';
|
|||
import Color from '../../style/color';
|
||||
|
||||
import UserIcon from '../userIcon';
|
||||
import CustomedList from '../customedList';
|
||||
|
||||
|
||||
export default class FollowUserList extends Component {
|
||||
|
@ -15,15 +14,78 @@ export default class FollowUserList extends Component {
|
|||
super(props);
|
||||
|
||||
this.dataSource = props.dataSource;
|
||||
this.listType = props.listType;
|
||||
this.listType = props.listType || 'undefined';
|
||||
|
||||
this.state = {
|
||||
users: [],
|
||||
hasMore: false,
|
||||
|
||||
refreshing: false,
|
||||
refreshFailed: false
|
||||
};
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
InteractionManager.runAfterInteractions(() => {
|
||||
this.refresh();
|
||||
});
|
||||
}
|
||||
|
||||
refresh(loadMore = false) {
|
||||
if (this.state.refreshing) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.setState({hasMore: false, refreshing: true, refreshFailed: false});
|
||||
this.dataSource.refresh(loadMore)
|
||||
.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({
|
||||
users: [],
|
||||
hasMore: false,
|
||||
refreshFailed: true
|
||||
});
|
||||
|
||||
}).done(() => {
|
||||
this.setState({
|
||||
refreshing: false
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
loadMore() {
|
||||
if (this.state.refreshing) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.refresh(true);
|
||||
}
|
||||
|
||||
render() {
|
||||
return (
|
||||
<View style={localStyle.container}>
|
||||
<CustomedList listType={this.props.listType} style={localStyle.list}
|
||||
<FlatList style={localStyle.list}
|
||||
|
||||
dataSource={this.props.dataSource}
|
||||
data={this.state.users}
|
||||
|
||||
keyExtractor={(item, index) => {
|
||||
return item.id ? item.id.toString() : index;
|
||||
}}
|
||||
|
||||
renderItem={({item}) => {
|
||||
return (
|
||||
|
@ -40,6 +102,12 @@ export default class FollowUserList extends Component {
|
|||
</Touchable>
|
||||
);
|
||||
}}
|
||||
|
||||
refreshing={this.state.refreshing}
|
||||
onRefresh={this.refresh.bind(this)}
|
||||
|
||||
onEndReachedThreshold={5}
|
||||
onEndReached={this.state.hasMore ? this.loadMore.bind(this) : null}
|
||||
/>
|
||||
</View>
|
||||
);
|
||||
|
|
Loading…
Reference in a new issue