7
7
RefreshControl ,
8
8
ScrollView ,
9
9
StyleSheet ,
10
+ Button ,
10
11
Text ,
11
12
View
12
13
} from 'react-native'
@@ -16,9 +17,9 @@ import RefreshableScrollView from './refreshableScrollView'
16
17
const { width, height } = Dimensions . get ( 'window' )
17
18
const PaginationStatus = {
18
19
firstLoad : 0 ,
19
- waiting : 1 ,
20
+ waiting : 1 , // Free process with user's doing-nothing
20
21
allLoaded : 2 ,
21
- fetching : 3
22
+ fetching : 3 // Waiting for fetch in `onRefresh()`
22
23
}
23
24
24
25
export default class UltimateListView extends Component {
@@ -37,6 +38,7 @@ export default class UltimateListView extends Component {
37
38
paginationFetchingView : null ,
38
39
paginationAllLoadedView : null ,
39
40
paginationWaitingView : null ,
41
+ paginationLoadMoreView : null ,
40
42
emptyView : null ,
41
43
separator : null ,
42
44
@@ -99,6 +101,7 @@ export default class UltimateListView extends Component {
99
101
paginationFetchingView : PropTypes . func ,
100
102
paginationAllLoadedView : PropTypes . func ,
101
103
paginationWaitingView : PropTypes . func ,
104
+ paginationLoadMoreView : PropTypes . func ,
102
105
emptyView : PropTypes . func ,
103
106
separator : PropTypes . func ,
104
107
@@ -162,7 +165,7 @@ export default class UltimateListView extends Component {
162
165
componentDidMount ( ) {
163
166
this . mounted = true
164
167
if ( this . props . firstLoader ) {
165
- this . props . onFetch ( this . getPage ( ) , this . postRefresh , this . endFetch )
168
+ this . props . onFetch ( 1 , this . postRefresh , this . endFetch )
166
169
}
167
170
}
168
171
@@ -171,27 +174,38 @@ export default class UltimateListView extends Component {
171
174
}
172
175
173
176
onRefresh = ( ) => {
174
- console . log ( 'onRefresh()' )
175
177
if ( this . mounted ) {
176
178
this . setState ( {
177
- isRefreshing : true
179
+ isRefreshing : true ,
180
+ paginationStatus : PaginationStatus . fetching
181
+ } , ( ) => {
182
+ this . props . onFetch ( 1 , this . postRefresh , this . endFetch )
178
183
} )
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 } )
181
197
}
182
198
}
183
199
184
200
onPaginate = async ( ) => {
185
201
if ( this . state . paginationStatus !== PaginationStatus . allLoaded && ! this . state . isRefreshing ) {
186
- console . log ( 'onPaginate()' )
187
202
await this . setState ( { paginationStatus : PaginationStatus . fetching } )
188
203
this . props . onFetch ( this . getPage ( ) + 1 , this . postPaginate , this . endFetch )
189
204
}
190
205
}
191
206
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 ) {
195
209
this . onPaginate ( )
196
210
}
197
211
}
@@ -232,17 +246,16 @@ export default class UltimateListView extends Component {
232
246
if ( rows . length < pageLimit ) {
233
247
paginationStatus = PaginationStatus . allLoaded
234
248
}
235
- this . updateRows ( rows , paginationStatus )
249
+ this . setPage ( 1 )
250
+ this . updateRows ( rows . slice ( ) , paginationStatus )
236
251
}
237
252
}
238
253
239
254
endFetch = ( ) => {
240
- // console.log('endRefresh()');
241
255
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 : [ ] } )
246
259
}
247
260
}
248
261
@@ -261,6 +274,8 @@ export default class UltimateListView extends Component {
261
274
}
262
275
263
276
updateRows = ( rows , paginationStatus ) => {
277
+ this . onRefreshEnd ( )
278
+
264
279
if ( rows ) {
265
280
this . setRows ( rows )
266
281
this . setState ( {
@@ -275,10 +290,6 @@ export default class UltimateListView extends Component {
275
290
paginationStatus
276
291
} )
277
292
}
278
-
279
- if ( this . props . refreshableMode === 'advanced' ) {
280
- this . endFetch ( )
281
- }
282
293
}
283
294
284
295
updateDataSource ( rows = [ ] ) {
@@ -318,11 +329,11 @@ export default class UltimateListView extends Component {
318
329
return null
319
330
}
320
331
321
- paginationWaitingView = ( paginateCallback ) => {
332
+ paginationWaitingView = ( ) => {
322
333
if ( this . props . pagination ) {
323
- if ( this . props . autoPagination ) {
334
+ if ( ! this . state . isRefreshing ) {
324
335
if ( this . props . paginationWaitingView ) {
325
- return this . props . paginationWaitingView ( paginateCallback )
336
+ return this . props . paginationWaitingView ( )
326
337
}
327
338
328
339
return (
@@ -340,6 +351,22 @@ export default class UltimateListView extends Component {
340
351
return null
341
352
}
342
353
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
+
343
370
renderHeader = ( ) => {
344
371
if ( this . props . header ) {
345
372
return this . props . header ( )
@@ -371,17 +398,17 @@ export default class UltimateListView extends Component {
371
398
renderEmptyView = ( ) => {
372
399
if ( this . state . paginationStatus !== PaginationStatus . firstLoad && this . props . emptyView ) {
373
400
return this . props . emptyView ( )
401
+ } else if ( this . state . paginationStatus === PaginationStatus . firstLoad ) {
402
+ return this . paginationFetchingView ( )
374
403
}
375
404
376
405
return null
377
406
}
378
407
379
408
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 ) {
385
412
return this . paginationWaitingView ( )
386
413
} else if ( this . getRows ( ) . length !== 0 && this . state . paginationStatus === PaginationStatus . allLoaded ) {
387
414
return this . paginationAllLoadedView ( )
@@ -441,6 +468,7 @@ export default class UltimateListView extends Component {
441
468
onEndReachedThreshold = { 0.1 }
442
469
{ ...this . props }
443
470
ref = { ref => this . _flatList = ref }
471
+ paginationStatus = { this . state . paginationStatus } // force update pure-component
444
472
data = { this . state . dataSource }
445
473
renderItem = { this . renderItem }
446
474
ItemSeparatorComponent = { this . renderSeparator }
0 commit comments