-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
135 lines (120 loc) · 3.23 KB
/
index.js
File metadata and controls
135 lines (120 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import React, {Component} from 'react';
import {Image, Dimensions, View} from 'react-native';
import {sha256} from 'react-native-sha256';
var RNFS = require('react-native-fs');
class CachedImage extends Component {
constructor(props) {
super(props);
this.state = {
image_uri: this.props.placeholder,
rectTop: 0,
rectBottom: 0,
};
if (!this.props.lazy_load) {
this.loadFile(this.props.image_uri);
}
}
componentDidMount() {
if (!this.props.disabled) {
this.startWatching();
}
}
componentWillUnmount() {
this.stopWatching();
}
UNSAFE_componentWillReceiveProps(nextProps) {
if (nextProps.disabled) {
this.stopWatching();
} else {
this.lastValue = null;
this.startWatching();
}
}
startWatching() {
if (this.interval) {
return;
}
this.interval = setInterval(() => {
if (!this.myview) {
return;
}
this.myview.measure((x, y, width, height, pageX, pageY) => {
this.setState({
rectTop: pageY,
rectBottom: pageY + height,
rectWidth: pageX + width,
});
});
this.isInViewPort();
}, this.props.delay || 100);
}
stopWatching() {
this.interval = clearInterval(this.interval);
}
isInViewPort() {
const window = Dimensions.get('window');
const isVisible =
this.state.rectBottom !== 0 &&
this.state.rectTop >= 0 &&
this.state.rectBottom <= window.height &&
this.state.rectWidth > 0 &&
this.state.rectWidth <= window.width;
if (this.lastValue !== isVisible) {
this.lastValue = isVisible;
this.loadFile(this.props.image_uri);
}
}
loadFile = (url) => {
return new Promise((resolve, reject) => {
// Create a SHA256 hash for the file name so that it is always same for this URL
sha256(url)
.then((hash) => {
// get the format of the file from the URL
let format = url.split('?')[0].split('.').slice(-1).pop();
// Generate file path for local dir
let filepath = RNFS.DocumentDirectoryPath + '/' + hash + '.' + format;
// Check if file exists locally
RNFS.exists(filepath).then((exists) => {
// If file exists pass the filepath as is
if (exists) {
this.setState({
image_uri: 'file://' + filepath,
});
resolve(filepath);
} else {
// If file doesnt exists download it
RNFS.downloadFile({fromUrl: url, toFile: filepath}).promise.then(
(r) => {
this.setState({
image_uri: 'file://' + filepath,
});
resolve(filepath);
},
);
}
});
})
.catch((err) => {
console.log(err.message);
});
});
};
render() {
return (
<>
<View>
<Image
ref={(component) => {
this.myview = component;
}}
style={{width: 400, height: 400}}
source={{
uri: this.state.image_uri,
}}
/>
</View>
</>
);
}
}
export default CachedImage;