Skip to content

Commit cf0e893

Browse files
committed
allow pulling webcam info from Github
1 parent 71b1b41 commit cf0e893

File tree

1 file changed

+42
-19
lines changed

1 file changed

+42
-19
lines changed

source/views/streaming/webcams.js

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,9 @@
55
*/
66

77
import React from 'react'
8-
import {
9-
StyleSheet,
10-
View,
11-
Text,
12-
FlatList,
13-
Image,
14-
Platform,
15-
} from 'react-native'
8+
import {StyleSheet, View, Text, FlatList, Image, Platform} from 'react-native'
9+
import delay from 'delay'
10+
import {reportNetworkProblem} from '../../lib/report-network-problem'
1611
import {TabBarIcon} from '../components/tabbar-icon'
1712
import {Touchable} from '../components/touchable'
1813
import * as c from '../components/colors'
@@ -21,6 +16,8 @@ import {webcamImages} from '../../../images/webcam-images'
2116
import {trackedOpenUrl} from '../components/open-url'
2217
import LinearGradient from 'react-native-linear-gradient'
2318

19+
const GITHUB_URL = 'https://stodevx.github.io/AAO-React-Native/webcams.json'
20+
2421
type WebcamType = {
2522
streamUrl: string,
2623
pageUrl: string,
@@ -29,36 +26,60 @@ type WebcamType = {
2926
accentColor: [number, number, number],
3027
}
3128

32-
type DProps = {
33-
webcams: Array<WebcamType>,
34-
}
35-
36-
type Props = {
37-
webcams: Array<WebcamType>,
38-
}
29+
type Props = {}
3930

4031
type State = {
32+
webcams: Array<WebcamType>,
4133
width: number,
34+
loading: boolean,
35+
refreshing: boolean,
4236
}
4337

44-
export class WebcamsView extends React.PureComponent<DProps, Props, State> {
38+
export class WebcamsView extends React.PureComponent<void, Props, State> {
4539
static navigationOptions = {
4640
tabBarLabel: 'Webcams',
4741
tabBarIcon: TabBarIcon('videocam'),
4842
}
4943

50-
static defaultProps = {
44+
state = {
5145
webcams: defaultData.data,
46+
loading: false,
47+
refreshing: false,
5248
}
5349

54-
state = {
50+
componentWillMount() {
51+
this.fetchData()
5552
}
5653

57-
componentWillMount() {
54+
refresh = async () => {
55+
const start = Date.now()
56+
this.setState(() => ({refreshing: true}))
57+
58+
await this.fetchData()
59+
60+
// wait 0.5 seconds – if we let it go at normal speed, it feels broken.
61+
const elapsed = Date.now() - start
62+
if (elapsed < 500) {
63+
await delay(500 - elapsed)
64+
}
65+
66+
this.setState(() => ({refreshing: false}))
5867
}
5968

69+
fetchData = async () => {
70+
this.setState(() => ({loading: true}))
71+
72+
let {data: webcams} = await fetchJson(GITHUB_URL).catch(err => {
73+
reportNetworkProblem(err)
74+
return defaultData
75+
})
6076

77+
if (process.env.NODE_ENV === 'development') {
78+
webcams = defaultData.data
79+
}
6180

81+
this.setState(() => ({webcams, loading: false}))
82+
}
6283

6384
renderItem = ({item}: {item: WebcamType}) =>
6485
<StreamThumbnail key={item.name} webcam={item} />
@@ -70,6 +91,8 @@ export class WebcamsView extends React.PureComponent<DProps, Props, State> {
7091
<FlatList
7192
keyExtractor={this.keyExtractor}
7293
renderItem={this.renderItem}
94+
refreshing={this.state.refreshing}
95+
onRefresh={this.refresh}
7396
data={this.props.webcams}
7497
numColumns={2}
7598
columnWrapperStyle={styles.row}

0 commit comments

Comments
 (0)