daily pastebin goal
78%
SHARE
TWEET

Untitled

a guest Jan 23rd, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import R from "@res/R";
  2. import PropTypes from "prop-types";
  3. import React from "react";
  4. import { ActivityIndicator, FlatList, View } from "react-native";
  5.  
  6. export default class FlatListSupport extends React.Component {
  7.     static propTypes = {
  8.         datasource: PropTypes.any,
  9.         renderItem: PropTypes.func
  10.     };
  11.  
  12.     viewModel = new FlatListSupportViewModel();
  13.  
  14.     mCallback = new CallBackLoadMore((result, error, nextRequest) => {
  15.         this.viewModel.mNextRequest = nextRequest;
  16.         if (nextRequest) {
  17.             this.viewModel.isLoadMore = true;
  18.         } else {
  19.             this.viewModel.isLoadMore = false;
  20.         }
  21.         this.viewModel.datas.push.apply(this.viewModel.datas, result);
  22.         this.setState({});
  23.     });
  24.  
  25.     componentDidMount() {
  26.         this.props.datasource.onInitPage(this.mCallback);
  27.     }
  28.  
  29.     componentWillReceiveProps() {
  30.         this.viewModel.datas = [];
  31.         this.mNextRequest = null;
  32.         this.props.datasource.onInitPage(this.mCallback);
  33.     }
  34.  
  35.     getFootter = () => {
  36.         if (this.viewModel.isLoadMore) {
  37.             return <View>
  38.                 <ActivityIndicator />
  39.             </View>
  40.            
  41.         } else {
  42.             return <View/>
  43.  
  44.         }
  45.     }
  46.  
  47.     render() {
  48.         if (this.viewModel.datas.length == 0) {
  49.             return (
  50.                 <View style={{ flex: 1, justifyContent: "center" }}>
  51.                     <ActivityIndicator size="small" color={R.colors.primary} />
  52.                 </View>
  53.             );
  54.         } else {
  55.             return (
  56.                 <FlatList
  57.                     data={this.viewModel.datas}
  58.                     extraData={this.state}
  59.                     onEndReached={() => {
  60.                         if (this.viewModel.isLoadMore) {
  61.                             this.props.datasource.onNext(
  62.                                 this.mCallback,
  63.                                 this.mNextRequest
  64.                             );
  65.                         }
  66.                     }}
  67.                     ListFooterComponent={
  68.                         this.getFootter()
  69.                     }
  70.                     renderItem={this.props.renderItem}
  71.                 />
  72.             );
  73.         }
  74.     }
  75. }
  76.  
  77. class CallBackLoadMore {
  78.     constructor(callback) {
  79.         this.callback = callback;
  80.     }
  81. }
  82.  
  83. class FlatListSupportViewModel {
  84.     mNextRequest = null;
  85.     datas = [];
  86.     isLoadMore = false;
  87. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top