forked from calebnance/expo-spotify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.js
More file actions
67 lines (54 loc) · 2.02 KB
/
functions.js
File metadata and controls
67 lines (54 loc) · 2.02 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
import { Image } from 'react-native';
import { Asset, Font, Permissions } from 'expo';
import preloadFonts from './preloadFonts';
import preloadImages from './preloadImages';
// cache fonts
// /////////////////////////////////////////////////////////////////////////////
export const cacheFonts = fonts => {
const mappedFonts = fonts.map(font => Font.loadAsync(font));
return mappedFonts;
};
// cache images
// /////////////////////////////////////////////////////////////////////////////
export const cacheImages = images => {
const imagesArray = Object.values(images);
return imagesArray.map(image => {
if (typeof image === 'string') {
return Image.prefetch(image);
}
return Asset.fromModule(image).downloadAsync();
});
};
// preload async
// /////////////////////////////////////////////////////////////////////////////
export const loadAssetsAsync = () => {
// preload assets
const fontAssets = cacheFonts(preloadFonts);
const imageAssets = cacheImages(preloadImages);
// promise load all
return Promise.all([...fontAssets, ...imageAssets]);
};
// camera permissions
// /////////////////////////////////////////////////////////////////////////////
export 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
// /////////////////////////////////////////////////////////////////////////////
export 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)}`;
};