Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ASPrimitiveTraitCollection] Always treat preferredContentSize as a potential nil #trivial #757

Merged
merged 3 commits into from
Mar 21, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions Source/Details/ASTraitCollection.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ extern ASPrimitiveContentSizeCategory ASPrimitiveContentSizeCategoryMake(UIConte

#pragma mark - ASPrimitiveTraitCollection

/**
* @abstract This is a struct equivalent of ASTraitCollection.
*
* @discussion It may be not safe to use this structure uninitialized. Please initialize it with ASPrimitiveTraitCollectionMakeDefault()
* or ASPrimitiveTraitCollectionFromUITraitCollection(UITraitCollection*).
*/
typedef struct ASPrimitiveTraitCollection {
UIUserInterfaceSizeClass horizontalSizeClass;
UIUserInterfaceSizeClass verticalSizeClass;
Expand Down Expand Up @@ -175,7 +181,7 @@ AS_SUBCLASSING_RESTRICTED
@property (nonatomic, assign, readonly) UIUserInterfaceStyle userInterfaceStyle;
#endif

@property (nonatomic, assign, readonly) UIContentSizeCategory preferredContentSizeCategory;
@property (nonatomic, assign, readonly) UIContentSizeCategory _Nonnull preferredContentSizeCategory;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need _Nonnull annotations since we already use NS_ASSUME_NONNULL_BEGIN at the beginning of this file (here). It's always good to be defensive and guard against unexpected values in the implementation though.


@property (nonatomic, assign, readonly) CGSize containerSize;

Expand All @@ -186,7 +192,7 @@ AS_SUBCLASSING_RESTRICTED

+ (ASTraitCollection *)traitCollectionWithUITraitCollection:(UITraitCollection *)traitCollection
containerSize:(CGSize)windowSize
fallbackContentSizeCategory:(UIContentSizeCategory)fallbackContentSizeCategory;
fallbackContentSizeCategory:(UIContentSizeCategory _Nonnull)fallbackContentSizeCategory;

#if TARGET_OS_TV
+ (ASTraitCollection *)traitCollectionWithHorizontalSizeClass:(UIUserInterfaceSizeClass)horizontalSizeClass
Expand All @@ -197,7 +203,7 @@ AS_SUBCLASSING_RESTRICTED
forceTouchCapability:(UIForceTouchCapability)forceTouchCapability
layoutDirection:(UITraitEnvironmentLayoutDirection)layoutDirection
userInterfaceStyle:(UIUserInterfaceStyle)userInterfaceStyle
preferredContentSizeCategory:(UIContentSizeCategory)preferredContentSizeCategory
preferredContentSizeCategory:(UIContentSizeCategory _Nonnull)preferredContentSizeCategory
containerSize:(CGSize)windowSize;
#else
+ (ASTraitCollection *)traitCollectionWithHorizontalSizeClass:(UIUserInterfaceSizeClass)horizontalSizeClass
Expand All @@ -207,7 +213,7 @@ AS_SUBCLASSING_RESTRICTED
userInterfaceIdiom:(UIUserInterfaceIdiom)userInterfaceIdiom
forceTouchCapability:(UIForceTouchCapability)forceTouchCapability
layoutDirection:(UITraitEnvironmentLayoutDirection)layoutDirection
preferredContentSizeCategory:(UIContentSizeCategory)preferredContentSizeCategory
preferredContentSizeCategory:(UIContentSizeCategory _Nonnull)preferredContentSizeCategory
containerSize:(CGSize)windowSize;
#endif

Expand Down
34 changes: 22 additions & 12 deletions Source/Details/ASTraitCollection.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ ASDISPLAYNODE_INLINE UIContentSizeCategory AS_UIContentSizeCategoryUnspecified()
}
}

ASDISPLAYNODE_INLINE ASPrimitiveContentSizeCategory safePrimitiveContentSizeCategory(ASPrimitiveContentSizeCategory sizeCategory) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we still should prefix this functions with AS

