forked from hmziqrs/react-native-hgl-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
92 lines (75 loc) · 1.99 KB
/
utils.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
import { PixelRatio, Keyboard } from 'react-native';
import DeviceInfo from 'react-native-device-info';
import { isTablet, isDebug } from './platform';
import dimensions from './dimensions';
let log = false;
export function scaling(unit) {
let ratio = dimensions.width / dimensions.height;
const pixelDensity = PixelRatio.get();
ratio = ratio + (pixelDensity + ratio) / 2;
ratio = pixelDensity + ratio;
if (!log) {
log = true;
console.log(
`ratio: ${pixelDensity}, ${DeviceInfo.getDeviceNameSync()} ${ratio}\nwidth: ${
dimensions.width
} check:${dimensions.width < 420 && pixelDensity > 2}\n\n`
);
}
if (dimensions.width < 420 && pixelDensity > 2) {
ratio *= 0.85;
if (ratio >= 4) {
ratio = 4;
}
}
return ratio * unit;
}
// export function scaling(percent) {
// const height = DeviceInfo.isLandscape()
// ? dimensions.width
// : dimensions.height;
// const deviceHeight = isIphoneX()
// ? height * 0.9
// : Platform.OS === 'android'
// ? height - dimensions.statusBarHeight
// : height;
// const heightPercent = (percent * deviceHeight) / 100;
// const per = Math.round(heightPercent);
// let unit = per;
// const max = 1;
// const min = 0.4;
// if (unit > max) {
// unit = max;
// }
// return per * unit;
// }
export function debugLog(...p) {
if (isDebug) {
console.log(...p);
}
}
export function hideKeyboard(callback = () => {}) {
Keyboard.dismiss();
return callback();
}
export function getActiveRouteName(navigationState) {
if (!navigationState) {
return null;
}
const route = navigationState.routes[navigationState.index];
// dive into nested navigators
if (route.routes) {
return getActiveRouteName(route);
}
return route.routeName;
}
export function sleep(data = null, duration = 1000) {
return new Promise(resolve => {
setTimeout(() => {
resolve(data);
}, duration);
});
}
export function ratio(n) {
return n + PixelRatio.get() * 0.1 * n;
}