-
Notifications
You must be signed in to change notification settings - Fork 0
/
TurboImage.js
174 lines (157 loc) · 4.59 KB
/
TurboImage.js
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import React, { Component } from "react";
import { StyleSheet, View } from "react-native";
import { FileSystem } from "expo";
import FadeInImage from "./FadeInImage";
const CACHE_DIR = `${FileSystem.cacheDirectory}turbo-expo-image/`;
export async function clearCache() {
await FileSystem.deleteAsync(CACHE_DIR, { idempotent: true });
await FileSystem.makeDirectoryAsync(CACHE_DIR);
}
class TurboImage extends Component {
state = {
imageLoading: false,
error: null,
imageUri: null,
thumbLoading: false,
thumbError: null,
thumbUri: null
};
mounted = true;
componentDidMount() {
this.startLoading();
}
componentDidUpdate(prevProps) {
const { uri } = this.props;
if (prevProps.uri !== uri) this.startLoading();
}
componentWillUnmount() {
this.mounted = false;
}
safeSetState = newState => {
if (this.mounted) this.setState(newState);
};
startLoading = () => {
const { uri, thumb } = this.props;
if (uri)
if (/^file:\/\//.test(uri)) {
this.safeSetState({ imageLoading: false, error: null, imageUri: uri });
} else {
this.loadImage({
uri: uri.replace(/ /gi, "+"), // THIS IS A TEMPORARY PATCH!!! TODO: ADDRESS B/E URL BUG
onExists: uri =>
this.safeSetState({
imageLoading: false,
error: null,
imageUri: uri
}),
onStart: () => {
this.safeSetState({ imageLoading: true });
if (thumb)
this.loadImage({
uri: thumb.replace(/ /gi, "+"), // THIS IS A TEMPORARY PATCH!!! TODO: ADDRESS B/E URL BUG
onStart: () => this.safeSetState({ thumbLoading: true }),
onExists: uri =>
this.safeSetState({
thumbLoading: false,
error: null,
thumbUri: uri
}),
onLoaded: uri =>
this.safeSetState({
thumbLoading: false,
error: null,
thumbUri: uri
}),
onError: error =>
this.safeSetState({
thumbLoading: false,
error,
imageUri: null
})
});
},
onLoaded: uri =>
this.safeSetState({
imageLoading: false,
error: null,
imageUri: uri
}),
onError: error =>
this.safeSetState({ imageLoading: false, error, imageUri: null })
});
}
};
async uriToName(uri) {
const filename = uri.substring(
uri.lastIndexOf("/"),
uri.indexOf("?") === -1 ? uri.length : uri.indexOf("?")
);
const ext =
filename.indexOf(".") === -1
? ".jpg"
: filename.substring(filename.lastIndexOf("."));
const hash = uri
.substring(uri.indexOf("://") > -1 ? uri.indexOf("://") + 3 : 0)
.replace(/\/|\?|\&| |:| |\\|\./gi, "");
const path = `${CACHE_DIR}${hash}${ext}`;
try {
await FileSystem.makeDirectoryAsync(CACHE_DIR);
} catch (e) {}
return path;
}
async loadImage({ onStart, onExists, onLoaded, onError, uri }) {
//onStart
onStart();
try {
const file = await this.uriToName(uri);
const cached = await FileSystem.getInfoAsync(file);
if (cached.exists) {
// file exists in cache
// onLoaded
onExists(cached.uri);
} else {
// file does not exist in cache so download it
const res = await FileSystem.downloadAsync(uri, file);
if (res && res.status === 200) {
onLoaded(res.uri);
} else {
console.log("TURBO EXPO IMAGE ERROR", uri, res);
onError(res.status);
}
}
} catch (error) {
onError(error);
console.log("TURBO EXPO IMAGE ERROR!", uri, error);
}
}
render() {
const { imageUri, thumbUri } = this.state;
const { style, children } = this.props;
return (
<View style={[style, turboImageStyles.placeholder]}>
<FadeInImage
uri={thumbUri}
style={[style, turboImageStyles.thumb]}
blurRadius={15}
key={thumbUri + "thumb"}
/>
<FadeInImage
uri={imageUri}
style={[style, turboImageStyles.thumb]}
key={imageUri + "image"}
/>
{children}
</View>
);
}
}
const turboImageStyles = StyleSheet.create({
placeholder: {
backgroundColor: "#f7f7f7",
padding: 0
},
thumb: {
position: "absolute"
}
});
export default TurboImage;