4
4
* This source code is licensed under the MIT license found in the
5
5
* LICENSE file in the root directory of this source tree.
6
6
*
7
- * @format
7
+ * @flow
8
8
*/
9
9
10
10
'use strict' ;
11
11
12
- const React = require ( 'react' ) ;
13
- const ReactNative = require ( 'react-native' ) ;
14
- const {
15
- Image,
12
+ import React from 'react' ;
13
+ import { Image ,
16
14
StyleSheet ,
17
15
Text ,
18
16
TouchableWithoutFeedback ,
19
17
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'
57
21
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" ;
93
27
94
- export default class ViewPagerAndroidExample extends React . Component {
28
+ export default class ViewPagerExample extends React . Component {
95
29
constructor ( props ) {
96
30
super ( props ) ;
97
31
@@ -139,8 +73,6 @@ export default class ViewPagerAndroidExample extends React.Component {
139
73
} else {
140
74
this . viewPager . setPageWithoutAnimation ( page ) ;
141
75
}
142
-
143
- this . setState ( { page} ) ;
144
76
} ;
145
77
146
78
createPage ( key ) {
@@ -170,7 +102,7 @@ export default class ViewPagerAndroidExample extends React.Component {
170
102
render ( ) {
171
103
const { page, pages, animationsAreEnabled} = this . state ;
172
104
return (
173
- < View style = { styles . container } >
105
+ < SafeAreaView style = { styles . container } >
174
106
< ViewPagerAndroid
175
107
style = { styles . viewPager }
176
108
initialPage = { 0 }
@@ -194,11 +126,7 @@ export default class ViewPagerAndroidExample extends React.Component {
194
126
this . setState ( { scrollEnabled : ! this . state . scrollEnabled } )
195
127
}
196
128
/>
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 } />
202
130
</ View >
203
131
< View style = { styles . buttons } >
204
132
{ animationsAreEnabled ? (
@@ -225,10 +153,6 @@ export default class ViewPagerAndroidExample extends React.Component {
225
153
enabled = { page > 0 }
226
154
onPress = { ( ) => this . move ( - 1 ) }
227
155
/>
228
- < Text style = { styles . buttonText } >
229
- Page { page + 1 } / { pages . length }
230
- </ Text >
231
- < ProgressBar numberOfPages = { pages . length } size = { 100 } progress = { this . state . progress } />
232
156
< Button
233
157
text = "Next"
234
158
enabled = { page < pages . length - 1 }
@@ -240,30 +164,27 @@ export default class ViewPagerAndroidExample extends React.Component {
240
164
onPress = { ( ) => this . go ( pages . length - 1 ) }
241
165
/>
242
166
</ 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 >
244
172
) ;
245
173
}
246
174
}
247
175
248
176
const styles = StyleSheet . create ( {
249
177
buttons : {
250
178
flexDirection : 'row' ,
251
- height : 30 ,
252
179
backgroundColor : 'black' ,
253
180
alignItems : 'center' ,
254
181
justifyContent : 'space-between' ,
255
182
} ,
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 ,
265
186
backgroundColor : 'black' ,
266
- opacity : 0.5 ,
187
+ justifyContent : 'space-between' ,
267
188
} ,
268
189
buttonText : {
269
190
color : 'white' ,
@@ -280,34 +201,6 @@ const styles = StyleSheet.create({
280
201
height : 200 ,
281
202
padding : 20 ,
282
203
} ,
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
- } ,
311
204
viewPager : {
312
205
flex : 1 ,
313
206
} ,
0 commit comments