Skip to content

Commit 71b1b41

Browse files
committed
use FlatList to render the two columns
1 parent 93ac145 commit 71b1b41

File tree

1 file changed

+26
-49
lines changed

1 file changed

+26
-49
lines changed

source/views/streaming/webcams.js

Lines changed: 26 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,17 @@ import {
99
StyleSheet,
1010
View,
1111
Text,
12-
ScrollView,
12+
FlatList,
1313
Image,
14-
Dimensions,
1514
Platform,
1615
} from 'react-native'
17-
import {Column} from '../components/layout'
1816
import {TabBarIcon} from '../components/tabbar-icon'
1917
import {Touchable} from '../components/touchable'
2018
import * as c from '../components/colors'
2119
import * as defaultData from '../../../docs/webcams.json'
2220
import {webcamImages} from '../../../images/webcam-images'
2321
import {trackedOpenUrl} from '../components/open-url'
2422
import LinearGradient from 'react-native-linear-gradient'
25-
import {partitionByIndex} from '../../lib/partition-by-index'
2623

2724
type WebcamType = {
2825
streamUrl: string,
@@ -55,39 +52,29 @@ export class WebcamsView extends React.PureComponent<DProps, Props, State> {
5552
}
5653

5754
state = {
58-
width: Dimensions.get('window').width,
5955
}
6056

6157
componentWillMount() {
62-
Dimensions.addEventListener('change', this.handleResizeEvent)
6358
}
6459

65-
componentWillUnmount() {
66-
Dimensions.removeEventListener('change', this.handleResizeEvent)
67-
}
6860

69-
handleResizeEvent = (event: {window: {width: number}}) => {
70-
this.setState(() => ({width: event.window.width}))
71-
}
7261

73-
render() {
74-
const columns = partitionByIndex(this.props.webcams)
7562

63+
renderItem = ({item}: {item: WebcamType}) =>
64+
<StreamThumbnail key={item.name} webcam={item} />
65+
66+
keyExtractor = (item: WebcamType) => item.name
67+
68+
render() {
7669
return (
77-
<ScrollView contentContainerStyle={styles.gridWrapper}>
78-
{columns.map((contents, i) =>
79-
<Column key={i} style={styles.column}>
80-
{contents.map(webcam =>
81-
<StreamThumbnail
82-
key={webcam.name}
83-
webcam={webcam}
84-
textColor="white"
85-
viewportWidth={this.state.width}
86-
/>,
87-
)}
88-
</Column>,
89-
)}
90-
</ScrollView>
70+
<FlatList
71+
keyExtractor={this.keyExtractor}
72+
renderItem={this.renderItem}
73+
data={this.props.webcams}
74+
numColumns={2}
75+
columnWrapperStyle={styles.row}
76+
contentContainerStyle={styles.container}
77+
/>
9178
)
9279
}
9380
}
@@ -96,7 +83,6 @@ class StreamThumbnail extends React.PureComponent {
9683
props: {
9784
webcam: WebcamType,
9885
textColor: 'white' | 'black',
99-
viewportWidth: number,
10086
}
10187

10288
handlePress = () => {
@@ -109,20 +95,17 @@ class StreamThumbnail extends React.PureComponent {
10995
}
11096

11197
render() {
112-
const {textColor, viewportWidth} = this.props
98+
const {textColor} = this.props
11399
const {name, thumbnail, accentColor} = this.props.webcam
114100

115101
const [r, g, b] = accentColor
116102
const baseColor = `rgba(${r}, ${g}, ${b}, 1)`
117103
const startColor = `rgba(${r}, ${g}, ${b}, 0.1)`
118104
const actualTextColor = c[textColor]
119105

120-
const width = viewportWidth / 2 - CELL_MARGIN * 1.5
121-
const cellRatio = 2.15625
122-
const height = width / cellRatio
123106

124107
return (
125-
<View style={[styles.cell, styles.rounded, {width, height}]}>
108+
<View style={styles.cell}>
126109
<Touchable
127110
highlight={true}
128111
underlayColor={baseColor}
@@ -150,30 +133,24 @@ class StreamThumbnail extends React.PureComponent {
150133
const CELL_MARGIN = 10
151134

152135
const styles = StyleSheet.create({
153-
column: {
154-
flex: 1,
136+
container: {
137+
marginVertical: CELL_MARGIN / 2,
155138
},
156-
gridWrapper: {
157-
marginHorizontal: CELL_MARGIN / 2,
158-
marginTop: CELL_MARGIN / 2,
159-
paddingBottom: CELL_MARGIN / 2,
160-
161-
alignItems: 'center',
139+
row: {
162140
justifyContent: 'center',
163-
flexDirection: 'row',
164-
flexWrap: 'wrap',
165-
},
166-
rounded: {
167-
// TODO: Android doesn't currently (0.42) respect both
168-
// overflow:hidden and border-radius.
169-
borderRadius: Platform.OS === 'android' ? 0 : 6,
141+
marginHorizontal: CELL_MARGIN / 2,
170142
},
171143
cell: {
144+
flex: 1,
172145
overflow: 'hidden',
173146
margin: CELL_MARGIN / 2,
174147
justifyContent: 'center',
175148

176149
elevation: 2,
150+
151+
// TODO: Android doesn't currently (0.42) respect both
152+
// overflow:hidden and border-radius.
153+
borderRadius: Platform.OS === 'android' ? 0 : 6,
177154
},
178155
image: {
179156
width: '100%',

0 commit comments

Comments
 (0)