Skip to content

Commit 2b806e9

Browse files
committed
Added download
1 parent 4e4fad3 commit 2b806e9

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed

app/views/Storage/Demos/Download.js

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,73 @@ import {
66
} from 'react-native'
77

88
import 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';
1010
import appStyles from '../../../styles/app';
11+
import { UploadedImage } from '../components/UploadedImage';
1112

13+
const CAT_PATH = 'assets/images/cat.jpg';
1214
export 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
)

app/views/Storage/components/UploadedImage.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ import React from 'react';
22
import { Text, Image } from 'react-native';
33
import { Card, CardItem } from 'native-base';
44

5-
export const UploadedImage = ({image}) => {
6-
console.log('hi', image);
5+
export const UploadedImage = ({image, title = 'File'}) => {
76
return (
87
<Card>
98
<CardItem>
10-
<Text>Uploaded file</Text>
9+
<Text>{title}</Text>
1110
</CardItem>
1211

1312
<CardItem>

0 commit comments

Comments
 (0)