index.js 1.5 KB
Newer Older
1
import React, {
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
2 3 4
  StyleSheet,
  ListView,
  Component,
5 6 7 8 9 10
} from "react-native";
import seedrandom from "seedrandom";
import Dimensions from "Dimensions";
const { width: viewportWidth } = Dimensions.get("window");
import {Surface} from "gl-react-native";
import Heart from "./Heart";
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

const sameColor = ([r,g,b], [R,G,B]) =>
  r===R && g===G && b===B;

const rowHasChanged = (r1, r2) =>
  !sameColor(r1.color, r2.color);

const increment = 3;
const seed = "gl-react is awesome";

const genRows = nb => {
  const rows = [];
  const random = seedrandom(seed);
  for (let i = 0; i < nb; i++) {
    rows.push({
      color: [ random(), random(), random() ]
    });
  }
  return rows;
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "#000",
  }
});

class Hearts extends Component {
  constructor (props) {
    super(props);
    this.state = {
      dataSource: new ListView.DataSource({
        rowHasChanged
      }).cloneWithRows(genRows(increment))
    };
  }
  more = () => {
    const { dataSource } = this.state;
    this.setState({
      dataSource: dataSource.cloneWithRows(genRows(increment + dataSource.getRowCount()))
    });
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
53
  };
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
  render () {
    return (
      <ListView
        style={styles.container}
        dataSource={this.state.dataSource}
        onEndReached={this.more}
        renderRow={({ color }) =>
          <Surface width={viewportWidth} height={viewportWidth}>
            <Heart color={color} />
          </Surface>}
      />
    );
  }
}

module.exports = Hearts;