77 RefreshControl ,
88 ScrollView ,
99 StyleSheet ,
10+ Button ,
1011 Text ,
1112 View
1213} from 'react-native'
@@ -16,9 +17,9 @@ import RefreshableScrollView from './refreshableScrollView'
1617const { width, height } = Dimensions . get ( 'window' )
1718const PaginationStatus = {
1819 firstLoad : 0 ,
19- waiting : 1 ,
20+ waiting : 1 , // Free process with user's doing-nothing
2021 allLoaded : 2 ,
21- fetching : 3
22+ fetching : 3 // Waiting for fetch in `onRefresh()`
2223}
2324
2425export default class UltimateListView extends Component {
@@ -37,6 +38,7 @@ export default class UltimateListView extends Component {
3738 paginationFetchingView : null ,
3839 paginationAllLoadedView : null ,
3940 paginationWaitingView : null ,
41+ paginationLoadMoreView : null ,
4042 emptyView : null ,
4143 separator : null ,
4244
@@ -99,6 +101,7 @@ export default class UltimateListView extends Component {
99101 paginationFetchingView : PropTypes . func ,
100102 paginationAllLoadedView : PropTypes . func ,
101103 paginationWaitingView : PropTypes . func ,
104+ paginationLoadMoreView : PropTypes . func ,
102105 emptyView : PropTypes . func ,
103106 separator : PropTypes . func ,
104107
@@ -162,7 +165,7 @@ export default class UltimateListView extends Component {
162165 componentDidMount ( ) {
163166 this . mounted = true
164167 if ( this . props . firstLoader ) {
165- this . props . onFetch ( this . getPage ( ) , this . postRefresh , this . endFetch )
168+ this . props . onFetch ( 1 , this . postRefresh , this . endFetch )
166169 }
167170 }
168171
@@ -171,27 +174,38 @@ export default class UltimateListView extends Component {
171174 }
172175
173176 onRefresh = ( ) => {
174- console . log ( 'onRefresh()' )
175177 if ( this . mounted ) {
176178 this . setState ( {
177- isRefreshing : true
179+ isRefreshing : true ,
180+ paginationStatus : PaginationStatus . fetching
181+ } , ( ) => {
182+ this . props . onFetch ( 1 , this . postRefresh , this . endFetch )
178183 } )
179- this . setPage ( 1 )
180- this . props . onFetch ( this . getPage ( ) , this . postRefresh , this . endFetch )
184+ }
185+ }
186+
187+ /**
188+ * onRefreshEnd
189+ *
190+ * @description Scroll to top before `onRefresh()` ending
191+ * * */
192+ onRefreshEnd = ( ) => {
193+ if ( this . props . refreshableMode === 'advanced' && this . _flatList . _listRef . _scrollRef . onRefreshEnd ) {
194+ this . _flatList . _listRef . _scrollRef . onRefreshEnd ( )
195+ } else if ( this . _flatList . _listRef . _scrollRef . scrollTo && this . state . isRefreshing ) {
196+ this . _flatList . _listRef . _scrollRef . scrollTo ( { x : 0 , y : 0 , animated : true } )
181197 }
182198 }
183199
184200 onPaginate = async ( ) => {
185201 if ( this . state . paginationStatus !== PaginationStatus . allLoaded && ! this . state . isRefreshing ) {
186- console . log ( 'onPaginate()' )
187202 await this . setState ( { paginationStatus : PaginationStatus . fetching } )
188203 this . props . onFetch ( this . getPage ( ) + 1 , this . postPaginate , this . endFetch )
189204 }
190205 }
191206
192- onEndReached = async ( ) => {
193- // console.log('onEndReached()');
194- if ( this . props . pagination && this . props . autoPagination && this . state . paginationStatus === PaginationStatus . waiting ) {
207+ onEndReached = ( { distanceFromEnd } ) => {
208+ if ( distanceFromEnd > 0 && this . props . pagination && this . props . autoPagination && this . state . paginationStatus === PaginationStatus . waiting ) {
195209 this . onPaginate ( )
196210 }
197211 }
@@ -232,17 +246,16 @@ export default class UltimateListView extends Component {
232246 if ( rows . length < pageLimit ) {
233247 paginationStatus = PaginationStatus . allLoaded
234248 }
235- this . updateRows ( rows , paginationStatus )
249+ this . setPage ( 1 )
250+ this . updateRows ( rows . slice ( ) , paginationStatus )
236251 }
237252 }
238253
239254 endFetch = ( ) => {
240- // console.log('endRefresh()');
241255 if ( this . mounted ) {
242- this . setState ( { isRefreshing : false , paginationStatus : PaginationStatus . allLoaded } )
243- if ( this . props . refreshableMode === 'advanced' && this . _flatList . _listRef . _scrollRef . onRefreshEnd ) {
244- this . _flatList . _listRef . _scrollRef . onRefreshEnd ( )
245- }
256+ this . onRefreshEnd ( )
257+
258+ this . setState ( { isRefreshing : false , paginationStatus : PaginationStatus . allLoaded , dataSource : [ ] } )
246259 }
247260 }
248261
@@ -261,6 +274,8 @@ export default class UltimateListView extends Component {
261274 }
262275
263276 updateRows = ( rows , paginationStatus ) => {
277+ this . onRefreshEnd ( )
278+
264279 if ( rows ) {
265280 this . setRows ( rows )
266281 this . setState ( {
@@ -275,10 +290,6 @@ export default class UltimateListView extends Component {
275290 paginationStatus
276291 } )
277292 }
278-
279- if ( this . props . refreshableMode === 'advanced' ) {
280- this . endFetch ( )
281- }
282293 }
283294
284295 updateDataSource ( rows = [ ] ) {
@@ -318,11 +329,11 @@ export default class UltimateListView extends Component {
318329 return null
319330 }
320331
321- paginationWaitingView = ( paginateCallback ) => {
332+ paginationWaitingView = ( ) => {
322333 if ( this . props . pagination ) {
323- if ( this . props . autoPagination ) {
334+ if ( ! this . state . isRefreshing ) {
324335 if ( this . props . paginationWaitingView ) {
325- return this . props . paginationWaitingView ( paginateCallback )
336+ return this . props . paginationWaitingView ( )
326337 }
327338
328339 return (
@@ -340,6 +351,22 @@ export default class UltimateListView extends Component {
340351 return null
341352 }
342353
354+ paginationLoadMoreView = ( paginateCallback ) => {
355+ if ( this . props . pagination ) {
356+ if ( ! this . state . isRefreshing ) {
357+ if ( this . props . paginationLoadMoreView ) {
358+ return this . props . paginationLoadMoreView ( paginateCallback )
359+ }
360+
361+ return (
362+ < Button title = { this . props . paginationBtnText } style = { styles . paginationBtnText } onPress = { paginateCallback } />
363+ )
364+ }
365+ }
366+
367+ return null
368+ }
369+
343370 renderHeader = ( ) => {
344371 if ( this . props . header ) {
345372 return this . props . header ( )
@@ -371,17 +398,17 @@ export default class UltimateListView extends Component {
371398 renderEmptyView = ( ) => {
372399 if ( this . state . paginationStatus !== PaginationStatus . firstLoad && this . props . emptyView ) {
373400 return this . props . emptyView ( )
401+ } else if ( this . state . paginationStatus === PaginationStatus . firstLoad ) {
402+ return this . paginationFetchingView ( )
374403 }
375404
376405 return null
377406 }
378407
379408 renderFooter = ( ) => {
380- if ( this . state . paginationStatus === PaginationStatus . firstLoad ) {
381- return this . paginationFetchingView ( )
382- } else if ( this . state . paginationStatus === PaginationStatus . fetching && this . props . autoPagination === false ) {
383- return this . paginationWaitingView ( this . onPaginate )
384- } else if ( this . state . paginationStatus === PaginationStatus . fetching && this . props . autoPagination === true ) {
409+ if ( this . state . paginationStatus === PaginationStatus . waiting && this . props . autoPagination === false ) {
410+ return this . paginationLoadMoreView ( this . onPaginate )
411+ } else if ( this . state . paginationStatus === PaginationStatus . fetching ) {
385412 return this . paginationWaitingView ( )
386413 } else if ( this . getRows ( ) . length !== 0 && this . state . paginationStatus === PaginationStatus . allLoaded ) {
387414 return this . paginationAllLoadedView ( )
@@ -441,6 +468,7 @@ export default class UltimateListView extends Component {
441468 onEndReachedThreshold = { 0.1 }
442469 { ...this . props }
443470 ref = { ref => this . _flatList = ref }
471+ paginationStatus = { this . state . paginationStatus } // force update pure-component
444472 data = { this . state . dataSource }
445473 renderItem = { this . renderItem }
446474 ItemSeparatorComponent = { this . renderSeparator }
0 commit comments