-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInfiniteCat.js
106 lines (94 loc) · 2.6 KB
/
InfiniteCat.js
1
2
3
4
5
6
7
8
9
10
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
* These cats do not exist
*
* http://thesecatsdonotexist.com
*/
import React from 'react';
import {
Dimensions,
SafeAreaView,
StyleSheet,
Text,
FlatList,
} from 'react-native';
import { systemWeights } from 'react-native-typography'
import FastImage from 'react-native-fast-image';
import Card from './Card';
export default function infiniteCat() {
const windowWidth = Dimensions.get('window').width;
const styles = StyleSheet.create({
titleText: {
marginTop: 64,
textAlign: "center",
fontSize: 50,
...systemWeights.thin
},
contentText: {
fontSize: 25,
textAlign: "center",
...systemWeights.regular
},
scrollView: {
alignSelf: "center",
}
});
let randomNumGen = (toInclusive) => {
return Math.round(Math.random() * toInclusive)
}
let createCatLocation = () => {
let result = 'https://d2ph5fj80uercy.cloudfront.net/0' + (randomNumGen(2) + 4) + '/cat' + randomNumGen(5000) + '.jpg'
console.log('Found cat at ' + result)
console.log('Attempting preload...')
try {
FastImage.preload([
{
uri: result,
headers: {}
}
])
} catch (error) {
// ignore
}
return result
}
let findMoreCats = () => {
cats.push(
{
val: createCatLocation(),
id: (counter++).toString()
}
)
}
var counter = 1;
let cats = [
{
id: "0",
val: createCatLocation()
}
]
return (
<SafeAreaView
style={{ flex: 1 }}>
<Text style={styles.titleText}>InfiniteCats</Text>
<Text style={styles.contentText}>No cats were harmed</Text>
<FlatList
horizontal={true}
decelerationRate="fast"
pagingEnabled={true}
windowSize={5}
showsHorizontalScrollIndicator={false}
contentInsetAdjustmentBehavior="automatic"
data={cats}
onEndReached={findMoreCats}
onEndReachedThreshold={0.9}
style={styles.scrollView}
renderItem={({ item }) => (
<Card
cardWidth={windowWidth - 50}
cardMargin={25}
source={item.val}
/>
)} />
</SafeAreaView>
);
}