@@ -9,6 +9,7 @@ import {SectionList, StyleSheet} from 'react-native'
99import { ListSeparator , ListSectionHeader } from '../components/list'
1010import { ListEmpty } from '../components/list'
1111import { ContactRow } from './contact-row'
12+ import delay from 'delay'
1213import { reportNetworkProblem } from '../../lib/report-network-problem'
1314import * as defaultData from '../../../docs/contact-info.json'
1415import groupBy from 'lodash/groupBy'
@@ -36,6 +37,7 @@ type Props = TopLevelViewPropsType
3637type State = {
3738 contacts : Array < ContactType > ,
3839 loading : boolean ,
40+ refreshing : boolean ,
3941}
4042
4143export class ContactsListView extends React . PureComponent < void , Props , State > {
@@ -47,15 +49,31 @@ export class ContactsListView extends React.PureComponent<void, Props, State> {
4749 state = {
4850 contacts : defaultData . data ,
4951 loading : true ,
52+ refreshing : false ,
5053 }
5154
5255 componentWillMount ( ) {
53- this . fetchData ( )
56+ this . fetchData ( ) . then ( ( ) => {
57+ this . setState ( ( ) => ( { loading : false } ) )
58+ } )
5459 }
5560
56- fetchData = async ( ) => {
57- this . setState ( ( ) => ( { loading : true } ) )
61+ refresh = async ( ) => {
62+ const start = Date . now ( )
63+ this . setState ( ( ) => ( { refreshing : true } ) )
64+
65+ await this . fetchData ( )
66+
67+ // wait 0.5 seconds – if we let it go at normal speed, it feels broken.
68+ const elapsed = Date . now ( ) - start
69+ if ( elapsed < 500 ) {
70+ await delay ( 500 - elapsed )
71+ }
72+
73+ this . setState ( ( ) => ( { refreshing : false } ) )
74+ }
5875
76+ fetchData = async ( ) => {
5977 let { data : contacts } = await fetchJson ( GITHUB_URL ) . catch ( err => {
6078 reportNetworkProblem ( err )
6179 return defaultData
@@ -65,7 +83,7 @@ export class ContactsListView extends React.PureComponent<void, Props, State> {
6583 contacts = defaultData . data
6684 }
6785
68- this . setState ( ( ) => ( { contacts, loading : false } ) )
86+ this . setState ( ( ) => ( { contacts} ) )
6987 }
7088
7189 onPressContact = ( contact : ContactType ) = > {
@@ -96,6 +114,8 @@ export class ContactsListView extends React.PureComponent<void, Props, State> {
96114 keyExtractor = { this . keyExtractor }
97115 renderSectionHeader = { this . renderSectionHeader }
98116 renderItem = { this . renderItem }
117+ refreshing = { this . state . refreshing }
118+ onRefresh = { this . refresh }
99119 />
100120 )
101121 }
0 commit comments