var React = require('react-native')
var {
ListView,
ActivityIndicatorIOS,
StyleSheet,
View,
Text,
} = React
var RefreshableListView = React.createClass({
getInitialState() {
return {
reloading: false,
}
},
handleScroll(e) {
// must be less than 40 px due to ScrollView bug (event only fires onces)
// TODO: fix this when onScroll works properly
if (e.nativeEvent.contentOffset.y < -40) {
this.reloadData()
}
this.props.onScroll && this.props.onScroll(e)
},
reloadData() {
if (this.willReload || this.state.reloading) return
this.willReload = true
Promise.all([
this.props.loadData(),
new Promise((resolve) => this.setState({reloading: true}, resolve)),
new Promise((resolve) => setTimeout(resolve, 300)),
]).then(([data]) => {
this.willReload = false
this.setState({reloading: false})
})
},
renderHeader() {
if (this.state.reloading) {
return (
{this.props.refreshDescription}
)
} else {
return null
}
},
render() {
return (
)
}
})
var styles = StyleSheet.create({
wrapper: {
height: 60,
marginTop: 10,
},
container: {
flex: 1,
justifyContent: 'space-around',
alignItems: 'center',
backgroundColor: 'white',
},
loading: {
height: 60,
},
})
RefreshableListView.DataSource = ListView.DataSource
module.exports = RefreshableListView