if (sizeCategory) {
return sizeCategory;
} else {
return AS_UIContentSizeCategoryUnspecified();
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, feel free to ignore this: It's a bit shorter to use ternary operator here, i.e:

return sizeCategory ? sizeCategory : AS_UIContentSizeCategoryUnspecified();


ASPrimitiveContentSizeCategory ASPrimitiveContentSizeCategoryMake(UIContentSizeCategory sizeCategory) {
if ([sizeCategory isEqualToString:UIContentSizeCategoryExtraSmall]) {
return UIContentSizeCategoryExtraSmall;
Expand Down Expand Up @@ -92,7 +100,7 @@ ASPrimitiveTraitCollection ASPrimitiveTraitCollectionMakeDefault() {
.displayGamut = UIDisplayGamutUnspecified,
.userInterfaceIdiom = UIUserInterfaceIdiomUnspecified,
.layoutDirection = UITraitEnvironmentLayoutDirectionUnspecified,
.preferredContentSizeCategory = ASPrimitiveContentSizeCategoryMake(AS_UIContentSizeCategoryUnspecified()),
.preferredContentSizeCategory = AS_UIContentSizeCategoryUnspecified(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we remove the ASPrimitiveContentSizeCategoryMake in here. If you read the comment above ASPrimitiveContentSizeCategory we have to use ASPrimitiveContentSizeCategoryMake

.containerSize = CGSizeZero,
};
}
Expand All @@ -115,13 +123,15 @@ ASPrimitiveTraitCollection ASPrimitiveTraitCollectionFromUITraitCollection(UITra
#if TARGET_OS_TV
environmentTraitCollection.userInterfaceStyle = traitCollection.userInterfaceStyle;
#endif
} else {
environmentTraitCollection.displayGamut = UIDisplayGamutSRGB; // We're on iOS 9 or lower, so this is not a P3 device.
}
return environmentTraitCollection;
}

BOOL ASPrimitiveTraitCollectionIsEqualToASPrimitiveTraitCollection(ASPrimitiveTraitCollection lhs, ASPrimitiveTraitCollection rhs) {
UIContentSizeCategory leftSizeCategory = (UIContentSizeCategory)lhs.preferredContentSizeCategory;
UIContentSizeCategory rightSizeCategory = (UIContentSizeCategory)rhs.preferredContentSizeCategory;
UIContentSizeCategory leftSizeCategory = (UIContentSizeCategory)safePrimitiveContentSizeCategory(lhs.preferredContentSizeCategory);
UIContentSizeCategory rightSizeCategory = (UIContentSizeCategory)safePrimitiveContentSizeCategory(rhs.preferredContentSizeCategory);

return
lhs.verticalSizeClass == rhs.verticalSizeClass &&
Expand Down Expand Up @@ -230,7 +240,7 @@ BOOL ASPrimitiveTraitCollectionIsEqualToASPrimitiveTraitCollection(ASPrimitiveTr
#if TARGET_OS_TV
[props addObject:@{ @"userInterfaceStyle": AS_NSStringFromUIUserInterfaceStyle(traits.userInterfaceStyle) }];
#endif
[props addObject:@{ @"preferredContentSizeCategory": (UIContentSizeCategory)traits.preferredContentSizeCategory }];
[props addObject:@{ @"preferredContentSizeCategory": (UIContentSizeCategory)safePrimitiveContentSizeCategory(traits.preferredContentSizeCategory) }];
[props addObject:@{ @"containerSize": NSStringFromCGSize(traits.containerSize) }];
return ASObjectDescriptionMakeWithoutObject(props);
}
Expand All @@ -249,7 +259,7 @@ - (instancetype)initWithHorizontalSizeClass:(UIUserInterfaceSizeClass)horizontal
forceTouchCapability:(UIForceTouchCapability)forceTouchCapability
layoutDirection:(UITraitEnvironmentLayoutDirection)layoutDirection
userInterfaceStyle:(UIUserInterfaceStyle)userInterfaceStyle
preferredContentSizeCategory:(UIContentSizeCategory)preferredContentSizeCategory
preferredContentSizeCategory:(UIContentSizeCategory _Nonnull)preferredContentSizeCategory
containerSize:(CGSize)windowSize
{
self = [super init];
Expand All @@ -276,7 +286,7 @@ + (instancetype)traitCollectionWithHorizontalSizeClass:(UIUserInterfaceSizeClass
forceTouchCapability:(UIForceTouchCapability)forceTouchCapability
layoutDirection:(UITraitEnvironmentLayoutDirection)layoutDirection
userInterfaceStyle:(UIUserInterfaceStyle)userInterfaceStyle
preferredContentSizeCategory:(UIContentSizeCategory)preferredContentSizeCategory
preferredContentSizeCategory:(UIContentSizeCategory _Nonnull)preferredContentSizeCategory
containerSize:(CGSize)windowSize
{
return [[self alloc] initWithHorizontalSizeClass:horizontalSizeClass
Expand All @@ -300,7 +310,7 @@ - (instancetype)initWithHorizontalSizeClass:(UIUserInterfaceSizeClass)horizontal
userInterfaceIdiom:(UIUserInterfaceIdiom)userInterfaceIdiom
forceTouchCapability:(UIForceTouchCapability)forceTouchCapability
layoutDirection:(UITraitEnvironmentLayoutDirection)layoutDirection
preferredContentSizeCategory:(UIContentSizeCategory)preferredContentSizeCategory
preferredContentSizeCategory:(UIContentSizeCategory _Nonnull)preferredContentSizeCategory
containerSize:(CGSize)windowSize
{
self = [super init];
Expand All @@ -325,7 +335,7 @@ + (instancetype)traitCollectionWithHorizontalSizeClass:(UIUserInterfaceSizeClass
userInterfaceIdiom:(UIUserInterfaceIdiom)userInterfaceIdiom
forceTouchCapability:(UIForceTouchCapability)forceTouchCapability
layoutDirection:(UITraitEnvironmentLayoutDirection)layoutDirection
preferredContentSizeCategory:(UIContentSizeCategory)preferredContentSizeCategory
preferredContentSizeCategory:(UIContentSizeCategory _Nonnull)preferredContentSizeCategory
containerSize:(CGSize)windowSize
{
return [[self alloc] initWithHorizontalSizeClass:horizontalSizeClass
Expand Down Expand Up @@ -383,7 +393,7 @@ + (instancetype)traitCollectionWithASPrimitiveTraitCollection:(ASPrimitiveTraitC
forceTouchCapability:traits.forceTouchCapability
layoutDirection:traits.layoutDirection
userInterfaceStyle:traits.userInterfaceStyle
preferredContentSizeCategory:(UIContentSizeCategory)traits.preferredContentSizeCategory
preferredContentSizeCategory:(UIContentSizeCategory _Nonnull)safePrimitiveContentSizeCategory(traits.preferredContentSizeCategory)
containerSize:traits.containerSize];
#else
return [self traitCollectionWithHorizontalSizeClass:traits.horizontalSizeClass
Expand All @@ -393,7 +403,7 @@ + (instancetype)traitCollectionWithASPrimitiveTraitCollection:(ASPrimitiveTraitC
userInterfaceIdiom:traits.userInterfaceIdiom
forceTouchCapability:traits.forceTouchCapability
layoutDirection:traits.layoutDirection
preferredContentSizeCategory:(UIContentSizeCategory)traits.preferredContentSizeCategory
preferredContentSizeCategory:(UIContentSizeCategory _Nonnull)safePrimitiveContentSizeCategory(traits.preferredContentSizeCategory)
containerSize:traits.containerSize];
#endif
}
Expand All @@ -409,7 +419,7 @@ + (instancetype)traitCollectionWithUITraitCollection:(UITraitCollection *)traitC

+ (instancetype)traitCollectionWithUITraitCollection:(UITraitCollection *)traitCollection
containerSize:(CGSize)windowSize
fallbackContentSizeCategory:(UIContentSizeCategory)fallbackContentSizeCategory
fallbackContentSizeCategory:(UIContentSizeCategory _Nonnull)fallbackContentSizeCategory
{
UIDisplayGamut displayGamut;
UITraitEnvironmentLayoutDirection layoutDirection;
Expand All @@ -425,7 +435,7 @@ + (instancetype)traitCollectionWithUITraitCollection:(UITraitCollection *)traitC
userInterfaceStyle = traitCollection.userInterfaceStyle;
#endif
} else {
displayGamut = UIDisplayGamutUnspecified;
displayGamut = UIDisplayGamutSRGB; // We're on iOS 9 or lower, so this is not a P3 device.
layoutDirection = UITraitEnvironmentLayoutDirectionUnspecified;
sizeCategory = fallbackContentSizeCategory;
#if TARGET_OS_TV
Expand Down