react-native-responsive
Version:
A React Native module to manage responsive layouts more efficiently
69 lines (57 loc) ⢠2.87 kB
JavaScript
import Model from "./model.js";
import { DeviceUtil } from "../apis";
class Device extends Model {
constructor(expected) {
super(expected);
}
debug(identifierName) {
console.log(
`š± DEVICE LOGS for ${identifierName}:\n`,
` š š± Device Width (css pixels): ${DeviceUtil.width} dip\n`,
` š š± Device Height (css pixels): ${DeviceUtil.height} dip\n`,
` š š± Device Width (physical pixels): ${DeviceUtil.physicalWidth} px\n`,
` š š± Device Height (physical pixels): ${DeviceUtil.physicalHeight} px\n`,
` š š± Device Pixel Ratio: ${DeviceUtil.pixelRatio}\n\n`,
` š š± props.deviceWidth: ${this.expected.deviceWidth} px\n`,
` š š± props.minDeviceWidth: ${this.expected.minDeviceWidth} px\n`,
` š š± props.maxDeviceWidth: ${this.expected.maxDeviceWidth} px\n`,
` š š± props.deviceHeight: ${this.expected.deviceHeight} px\n`,
` š š± props.minDeviceHeight: ${this.expected.minDeviceHeight} px\n`,
` š š± props.maxDeviceHeight: ${this.expected.maxDeviceHeight} px\n`,
` š š± props.devicePixelRatio: ${this.expected.devicePixelRatio}\n`,
` š š± props.minDevicePixelRatio: ${this.expected.minDevicePixelRatio}\n`,
` š š± props.maxDevicePixelRatio: ${this.expected.maxDevicePixelRatio}\n`,
);
}
isValid() {
return this.isValidWidth(this.expected) && this.isValidHeight(this.expected) && this.isValidPixelRatio(this.expected);
}
isValidWidth(expected) {
return Device.isInIntervalOrEqual(DeviceUtil.width, expected.deviceWidth, expected.minDeviceWidth, expected.maxDeviceWidth);
}
isValidHeight(expected) {
return Device.isInIntervalOrEqual(DeviceUtil.height, expected.deviceHeight, expected.minDeviceHeight, expected.maxDeviceHeight);
}
isValidPixelRatio(expected) {
return Device.isInIntervalOrEqual(DeviceUtil.pixelRatio, expected.devicePixelRatio, expected.minDevicePixelRatio, expected.maxDevicePixelRatio);
}
static isValidWidthFromOperator(operator, expectedWidth) {
return Device.isInIntervalOrEqualFromOperator(operator, expectedWidth, DeviceUtil.width);
}
static isValidHeightFromOperator(operator, expectedHeight) {
return Device.isInIntervalOrEqualFromOperator(operator, expectedHeight, DeviceUtil.height);
}
static isValidPixelRatioFromOperator(operator, expectedPixelRatio) {
return Device.isInIntervalOrEqualFromOperator(operator, expectedPixelRatio, DeviceUtil.pixelRatio);
}
static get information() {
return [
`š š± Device Width (css pixels): ${DeviceUtil.width} dip`,
`š š± Device Height (css pixels): ${DeviceUtil.height} dip`,
`š š± Device Width (physical pixels): ${DeviceUtil.physicalWidth} px`,
`š š± Device Height (physical pixels): ${DeviceUtil.physicalHeight} px`,
`š š± Device Pixel Ratio: ${DeviceUtil.pixelRatio}`
];
}
}
export default Device;