Unverified Commit 62a21860 authored by Mehdi Achour's avatar Mehdi Achour Committed by GitHub

feat: add empty polyfill for react-native-web (#339)

parent 0be171c7
...@@ -12,6 +12,7 @@ Device Information for [React Native](https://github.com/facebook/react-native). ...@@ -12,6 +12,7 @@ Device Information for [React Native](https://github.com/facebook/react-native).
* [API](#api) * [API](#api)
* [Troubleshooting](#troubleshooting) * [Troubleshooting](#troubleshooting)
* [Release Notes](#release-notes) * [Release Notes](#release-notes)
* [react-native-web](#react-native-web)
## Installation ## Installation
...@@ -433,7 +434,7 @@ const freeDiskStorage = DeviceInfo.getFreeDiskStorage(); ...@@ -433,7 +434,7 @@ const freeDiskStorage = DeviceInfo.getFreeDiskStorage();
**Notes** **Notes**
> From [developer.android.com](https://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory()): > From [developer.android.com](<https://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory()>):
> >
> Return the primary shared/external storage directory. > Return the primary shared/external storage directory.
> >
...@@ -804,35 +805,37 @@ When installing or using `react-native-device-info`, you may encounter the follo ...@@ -804,35 +805,37 @@ When installing or using `react-native-device-info`, you may encounter the follo
<details> <details>
<summary>[android] - Unable to merge dex / Multiple dex files</summary> <summary>[android] - Unable to merge dex / Multiple dex files</summary>
`react-native-device-info` uses `com.google.android.gms:play-services-gcm` to provide [getInstance()][#getinstance]. `react-native-device-info` uses `com.google.android.gms:play-services-gcm` to provide [getInstance()][#getinstance].
This can lead to conflicts when building the Android application. This can lead to conflicts when building the Android application.
If you're using a different version of `com.google.android.gms:play-services-gcm` in your app, you can define the If you're using a different version of `com.google.android.gms:play-services-gcm` in your app, you can define the
`googlePlayServicesVersion` gradle variable in your `build.gradle` file to tell `react-native-device-info` what version `googlePlayServicesVersion` gradle variable in your `build.gradle` file to tell `react-native-device-info` what version
it should require. it should require.
If you're using a different library that conflicts with `com.google.android.gms:play-services-gcm`, you can simply If you're using a different library that conflicts with `com.google.android.gms:play-services-gcm`, you can simply
ignore this dependency in your gradle file: ignore this dependency in your gradle file:
``` ```
compile(project(':react-native-device-info')) { compile(project(':react-native-device-info')) {
exclude group: 'com.google.android.gms' exclude group: 'com.google.android.gms'
} }
``` ```
</details> </details>
<details> <details>
<summary>[ios] - ld: library not found for -lRNDeviceInfo-tvOS</summary> <summary>[ios] - ld: library not found for -lRNDeviceInfo-tvOS</summary>
Seems to be a bug caused by `react-native link`. You can manually delete `libRNDeviceInfo-tvOS.a` in `Xcode -> [Your iOS build target] -> Build Phrases -> Link Binary with Libraries`. Seems to be a bug caused by `react-native link`. You can manually delete `libRNDeviceInfo-tvOS.a` in `Xcode -> [Your iOS build target] -> Build Phrases -> Link Binary with Libraries`.
</details> </details>
<details> <details>
<summary>[tests] - Cannot run my test suite when using this library</summary> <summary>[tests] - Cannot run my test suite when using this library</summary>
`react-native-device-info` contains native code, and needs to be mocked. `react-native-device-info` contains native code, and needs to be mocked.
Here's how to do it with jest for example: Here's how to do it with jest for example:
``` ```
// in your package.json: // in your package.json:
...@@ -849,9 +852,15 @@ jest.mock('react-native-device-info', () => { ...@@ -849,9 +852,15 @@ jest.mock('react-native-device-info', () => {
}; };
}); });
``` ```
</details>
</details>
## Release Notes ## Release Notes
See the [CHANGELOG.md](https://github.com/rebeccahughes/react-native-device-info/blob/master/CHANGELOG.md). See the [CHANGELOG.md](https://github.com/rebeccahughes/react-native-device-info/blob/master/CHANGELOG.md).
## react-native-web
As a courtesy to developers, this library was made compatible in v0.17.0 with [react-native-web](https://github.com/necolas/react-native-web) by providing an empty polyfill in order to avoid breaking builds.
Only [getUserAgent()](#getuseragent) will return a correct value. All other API methods will return an "empty" value of its documented return type: `0` for numbers, `''` for strings, `false` for booleans.
/** /**
* @providesModule react-native-device-info * @providesModule react-native-device-info
*/ */
import { Platform, NativeModules } from 'react-native';
var RNDeviceInfo = require('react-native').NativeModules.RNDeviceInfo; var RNDeviceInfo = NativeModules.RNDeviceInfo;
if (!RNDeviceInfo && Platform.OS === 'web') {
RNDeviceInfo = require('./web/RNDeviceInfoWeb');
}
module.exports = { module.exports = {
getUniqueID: function() { getUniqueID: function() {
...@@ -104,10 +109,10 @@ module.exports = { ...@@ -104,10 +109,10 @@ module.exports = {
getMaxMemory: function() { getMaxMemory: function() {
return RNDeviceInfo.maxMemory; return RNDeviceInfo.maxMemory;
}, },
getTotalDiskCapacity: function () { getTotalDiskCapacity: function() {
return RNDeviceInfo.totalDiskCapacity; return RNDeviceInfo.totalDiskCapacity;
}, },
getFreeDiskStorage: function () { getFreeDiskStorage: function() {
return RNDeviceInfo.freeDiskStorage; return RNDeviceInfo.freeDiskStorage;
} },
}; };
/**
* react-native-web empty polyfill.
*/
module.exports = {
uniqueId: '',
instanceId: '',
serialNumber: '',
getIpAddress: () => new Promise((resolve, reject) => resolve('')),
getMacAddress: () => new Promise((resolve, reject) => resolve('')),
deviceId: '',
systemManufacturer: '',
model: '',
brand: '',
systemName: '',
systemVersion: '',
apiLevel: 0,
bundleId: '',
appName: '',
buildNumber: 0,
appVersion: 0,
deviceName: '',
userAgent: window.navigator.userAgent,
deviceLocale: '',
deviceCountry: '',
timezone: '',
fontScale: 0,
isEmulator: false,
isTablet: false,
is24Hour: false,
isPinOrFingerprintSet: callback => callback && callback(false),
firstInstallTime: 0,
lastUpdateTime: 0,
phoneNumber: '',
carrier: '',
totalMemory: 0,
maxMemory: 0,
totalDiskCapacity: 0,
freeDiskStorage: 0,
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment