Skip to content
This repository has been archived by the owner on Jan 18, 2024. It is now read-only.

Commit

Permalink
Add UIDeviceFamily
Browse files Browse the repository at this point in the history
  • Loading branch information
brentvatne committed Mar 20, 2020
1 parent f57bd04 commit a44b1f0
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 29 deletions.
39 changes: 39 additions & 0 deletions packages/config/src/ios/DeviceFamily.ts
@@ -0,0 +1,39 @@
import { InfoPlist } from './IosConfig.types';
import { ExpoConfig } from '../Config.types';

export function getSupportsTablet(config: ExpoConfig) {
if (config.ios?.supportsTablet) {
return true;
}

return false;
}

export function getIsTabletOnly(config: ExpoConfig) {
if (config.ios?.isTabletOnly) {
return true;
}

return false;
}

export function getDeviceFamilies(config: ExpoConfig) {
let supportsTablet = getSupportsTablet(config);
let isTabletOnly = getIsTabletOnly(config);

// 1 is iPhone, 2 is iPad
if (isTabletOnly) {
return [2];
} else if (supportsTablet) {
return [1, 2];
} else {
return [1];
}
}

export function setDeviceFamily(config: ExpoConfig, infoPlist: InfoPlist) {
return {
...infoPlist,
UIDeviceFamily: getDeviceFamilies(config),
};
}
55 changes: 55 additions & 0 deletions packages/config/src/ios/__tests__/DeviceFamily-test.ts
@@ -0,0 +1,55 @@
import {
getDeviceFamilies,
getIsTabletOnly,
getSupportsTablet,
setDeviceFamily,
} from '../DeviceFamily';

const TABLET_AND_PHONE_SUPPORTED = [1, 2];
const ONLY_PHONE_SUPPORTED = [1];
const ONLY_TABLET_SUPPORTED = [2];

describe('device family', () => {
it(`returns false ios.isTabletOnly is not provided`, () => {
expect(getIsTabletOnly({ ios: {} })).toBe(false);
});

it(`returns true ios.isTabletOnly is provided`, () => {
expect(getIsTabletOnly({ ios: { isTabletOnly: true } })).toBe(true);
});

it(`returns false ios.supportsTablet is provided`, () => {
expect(getSupportsTablet({ ios: {} })).toBe(false);
});

it(`returns true ios.supportsTablet is provided`, () => {
expect(getSupportsTablet({ ios: { supportsTablet: true } })).toBe(true);
});

it(`supports tablet and mobile if supportsTablet is true`, () => {
expect(getDeviceFamilies({ ios: { supportsTablet: true } })).toEqual(
TABLET_AND_PHONE_SUPPORTED
);
});

it(`supports only mobile if supportsTablet is blank/false and isTabletOnly is blank/false`, () => {
expect(getDeviceFamilies({ ios: {} })).toEqual(ONLY_PHONE_SUPPORTED);
expect(getDeviceFamilies({ ios: { supportsTablet: false, isTabletOnly: false } })).toEqual(
ONLY_PHONE_SUPPORTED
);
});

it(`supports only tablet if isTabletOnly is true`, () => {
expect(getDeviceFamilies({ ios: { isTabletOnly: true } })).toEqual(ONLY_TABLET_SUPPORTED);
});

it(`sets to phone only if no value is provided`, () => {
expect(setDeviceFamily({}, {})).toMatchObject({ UIDeviceFamily: ONLY_PHONE_SUPPORTED });
});

it(`sets to given config when provided`, () => {
expect(setDeviceFamily({ ios: { supportsTablet: true } }, {})).toMatchObject({
UIDeviceFamily: TABLET_AND_PHONE_SUPPORTED,
});
});
});
2 changes: 2 additions & 0 deletions packages/config/src/ios/__tests__/UserInterfaceStyle-test.ts
@@ -1,6 +1,7 @@
import { getUserInterfaceStyle, setUserInterfaceStyle } from '../UserInterfaceStyle';

describe('user interface style', () => {
// TODO: should we default to 'Light' just as we do in the client if none is specified?
it(`returns null if no userInterfaceStyle is provided`, () => {
expect(getUserInterfaceStyle({})).toBe(null);
});
Expand All @@ -25,6 +26,7 @@ describe('user interface style', () => {
});
});

// TODO: should we default to 'Light' just as we do in the client if none is specified?
it(`makes no changes to the infoPlist if the value is invalid`, () => {
expect(setUserInterfaceStyle({ userInterfaceStyle: 'not-a-real-one' }, {})).toMatchObject({});
});
Expand Down
2 changes: 2 additions & 0 deletions packages/config/src/ios/index.ts
@@ -1,4 +1,5 @@
import * as BundleIdenitifer from './BundleIdentifier';
import * as DeviceFamily from './DeviceFamily';
import * as Version from './Version';
import * as Name from './Name';
import * as Scheme from './Scheme';
Expand All @@ -13,6 +14,7 @@ import { InfoPlist } from './IosConfig.types';
export {
BundleIdenitifer,
CustomInfoPlistEntries,
DeviceFamily,
Name,
Scheme,
UserInterfaceStyle,
Expand Down
31 changes: 2 additions & 29 deletions packages/xdl/src/detach/IosNSBundle.ts
Expand Up @@ -395,13 +395,7 @@ async function _configureInfoPlistAsync(context: AnyStandaloneContext): Promise<
}
}

// 1 is iPhone, 2 is iPad
infoPlist.UIDeviceFamily = config.ios && config.ios.supportsTablet ? [1, 2] : [1];

// allow iPad-only
if (config.ios && config.ios.isTabletOnly) {
infoPlist.UIDeviceFamily = [2];
}
infoPlist = IOSConfig.DeviceFamily.setDeviceFamily(config, infoPlist);

// Whether requires full screen on iPad
infoPlist.UIRequiresFullScreen = config.ios && config.ios.requireFullScreen;
Expand All @@ -413,11 +407,7 @@ async function _configureInfoPlistAsync(context: AnyStandaloneContext): Promise<
infoPlist.UIRequiresFullScreen = Boolean(infoPlist.UIRequiresFullScreen);

// Put `ios.userInterfaceStyle` into `UIUserInterfaceStyle` property of Info.plist
const userInterfaceStyle = config.ios && config.ios.userInterfaceStyle;
if (userInterfaceStyle) {
// To convert our config value to the InfoPlist value, we can just capitalize it.
infoPlist.UIUserInterfaceStyle = _mapUserInterfaceStyleForInfoPlist(userInterfaceStyle);
}
infoPlist = IOSConfig.UserInterfaceStyle.setUserInterfaceStyle(config, infoPlist);

// context-specific plist changes
if (context instanceof StandaloneContextUser) {
Expand Down Expand Up @@ -599,20 +589,3 @@ export async function configureAsync(context: AnyStandaloneContext): Promise<voi
]);
}
}

function _mapUserInterfaceStyleForInfoPlist(userInterfaceStyle: string): string | undefined {
switch (userInterfaceStyle) {
case 'light':
return 'Light';
case 'dark':
return 'Dark';
case 'automatic':
return 'Automatic';
default:
logger.warn(
`User interface style "${userInterfaceStyle}" is not supported. Supported values: "light", "dark", "automatic".`
);
}

return undefined;
}

0 comments on commit a44b1f0

Please sign in to comment.