@@ -36,7 +36,7 @@ type ImageSource = $ReadOnly<{|
36
36
type NetworkImageCallbackExampleState = { |
37
37
events : Array < string > ,
38
38
startLoadPrefetched : boolean ,
39
- mountTime : Date ,
39
+ mountTime : number ,
40
40
| } ;
41
41
42
42
type NetworkImageCallbackExampleProps = $ReadOnly < { |
@@ -51,11 +51,11 @@ class NetworkImageCallbackExample extends React.Component<
51
51
state = {
52
52
events : [ ] ,
53
53
startLoadPrefetched : false ,
54
- mountTime : new Date ( ) ,
54
+ mountTime : Date . now ( ) ,
55
55
} ;
56
56
57
57
UNSAFE_componentWillMount ( ) {
58
- this . setState ( { mountTime : new Date ( ) } ) ;
58
+ this . setState ( { mountTime : Date . now ( ) } ) ;
59
59
}
60
60
61
61
_loadEventFired = ( event : string ) => {
@@ -72,43 +72,50 @@ class NetworkImageCallbackExample extends React.Component<
72
72
source = { this . props . source }
73
73
style = { [ styles . base , { overflow : 'visible' } ] }
74
74
onLoadStart = { ( ) =>
75
- this . _loadEventFired ( `✔ onLoadStart (+${ new Date ( ) - mountTime } ms)` )
75
+ this . _loadEventFired ( `✔ onLoadStart (+${ Date . now ( ) - mountTime } ms)` )
76
76
}
77
+ onProgress = { event => {
78
+ const { loaded, total} = event . nativeEvent ;
79
+ const percent = Math . round ( ( loaded / total ) * 100 ) ;
80
+ this . _loadEventFired (
81
+ `✔ onProgress ${ percent } % (+${ Date . now ( ) - mountTime } ms)` ,
82
+ ) ;
83
+ } }
77
84
onLoad = { event => {
78
85
if ( event . nativeEvent . source ) {
79
86
const url = event . nativeEvent . source . uri ;
80
87
this . _loadEventFired (
81
- `✔ onLoad (+${ new Date ( ) - mountTime } ms) for URL ${ url } ` ,
88
+ `✔ onLoad (+${ Date . now ( ) - mountTime } ms) for URL ${ url } ` ,
82
89
) ;
83
90
} else {
84
- this . _loadEventFired ( `✔ onLoad (+${ new Date ( ) - mountTime } ms)` ) ;
91
+ this . _loadEventFired ( `✔ onLoad (+${ Date . now ( ) - mountTime } ms)` ) ;
85
92
}
86
93
} }
87
94
onLoadEnd = { ( ) => {
88
- this . _loadEventFired ( `✔ onLoadEnd (+${ new Date ( ) - mountTime } ms)` ) ;
95
+ this . _loadEventFired ( `✔ onLoadEnd (+${ Date . now ( ) - mountTime } ms)` ) ;
89
96
this . setState ( { startLoadPrefetched : true } , ( ) => {
90
97
prefetchTask . then (
91
98
( ) => {
92
99
this . _loadEventFired (
93
- `✔ Prefetch OK (+${ new Date ( ) - mountTime } ms)` ,
100
+ `✔ Prefetch OK (+${ Date . now ( ) - mountTime } ms)` ,
94
101
) ;
95
102
Image . queryCache ( [ IMAGE_PREFETCH_URL ] ) . then ( map => {
96
103
const result = map [ IMAGE_PREFETCH_URL ] ;
97
104
if ( result ) {
98
105
this . _loadEventFired (
99
- `✔ queryCache "${ result } " (+${ new Date ( ) -
106
+ `✔ queryCache "${ result } " (+${ Date . now ( ) -
100
107
mountTime } ms)`,
101
108
) ;
102
109
} else {
103
110
this . _loadEventFired (
104
- `✘ queryCache (+${ new Date ( ) - mountTime } ms)` ,
111
+ `✘ queryCache (+${ Date . now ( ) - mountTime } ms)` ,
105
112
) ;
106
113
}
107
114
} ) ;
108
115
} ,
109
116
error => {
110
117
this . _loadEventFired (
111
- `✘ Prefetch failed (+${ new Date ( ) - mountTime } ms)` ,
118
+ `✘ Prefetch failed (+${ Date . now ( ) - mountTime } ms)` ,
112
119
) ;
113
120
} ,
114
121
) ;
@@ -121,26 +128,26 @@ class NetworkImageCallbackExample extends React.Component<
121
128
style = { [ styles . base , { overflow : 'visible' } ] }
122
129
onLoadStart = { ( ) =>
123
130
this . _loadEventFired (
124
- `✔ (prefetched) onLoadStart (+${ new Date ( ) - mountTime } ms)` ,
131
+ `✔ (prefetched) onLoadStart (+${ Date . now ( ) - mountTime } ms)` ,
125
132
)
126
133
}
127
134
onLoad = { event => {
128
135
// Currently this image source feature is only available on iOS.
129
136
if ( event . nativeEvent . source ) {
130
137
const url = event . nativeEvent . source . uri ;
131
138
this . _loadEventFired (
132
- `✔ (prefetched) onLoad (+${ new Date ( ) -
139
+ `✔ (prefetched) onLoad (+${ Date . now ( ) -
133
140
mountTime } ms) for URL ${ url } `,
134
141
) ;
135
142
} else {
136
143
this . _loadEventFired (
137
- `✔ (prefetched) onLoad (+${ new Date ( ) - mountTime } ms)` ,
144
+ `✔ (prefetched) onLoad (+${ Date . now ( ) - mountTime } ms)` ,
138
145
) ;
139
146
}
140
147
} }
141
148
onLoadEnd = { ( ) =>
142
149
this . _loadEventFired (
143
- `✔ (prefetched) onLoadEnd (+${ new Date ( ) - mountTime } ms)` ,
150
+ `✔ (prefetched) onLoadEnd (+${ Date . now ( ) - mountTime } ms)` ,
144
151
)
145
152
}
146
153
/>
@@ -152,9 +159,9 @@ class NetworkImageCallbackExample extends React.Component<
152
159
}
153
160
154
161
type NetworkImageExampleState = { |
155
- error : boolean ,
162
+ error : ? string ,
156
163
loading : boolean ,
157
- progress : number ,
164
+ progress : $ReadOnlyArray < number > ,
158
165
| } ;
159
166
160
167
type NetworkImageExampleProps = $ReadOnly < { |
@@ -166,38 +173,38 @@ class NetworkImageExample extends React.Component<
166
173
NetworkImageExampleState ,
167
174
> {
168
175
state = {
169
- error : false ,
176
+ error : null ,
170
177
loading : false ,
171
- progress : 0 ,
178
+ progress : [ ] ,
172
179
} ;
173
180
174
181
render ( ) {
175
- const loader = this . state . loading ? (
176
- < View style = { styles . progress } >
177
- < Text > { this . state . progress } %</ Text >
178
- < ActivityIndicator style = { { marginLeft : 5 } } />
179
- </ View >
180
- ) : null ;
181
- return this . state . error ? (
182
+ return this . state . error != null ? (
182
183
< Text > { this . state . error } </ Text >
183
184
) : (
184
- < ImageBackground
185
- source = { this . props . source }
186
- style = { [ styles . base , { overflow : 'visible' } ] }
187
- onLoadStart = { e => this . setState ( { loading : true } ) }
188
- onError = { e =>
189
- this . setState ( { error : e . nativeEvent . error , loading : false } )
190
- }
191
- onProgress = { e =>
192
- this . setState ( {
193
- progress : Math . round (
194
- ( 100 * e . nativeEvent . loaded ) / e . nativeEvent . total ,
195
- ) ,
196
- } )
197
- }
198
- onLoad = { ( ) => this . setState ( { loading : false , error : false } ) } >
199
- { loader }
200
- </ ImageBackground >
185
+ < >
186
+ < Image
187
+ source = { this . props . source }
188
+ style = { [ styles . base , { overflow : 'visible' } ] }
189
+ onLoadStart = { e => this . setState ( { loading : true } ) }
190
+ onError = { e =>
191
+ this . setState ( { error : e . nativeEvent . error , loading : false } )
192
+ }
193
+ onProgress = { e => {
194
+ const { loaded, total} = e . nativeEvent ;
195
+ this . setState ( prevState => ( {
196
+ progress : [
197
+ ...prevState . progress ,
198
+ Math . round ( ( 100 * loaded ) / total ) ,
199
+ ] ,
200
+ } ) ) ;
201
+ } }
202
+ onLoad = { ( ) => this . setState ( { loading : false , error : null } ) }
203
+ />
204
+ < Text >
205
+ { this . state . progress . map ( progress => `${ progress } %` ) . join ( '\n' ) }
206
+ </ Text >
207
+ </ >
201
208
) ;
202
209
}
203
210
}
@@ -346,12 +353,6 @@ const styles = StyleSheet.create({
346
353
width : 38 ,
347
354
height : 38 ,
348
355
} ,
349
- progress : {
350
- flex : 1 ,
351
- alignItems : 'center' ,
352
- flexDirection : 'row' ,
353
- width : 100 ,
354
- } ,
355
356
leftMargin : {
356
357
marginLeft : 10 ,
357
358
} ,
@@ -465,7 +466,6 @@ exports.examples = [
465
466
/>
466
467
) ;
467
468
} ,
468
- platform : 'ios' ,
469
469
} ,
470
470
{
471
471
title : 'Image Download Progress' ,
@@ -478,7 +478,6 @@ exports.examples = [
478
478
/>
479
479
) ;
480
480
} ,
481
- platform : 'ios' ,
482
481
} ,
483
482
{
484
483
title : 'defaultSource' ,
0 commit comments