@@ -6,25 +6,73 @@ import {
66} from 'react-native'
77
88import env from '../../../../config/environment' ;
9- import { Container , Header , Title , Content , Button } from 'native-base' ;
9+ import { Container , Header , Title , Content , Button , Spinner } from 'native-base' ;
1010import appStyles from '../../../styles/app' ;
11+ import { UploadedImage } from '../components/UploadedImage' ;
1112
13+ const CAT_PATH = 'assets/images/cat.jpg' ;
1214export class DownloadDemo extends React . Component {
15+
16+ constructor ( props ) {
17+ super ( props ) ;
18+
19+ this . state = {
20+ downloading : false ,
21+ downloadProgress : 0 ,
22+ downloadedFile : null ,
23+ downloadingError : null
24+ }
25+ }
1326
1427 componentWillMount ( ) {
1528 const { firestack} = this . props ;
29+ firestack . storage . setStorageUrl ( 'firestack-example.appspot.com' ) ;
1630 }
1731
1832 componentWillUnmount ( ) {
1933 const { firestack} = this . props ;
2034 }
2135
2236 downloadByUrl ( ) {
23- const { firestack} = this . props ;
24- console . log ( 'downloadByUrl called' ) ;
37+ const { firestack} = this . props ;
38+ const storage = firestack . storage ;
39+ const ref = storage . ref ( CAT_PATH ) ;
40+ const tmpDir = firestack . constants . TEMP_DIRECTORY_PATH ;
41+ const localPath = `${ tmpDir } /downloadedCat.jpg` ;
42+
43+ this . setState ( {
44+ downloadProgress : 0 ,
45+ downloading : true ,
46+ downloadedFile : null ,
47+ downloadingError : null
48+ } , ( ) => {
49+ ref . download ( localPath , ( msg ) => {
50+ if ( msg . eventName === 'download_progress' ) {
51+ this . setState ( {
52+ downloadProgress : msg . progress
53+ } ) ;
54+ }
55+ } )
56+ . then ( ( res ) => {
57+ console . log ( 'download completed' , res )
58+ this . setState ( {
59+ downloading : false ,
60+ downloadedFile : Object . assign ( { } , res , { localPath} )
61+ } )
62+ } )
63+ . catch ( err => {
64+ console . log ( 'an error in uploading' , err ) ;
65+ this . setState ( {
66+ downloading : false ,
67+ downloadingError : err
68+ } )
69+ } ) ;
70+ } ) ;
2571 }
2672
2773 render ( ) {
74+ const { downloadedFile, downloading } = this . state ;
75+
2876 return (
2977 < Container >
3078 < Content >
@@ -33,6 +81,11 @@ export class DownloadDemo extends React.Component {
3381 onPress = { this . downloadByUrl . bind ( this ) } >
3482 Download with URL
3583 </ Button >
84+
85+ { downloadedFile &&
86+ < UploadedImage
87+ image = { { uri : downloadedFile . localPath } } /> }
88+ { downloading && < Spinner /> }
3689 </ Content >
3790 </ Container >
3891 )
0 commit comments