Skip to content

Commit b54aa36

Browse files
PTROCKIferrannp
authored andcommitted
feat: iOS implementation (#28)
1 parent 9b51b2c commit b54aa36

24 files changed

+965
-247
lines changed

example/Common.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// @flow
2+
3+
export const PAGES = 5;
4+
export const BGCOLOR = ['#fdc08e', '#fff6b9', '#99d1b7', '#dde5fe', '#f79273'];
5+
export const IMAGE_URIS = [
6+
'https://apod.nasa.gov/apod/image/1410/20141008tleBaldridge001h990.jpg',
7+
'https://apod.nasa.gov/apod/image/1409/volcanicpillar_vetter_960.jpg',
8+
'https://apod.nasa.gov/apod/image/1409/m27_snyder_960.jpg',
9+
'https://apod.nasa.gov/apod/image/1409/PupAmulti_rot0.jpg',
10+
'https://apod.nasa.gov/apod/image/1510/lunareclipse_27Sep_beletskycrop4.jpg',
11+
];
12+
export const thumbsUp = '\uD83D\uDC4D';
13+
14+
export const createPage = (key) => {
15+
return {
16+
key: key,
17+
style: {
18+
backgroundColor: BGCOLOR[key % BGCOLOR.length],
19+
alignItems: 'center',
20+
padding: 20,
21+
},
22+
imgSource: { uri: IMAGE_URIS[key % BGCOLOR.length] }
23+
}
24+
};

example/ViewPagerAndroidExample.ios.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

example/ViewPagerAndroidExample.android.js renamed to example/ViewPagerExample.js

Lines changed: 23 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -4,94 +4,28 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @format
7+
* @flow
88
*/
99

1010
'use strict';
1111

12-
const React = require('react');
13-
const ReactNative = require('react-native');
14-
const {
15-
Image,
12+
import React from 'react';
13+
import { Image,
1614
StyleSheet,
1715
Text,
1816
TouchableWithoutFeedback,
1917
TouchableOpacity,
20-
View,
21-
} = ReactNative;
22-
import ViewPagerAndroid from '@react-native-community/viewpager';
23-
24-
const PAGES = 5;
25-
const BGCOLOR = ['#fdc08e', '#fff6b9', '#99d1b7', '#dde5fe', '#f79273'];
26-
const IMAGE_URIS = [
27-
'https://apod.nasa.gov/apod/image/1410/20141008tleBaldridge001h990.jpg',
28-
'https://apod.nasa.gov/apod/image/1409/volcanicpillar_vetter_960.jpg',
29-
'https://apod.nasa.gov/apod/image/1409/m27_snyder_960.jpg',
30-
'https://apod.nasa.gov/apod/image/1409/PupAmulti_rot0.jpg',
31-
'https://apod.nasa.gov/apod/image/1510/lunareclipse_27Sep_beletskycrop4.jpg',
32-
];
33-
34-
type Props = $ReadOnly<{||}>;
35-
type State = {|likes: number|};
36-
class LikeCount extends React.Component<Props, State> {
37-
state = {
38-
likes: 7,
39-
};
40-
41-
onClick = () => {
42-
this.setState({likes: this.state.likes + 1});
43-
};
44-
45-
render() {
46-
const thumbsUp = '\uD83D\uDC4D';
47-
return (
48-
<View style={styles.likeContainer}>
49-
<TouchableOpacity onPress={this.onClick} style={styles.likeButton}>
50-
<Text style={styles.likesText}>{thumbsUp + ' Like'}</Text>
51-
</TouchableOpacity>
52-
<Text style={styles.likesText}>{this.state.likes + ' likes'}</Text>
53-
</View>
54-
);
55-
}
56-
}
18+
View,
19+
SafeAreaView,
20+
Platform } from 'react-native'
5721

58-
class Button extends React.Component {
59-
_handlePress = () => {
60-
if (this.props.enabled && this.props.onPress) {
61-
this.props.onPress();
62-
}
63-
};
64-
65-
render() {
66-
return (
67-
<TouchableWithoutFeedback onPress={this._handlePress}>
68-
<View
69-
style={[
70-
styles.button,
71-
this.props.enabled ? {} : styles.buttonDisabled,
72-
]}>
73-
<Text style={styles.buttonText}>{this.props.text}</Text>
74-
</View>
75-
</TouchableWithoutFeedback>
76-
);
77-
}
78-
}
79-
80-
class ProgressBar extends React.Component {
81-
render() {
82-
const fractionalPosition =
83-
this.props.progress.position + this.props.progress.offset;
84-
const progressBarSize =
85-
(fractionalPosition / (this.props.numberOfPages - 1)) * this.props.size;
86-
return (
87-
<View style={[styles.progressBarContainer, {width: this.props.size}]}>
88-
<View style={[styles.progressBar, {width: progressBarSize}]} />
89-
</View>
90-
);
91-
}
92-
}
22+
import ViewPagerAndroid from '@react-native-community/viewpager';
23+
import { PAGES, BGCOLOR, IMAGE_URIS, createPage } from "./Common";
24+
import { Button } from "./src/component/Button";
25+
import { LikeCount } from "./src/component/LikeCount";
26+
import { ProgressBar } from "./src/component/ProgressBar";
9327

94-
export default class ViewPagerAndroidExample extends React.Component {
28+
export default class ViewPagerExample extends React.Component {
9529
constructor(props) {
9630
super(props);
9731

@@ -139,8 +73,6 @@ export default class ViewPagerAndroidExample extends React.Component {
13973
} else {
14074
this.viewPager.setPageWithoutAnimation(page);
14175
}
142-
143-
this.setState({page});
14476
};
14577

14678
createPage(key) {
@@ -170,7 +102,7 @@ export default class ViewPagerAndroidExample extends React.Component {
170102
render() {
171103
const {page, pages, animationsAreEnabled} = this.state;
172104
return (
173-
<View style={styles.container}>
105+
<SafeAreaView style={styles.container}>
174106
<ViewPagerAndroid
175107
style={styles.viewPager}
176108
initialPage={0}
@@ -194,11 +126,7 @@ export default class ViewPagerAndroidExample extends React.Component {
194126
this.setState({scrollEnabled: !this.state.scrollEnabled})
195127
}
196128
/>
197-
<Button
198-
enabled={true}
199-
text="Add new page"
200-
onPress={this.addPage}
201-
/>
129+
<Button enabled={true} text="Add new page" onPress={this.addPage} />
202130
</View>
203131
<View style={styles.buttons}>
204132
{animationsAreEnabled ? (
@@ -225,10 +153,6 @@ export default class ViewPagerAndroidExample extends React.Component {
225153
enabled={page > 0}
226154
onPress={() => this.move(-1)}
227155
/>
228-
<Text style={styles.buttonText}>
229-
Page {page + 1} / {pages.length}
230-
</Text>
231-
<ProgressBar numberOfPages={pages.length} size={100} progress={this.state.progress} />
232156
<Button
233157
text="Next"
234158
enabled={page < pages.length - 1}
@@ -240,30 +164,27 @@ export default class ViewPagerAndroidExample extends React.Component {
240164
onPress={() => this.go(pages.length - 1)}
241165
/>
242166
</View>
243-
</View>
167+
<View style={styles.progress}>
168+
<Text style={styles.buttonText}> Page {page + 1} / {pages.length} </Text>
169+
<ProgressBar numberOfPages={pages.length} size={300} progress={this.state.progress} />
170+
</View>
171+
</SafeAreaView>
244172
);
245173
}
246174
}
247175

248176
const styles = StyleSheet.create({
249177
buttons: {
250178
flexDirection: 'row',
251-
height: 30,
252179
backgroundColor: 'black',
253180
alignItems: 'center',
254181
justifyContent: 'space-between',
255182
},
256-
button: {
257-
flex: 1,
258-
width: 0,
259-
margin: 5,
260-
borderColor: 'gray',
261-
borderWidth: 1,
262-
backgroundColor: 'gray',
263-
},
264-
buttonDisabled: {
183+
progress: {
184+
flexDirection: 'row',
185+
height: 40,
265186
backgroundColor: 'black',
266-
opacity: 0.5,
187+
justifyContent: 'space-between',
267188
},
268189
buttonText: {
269190
color: 'white',
@@ -280,34 +201,6 @@ const styles = StyleSheet.create({
280201
height: 200,
281202
padding: 20,
282203
},
283-
likeButton: {
284-
backgroundColor: 'rgba(0, 0, 0, 0.1)',
285-
borderColor: '#333333',
286-
borderWidth: 1,
287-
borderRadius: 5,
288-
flex: 1,
289-
margin: 8,
290-
},
291-
likeContainer: {
292-
flexDirection: 'row',
293-
height: 45,
294-
},
295-
likesText: {
296-
flex: 1,
297-
fontSize: 18,
298-
alignSelf: 'center',
299-
},
300-
progressBarContainer: {
301-
height: 10,
302-
margin: 10,
303-
borderColor: '#eeeeee',
304-
borderWidth: 2,
305-
},
306-
progressBar: {
307-
alignSelf: 'flex-start',
308-
flex: 1,
309-
backgroundColor: '#eeeeee',
310-
},
311204
viewPager: {
312205
flex: 1,
313206
},

example/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import React from 'react';
44
import {AppRegistry} from 'react-native';
55

66
import {name as appName} from './app.json';
7-
import ViewPagerAndroidExample from "./ViewPagerAndroidExample";
7+
import ViewPagerExample from "./ViewPagerExample";
88

99
class ExampleApp extends React.Component<{}> {
1010
render() {
1111
return (
12-
<ViewPagerAndroidExample />
12+
<ViewPagerExample />
1313
)
1414
}
1515
}

0 commit comments

Comments
 (0)