-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathdisplay.js
123 lines (112 loc) · 2.46 KB
/
display.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
var {
DeviceEventEmitter,
Dimensions,
NativeModules
} = require('react-native');
var DeviceUtil = NativeModules.DisplayDeviceUtil;
class Display {
updateProps(newWidth, newHeight) {
this.width = newWidth;
this.height = newHeight;
}
constructor() {
this.width = Dimensions.get("window").width;
this.height = Dimensions.get("window").height;
//Enable console messages by changing to true: Display.verbose = true
this.verbose = false;
}
percentage(type, value) {
if (type == 'width') {
return value * (this.height / 100);
} else if (type == 'height') {
return value * (this.width / 100);
} else {
return 'Invalid Type (width / height)';
}
}
isPortrait() {
if (this.width < this.height) {
return true;
} else {
return false;
}
}
isLandscape() {
if (this.width > this.height) {
return true;
} else {
return false;
}
}
isTablet() {
return DeviceUtil.isTablet;
}
isPhone() {
return DeviceUtil.isPhone;
}
onOrientationDidChange(handler) {
var main = this;
return DeviceEventEmitter.addListener(
'orientationDidChange',
function(newDimensions) {
main.updateProps(newDimensions.width, newDimensions.height);
var orientation = main.updateOrientation(newDimensions.orientation);
handler(newDimensions.width, newDimensions.height, orientation);
}
);
}
updateOrientation(orientation) {
var o = {};
switch (orientation) {
case 1:
o = {
current: 'portrait',
idle: false,
facing: ''
};
break;
case 2:
o = {
current: 'portrait',
idle: false,
facing: ''
};
break;
case 3:
o = {
current: 'landscape',
idle: false,
facing: ''
};
break;
case 4:
o = {
current: 'landscape',
idle: false,
facing: ''
};
break;
case 5:
o = {
current: this.orientation.current,
idle: true,
facing: 'up'
};
break;
case 6:
o = {
current: this.orientation.current,
idle: true,
facing: 'down'
};
break;
}
if (this.verbose) {
console.log('Orientation Updated');
console.log(o);
}
this.orientation = o;
return o;
}
}
module.exports = new Display();