-
Notifications
You must be signed in to change notification settings - Fork 24
/
functions.js
71 lines (59 loc) · 2.06 KB
/
functions.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
import { Image } from 'react-native';
import { Asset } from 'expo-asset';
import * as Font from 'expo-font';
import { Permissions } from 'expo';
import preloadFonts from './preloadFonts';
import preloadImages from './preloadImages';
// cache fonts
// /////////////////////////////////////////////////////////////////////////////
const cacheFonts = fonts => fonts.map(font => Font.loadAsync(font));
// cache images
// /////////////////////////////////////////////////////////////////////////////
const cacheImages = images => {
return Object.values(images).map(image => {
if (typeof image === 'string') {
return Image.prefetch(image);
}
return Asset.fromModule(image).downloadAsync();
});
};
// preload async
// /////////////////////////////////////////////////////////////////////////////
const loadAssetsAsync = async () => {
// preload assets
const fontAssets = cacheFonts(preloadFonts);
const imageAssets = cacheImages(preloadImages);
// promise load all
return Promise.all([...fontAssets, ...imageAssets]);
};
// camera permissions
// /////////////////////////////////////////////////////////////////////////////
const cameraAccessAsync = async () => {
// get exisiting camera permissions first
const { status: existingStatus } = await Permissions.getAsync(
Permissions.CAMERA
);
let finalStatus = existingStatus;
// ask again to grant camera permissions (if not already allowed)
if (existingStatus !== 'granted') {
const { status } = await Permissions.askAsync(Permissions.CAMERA);
finalStatus = status;
}
return finalStatus === 'granted';
};
// format seconds
// /////////////////////////////////////////////////////////////////////////////
const formatTime = sec => {
const padTime = (num, size) => `000${num}`.slice(size * -1);
const time = parseFloat(sec).toFixed(3);
const minutes = Math.floor(time / 60) % 60;
const seconds = Math.floor(time - minutes * 60);
return `${padTime(minutes, 1)}:${padTime(seconds, 2)}`;
};
export default {
cacheFonts,
cacheImages,
loadAssetsAsync,
cameraAccessAsync,
formatTime
};