Skip to content

Commit

Permalink
chore(refactor): using getSupportedPlatformInfoFunctions for another …
Browse files Browse the repository at this point in the history
…batch of APIs (#1090)

* refactor(src/index): rewriting `getPhoneNumber*`

rewrites functions to use `getSupportedPlatformInfoFunctions`

re #1085

* refactor(src/index.ts): rewriting `getTotalMemory*`

rewrites functions to use `getSupportedPlatformInfoFunctions`

re #1085

* refactor(src/index.ts): rewriting `getBatteryLevel*`

rewrites functions to use `getSupportedPlatformInfoFunctions`

re #1085

* refactor(src/index.ts): rewriting `isBatteryCharging*`

rewrites functions to use `getSupportedPlatformInfoFunctions`

re #1085

* refactor(src/index.ts): rewriting `isAirplaneMode*`

rewrites functions to use `getSupportedPlatformInfoFunctions`

re #1085

* refactor(src/index.ts): rewriting `isLocationEnabled*`

rewrites functions to use `getSupportedPlatformInfoFunctions`

re #1085

* refactor(src/index.ts): rewriting `getAvailableLocationProviders*`

rewrites functions to use `getSupportedPlatformInfoFunctions`

re #1085
  • Loading branch information
schie committed Oct 8, 2020
1 parent 0ee9dfa commit 570f130
Showing 1 changed file with 46 additions and 120 deletions.
166 changes: 46 additions & 120 deletions src/index.ts
Expand Up @@ -794,19 +794,12 @@ export function getLastUpdateTimeSync() {
return lastUpdateTime;
}

export async function getPhoneNumber() {
if (Platform.OS === 'android') {
return RNDeviceInfo.getPhoneNumber();
}
return 'unknown';
}

export function getPhoneNumberSync() {
if (Platform.OS === 'android') {
return RNDeviceInfo.getPhoneNumberSync();
}
return 'unknown';
}
export const [getPhoneNumber, getPhoneNumberSync] = getSupportedPlatformInfoFunctions({
supportedPlatforms: ['android'],
getter: () => RNDeviceInfo.getPhoneNumber(),
syncGetter: () => RNDeviceInfo.getPhoneNumberSync(),
defaultValue: 'unknown',
});

export const [getCarrier, getCarrierSync] = getSupportedPlatformInfoFunctions({
supportedPlatforms: ['android', 'ios'],
Expand All @@ -815,38 +808,13 @@ export const [getCarrier, getCarrierSync] = getSupportedPlatformInfoFunctions({
defaultValue: 'unknown',
});

let totalMemory: number;
export async function getTotalMemory() {
if (!totalMemory) {
if (
Platform.OS === 'android' ||
Platform.OS === 'ios' ||
Platform.OS === 'windows' ||
Platform.OS === 'web'
) {
totalMemory = await RNDeviceInfo.getTotalMemory();
} else {
totalMemory = -1;
}
}
return totalMemory;
}

export function getTotalMemorySync() {
if (!totalMemory) {
if (
Platform.OS === 'android' ||
Platform.OS === 'ios' ||
Platform.OS === 'windows' ||
Platform.OS === 'web'
) {
totalMemory = RNDeviceInfo.getTotalMemorySync();
} else {
totalMemory = -1;
}
}
return totalMemory;
}
export const [getTotalMemory, getTotalMemorySync] = getSupportedPlatformInfoFunctions({
memoKey: 'totalMemory',
supportedPlatforms: ['android', 'ios', 'windows', 'web'],
getter: () => RNDeviceInfo.getTotalMemory(),
syncGetter: () => RNDeviceInfo.getTotalMemorySync(),
defaultValue: -1,
});

let maxMemory: number;
export async function getMaxMemory() {
Expand Down Expand Up @@ -943,29 +911,12 @@ export function getFreeDiskStorageOldSync() {
return -1;
}

export async function getBatteryLevel() {
if (
Platform.OS === 'android' ||
Platform.OS === 'ios' ||
Platform.OS === 'windows' ||
Platform.OS === 'web'
) {
return RNDeviceInfo.getBatteryLevel();
}
return -1;
}

export function getBatteryLevelSync() {
if (
Platform.OS === 'android' ||
Platform.OS === 'ios' ||
Platform.OS === 'windows' ||
Platform.OS === 'web'
) {
return RNDeviceInfo.getBatteryLevelSync();
}
return -1;
}
export const [getBatteryLevel, getBatteryLevelSync] = getSupportedPlatformInfoFunctions({
supportedPlatforms: ['android', 'ios', 'windows', 'web'],
getter: () => RNDeviceInfo.getBatteryLevel(),
syncGetter: () => RNDeviceInfo.getBatteryLevelSync(),
defaultValue: -1,
});

export async function getPowerState(): Promise<PowerState | {}> {
if (Platform.OS === 'ios' || Platform.OS === 'android' || Platform.OS === 'web') {
Expand All @@ -981,19 +932,12 @@ export function getPowerStateSync() {
return {};
}

export async function isBatteryCharging() {
if (Platform.OS === 'android' || Platform.OS === 'ios' || Platform.OS === 'web') {
return RNDeviceInfo.isBatteryCharging();
}
return false;
}

export function isBatteryChargingSync() {
if (Platform.OS === 'android' || Platform.OS === 'ios' || Platform.OS === 'web') {
return RNDeviceInfo.isBatteryChargingSync();
}
return false;
}
export const [isBatteryCharging, isBatteryChargingSync] = getSupportedPlatformInfoFunctions({
supportedPlatforms: ['android', 'ios', 'web'],
getter: () => RNDeviceInfo.isBatteryCharging(),
syncGetter: () => RNDeviceInfo.isBatteryChargingSync(),
defaultValue: false,
});

export async function isLandscape() {
return Promise.resolve(isLandscapeSync());
Expand All @@ -1004,19 +948,12 @@ export function isLandscapeSync() {
return width >= height;
}

export async function isAirplaneMode() {
if (Platform.OS === 'android' || Platform.OS === 'web') {
return RNDeviceInfo.isAirplaneMode();
}
return Promise.resolve(false);
}

export function isAirplaneModeSync() {
if (Platform.OS === 'android' || Platform.OS === 'web') {
return RNDeviceInfo.isAirplaneModeSync();
}
return false;
}
export const [isAirplaneMode, isAirplaneModeSync] = getSupportedPlatformInfoFunctions({
supportedPlatforms: ['android', 'web'],
getter: () => RNDeviceInfo.isAirplaneMode(),
syncGetter: () => RNDeviceInfo.isAirplaneModeSync(),
defaultValue: false,
});

let deviceType: DeviceType;
export function getDeviceType() {
Expand Down Expand Up @@ -1138,19 +1075,12 @@ export function getSystemAvailableFeaturesSync() {
return [];
}

export async function isLocationEnabled() {
if (Platform.OS === 'android' || Platform.OS === 'ios' || Platform.OS === 'web') {
return RNDeviceInfo.isLocationEnabled();
}
return false;
}

export function isLocationEnabledSync() {
if (Platform.OS === 'android' || Platform.OS === 'ios' || Platform.OS === 'web') {
return RNDeviceInfo.isLocationEnabledSync();
}
return false;
}
export const [isLocationEnabled, isLocationEnabledSync] = getSupportedPlatformInfoFunctions({
supportedPlatforms: ['android', 'ios', 'web'],
getter: () => RNDeviceInfo.isLocationEnabled(),
syncGetter: () => RNDeviceInfo.isLocationEnabledSync(),
defaultValue: false,
});

export async function isHeadphonesConnected() {
if (Platform.OS === 'android' || Platform.OS === 'ios') {
Expand All @@ -1166,19 +1096,15 @@ export function isHeadphonesConnectedSync() {
return false;
}

export async function getAvailableLocationProviders() {
if (Platform.OS === 'android' || Platform.OS === 'ios') {
return RNDeviceInfo.getAvailableLocationProviders();
}
return {};
}

export function getAvailableLocationProvidersSync() {
if (Platform.OS === 'android' || Platform.OS === 'ios') {
return RNDeviceInfo.getAvailableLocationProvidersSync();
}
return {};
}
export const [
getAvailableLocationProviders,
getAvailableLocationProvidersSync,
] = getSupportedPlatformInfoFunctions({
supportedPlatforms: ['android', 'ios'],
getter: () => RNDeviceInfo.getAvailableLocationProviders(),
syncGetter: () => RNDeviceInfo.getAvailableLocationProvidersSync(),
defaultValue: {},
});

export async function getDeviceToken() {
if (Platform.OS === 'ios') {
Expand Down

0 comments on commit 570f130

Please sign in to comment.