-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
[ASTraitCollection] Add missing properties to ASTraitCollection #625
[ASTraitCollection] Add missing properties to ASTraitCollection #625
Conversation
* ASTraitCollection now completely reflects UITraitCollection * Add ASContentSizeCategory enum that corresponds to UIContentSizeCategory and can be used inside a struct.
🚫 CI failed with log |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR, really appreciated!
In general looks good to me, would like to have another team member to take a look over it though.
|
||
// UIContentSizeCategoryUnspecified is available only in iOS 10.0 and later. | ||
// This constant is used as a fallback for older iOS versions. | ||
UIContentSizeCategory const AS_UIContentSizeCategoryUnspecified = @"_UICTContentSizeCategoryUnspecified"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we make this static?
@{ | ||
// We will fallback to ASContentSizeCategoryUnspecified so there is no need to include it in this dictionary. | ||
|
||
[NSNumber numberWithInteger:ASContentSizeCategoryExtraSmall]: UIContentSizeCategoryExtraSmall, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use object literals in this case? @(ASContentSizeCategoryExtraSmall)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the long delay getting a review on this @ypogribnyi and thank you for contributing!
/** | ||
* ASContentSizeCategory is a UIContentSizeCategory that can be used in a struct. | ||
*/ | ||
typedef NS_ENUM(NSInteger, ASContentSizeCategory) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's do something clever, and use __unsafe_unretained UIContentSizeCategory
as a struct member:
typedef struct {
// unretained is required because ARC can't manage struct memory
__unsafe_unretained UIContentSizeCategory category;
BOOL flag;
} ASTestStruct;
* Use __unsafe_unretained UIContentSizeCategory instead of the enum.
Updated |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some small changes and we're good to go!
Source/Details/ASTraitCollection.h
Outdated
horizontalSizeClass:(UIUserInterfaceSizeClass)horizontalSizeClass | ||
verticalSizeClass:(UIUserInterfaceSizeClass)verticalSizeClass | ||
forceTouchCapability:(UIForceTouchCapability)forceTouchCapability | ||
containerSize:(CGSize)windowSize; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be considered a breaking API change. Instead, I think we should preserve this API and call the full version internally with default values. We can also deprecate it, but that's as far as we can go for now, IMHO.
Source/Details/ASTraitCollection.h
Outdated
userInterfaceIdiom:(UIUserInterfaceIdiom)userInterfaceIdiom | ||
forceTouchCapability:(UIForceTouchCapability)forceTouchCapability | ||
layoutDirection:(UITraitEnvironmentLayoutDirection)layoutDirection | ||
#if TARGET_OS_TV |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand your reasoning here. However, for maintainability, I think it's best to declare a separate API for tvOS with this extra param.
Source/Details/ASTraitCollection.m
Outdated
if (AS_AT_LEAST_IOS10) { | ||
return UIContentSizeCategoryUnspecified; | ||
} | ||
else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Merge this line to the previous one.
Source/Details/ASTraitCollection.m
Outdated
return UIContentSizeCategoryAccessibilityExtraExtraExtraLarge; | ||
} | ||
|
||
if (AS_AT_LEAST_IOS10) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fallback logic is used multiple times in this diff. I think we can encapsulate it into a function and use everywhere.
Source/Details/ASTraitCollection.m
Outdated
|
||
// preferredContentSizeCategory always points to one of UIContentSizeCategory constants. | ||
// Assuming their values do not duplicate, we can simply compare pointers, avoiding string comparison. | ||
lhs.preferredContentSizeCategory == rhs.preferredContentSizeCategory && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the detailed comment. I actually expect -[NSString isEqualToString:]
to compare memory addresses early on. Without concrete numbers, I'd consider this optimization unnecessary, also because calling -isEqualToString:
gives us better maintainability.
Interesting find: https://gist.github.com/0xced/2275014#file-nsstring-m-L76
… ASTraitCollection-missing-properties # Conflicts: # CHANGELOG.md # Source/Details/ASTraitCollection.m
* Restore one of the ASTraitCollection constructors with a deprecation notice. * Clean up API by the separation of tvOS-specific interfaces. * Use [NSString -isEqualToString:] for ASPrimitiveContentSizeCategory equality tests for better readability. * Encapsulate fallback logic for UIContentSizeCategoryUnspecified.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! 🎉
lhs.userInterfaceStyle == rhs.userInterfaceStyle && | ||
#endif | ||
|
||
[leftSizeCategory isEqualToString:rightSizeCategory] && // Simple pointer comparison should be sufficient here |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:)
…ureGroup#625) * [ASTraitCollection] Add missing properties to ASTraitCollection * ASTraitCollection now completely reflects UITraitCollection * Add ASContentSizeCategory enum that corresponds to UIContentSizeCategory and can be used inside a struct. * * Remove enum ASContentSizeCategory. * Use __unsafe_unretained UIContentSizeCategory instead of the enum. * Added ASPrimitiveTraitCollection lifetime test * Changes requested at code review: * Restore one of the ASTraitCollection constructors with a deprecation notice. * Clean up API by the separation of tvOS-specific interfaces. * Use [NSString -isEqualToString:] for ASPrimitiveContentSizeCategory equality tests for better readability. * Encapsulate fallback logic for UIContentSizeCategoryUnspecified. * Fix failing test
In comparison to
UITraitCollection
, some properties were missing fromASTraitCollection
.These are needed for
ASDisplayNode
s to conveniently access information about current interface direction, DynamicType size category etc.